Bitcoin ABC 0.31.6
P2P Digital Currency
wallet.h
Go to the documentation of this file.
1// Copyright (c) 2018 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_INTERFACES_WALLET_H
6#define BITCOIN_INTERFACES_WALLET_H
7
8#include <consensus/amount.h>
9#include <interfaces/chain.h> // For ChainClient
11#include <primitives/transaction.h> // For CTxOut
12#include <pubkey.h> // For CKeyID and CScriptID (definitions needed in CTxDestination instantiation)
13#include <script/sighashtype.h>
14#include <script/standard.h> // For CTxDestination
15#include <support/allocators/secure.h> // For SecureString
16#include <util/message.h>
17#include <util/result.h>
18#include <util/ui_change_type.h>
19
20#include <cstdint>
21#include <functional>
22#include <map>
23#include <memory>
24#include <string>
25#include <tuple>
26#include <utility>
27#include <vector>
28
29class CChainParams;
30class CCoinControl;
31class CKey;
33class COutPoint;
34class CTransaction;
35class CWallet;
36enum class FeeReason;
37enum class OutputType;
38enum class TransactionError;
39enum isminetype : unsigned int;
40struct CRecipient;
42struct WalletContext;
43typedef uint8_t isminefilter;
44struct TxId;
45struct bilingual_str;
46
47namespace interfaces {
48
49class Handler;
50struct WalletAddress;
51struct WalletBalances;
52struct WalletTx;
53struct WalletTxOut;
54struct WalletTxStatus;
55
56using WalletOrderForm = std::vector<std::pair<std::string, std::string>>;
57using WalletValueMap = std::map<std::string, std::string>;
58
60class Wallet {
61public:
62 virtual ~Wallet() {}
63
65 virtual bool encryptWallet(const SecureString &wallet_passphrase) = 0;
66
68 virtual bool isCrypted() = 0;
69
71 virtual bool lock() = 0;
72
74 virtual bool unlock(const SecureString &wallet_passphrase) = 0;
75
77 virtual bool isLocked() = 0;
78
80 virtual bool
81 changeWalletPassphrase(const SecureString &old_wallet_passphrase,
82 const SecureString &new_wallet_passphrase) = 0;
83
85 virtual void abortRescan() = 0;
86
88 virtual bool backupWallet(const std::string &filename) = 0;
89
91 virtual std::string getWalletName() = 0;
92
94 virtual const CChainParams &getChainParams() = 0;
95
96 // Get a new address.
98 getNewDestination(const OutputType type, const std::string &label) = 0;
99
101 virtual bool getPubKey(const CScript &script, const CKeyID &address,
102 CPubKey &pub_key) = 0;
103
105 virtual SigningResult signMessage(const std::string &message,
106 const PKHash &pkhash,
107 std::string &str_sig) = 0;
108
110 virtual bool isSpendable(const CTxDestination &dest) = 0;
111
113 virtual bool haveWatchOnly() = 0;
114
116 virtual bool setAddressBook(const CTxDestination &dest,
117 const std::string &name,
118 const std::string &purpose) = 0;
119
120 // Remove address.
121 virtual bool delAddressBook(const CTxDestination &dest) = 0;
122
124 virtual bool getAddress(const CTxDestination &dest, std::string *name,
125 isminetype *is_mine, std::string *purpose) = 0;
126
128 virtual std::vector<WalletAddress> getAddresses() = 0;
129
131 virtual bool addDestData(const CTxDestination &dest, const std::string &key,
132 const std::string &value) = 0;
133
135 virtual bool eraseDestData(const CTxDestination &dest,
136 const std::string &key) = 0;
137
139 virtual std::vector<std::string>
140 getDestValues(const std::string &prefix) = 0;
141
143 virtual void lockCoin(const COutPoint &output) = 0;
144
146 virtual void unlockCoin(const COutPoint &output) = 0;
147
149 virtual bool isLockedCoin(const COutPoint &output) = 0;
150
152 virtual void listLockedCoins(std::vector<COutPoint> &outputs) = 0;
153
156 createTransaction(const std::vector<CRecipient> &recipients,
157 const CCoinControl &coin_control, bool sign,
158 int &change_pos, Amount &fee) = 0;
159
162 WalletOrderForm order_form) = 0;
163
165 virtual bool transactionCanBeAbandoned(const TxId &txid) = 0;
166
168 virtual bool abandonTransaction(const TxId &txid) = 0;
169
171 virtual CTransactionRef getTx(const TxId &txid) = 0;
172
174 virtual WalletTx getWalletTx(const TxId &txid) = 0;
175
177 virtual std::vector<WalletTx> getWalletTxs() = 0;
178
181 virtual bool tryGetTxStatus(const TxId &txid, WalletTxStatus &tx_status,
182 int &num_blocks, int64_t &block_time) = 0;
183
185 virtual WalletTx getWalletTxDetails(const TxId &txid,
186 WalletTxStatus &tx_status,
187 WalletOrderForm &order_form,
188 bool &in_mempool, int &num_blocks) = 0;
189
191 virtual TransactionError fillPSBT(SigHashType sighash_type, bool sign,
192 bool bip32derivs,
194 bool &complete) const = 0;
195
198
200 virtual bool tryGetBalances(WalletBalances &balances,
201 BlockHash &block_hash) = 0;
202
204 virtual Amount getBalance() = 0;
205
207 virtual Amount getAvailableBalance(const CCoinControl &coin_control) = 0;
208
210 virtual isminetype txinIsMine(const CTxIn &txin) = 0;
211
213 virtual isminetype txoutIsMine(const CTxOut &txout) = 0;
214
216 virtual Amount getDebit(const CTxIn &txin, isminefilter filter) = 0;
217
219 virtual Amount getCredit(const CTxOut &txout, isminefilter filter) = 0;
220
223 using CoinsList = std::map<CTxDestination,
224 std::vector<std::tuple<COutPoint, WalletTxOut>>>;
225 virtual CoinsList listCoins() = 0;
226
228 virtual std::vector<WalletTxOut>
229 getCoins(const std::vector<COutPoint> &outputs) = 0;
230
232 virtual Amount getRequiredFee(unsigned int tx_bytes) = 0;
233
235 virtual Amount getMinimumFee(unsigned int tx_bytes,
236 const CCoinControl &coin_control) = 0;
237
238 // Return whether HD enabled.
239 virtual bool hdEnabled() = 0;
240
241 // Return whether the wallet is blank.
242 virtual bool canGetAddresses() const = 0;
243
244 // Return whether private keys enabled.
245 virtual bool privateKeysDisabled() = 0;
246
247 // Get default address type.
249
252
253 // Remove wallet.
254 virtual void remove() = 0;
255
257 virtual bool isLegacy() = 0;
258
260 using UnloadFn = std::function<void()>;
261 virtual std::unique_ptr<Handler> handleUnload(UnloadFn fn) = 0;
262
265 std::function<void(const std::string &title, int progress)>;
266 virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
267
269 using StatusChangedFn = std::function<void()>;
270 virtual std::unique_ptr<Handler>
272
274 using AddressBookChangedFn = std::function<void(
275 const CTxDestination &address, const std::string &label, bool is_mine,
276 const std::string &purpose, ChangeType status)>;
277 virtual std::unique_ptr<Handler>
279
282 std::function<void(const TxId &txid, ChangeType status)>;
283 virtual std::unique_ptr<Handler>
285
287 using WatchOnlyChangedFn = std::function<void(bool have_watch_only)>;
288 virtual std::unique_ptr<Handler>
290
292 using CanGetAddressesChangedFn = std::function<void()>;
293 virtual std::unique_ptr<Handler>
295
297 virtual CWallet *wallet() { return nullptr; }
298};
299
303class WalletClient : public ChainClient {
304public:
307 createWallet(const std::string &name, const SecureString &passphrase,
308 uint64_t wallet_creation_flags,
309 std::vector<bilingual_str> &warnings) = 0;
310
313 loadWallet(const std::string &name,
314 std::vector<bilingual_str> &warnings) = 0;
315
317 virtual std::string getWalletDir() = 0;
318
321 restoreWallet(const fs::path &backup_file, const std::string &wallet_name,
322 std::vector<bilingual_str> &warnings) = 0;
323
325 virtual std::vector<std::string> listWalletDir() = 0;
326
328 virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
329
333 using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
334 virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;
335
337 virtual WalletContext *context() { return nullptr; }
338};
339
344 std::string name;
345 std::string purpose;
346
348 std::string nameIn, std::string purposeIn)
349 : dest(std::move(destIn)), is_mine(isMineIn), name(std::move(nameIn)),
350 purpose(std::move(purposeIn)) {}
351};
352
358 bool have_watch_only = false;
362
363 bool balanceChanged(const WalletBalances &prev) const {
364 return balance != prev.balance ||
371 }
372};
373
374// Wallet transaction information.
375struct WalletTx {
377 std::vector<isminetype> txin_is_mine;
378 std::vector<isminetype> txout_is_mine;
379 std::vector<CTxDestination> txout_address;
380 std::vector<isminetype> txout_address_is_mine;
384 int64_t time;
385 std::map<std::string, std::string> value_map;
387};
388
394 unsigned int time_received;
395 uint32_t lock_time;
400};
401
405 int64_t time;
407 bool is_spent = false;
408};
409
412std::unique_ptr<Wallet> MakeWallet(WalletContext &context,
413 const std::shared_ptr<CWallet> &wallet);
414
417std::unique_ptr<WalletClient> MakeWalletClient(Chain &chain, ArgsManager &args);
418
419} // namespace interfaces
420
421#endif // BITCOIN_INTERFACES_WALLET_H
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:86
Coin Control Features.
Definition: coincontrol.h:21
An encapsulated secp256k1 private key.
Definition: key.h:28
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:22
A mutable version of CTransaction.
Definition: transaction.h:274
An encapsulated public key.
Definition: pubkey.h:31
CTransaction()
Construct a CTransaction that qualifies as IsNull()
Definition: transaction.cpp:68
An output of a transaction.
Definition: transaction.h:128
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:269
Signature hash type wrapper class.
Definition: sighashtype.h:37
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:30
Interface to let node manage chain clients (wallets, or maybe tools for monitoring and analysis in th...
Definition: chain.h:335
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:136
Wallet chain client that in addition to having chain client methods for starting up,...
Definition: wallet.h:303
virtual WalletContext * context()
Return pointer to internal context, useful for testing.
Definition: wallet.h:337
virtual std::vector< std::string > listWalletDir()=0
Return available wallets in wallet directory.
virtual util::Result< std::unique_ptr< Wallet > > restoreWallet(const fs::path &backup_file, const std::string &wallet_name, std::vector< bilingual_str > &warnings)=0
Restore backup wallet.
virtual std::unique_ptr< Handler > handleLoadWallet(LoadWalletFn fn)=0
virtual std::string getWalletDir()=0
Return default wallet directory.
std::function< void(std::unique_ptr< Wallet > wallet)> LoadWalletFn
Register handler for load wallet messages.
Definition: wallet.h:333
virtual std::vector< std::unique_ptr< Wallet > > getWallets()=0
Return interfaces for accessing wallets (if any).
virtual util::Result< std::unique_ptr< Wallet > > loadWallet(const std::string &name, std::vector< bilingual_str > &warnings)=0
Load existing wallet.
virtual util::Result< std::unique_ptr< Wallet > > createWallet(const std::string &name, const SecureString &passphrase, uint64_t wallet_creation_flags, std::vector< bilingual_str > &warnings)=0
Create new wallet.
Interface for accessing a wallet.
Definition: wallet.h:60
std::function< void(const TxId &txid, ChangeType status)> TransactionChangedFn
Register handler for transaction changed messages.
Definition: wallet.h:282
virtual std::unique_ptr< Handler > handleShowProgress(ShowProgressFn fn)=0
virtual CoinsList listCoins()=0
virtual bool getPubKey(const CScript &script, const CKeyID &address, CPubKey &pub_key)=0
Get public key.
virtual std::vector< WalletAddress > getAddresses()=0
Get wallet address list.
virtual bool encryptWallet(const SecureString &wallet_passphrase)=0
Encrypt wallet.
std::function< void(bool have_watch_only)> WatchOnlyChangedFn
Register handler for watchonly changed messages.
Definition: wallet.h:287
virtual bool isLegacy()=0
Return whether is a legacy wallet.
virtual std::unique_ptr< Handler > handleStatusChanged(StatusChangedFn fn)=0
virtual util::Result< CTransactionRef > createTransaction(const std::vector< CRecipient > &recipients, const CCoinControl &coin_control, bool sign, int &change_pos, Amount &fee)=0
Create transaction.
virtual std::vector< WalletTx > getWalletTxs()=0
Get list of all wallet transactions.
virtual bool canGetAddresses() const =0
virtual Amount getAvailableBalance(const CCoinControl &coin_control)=0
Get available balance.
virtual std::vector< WalletTxOut > getCoins(const std::vector< COutPoint > &outputs)=0
Return wallet transaction output information.
virtual bool transactionCanBeAbandoned(const TxId &txid)=0
Return whether transaction can be abandoned.
std::function< void()> UnloadFn
Register handler for unload message.
Definition: wallet.h:260
virtual bool isLocked()=0
Return whether wallet is locked.
virtual std::unique_ptr< Handler > handleAddressBookChanged(AddressBookChangedFn fn)=0
virtual WalletTx getWalletTxDetails(const TxId &txid, WalletTxStatus &tx_status, WalletOrderForm &order_form, bool &in_mempool, int &num_blocks)=0
Get transaction details.
virtual std::unique_ptr< Handler > handleCanGetAddressesChanged(CanGetAddressesChangedFn fn)=0
virtual WalletTx getWalletTx(const TxId &txid)=0
Get transaction information.
virtual CWallet * wallet()
Return pointer to internal wallet class, useful for testing.
Definition: wallet.h:297
virtual void unlockCoin(const COutPoint &output)=0
Unlock coin.
virtual util::Result< CTxDestination > getNewDestination(const OutputType type, const std::string &label)=0
virtual isminetype txinIsMine(const CTxIn &txin)=0
Return whether transaction input belongs to wallet.
virtual bool addDestData(const CTxDestination &dest, const std::string &key, const std::string &value)=0
Add dest data.
virtual void abortRescan()=0
Abort a rescan.
virtual ~Wallet()
Definition: wallet.h:62
virtual bool unlock(const SecureString &wallet_passphrase)=0
Unlock wallet.
std::function< void(const CTxDestination &address, const std::string &label, bool is_mine, const std::string &purpose, ChangeType status)> AddressBookChangedFn
Register handler for address book changed messages.
Definition: wallet.h:276
virtual bool changeWalletPassphrase(const SecureString &old_wallet_passphrase, const SecureString &new_wallet_passphrase)=0
Change wallet passphrase.
virtual const CChainParams & getChainParams()=0
Get chainparams.
virtual std::string getWalletName()=0
Get wallet name.
virtual bool setAddressBook(const CTxDestination &dest, const std::string &name, const std::string &purpose)=0
Add or update address.
std::function< void(const std::string &title, int progress)> ShowProgressFn
Register handler for show progress messages.
Definition: wallet.h:265
virtual bool isSpendable(const CTxDestination &dest)=0
Return whether wallet has private key.
virtual std::vector< std::string > getDestValues(const std::string &prefix)=0
Get dest values with prefix.
virtual bool hdEnabled()=0
virtual bool isLockedCoin(const COutPoint &output)=0
Return whether coin is locked.
std::map< CTxDestination, std::vector< std::tuple< COutPoint, WalletTxOut > > > CoinsList
Return AvailableCoins + LockedCoins grouped by wallet address.
Definition: wallet.h:224
virtual WalletBalances getBalances()=0
Get balances.
virtual OutputType getDefaultAddressType()=0
virtual bool tryGetBalances(WalletBalances &balances, BlockHash &block_hash)=0
Get balances if possible without blocking.
virtual Amount getDebit(const CTxIn &txin, isminefilter filter)=0
Return debit amount if transaction input belongs to wallet.
virtual bool backupWallet(const std::string &filename)=0
Back up wallet.
virtual void commitTransaction(CTransactionRef tx, WalletValueMap value_map, WalletOrderForm order_form)=0
Commit transaction.
virtual std::unique_ptr< Handler > handleTransactionChanged(TransactionChangedFn fn)=0
virtual void listLockedCoins(std::vector< COutPoint > &outputs)=0
List locked coins.
virtual CTransactionRef getTx(const TxId &txid)=0
Get a transaction.
virtual bool abandonTransaction(const TxId &txid)=0
Abandon transaction.
virtual Amount getBalance()=0
Get balance.
virtual std::unique_ptr< Handler > handleUnload(UnloadFn fn)=0
virtual bool eraseDestData(const CTxDestination &dest, const std::string &key)=0
Erase dest data.
virtual bool delAddressBook(const CTxDestination &dest)=0
virtual void remove()=0
virtual TransactionError fillPSBT(SigHashType sighash_type, bool sign, bool bip32derivs, PartiallySignedTransaction &psbtx, bool &complete) const =0
Fill PSBT.
std::function< void()> StatusChangedFn
Register handler for status changed messages.
Definition: wallet.h:269
virtual bool haveWatchOnly()=0
Return whether wallet has watch only keys.
virtual isminetype txoutIsMine(const CTxOut &txout)=0
Return whether transaction output belongs to wallet.
virtual bool tryGetTxStatus(const TxId &txid, WalletTxStatus &tx_status, int &num_blocks, int64_t &block_time)=0
Try to get updated status for a particular transaction, if possible without blocking.
virtual SigningResult signMessage(const std::string &message, const PKHash &pkhash, std::string &str_sig)=0
Sign message.
virtual bool privateKeysDisabled()=0
virtual bool getAddress(const CTxDestination &dest, std::string *name, isminetype *is_mine, std::string *purpose)=0
Look up address in wallet, return whether exists.
virtual Amount getCredit(const CTxOut &txout, isminefilter filter)=0
Return credit amount if transaction input belongs to wallet.
virtual bool lock()=0
Lock wallet.
virtual std::unique_ptr< Handler > handleWatchOnlyChanged(WatchOnlyChangedFn fn)=0
virtual void lockCoin(const COutPoint &output)=0
Lock coin.
virtual Amount getMinimumFee(unsigned int tx_bytes, const CCoinControl &coin_control)=0
Get minimum fee.
std::function< void()> CanGetAddressesChangedFn
Register handler for keypool changed messages.
Definition: wallet.h:292
virtual bool isCrypted()=0
Return whether wallet is encrypted.
virtual Amount getDefaultMaxTxFee()=0
Get max tx fee.
virtual Amount getRequiredFee(unsigned int tx_bytes)=0
Get required fee.
TransactionError
Definition: error.h:22
uint8_t isminefilter
Definition: wallet.h:43
isminetype
IsMine() return codes.
Definition: ismine.h:18
SigningResult
Definition: message.h:47
std::unique_ptr< Wallet > MakeWallet(const std::shared_ptr< CWallet > &wallet)
Definition: dummywallet.cpp:44
std::vector< std::pair< std::string, std::string > > WalletOrderForm
Definition: wallet.h:56
std::unique_ptr< WalletClient > MakeWalletClient(Chain &chain, ArgsManager &args)
Return implementation of ChainClient interface for a wallet client.
Definition: interfaces.cpp:621
std::map< std::string, std::string > WalletValueMap
Definition: wallet.h:57
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
OutputType
Definition: outputtype.h:16
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
const char * prefix
Definition: rest.cpp:817
const char * name
Definition: rest.cpp:47
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:55
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:85
Definition: amount.h:19
static constexpr Amount zero() noexcept
Definition: amount.h:32
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
A version of CTransaction with the PSBT format.
Definition: psbt.h:334
A TxId is the identifier of a transaction.
Definition: txid.h:14
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:35
Bilingual messages:
Definition: translation.h:17
Information about one wallet address.
Definition: wallet.h:341
CTxDestination dest
Definition: wallet.h:342
std::string purpose
Definition: wallet.h:345
WalletAddress(CTxDestination destIn, isminetype isMineIn, std::string nameIn, std::string purposeIn)
Definition: wallet.h:347
Collection of wallet balances.
Definition: wallet.h:354
Amount immature_watch_only_balance
Definition: wallet.h:361
Amount unconfirmed_watch_only_balance
Definition: wallet.h:360
bool balanceChanged(const WalletBalances &prev) const
Definition: wallet.h:363
std::vector< CTxDestination > txout_address
Definition: wallet.h:379
std::vector< isminetype > txout_is_mine
Definition: wallet.h:378
CTransactionRef tx
Definition: wallet.h:376
std::map< std::string, std::string > value_map
Definition: wallet.h:385
std::vector< isminetype > txout_address_is_mine
Definition: wallet.h:380
std::vector< isminetype > txin_is_mine
Definition: wallet.h:377
Wallet transaction output.
Definition: wallet.h:403
Updated transaction status.
Definition: wallet.h:390
unsigned int time_received
Definition: wallet.h:394
ChangeType
General change type (added, updated, removed).
Definition: ui_change_type.h:9