|
25 | 25 | #include <bcos-crypto/hash/Keccak256.h> |
26 | 26 | #include <bcos-crypto/interfaces/crypto/Hash.h> |
27 | 27 | #include <bcos-utilities/DataConvertUtility.h> |
| 28 | +#include <evmc/evmc.h> |
28 | 29 | #include <fmt/format.h> |
29 | 30 | #include <boost/algorithm/string.hpp> |
| 31 | +#include <memory> |
30 | 32 | #include <string> |
31 | 33 |
|
32 | 34 | namespace bcos |
@@ -120,26 +122,37 @@ inline std::string newEVMAddress( |
120 | 122 | return newEVMAddress(*_hashImpl, blockNumber, contextID, seq); |
121 | 123 | } |
122 | 124 |
|
123 | | - |
124 | 125 | // keccak256(rlp.encode([normalize_address(sender), nonce]))[12:] |
125 | | -inline std::string newLegacyEVMAddress(bytesConstRef sender, u256 nonce) noexcept |
| 126 | +inline evmc_address newLegacyEVMAddress(bytesConstRef sender, const u256& nonce) noexcept |
126 | 127 | { |
127 | | - codec::rlp::Header header{true, 1 + sender.size()}; |
| 128 | + codec::rlp::Header header{.isList = true, .payloadLength = 1 + sender.size()}; |
128 | 129 | header.payloadLength += codec::rlp::length(nonce); |
129 | 130 | bcos::bytes rlp; |
130 | 131 | codec::rlp::encodeHeader(rlp, header); |
131 | 132 | codec::rlp::encode(rlp, sender); |
132 | 133 | codec::rlp::encode(rlp, nonce); |
133 | 134 | auto hash = bcos::crypto::keccak256Hash(ref(rlp)); |
| 135 | + evmc_address address; |
| 136 | + std::uninitialized_copy(hash.begin() + 12, hash.end(), address.bytes); |
| 137 | + |
| 138 | + return address; |
| 139 | +} |
| 140 | + |
| 141 | +inline std::string newLegacyEVMAddressString(bytesConstRef sender, const u256& nonce) noexcept |
| 142 | +{ |
| 143 | + auto address = newLegacyEVMAddress(sender, nonce); |
| 144 | + auto view = std::span{address.bytes}; |
134 | 145 | std::string out; |
135 | | - boost::algorithm::hex_lower(hash.begin() + 12, hash.end(), std::back_inserter(out)); |
| 146 | + out.reserve(view.size() * 2); |
| 147 | + boost::algorithm::hex_lower(view.begin(), view.end(), std::back_inserter(out)); |
136 | 148 | return out; |
137 | 149 | } |
138 | 150 |
|
139 | | -inline std::string newLegacyEVMAddress(bytesConstRef sender, std::string const& nonce) noexcept |
| 151 | +inline std::string newLegacyEVMAddressString( |
| 152 | + bytesConstRef sender, std::string const& nonce) noexcept |
140 | 153 | { |
141 | 154 | const auto uNonce = hex2u(nonce); |
142 | | - return newLegacyEVMAddress(sender, uNonce); |
| 155 | + return newLegacyEVMAddressString(sender, uNonce); |
143 | 156 | } |
144 | 157 |
|
145 | 158 | // EIP-1014 |
|
0 commit comments