OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
parsec/agent/runners/evm/util.hpp
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#ifndef OPENCBDC_TX_SRC_PARSEC_AGENT_RUNNERS_EVM_UTIL_H_
7#define OPENCBDC_TX_SRC_PARSEC_AGENT_RUNNERS_EVM_UTIL_H_
8
9#include "messages.hpp"
12#include "util/common/hash.hpp"
13#include "util/common/keys.hpp"
15
16#include <evmc/evmc.hpp>
17#include <evmc/hex.hpp>
18#include <memory>
19#include <secp256k1.h>
20#include <secp256k1_extrakeys.h>
21#include <secp256k1_recovery.h>
22
27 auto to_uint64(const evmc::uint256be& v) -> uint64_t;
28
33 template<typename T>
34 auto to_hex(const T& v) -> std::string {
35 return evmc::hex(evmc::bytes(v.bytes, sizeof(v.bytes)));
36 }
37
38 auto to_hex_trimmed(const evmc::bytes32& b,
39 const std::string& prefix = "0x") -> std::string;
40
46 void add_to_bloom(cbdc::buffer& bloom, const cbdc::buffer& entry);
47
53 template<typename T>
54 auto from_hex(const std::string& hex) ->
55 typename std::enable_if_t<std::is_same<T, evmc::bytes32>::value
56 || std::is_same<T, evmc::address>::value,
57 std::optional<T>> {
58 auto maybe_bytes = cbdc::buffer::from_hex_prefixed(hex);
59 if(!maybe_bytes.has_value()) {
60 return std::nullopt;
61 }
62 if(maybe_bytes.value().size() != sizeof(T)) {
63 return std::nullopt;
64 }
65
66 auto val = T();
67 std::memcpy(val.bytes,
68 maybe_bytes.value().data(),
69 maybe_bytes.value().size());
70 return val;
71 }
72
77 auto uint256be_from_hex(const std::string& hex)
78 -> std::optional<evmc::uint256be>;
79
84 const std::shared_ptr<logging::log>& log,
85 const std::shared_ptr<parsec::broker::interface>& broker) -> bool;
86}
87
88#endif
Buffer to store and retrieve byte data.
Definition buffer.hpp:15
static auto from_hex_prefixed(const std::string &hex, const std::string &prefix="0x") -> std::optional< buffer >
Creates a new buffer from the provided hex string optionally prefixed with a prefix sequence.
Definition buffer.cpp:108
auto mint_initial_accounts(const std::shared_ptr< logging::log > &log, const std::shared_ptr< parsec::broker::interface > &broker) -> bool
Mints a set of initial accounts with funds, bypassing the agent.
auto to_hex_trimmed(const evmc::bytes32 &b, const std::string &prefix) -> std::string
auto to_hex(const evmc::address &addr) -> std::string
auto uint256be_from_hex(const std::string &hex) -> std::optional< evmc::uint256be >
Generates a uint256be from a hex string.
auto from_hex(const std::string &hex) -> typename std::enable_if_t< std::is_same< T, evmc::bytes32 >::value||std::is_same< T, evmc::address >::value, std::optional< T > >
Parses hexadecimal representation in string format to T.
void add_to_bloom(cbdc::buffer &bloom, const cbdc::buffer &entry)
Adds an entry to a bloom value.
auto to_uint64(const evmc::uint256be &v) -> uint64_t
Converts an uint256be to a uint64_t, ignoring higher order bits.