OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
address.cpp
Go to the documentation of this file.
1// Copyright (c) 2022 MIT Digital Currency Initiative,
2// Federal Reserve Bank of Boston
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include "address.hpp"
7
8#include "crypto/sha256.h"
9#include "format.hpp"
10#include "hash.hpp"
11#include "rlp.hpp"
12#include "util.hpp"
13#include "util/common/hash.hpp"
15
16#include <optional>
17#include <secp256k1.h>
18
20 auto contract_address(const evmc::address& sender,
21 const evmc::uint256be& nonce) -> evmc::address {
22 auto new_addr = evmc::address();
23 auto buf = make_buffer(make_rlp_array(make_rlp_value(sender),
24 make_rlp_value(nonce, true)));
25 auto addr_hash = keccak_data(buf.data(), buf.size());
26 constexpr auto addr_offset = addr_hash.size() - sizeof(new_addr.bytes);
27 std::memcpy(new_addr.bytes,
28 addr_hash.data() + addr_offset,
29 sizeof(new_addr.bytes));
30 return new_addr;
31 }
32
33 auto contract_address2(const evmc::address& sender,
34 const evmc::bytes32& salt,
35 const cbdc::hash_t& bytecode_hash)
36 -> evmc::address {
37 // Specs: https://eips.ethereum.org/EIPS/eip-1014
38
39 auto new_addr = evmc::address();
40 auto buf = cbdc::buffer();
41 static constexpr uint8_t contract_address2_preimage_prefix = 0xFF;
42 auto b = std::byte(contract_address2_preimage_prefix);
43 buf.append(&b, sizeof(b));
44 buf.append(sender.bytes, sizeof(sender.bytes));
45 buf.append(salt.bytes, sizeof(salt.bytes));
46 buf.append(bytecode_hash.data(), bytecode_hash.size());
47
48 auto addr_hash = keccak_data(buf.data(), buf.size());
49 constexpr auto addr_offset = addr_hash.size() - sizeof(new_addr.bytes);
50 std::memcpy(new_addr.bytes,
51 addr_hash.data() + addr_offset,
52 sizeof(new_addr.bytes));
53 return new_addr;
54 }
55
56 auto eth_addr(const std::unique_ptr<secp256k1_pubkey>& pk,
57 const std::shared_ptr<secp256k1_context>& ctx)
58 -> evmc::address {
59 static constexpr int uncompressed_pubkey_len = 65;
60 auto pubkey_serialized
61 = std::array<unsigned char, uncompressed_pubkey_len>();
62 auto pubkey_size = pubkey_serialized.size();
63 [[maybe_unused]] const auto ser_ret
64 = ::secp256k1_ec_pubkey_serialize(ctx.get(),
65 pubkey_serialized.data(),
66 &pubkey_size,
67 pk.get(),
68 SECP256K1_EC_UNCOMPRESSED);
69 assert(ser_ret == 1);
70
71 auto pubkey_buffer = cbdc::buffer();
72 pubkey_buffer.extend(uncompressed_pubkey_len);
73 std::memcpy(pubkey_buffer.data(),
74 pubkey_serialized.data(),
75 pubkey_serialized.size());
76
77 auto addr_hash = cbdc::keccak_data(pubkey_buffer.data_at(1),
78 uncompressed_pubkey_len - 1);
79 auto addr = evmc::address();
80 constexpr auto addr_offset = addr_hash.size() - sizeof(addr.bytes);
81 std::memcpy(addr.bytes,
82 addr_hash.data() + addr_offset,
83 sizeof(addr.bytes));
84 return addr;
85 }
86
87 auto eth_addr(const cbdc::privkey_t& key,
88 const std::shared_ptr<secp256k1_context>& ctx)
89 -> evmc::address {
90 auto pk = std::make_unique<secp256k1_pubkey>();
91 [[maybe_unused]] const auto pub_ret
92 = ::secp256k1_ec_pubkey_create(ctx.get(), pk.get(), key.data());
93 assert(pub_ret == 1);
94 return eth_addr(pk, ctx);
95 }
96}
auto eth_addr(const std::unique_ptr< secp256k1_pubkey > &pk, const std::shared_ptr< secp256k1_context > &ctx) -> evmc::address
Calculates an eth address from a public key.
Definition address.cpp:56
auto contract_address2(const evmc::address &sender, const evmc::bytes32 &salt, const cbdc::hash_t &bytecode_hash) -> evmc::address
Calculates a contract address for the CREATE2 call keccak256(0xFF | sender | salt | keccak256(bytecod...
Definition address.cpp:33
auto contract_address(const evmc::address &sender, const evmc::uint256be &nonce) -> evmc::address
Calculates a contract address for the CREATE call keccak256(rlp([sender,nonce]))
Definition address.cpp:20
std::array< unsigned char, cbdc::hash_size > hash_t
SHA256 hash container.
auto make_rlp_value(const T &obj, bool trim_leading_zeroes=false) -> rlp_value
Turns an existing value into an rlp_value by first serializing it as a cbdc::buffer,...
Definition rlp.hpp:126
std::array< unsigned char, pubkey_len > privkey_t
A private key of a public/private keypair.
Definition keys.hpp:23
@ buffer
A singular RLP value (byte array)
auto keccak_data(const void *data, size_t len) -> hash_t
Calculates the Keccak256 hash of the specified data.
auto make_buffer(const T &obj) -> std::enable_if_t< std::is_same_v< B, nuraft::ptr< nuraft::buffer > >, nuraft::ptr< nuraft::buffer > >
Serialize object into nuraft::buffer using a cbdc::nuraft_serializer.
auto make_rlp_array(const Args &... values) -> rlp_value
Turns multiple rlp_value objects into an rlp_value of type array.
Definition rlp.hpp:153