OpenCBDC Transaction Processor
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends
wallet.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_TRANSACTION_WALLET_H_
7#define OPENCBDC_TX_SRC_TRANSACTION_WALLET_H_
8
13
14#include <atomic>
15#include <fstream>
16#include <list>
17#include <memory>
18#include <mutex>
19#include <optional>
20#include <random>
21#include <secp256k1.h>
22#include <set>
23#include <shared_mutex>
24#include <unordered_map>
25#include <unordered_set>
26
27namespace cbdc::transaction {
32 class wallet {
33 public:
37 wallet();
38
48 auto mint_new_coins(size_t n_outputs, uint32_t output_val) -> full_tx;
49
61 auto send_to(uint32_t amount, const pubkey_t& payee, bool sign_tx)
62 -> std::optional<full_tx>;
63
79 auto send_to(size_t input_count,
80 size_t output_count,
81 const pubkey_t& payee,
82 bool sign_tx) -> std::optional<full_tx>;
83
98 auto fan(size_t output_count,
99 uint32_t value,
100 const pubkey_t& payee,
101 bool sign_tx) -> std::optional<transaction::full_tx>;
102
116 static auto export_send_inputs(const full_tx& send_tx,
117 const pubkey_t& payee)
118 -> std::vector<input>;
119
123 auto generate_key() -> pubkey_t;
124
134 auto seed(const privkey_t& privkey,
135 uint32_t value,
136 size_t begin_seed,
137 size_t end_seed) -> bool;
138
153 void seed_readonly(const hash_t& witness_commitment,
154 uint32_t value,
155 size_t begin_seed,
156 size_t end_seed);
157
169 void confirm_transaction(const full_tx& tx);
170
173 void sign(full_tx& tx) const;
174
177 auto is_spendable(const input& in) const -> bool;
178
182 auto balance() const -> uint64_t;
183
186 auto count() const -> size_t;
187
190 void save(const std::string& wallet_file) const;
191
195 void load(const std::string& wallet_file);
196
206 auto create_seeded_transaction(size_t seed_idx)
207 -> std::optional<full_tx>;
208
212 void confirm_inputs(const std::vector<input>& credits);
213
214 private:
215 struct cmp_input {
216 auto operator()(const input& lhs, const input& rhs) const -> bool {
217 // First sort by tx hash then output index
218 return std::tie(lhs.m_prevout.m_tx_id, lhs.m_prevout.m_index)
219 < std::tie(rhs.m_prevout.m_tx_id, rhs.m_prevout.m_index);
220 }
221 };
222
225 mutable std::shared_mutex m_utxos_mut;
226 uint64_t m_balance{0};
228 std::set<input, cmp_input> m_utxos_set;
229 size_t m_seed_from{0};
230 size_t m_seed_to{0};
231 uint32_t m_seed_value{0};
232 hash_t m_seed_witness_commitment{0};
234 std::list<input> m_spend_queue;
235
239 mutable std::shared_mutex m_keys_mut;
240 std::unordered_map<pubkey_t,
241 privkey_t,
243 m_keys;
244 std::vector<pubkey_t> m_pubkeys;
245 std::default_random_engine m_shuffle;
246
247 // TODO: currently this map grows unbounded, we need to garbage
248 // collect it
249 std::unordered_map<hash_t, pubkey_t, hashing::const_sip_hash<hash_t>>
250 m_witness_programs;
251
257 auto create_seeded_input(size_t seed_idx) -> std::optional<input>;
258
259 static const inline auto m_secp
260 = std::unique_ptr<secp256k1_context,
261 decltype(&secp256k1_context_destroy)>(
262 secp256k1_context_create(SECP256K1_CONTEXT_SIGN),
263 &secp256k1_context_destroy);
264
265 static const inline auto m_random_source
266 = std::make_unique<random_source>(config::random_source);
267
272 void update_balance(const std::vector<input>& credits,
273 const std::vector<input>& debits);
274
275 auto accumulate_inputs(uint64_t amount)
276 -> std::optional<std::pair<full_tx, uint64_t>>;
277 };
278}
279
280#endif // OPENCBDC_TX_SRC_TRANSACTION_WALLET_H_
Cryptographic wallet for digital currency assets and secrets.
Definition wallet.hpp:32
void confirm_inputs(const std::vector< input > &credits)
Given a set of credit inputs, add the UTXOs and update the wallet's balance.
Definition wallet.cpp:449
void seed_readonly(const hash_t &witness_commitment, uint32_t value, size_t begin_seed, size_t end_seed)
Marks the wallet as having read-only pre-seeded outputs to spend.
Definition wallet.cpp:260
auto generate_key() -> pubkey_t
Generates a new public key at which this wallet can receive payments via send_to.
Definition wallet.cpp:124
void load(const std::string &wallet_file)
Overwrites the current state of the wallet with data loaded from a file saved via the Wallet::save fu...
Definition wallet.cpp:329
void confirm_transaction(const full_tx &tx)
Confirms a transaction.
Definition wallet.cpp:271
auto mint_new_coins(size_t n_outputs, uint32_t output_val) -> full_tx
Mints new spendable outputs.
Definition wallet.cpp:25
auto count() const -> size_t
Returns the number of UTXOs stored in this wallet.
Definition wallet.cpp:298
static auto export_send_inputs(const full_tx &send_tx, const pubkey_t &payee) -> std::vector< input >
Extracts the transaction data that recipients need from senders to confirm pending transfers.
Definition wallet.cpp:109
auto send_to(uint32_t amount, const pubkey_t &payee, bool sign_tx) -> std::optional< full_tx >
Generates a new send transaction with a set value.
Definition wallet.cpp:46
void sign(full_tx &tx) const
Signs each of the transaction's inputs using Schnorr signatures.
Definition wallet.cpp:158
void save(const std::string &wallet_file) const
Save the state of the wallet to a binary data file.
Definition wallet.cpp:307
auto is_spendable(const input &in) const -> bool
Checks if the input is spendable by the current wallet.
Definition wallet.cpp:536
auto create_seeded_transaction(size_t seed_idx) -> std::optional< full_tx >
Creates a new transaction from seeded outputs.
Definition wallet.cpp:82
auto fan(size_t output_count, uint32_t value, const pubkey_t &payee, bool sign_tx) -> std::optional< transaction::full_tx >
Generates a transaction sending multiple outputs of a set value.
Definition wallet.cpp:454
auto seed(const privkey_t &privkey, uint32_t value, size_t begin_seed, size_t end_seed) -> bool
Marks the wallet as having pre-seeded outputs to spend.
Definition wallet.cpp:236
wallet()
Constructor.
Definition wallet.cpp:17
auto balance() const -> uint64_t
Returns the total balance of the wallet, e.g.
Definition wallet.cpp:288
Tools for reading options from a configuration file and building application-specific parameter sets ...
struct secp256k1_context_struct secp256k1_context
Definition keys.hpp:14
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.
Definition keys.hpp:23
std::array< unsigned char, pubkey_len > pubkey_t
A public key of a public/private keypair.
Definition keys.hpp:25
Pseudorandom number generator (PRNG) for generating random data from a given entropy source.
SipHash function to generate STL data structure hash keys for system IDs.
Definition hashmap.hpp:27
A complete transaction.
An input for a new transaction.
out_point m_prevout
The unique identifier of the output.
hash_t m_tx_id
The hash of the transaction which created the out_point.
uint64_t m_index
The index of the output in the transaction's output list.