Bitcoin ABC 0.32.12
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
9#include <consensus/amount.h>
10#include <interfaces/chain.h> // For ChainClient
12#include <primitives/transaction.h> // For CTxOut
13#include <pubkey.h> // For CKeyID and CScriptID (definitions needed in CTxDestination instantiation)
14#include <script/sighashtype.h>
15#include <script/standard.h> // For CTxDestination
16#include <support/allocators/secure.h> // For SecureString
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;
38namespace common {
39enum class PSBTError;
40} // namespace common
41enum isminetype : unsigned int;
42struct CRecipient;
44struct WalletContext;
45typedef uint8_t isminefilter;
46struct TxId;
47struct bilingual_str;
48
49namespace interfaces {
50
51class Handler;
52struct WalletAddress;
53struct WalletBalances;
54struct WalletTx;
55struct WalletTxOut;
56struct WalletTxStatus;
57
58using WalletOrderForm = std::vector<std::pair<std::string, std::string>>;
59using WalletValueMap = std::map<std::string, std::string>;
60
62class Wallet {
63public:
64 virtual ~Wallet() {}
65
67 virtual bool encryptWallet(const SecureString &wallet_passphrase) = 0;
68
70 virtual bool isCrypted() = 0;
71
73 virtual bool lock() = 0;
74
76 virtual bool unlock(const SecureString &wallet_passphrase) = 0;
77
79 virtual bool isLocked() = 0;
80
82 virtual bool
83 changeWalletPassphrase(const SecureString &old_wallet_passphrase,
84 const SecureString &new_wallet_passphrase) = 0;
85
87 virtual void abortRescan() = 0;
88
90 virtual bool backupWallet(const std::string &filename) = 0;
91
93 virtual std::string getWalletName() = 0;
94
96 virtual const CChainParams &getChainParams() = 0;
97
98 // Get a new address.
100 getNewDestination(const OutputType type, const std::string &label) = 0;
101
103 virtual bool getPubKey(const CScript &script, const CKeyID &address,
104 CPubKey &pub_key) = 0;
105
107 virtual SigningResult signMessage(const std::string &message,
108 const PKHash &pkhash,
109 std::string &str_sig) = 0;
110
112 virtual bool isSpendable(const CTxDestination &dest) = 0;
113
115 virtual bool haveWatchOnly() = 0;
116
118 virtual bool setAddressBook(const CTxDestination &dest,
119 const std::string &name,
120 const std::string &purpose) = 0;
121
122 // Remove address.
123 virtual bool delAddressBook(const CTxDestination &dest) = 0;
124
126 virtual bool getAddress(const CTxDestination &dest, std::string *name,
127 isminetype *is_mine, std::string *purpose) = 0;
128
130 virtual std::vector<WalletAddress> getAddresses() = 0;
131
133 virtual bool addDestData(const CTxDestination &dest, const std::string &key,
134 const std::string &value) = 0;
135
137 virtual bool eraseDestData(const CTxDestination &dest,
138 const std::string &key) = 0;
139
141 virtual std::vector<std::string>
142 getDestValues(const std::string &prefix) = 0;
143
145 virtual void lockCoin(const COutPoint &output) = 0;
146
148 virtual void unlockCoin(const COutPoint &output) = 0;
149
151 virtual bool isLockedCoin(const COutPoint &output) = 0;
152
154 virtual void listLockedCoins(std::vector<COutPoint> &outputs) = 0;
155
158 createTransaction(const std::vector<CRecipient> &recipients,
159 const CCoinControl &coin_control, bool sign,
160 int &change_pos, Amount &fee) = 0;
161
164 WalletOrderForm order_form) = 0;
165
167 virtual bool transactionCanBeAbandoned(const TxId &txid) = 0;
168
170 virtual bool abandonTransaction(const TxId &txid) = 0;
171
173 virtual CTransactionRef getTx(const TxId &txid) = 0;
174
176 virtual WalletTx getWalletTx(const TxId &txid) = 0;
177
179 virtual std::vector<WalletTx> getWalletTxs() = 0;
180
183 virtual bool tryGetTxStatus(const TxId &txid, WalletTxStatus &tx_status,
184 int &num_blocks, int64_t &block_time) = 0;
185
187 virtual WalletTx getWalletTxDetails(const TxId &txid,
188 WalletTxStatus &tx_status,
189 WalletOrderForm &order_form,
190 bool &in_mempool, int &num_blocks) = 0;
191
193 virtual std::optional<common::PSBTError>
194 fillPSBT(SigHashType sighash_type, bool sign, bool bip32derivs,
195 PartiallySignedTransaction &psbtx, bool &complete) const = 0;
196
199
201 virtual bool tryGetBalances(WalletBalances &balances,
202 BlockHash &block_hash) = 0;
203
205 virtual Amount getBalance() = 0;
206
208 virtual Amount getAvailableBalance(const CCoinControl &coin_control) = 0;
209
211 virtual isminetype txinIsMine(const CTxIn &txin) = 0;
212
214 virtual isminetype txoutIsMine(const CTxOut &txout) = 0;
215
217 virtual Amount getDebit(const CTxIn &txin, isminefilter filter) = 0;
218
220 virtual Amount getCredit(const CTxOut &txout, isminefilter filter) = 0;
221
224 using CoinsList = std::map<CTxDestination,
225 std::vector<std::tuple<COutPoint, WalletTxOut>>>;
226 virtual CoinsList listCoins() = 0;
227
229 virtual std::vector<WalletTxOut>
230 getCoins(const std::vector<COutPoint> &outputs) = 0;
231
233 virtual Amount getRequiredFee(unsigned int tx_bytes) = 0;
234
236 virtual Amount getMinimumFee(unsigned int tx_bytes,
237 const CCoinControl &coin_control) = 0;
238
239 // Return whether HD enabled.
240 virtual bool hdEnabled() = 0;
241
242 // Return whether the wallet is blank.
243 virtual bool canGetAddresses() const = 0;
244
245 // Return whether private keys enabled.
246 virtual bool privateKeysDisabled() = 0;
247
248 // Get default address type.
250
253
254 // Remove wallet.
255 virtual void remove() = 0;
256
258 virtual bool isLegacy() = 0;
259
261 using UnloadFn = std::function<void()>;
262 virtual std::unique_ptr<Handler> handleUnload(UnloadFn fn) = 0;
263
266 std::function<void(const std::string &title, int progress)>;
267 virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
268
270 using StatusChangedFn = std::function<void()>;
271 virtual std::unique_ptr<Handler>
273
275 using AddressBookChangedFn = std::function<void(
276 const CTxDestination &address, const std::string &label, bool is_mine,
277 const std::string &purpose, ChangeType status)>;
278 virtual std::unique_ptr<Handler>
280
283 std::function<void(const TxId &txid, ChangeType status)>;
284 virtual std::unique_ptr<Handler>
286
288 using WatchOnlyChangedFn = std::function<void(bool have_watch_only)>;
289 virtual std::unique_ptr<Handler>
291
293 using CanGetAddressesChangedFn = std::function<void()>;
294 virtual std::unique_ptr<Handler>
296
298 virtual CWallet *wallet() { return nullptr; }
299};
300
304class WalletClient : public ChainClient {
305public:
308 createWallet(const std::string &name, const SecureString &passphrase,
309 uint64_t wallet_creation_flags,
310 std::vector<bilingual_str> &warnings) = 0;
311
314 loadWallet(const std::string &name,
315 std::vector<bilingual_str> &warnings) = 0;
316
318 virtual std::string getWalletDir() = 0;
319
322 restoreWallet(const fs::path &backup_file, const std::string &wallet_name,
323 std::vector<bilingual_str> &warnings) = 0;
324
326 virtual std::vector<std::string> listWalletDir() = 0;
327
329 virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
330
334 using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
335 virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;
336
338 virtual WalletContext *context() { return nullptr; }
339};
340
345 std::string name;
346 std::string purpose;
347
349 std::string nameIn, std::string purposeIn)
350 : dest(std::move(destIn)), is_mine(isMineIn), name(std::move(nameIn)),
351 purpose(std::move(purposeIn)) {}
352};
353
359 bool have_watch_only = false;
363
364 bool balanceChanged(const WalletBalances &prev) const {
365 return balance != prev.balance ||
372 }
373};
374
375// Wallet transaction information.
376struct WalletTx {
378 std::vector<isminetype> txin_is_mine;
379 std::vector<isminetype> txout_is_mine;
380 std::vector<CTxDestination> txout_address;
381 std::vector<isminetype> txout_address_is_mine;
385 int64_t time;
386 std::map<std::string, std::string> value_map;
388};
389
395 unsigned int time_received;
396 uint32_t lock_time;
401};
402
406 int64_t time;
408 bool is_spent = false;
409};
410
413std::unique_ptr<Wallet> MakeWallet(WalletContext &context,
414 const std::shared_ptr<CWallet> &wallet);
415
418std::unique_ptr<WalletClient> MakeWalletClient(Chain &chain, ArgsManager &args);
419
420} // namespace interfaces
421
422#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:272
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:332
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:304
virtual WalletContext * context()
Return pointer to internal context, useful for testing.
Definition: wallet.h:338
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:334
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:62
std::function< void(const TxId &txid, ChangeType status)> TransactionChangedFn
Register handler for transaction changed messages.
Definition: wallet.h:283
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:288
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:261
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:298
virtual void unlockCoin(const COutPoint &output)=0
Unlock coin.
virtual std::optional< common::PSBTError > fillPSBT(SigHashType sighash_type, bool sign, bool bip32derivs, PartiallySignedTransaction &psbtx, bool &complete) const =0
Fill PSBT.
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:64
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:277
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:266
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:225
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
std::function< void()> StatusChangedFn
Register handler for status changed messages.
Definition: wallet.h:270
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:293
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.
uint8_t isminefilter
Definition: wallet.h:45
isminetype
IsMine() return codes.
Definition: ismine.h:18
Definition: args.cpp:864
PSBTError
Definition: types.h:17
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:58
std::unique_ptr< WalletClient > MakeWalletClient(Chain &chain, ArgsManager &args)
Return implementation of ChainClient interface for a wallet client.
Definition: interfaces.cpp:622
std::map< std::string, std::string > WalletValueMap
Definition: wallet.h:59
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:813
const char * name
Definition: rest.cpp:47
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:55
SigningResult
Definition: signmessage.h:47
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:85
Definition: amount.h:21
static constexpr Amount zero() noexcept
Definition: amount.h:34
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:342
CTxDestination dest
Definition: wallet.h:343
std::string purpose
Definition: wallet.h:346
WalletAddress(CTxDestination destIn, isminetype isMineIn, std::string nameIn, std::string purposeIn)
Definition: wallet.h:348
Collection of wallet balances.
Definition: wallet.h:355
Amount immature_watch_only_balance
Definition: wallet.h:362
Amount unconfirmed_watch_only_balance
Definition: wallet.h:361
bool balanceChanged(const WalletBalances &prev) const
Definition: wallet.h:364
std::vector< CTxDestination > txout_address
Definition: wallet.h:380
std::vector< isminetype > txout_is_mine
Definition: wallet.h:379
CTransactionRef tx
Definition: wallet.h:377
std::map< std::string, std::string > value_map
Definition: wallet.h:386
std::vector< isminetype > txout_address_is_mine
Definition: wallet.h:381
std::vector< isminetype > txin_is_mine
Definition: wallet.h:378
Wallet transaction output.
Definition: wallet.h:404
Updated transaction status.
Definition: wallet.h:391
unsigned int time_received
Definition: wallet.h:395
ChangeType
General change type (added, updated, removed).
Definition: ui_change_type.h:9