9#include "crypto/sha256.h"
23 secp256k1_ecdsa_recoverable_signature& sig,
28 std::reverse_copy(&sig.data[0],
29 &sig.data[
sizeof(esig.m_r.bytes)],
32 &sig.data[
sizeof(esig.m_r.bytes)],
33 &sig.data[
sizeof(esig.m_r.bytes) +
sizeof(esig.m_s.bytes)],
38 &sig.data[
sizeof(esig.m_r.bytes) +
sizeof(esig.m_s.bytes)],
39 &sig.data[
sizeof(esig.m_r.bytes) +
sizeof(esig.m_s.bytes)
46 v_large += eip155_v_offset;
47 v_large += (chain_id * 2);
49 esig.m_v = evmc::uint256be(v_large);
57 -> std::optional<secp256k1_ecdsa_recoverable_signature> {
58 auto sig = secp256k1_ecdsa_recoverable_signature{};
60 std::reverse_copy(esig.m_r.bytes,
61 &esig.m_r.bytes[
sizeof(esig.m_r.bytes)],
63 std::reverse_copy(esig.m_s.bytes,
64 &esig.m_s.bytes[
sizeof(esig.m_s.bytes)],
65 &sig.data[
sizeof(esig.m_r.bytes)]);
70 v_large -= eip155_v_offset;
71 v_large -= (chain_id * 2);
72 if(v_large > std::numeric_limits<uint8_t>::max()) {
76 && v_large >= pre_eip155_v_offset) {
77 v_large -= pre_eip155_v_offset;
79 auto v =
static_cast<uint8_t
>(v_large);
80 std::memcpy(&sig.data[
sizeof(sig.data) -
sizeof(v)], &v,
sizeof(v));
88 const std::shared_ptr<secp256k1_context>& ctx,
90 secp256k1_ecdsa_recoverable_signature sig;
91 [[maybe_unused]]
const auto sig_ret = secp256k1_ecdsa_sign_recoverable(
96 secp256k1_nonce_function_rfc6979,
105 const std::shared_ptr<secp256k1_context>& ctx,
106 uint64_t chain_id) -> std::optional<evmc::address> {
107 auto sighash =
sig_hash(tx, chain_id);
114 if(!maybe_sig.has_value()) {
118 auto sig = maybe_sig.value();
121 auto pk = std::make_unique<secp256k1_pubkey>();
122 [[maybe_unused]]
const auto rec_ret
123 = secp256k1_ecdsa_recover(ctx.get(),
135 uint64_t chain_id) ->
hash_t {
136 auto rlp_buf =
tx_encode(tx, chain_id,
true);
auto tx_encode(const cbdc::parsec::agent::runner::evm_tx &tx, uint64_t chain_id, bool for_sighash) -> cbdc::buffer
Converts the given transaction to an RLP encoded buffer conforming to Ethereums conventions.
auto eth_sign(const privkey_t &key, hash_t &hash, evm_tx_type type, const std::shared_ptr< secp256k1_context > &ctx, uint64_t chain_id) -> evm_sig
Signs a hash using a privkey_t using ecdsa and produces an evm_sig struct Used primarily in unit test...
auto check_signature(const cbdc::parsec::agent::runner::evm_tx &tx, const std::shared_ptr< secp256k1_context > &ctx, uint64_t chain_id) -> std::optional< evmc::address >
Checks the signature of an EVM transaction.
auto secp256k1_ecdsa_recoverable_signature_to_evm_sig(secp256k1_ecdsa_recoverable_signature &sig, evm_tx_type type, uint64_t chain_id) -> evm_sig
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.
evm_tx_type
 EVM transaction types.
auto evm_sig_to_secp256k1_ecdsa_recoverable_signature(const evm_sig &esig, evm_tx_type type, uint64_t chain_id) -> std::optional< secp256k1_ecdsa_recoverable_signature >
auto sig_hash(const cbdc::parsec::agent::runner::evm_tx &tx, uint64_t chain_id) -> hash_t
Calculates the hash for creating / validating the signature.
auto to_uint64(const evmc::uint256be &v) -> uint64_t
Converts an uint256be to a uint64_t, ignoring higher order bits.
std::array< unsigned char, cbdc::hash_size > hash_t
SHA256 hash container.
std::array< unsigned char, pubkey_len > privkey_t
A private key of a public/private keypair.
auto keccak_data(const void *data, size_t len) -> hash_t
Calculates the Keccak256 hash of the specified data.