OpenCBDC Transaction Processor
Loading...
Searching...
No Matches
uhs/client/client.hpp
Go to the documentation of this file.
1// Copyright (c) 2021 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_CLIENT_CLIENT_H_
7#define OPENCBDC_TX_SRC_CLIENT_CLIENT_H_
8
12
13namespace cbdc {
14 namespace address {
15 static constexpr auto bits_per_byte = 8;
16 static constexpr auto bech32_bits_per_symbol = 5;
17
18 auto decode(const std::string& addr_str)
19 -> std::optional<cbdc::hash_t>;
20 }
21
23 class client {
24 public:
33 std::shared_ptr<logging::log> logger,
34 std::string wallet_file,
35 std::string client_file);
36
37 virtual ~client() = default;
38
39 client(const client&) = delete;
40 auto operator=(const client&) -> client& = delete;
41
42 client(client&&) = delete;
43 auto operator=(client&&) -> client& = delete;
44
50 auto init() -> bool;
51
58 static auto print_amount(uint64_t val) -> std::string;
59
69 auto mint(size_t n_outputs, uint32_t output_val)
71
82 auto send(uint32_t value, const pubkey_t& payee)
83 -> std::pair<std::optional<transaction::full_tx>,
84 std::optional<cbdc::sentinel::execute_response>>;
85
98 auto fan(uint32_t count, uint32_t value, const pubkey_t& payee)
99 -> std::pair<std::optional<transaction::full_tx>,
100 std::optional<cbdc::sentinel::execute_response>>;
101
114 static auto export_send_inputs(const transaction::full_tx& send_tx,
115 const pubkey_t& payee)
116 -> std::vector<transaction::input>;
117
126
134 virtual auto sync() -> bool = 0;
135
140 auto new_address() -> pubkey_t;
141
145 auto balance() -> uint64_t;
146
152 auto utxo_count() -> size_t;
153
156 auto pending_tx_count() -> size_t;
157
165 auto pending_input_count() -> size_t;
166
177 auto confirm_transaction(const hash_t& tx_id) -> bool;
178
188 auto create_transaction(uint32_t value, const pubkey_t& payee)
189 -> std::optional<transaction::full_tx>;
190
199 -> std::optional<cbdc::sentinel::execute_response>;
200
211 auto abandon_transaction(const hash_t& tx_id) -> bool;
212
222 auto check_pending(const transaction::input& inp) -> bool;
223
228
232 enum class address_type : uint8_t {
234 public_key = 0
235 };
236
237 protected:
243 virtual auto init_derived() -> bool = 0;
244
252 virtual auto send_mint_tx(const transaction::full_tx& mint_tx) -> bool
253 = 0;
254
261 [[nodiscard]] auto pending_txs() const
262 -> std::unordered_map<hash_t, transaction::full_tx, hashing::null>;
263
270 [[nodiscard]] auto pending_inputs() const
271 -> std::unordered_map<hash_t, transaction::input, hashing::null>;
272
273 private:
274 cbdc::config::options m_opts;
275 std::shared_ptr<logging::log> m_logger;
276
277 cbdc::sentinel::rpc::client m_sentinel_client;
278
281 std::unordered_map<hash_t, transaction::full_tx, hashing::null>
282 m_pending_txs;
283
287 std::unordered_map<hash_t, transaction::input, hashing::null>
288 m_pending_spend;
289
292 std::unordered_map<hash_t, transaction::input, hashing::null>
293 m_pending_inputs;
294
295 transaction::wallet m_wallet{};
296
297 std::string m_client_file;
298 std::string m_wallet_file;
299
303 void import_transaction(const transaction::full_tx& tx);
304
305 void load_client_state();
306 void save_client_state();
307
308 void save();
309
310 void register_pending_tx(const transaction::full_tx& tx);
311 };
312}
313
314#endif // OPENCBDC_TX_SRC_CLIENT_CLIENT_H_
External client for sending new transactions to the system.
auto pending_inputs() const -> std::unordered_map< hash_t, transaction::input, hashing::null >
Returns the set of imported inputs from senders.
auto fan(uint32_t count, uint32_t value, const pubkey_t &payee) -> std::pair< std::optional< transaction::full_tx >, std::optional< cbdc::sentinel::execute_response > >
Send a specified number of fixed-value outputs from this client's wallet to a target address.
auto init() -> bool
Initializes the client.
virtual auto sync() -> bool=0
Checks the status of pending transactions and updates the wallet's balance with the result.
auto pending_tx_count() -> size_t
Returns the number of unconfirmed transactions.
client(client &&)=delete
virtual auto send_mint_tx(const transaction::full_tx &mint_tx) -> bool=0
Sends the given minting transaction to a service that will accept and process it.
auto confirm_transaction(const hash_t &tx_id) -> bool
Confirms the transaction with the given ID.
client(cbdc::config::options opts, std::shared_ptr< logging::log > logger, std::string wallet_file, std::string client_file)
Constructor.
auto balance() -> uint64_t
Returns the balance in this client's wallet.
auto new_address() -> pubkey_t
Generates a new wallet address that other clients can use to send money to this client using send.
auto abandon_transaction(const hash_t &tx_id) -> bool
Abandons a transaction currently awaiting confirmation.
void sign_transaction(transaction::full_tx &tx)
Signs the given transaction for as far as client's wallet contains the transaction's keys.
auto pending_txs() const -> std::unordered_map< hash_t, transaction::full_tx, hashing::null >
Returns the set of transactions pending confirmation.
virtual auto init_derived() -> bool=0
Initializes the derived class.
virtual ~client()=default
void import_send_input(const transaction::input &in)
Imports transaction data from a sender.
auto send_transaction(const transaction::full_tx &tx) -> std::optional< cbdc::sentinel::execute_response >
Send the given transaction to the sentinel.
auto operator=(const client &) -> client &=delete
auto operator=(client &&) -> client &=delete
auto create_transaction(uint32_t value, const pubkey_t &payee) -> std::optional< transaction::full_tx >
Create a new transaction.
static auto export_send_inputs(const transaction::full_tx &send_tx, const pubkey_t &payee) -> std::vector< transaction::input >
Extracts the transaction data that recipients need from senders to confirm pending transfers.
auto utxo_count() -> size_t
Returns the number of UTXOs in this client's wallet.
address_type
Client address type signifier.
@ public_key
Pay-to-Public-Key (P2PK) address data.
auto check_pending(const transaction::input &inp) -> bool
Checks the client's pending transaction set for the specified transaction.
client(const client &)=delete
auto pending_input_count() -> size_t
Returns the number of pending received inputs.
auto send(uint32_t value, const pubkey_t &payee) -> std::pair< std::optional< transaction::full_tx >, std::optional< cbdc::sentinel::execute_response > >
Send a specified amount from this client's wallet to a target address.
auto mint(size_t n_outputs, uint32_t output_val) -> transaction::full_tx
Creates the specified number spendable outputs each with the specified value.
static auto print_amount(uint64_t val) -> std::string
Format a value given in currency base units as USD.
auto decode(const std::string &addr_str) -> std::optional< cbdc::hash_t >
std::array< unsigned char, cbdc::hash_size > hash_t
SHA256 hash container.
std::array< unsigned char, pubkey_len > pubkey_t
A public key of a public/private keypair.
Definition keys.hpp:25
Project-wide configuration options.
Definition config.hpp:132
A complete transaction.
An input for a new transaction.