Bitcoin Core 22.99.0
P2P Digital Currency
wallet.h
Go to the documentation of this file.
1// Copyright (c) 2018-2020 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
10#include <pubkey.h> // For CKeyID and CScriptID (definitions needed in CTxDestination instantiation)
11#include <script/standard.h> // For CTxDestination
12#include <support/allocators/secure.h> // For SecureString
13#include <util/message.h>
14#include <util/ui_change_type.h>
15
16#include <functional>
17#include <map>
18#include <memory>
19#include <stdint.h>
20#include <string>
21#include <tuple>
22#include <utility>
23#include <vector>
24
25class CCoinControl;
26class CFeeRate;
27class CKey;
28class CWallet;
29enum class FeeReason;
30enum class OutputType;
31enum class TransactionError;
32enum isminetype : unsigned int;
33struct CRecipient;
35struct WalletContext;
36struct bilingual_str;
37typedef uint8_t isminefilter;
38
39namespace interfaces {
40
41class Handler;
42struct WalletAddress;
43struct WalletBalances;
44struct WalletTx;
45struct WalletTxOut;
46struct WalletTxStatus;
47
48using WalletOrderForm = std::vector<std::pair<std::string, std::string>>;
49using WalletValueMap = std::map<std::string, std::string>;
50
52class Wallet
53{
54public:
55 virtual ~Wallet() {}
56
58 virtual bool encryptWallet(const SecureString& wallet_passphrase) = 0;
59
61 virtual bool isCrypted() = 0;
62
64 virtual bool lock() = 0;
65
67 virtual bool unlock(const SecureString& wallet_passphrase) = 0;
68
70 virtual bool isLocked() = 0;
71
73 virtual bool changeWalletPassphrase(const SecureString& old_wallet_passphrase,
74 const SecureString& new_wallet_passphrase) = 0;
75
77 virtual void abortRescan() = 0;
78
80 virtual bool backupWallet(const std::string& filename) = 0;
81
83 virtual std::string getWalletName() = 0;
84
85 // Get a new address.
86 virtual bool getNewDestination(const OutputType type, const std::string label, CTxDestination& dest) = 0;
87
89 virtual bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) = 0;
90
92 virtual SigningResult signMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) = 0;
93
95 virtual bool isSpendable(const CTxDestination& dest) = 0;
96
98 virtual bool haveWatchOnly() = 0;
99
101 virtual bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::string& purpose) = 0;
102
103 // Remove address.
104 virtual bool delAddressBook(const CTxDestination& dest) = 0;
105
107 virtual bool getAddress(const CTxDestination& dest,
108 std::string* name,
109 isminetype* is_mine,
110 std::string* purpose) = 0;
111
113 virtual std::vector<WalletAddress> getAddresses() = 0;
114
116 virtual std::vector<std::string> getAddressReceiveRequests() = 0;
117
119 virtual bool setAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& value) = 0;
120
122 virtual bool displayAddress(const CTxDestination& dest) = 0;
123
125 virtual bool lockCoin(const COutPoint& output, const bool write_to_db) = 0;
126
128 virtual bool unlockCoin(const COutPoint& output) = 0;
129
131 virtual bool isLockedCoin(const COutPoint& output) = 0;
132
134 virtual void listLockedCoins(std::vector<COutPoint>& outputs) = 0;
135
137 virtual CTransactionRef createTransaction(const std::vector<CRecipient>& recipients,
138 const CCoinControl& coin_control,
139 bool sign,
140 int& change_pos,
141 CAmount& fee,
142 bilingual_str& fail_reason) = 0;
143
146 WalletValueMap value_map,
147 WalletOrderForm order_form) = 0;
148
150 virtual bool transactionCanBeAbandoned(const uint256& txid) = 0;
151
153 virtual bool abandonTransaction(const uint256& txid) = 0;
154
156 virtual bool transactionCanBeBumped(const uint256& txid) = 0;
157
159 virtual bool createBumpTransaction(const uint256& txid,
160 const CCoinControl& coin_control,
161 std::vector<bilingual_str>& errors,
162 CAmount& old_fee,
163 CAmount& new_fee,
164 CMutableTransaction& mtx) = 0;
165
168
170 virtual bool commitBumpTransaction(const uint256& txid,
172 std::vector<bilingual_str>& errors,
173 uint256& bumped_txid) = 0;
174
176 virtual CTransactionRef getTx(const uint256& txid) = 0;
177
179 virtual WalletTx getWalletTx(const uint256& txid) = 0;
180
182 virtual std::vector<WalletTx> getWalletTxs() = 0;
183
185 virtual bool tryGetTxStatus(const uint256& txid,
186 WalletTxStatus& tx_status,
187 int& num_blocks,
188 int64_t& block_time) = 0;
189
192 WalletTxStatus& tx_status,
193 WalletOrderForm& order_form,
194 bool& in_mempool,
195 int& num_blocks) = 0;
196
198 virtual TransactionError fillPSBT(int sighash_type,
199 bool sign,
200 bool bip32derivs,
201 size_t* n_signed,
203 bool& complete) = 0;
204
207
209 virtual bool tryGetBalances(WalletBalances& balances, uint256& block_hash) = 0;
210
212 virtual CAmount getBalance() = 0;
213
215 virtual CAmount getAvailableBalance(const CCoinControl& coin_control) = 0;
216
218 virtual isminetype txinIsMine(const CTxIn& txin) = 0;
219
221 virtual isminetype txoutIsMine(const CTxOut& txout) = 0;
222
224 virtual CAmount getDebit(const CTxIn& txin, isminefilter filter) = 0;
225
227 virtual CAmount getCredit(const CTxOut& txout, isminefilter filter) = 0;
228
231 using CoinsList = std::map<CTxDestination, std::vector<std::tuple<COutPoint, WalletTxOut>>>;
232 virtual CoinsList listCoins() = 0;
233
235 virtual std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) = 0;
236
238 virtual CAmount getRequiredFee(unsigned int tx_bytes) = 0;
239
241 virtual CAmount getMinimumFee(unsigned int tx_bytes,
242 const CCoinControl& coin_control,
243 int* returned_target,
244 FeeReason* reason) = 0;
245
247 virtual unsigned int getConfirmTarget() = 0;
248
249 // Return whether HD enabled.
250 virtual bool hdEnabled() = 0;
251
252 // Return whether the wallet is blank.
253 virtual bool canGetAddresses() = 0;
254
255 // Return whether private keys enabled.
256 virtual bool privateKeysDisabled() = 0;
257
258 // Return whether wallet uses an external signer.
259 virtual bool hasExternalSigner() = 0;
260
261 // Get default address type.
263
266
267 // Remove wallet.
268 virtual void remove() = 0;
269
271 virtual bool isLegacy() = 0;
272
274 using UnloadFn = std::function<void()>;
275 virtual std::unique_ptr<Handler> handleUnload(UnloadFn fn) = 0;
276
278 using ShowProgressFn = std::function<void(const std::string& title, int progress)>;
279 virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
280
282 using StatusChangedFn = std::function<void()>;
283 virtual std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) = 0;
284
286 using AddressBookChangedFn = std::function<void(const CTxDestination& address,
287 const std::string& label,
288 bool is_mine,
289 const std::string& purpose,
290 ChangeType status)>;
291 virtual std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) = 0;
292
294 using TransactionChangedFn = std::function<void(const uint256& txid, ChangeType status)>;
295 virtual std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) = 0;
296
298 using WatchOnlyChangedFn = std::function<void(bool have_watch_only)>;
299 virtual std::unique_ptr<Handler> handleWatchOnlyChanged(WatchOnlyChangedFn fn) = 0;
300
302 using CanGetAddressesChangedFn = std::function<void()>;
303 virtual std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) = 0;
304
306 virtual CWallet* wallet() { return nullptr; }
307};
308
313{
314public:
316 virtual std::unique_ptr<Wallet> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0;
317
319 virtual std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) = 0;
320
322 virtual std::string getWalletDir() = 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
342{
345 std::string name;
346 std::string purpose;
347
349 : dest(std::move(dest)), is_mine(is_mine), name(std::move(name)), purpose(std::move(purpose))
350 {
351 }
352};
353
356{
360 bool have_watch_only = false;
364
365 bool balanceChanged(const WalletBalances& prev) const
366 {
367 return balance != prev.balance || unconfirmed_balance != prev.unconfirmed_balance ||
371 }
372};
373
374// Wallet transaction information.
376{
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
392{
396 unsigned int time_received;
397 uint32_t lock_time;
403};
404
407{
409 int64_t time;
411 bool is_spent = false;
412};
413
416std::unique_ptr<Wallet> MakeWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet);
417
420std::unique_ptr<WalletClient> MakeWalletClient(Chain& chain, ArgsManager& args);
421
422} // namespace interfaces
423
424#endif // BITCOIN_INTERFACES_WALLET_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
Coin Control Features.
Definition: coincontrol.h:29
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:30
An encapsulated private key.
Definition: key.h:27
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:23
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:27
An encapsulated public key.
Definition: pubkey.h:33
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
An input of a transaction.
Definition: transaction.h:66
An output of a transaction.
Definition: transaction.h:129
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:229
Interface to let node manage chain clients (wallets, or maybe tools for monitoring and analysis in th...
Definition: chain.h:298
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Definition: chain.h:93
Wallet chain client that in addition to having chain client methods for starting up,...
Definition: wallet.h:313
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 std::unique_ptr< Wallet > createWallet(const std::string &name, const SecureString &passphrase, uint64_t wallet_creation_flags, bilingual_str &error, std::vector< bilingual_str > &warnings)=0
Create new 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 std::unique_ptr< Wallet > loadWallet(const std::string &name, bilingual_str &error, std::vector< bilingual_str > &warnings)=0
Load existing wallet.
Interface for accessing a wallet.
Definition: wallet.h:53
virtual std::unique_ptr< Handler > handleShowProgress(ShowProgressFn fn)=0
virtual bool unlockCoin(const COutPoint &output)=0
Unlock coin.
virtual bool getNewDestination(const OutputType type, const std::string label, CTxDestination &dest)=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.
std::function< void(const std::string &title, int progress)> ShowProgressFn
Register handler for show progress messages.
Definition: wallet.h:278
virtual WalletTx getWalletTx(const uint256 &txid)=0
Get transaction information.
virtual bool tryGetTxStatus(const uint256 &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 bool createBumpTransaction(const uint256 &txid, const CCoinControl &coin_control, std::vector< bilingual_str > &errors, CAmount &old_fee, CAmount &new_fee, CMutableTransaction &mtx)=0
Create bump transaction.
virtual bool encryptWallet(const SecureString &wallet_passphrase)=0
Encrypt wallet.
virtual CAmount getCredit(const CTxOut &txout, isminefilter filter)=0
Return credit amount if transaction input belongs to wallet.
std::function< void(bool have_watch_only)> WatchOnlyChangedFn
Register handler for watchonly changed messages.
Definition: wallet.h:298
virtual bool isLegacy()=0
Return whether is a legacy wallet.
virtual std::unique_ptr< Handler > handleStatusChanged(StatusChangedFn fn)=0
virtual std::vector< WalletTx > getWalletTxs()=0
Get list of all wallet transactions.
std::function< void(const uint256 &txid, ChangeType status)> TransactionChangedFn
Register handler for transaction changed messages.
Definition: wallet.h:294
virtual std::vector< WalletTxOut > getCoins(const std::vector< COutPoint > &outputs)=0
Return wallet transaction output information.
std::function< void()> UnloadFn
Register handler for unload message.
Definition: wallet.h:274
virtual TransactionError fillPSBT(int sighash_type, bool sign, bool bip32derivs, size_t *n_signed, PartiallySignedTransaction &psbtx, bool &complete)=0
Fill PSBT.
virtual bool isLocked()=0
Return whether wallet is locked.
virtual std::unique_ptr< Handler > handleAddressBookChanged(AddressBookChangedFn fn)=0
virtual std::unique_ptr< Handler > handleCanGetAddressesChanged(CanGetAddressesChangedFn fn)=0
virtual CAmount getAvailableBalance(const CCoinControl &coin_control)=0
Get available balance.
virtual CWallet * wallet()
Return pointer to internal wallet class, useful for testing.
Definition: wallet.h:306
virtual CAmount getDebit(const CTxIn &txin, isminefilter filter)=0
Return debit amount if transaction input belongs to wallet.
virtual CAmount getRequiredFee(unsigned int tx_bytes)=0
Get required fee.
virtual isminetype txinIsMine(const CTxIn &txin)=0
Return whether transaction input belongs to wallet.
virtual void abortRescan()=0
Abort a rescan.
virtual unsigned int getConfirmTarget()=0
Get tx confirm target.
virtual ~Wallet()
Definition: wallet.h:55
virtual bool unlock(const SecureString &wallet_passphrase)=0
Unlock wallet.
virtual bool changeWalletPassphrase(const SecureString &old_wallet_passphrase, const SecureString &new_wallet_passphrase)=0
Change wallet passphrase.
virtual std::string getWalletName()=0
Get wallet name.
virtual bool hasExternalSigner()=0
virtual CTransactionRef createTransaction(const std::vector< CRecipient > &recipients, const CCoinControl &coin_control, bool sign, int &change_pos, CAmount &fee, bilingual_str &fail_reason)=0
Create transaction.
virtual bool setAddressBook(const CTxDestination &dest, const std::string &name, const std::string &purpose)=0
Add or update address.
virtual bool tryGetBalances(WalletBalances &balances, uint256 &block_hash)=0
Get balances if possible without blocking.
virtual CAmount getDefaultMaxTxFee()=0
Get max tx fee.
virtual bool isSpendable(const CTxDestination &dest)=0
Return whether wallet has private key.
virtual CAmount getBalance()=0
Get balance.
virtual bool displayAddress(const CTxDestination &dest)=0
Display address on external signer.
virtual bool hdEnabled()=0
virtual bool isLockedCoin(const COutPoint &output)=0
Return whether coin is locked.
virtual bool signBumpTransaction(CMutableTransaction &mtx)=0
Sign bump transaction.
std::map< CTxDestination, std::vector< std::tuple< COutPoint, WalletTxOut > > > CoinsList
Return AvailableCoins + LockedCoins grouped by wallet address.
Definition: wallet.h:231
virtual WalletBalances getBalances()=0
Get balances.
virtual OutputType getDefaultAddressType()=0
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:290
virtual bool transactionCanBeBumped(const uint256 &txid)=0
Return whether transaction can be bumped.
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 CTransactionRef getTx(const uint256 &txid)=0
Get a transaction.
virtual void listLockedCoins(std::vector< COutPoint > &outputs)=0
List locked coins.
virtual CAmount getMinimumFee(unsigned int tx_bytes, const CCoinControl &coin_control, int *returned_target, FeeReason *reason)=0
Get minimum fee.
virtual bool lockCoin(const COutPoint &output, const bool write_to_db)=0
Lock coin.
virtual std::unique_ptr< Handler > handleUnload(UnloadFn fn)=0
virtual WalletTx getWalletTxDetails(const uint256 &txid, WalletTxStatus &tx_status, WalletOrderForm &order_form, bool &in_mempool, int &num_blocks)=0
Get transaction details.
virtual bool delAddressBook(const CTxDestination &dest)=0
virtual void remove()=0
virtual bool commitBumpTransaction(const uint256 &txid, CMutableTransaction &&mtx, std::vector< bilingual_str > &errors, uint256 &bumped_txid)=0
Commit bump transaction.
std::function< void()> StatusChangedFn
Register handler for status changed messages.
Definition: wallet.h:282
virtual bool transactionCanBeAbandoned(const uint256 &txid)=0
Return whether transaction can be abandoned.
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 setAddressReceiveRequest(const CTxDestination &dest, const std::string &id, const std::string &value)=0
Save or remove receive request.
virtual SigningResult signMessage(const std::string &message, const PKHash &pkhash, std::string &str_sig)=0
Sign message.
virtual bool abandonTransaction(const uint256 &txid)=0
Abandon transaction.
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 bool lock()=0
Lock wallet.
virtual std::unique_ptr< Handler > handleWatchOnlyChanged(WatchOnlyChangedFn fn)=0
std::function< void()> CanGetAddressesChangedFn
Register handler for keypool changed messages.
Definition: wallet.h:302
virtual std::vector< std::string > getAddressReceiveRequests()=0
Get receive requests.
virtual bool canGetAddresses()=0
virtual bool isCrypted()=0
Return whether wallet is encrypted.
256-bit opaque blob.
Definition: uint256.h:124
TransactionError
Definition: error.h:22
uint8_t isminefilter
Definition: wallet.h:36
isminetype
IsMine() return codes, which depend on ScriptPubKeyMan implementation.
Definition: ismine.h:39
SigningResult
Definition: message.h:42
std::unique_ptr< Wallet > MakeWallet(const std::shared_ptr< CWallet > &wallet)
Definition: dummywallet.cpp:62
std::vector< std::pair< std::string, std::string > > WalletOrderForm
Definition: wallet.h:48
std::unique_ptr< WalletClient > MakeWalletClient(Chain &chain, ArgsManager &args)
Return implementation of ChainClient interface for a wallet client.
Definition: dummywallet.cpp:67
std::map< std::string, std::string > WalletValueMap
Definition: wallet.h:49
OutputType
Definition: outputtype.h:18
FeeReason
Definition: fees.h:43
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
const char * name
Definition: rest.cpp:43
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:59
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:157
A mutable version of CTransaction.
Definition: transaction.h:345
A version of CTransaction with the PSBT format.
Definition: psbt.h:392
WalletContext struct containing references to state shared between CWallet instances,...
Definition: context.h:34
Bilingual messages:
Definition: translation.h:16
Information about one wallet address.
Definition: wallet.h:342
CTxDestination dest
Definition: wallet.h:343
WalletAddress(CTxDestination dest, isminetype is_mine, std::string name, std::string purpose)
Definition: wallet.h:348
std::string purpose
Definition: wallet.h:346
Collection of wallet balances.
Definition: wallet.h:356
CAmount unconfirmed_watch_only_balance
Definition: wallet.h:362
CAmount immature_watch_only_balance
Definition: wallet.h:363
bool balanceChanged(const WalletBalances &prev) const
Definition: wallet.h:365
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:407
Updated transaction status.
Definition: wallet.h:392
unsigned int time_received
Definition: wallet.h:396
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
ChangeType
General change type (added, updated, removed).
Definition: ui_change_type.h:9