Bitcoin ABC 0.34.2
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 <addresstype.h>
10#include <consensus/amount.h>
11#include <interfaces/chain.h>
14#include <pubkey.h>
15#include <script/script.h>
16#include <script/sighashtype.h>
18#include <util/result.h>
19#include <util/ui_change_type.h>
20
21#include <cstdint>
22#include <functional>
23#include <map>
24#include <memory>
25#include <string>
26#include <tuple>
27#include <utility>
28#include <vector>
29
30class CChainParams;
31class CCoinControl;
32class CKey;
34class COutPoint;
35class CTransaction;
36class CWallet;
37enum class FeeReason;
38enum class OutputType;
39namespace common {
40enum class PSBTError;
41} // namespace common
42enum isminetype : unsigned int;
43struct CRecipient;
45struct WalletContext;
46typedef uint8_t isminefilter;
47struct TxId;
48struct bilingual_str;
49
50namespace interfaces {
51
52class Handler;
53struct WalletAddress;
54struct WalletBalances;
55struct WalletTx;
56struct WalletTxOut;
57struct WalletTxStatus;
58
59using WalletOrderForm = std::vector<std::pair<std::string, std::string>>;
60using WalletValueMap = std::map<std::string, std::string>;
61
63class Wallet {
64public:
65 virtual ~Wallet() = default;
66
68 virtual bool encryptWallet(const SecureString &wallet_passphrase) = 0;
69
71 virtual bool isCrypted() = 0;
72
74 virtual bool lock() = 0;
75
77 virtual bool unlock(const SecureString &wallet_passphrase) = 0;
78
80 virtual bool isLocked() = 0;
81
83 virtual bool
84 changeWalletPassphrase(const SecureString &old_wallet_passphrase,
85 const SecureString &new_wallet_passphrase) = 0;
86
88 virtual void abortRescan() = 0;
89
91 virtual bool backupWallet(const std::string &filename) = 0;
92
94 virtual std::string getWalletName() = 0;
95
97 virtual const CChainParams &getChainParams() = 0;
98
99 // Get a new address.
101 getNewDestination(const OutputType type, const std::string &label) = 0;
102
104 virtual bool getPubKey(const CScript &script, const CKeyID &address,
105 CPubKey &pub_key) = 0;
106
108 virtual SigningResult signMessage(const std::string &message,
109 const PKHash &pkhash,
110 std::string &str_sig) = 0;
111
113 virtual bool isSpendable(const CTxDestination &dest) = 0;
114
116 virtual bool haveWatchOnly() = 0;
117
119 virtual bool setAddressBook(const CTxDestination &dest,
120 const std::string &name,
121 const std::string &purpose) = 0;
122
123 // Remove address.
124 virtual bool delAddressBook(const CTxDestination &dest) = 0;
125
127 virtual bool getAddress(const CTxDestination &dest, std::string *name,
128 isminetype *is_mine, std::string *purpose) = 0;
129
131 virtual std::vector<WalletAddress> getAddresses() = 0;
132
134 virtual bool addDestData(const CTxDestination &dest, const std::string &key,
135 const std::string &value) = 0;
136
138 virtual bool eraseDestData(const CTxDestination &dest,
139 const std::string &key) = 0;
140
142 virtual std::vector<std::string>
143 getDestValues(const std::string &prefix) = 0;
144
146 virtual void lockCoin(const COutPoint &output) = 0;
147
149 virtual void unlockCoin(const COutPoint &output) = 0;
150
152 virtual bool isLockedCoin(const COutPoint &output) = 0;
153
155 virtual void listLockedCoins(std::vector<COutPoint> &outputs) = 0;
156
159 createTransaction(const std::vector<CRecipient> &recipients,
160 const CCoinControl &coin_control, bool sign,
161 int &change_pos, Amount &fee) = 0;
162
165 WalletOrderForm order_form) = 0;
166
168 virtual bool transactionCanBeAbandoned(const TxId &txid) = 0;
169
171 virtual bool abandonTransaction(const TxId &txid) = 0;
172
174 virtual CTransactionRef getTx(const TxId &txid) = 0;
175
177 virtual WalletTx getWalletTx(const TxId &txid) = 0;
178
180 virtual std::vector<WalletTx> getWalletTxs() = 0;
181
184 virtual bool tryGetTxStatus(const TxId &txid, WalletTxStatus &tx_status,
185 int &num_blocks, int64_t &block_time) = 0;
186
188 virtual WalletTx getWalletTxDetails(const TxId &txid,
189 WalletTxStatus &tx_status,
190 WalletOrderForm &order_form,
191 bool &in_mempool, int &num_blocks) = 0;
192
194 virtual std::optional<common::PSBTError>
195 fillPSBT(SigHashType sighash_type, bool sign, bool bip32derivs,
196 PartiallySignedTransaction &psbtx, bool &complete) const = 0;
197
200
202 virtual bool tryGetBalances(WalletBalances &balances,
203 BlockHash &block_hash) = 0;
204
206 virtual Amount getBalance() = 0;
207
209 virtual Amount getAvailableBalance(const CCoinControl &coin_control) = 0;
210
212 virtual isminetype txinIsMine(const CTxIn &txin) = 0;
213
215 virtual isminetype txoutIsMine(const CTxOut &txout) = 0;
216
218 virtual Amount getDebit(const CTxIn &txin, isminefilter filter) = 0;
219
221 virtual Amount getCredit(const CTxOut &txout, isminefilter filter) = 0;
222
225 using CoinsList = std::map<CTxDestination,
226 std::vector<std::tuple<COutPoint, WalletTxOut>>>;
227 virtual CoinsList listCoins() = 0;
228
230 virtual std::vector<WalletTxOut>
231 getCoins(const std::vector<COutPoint> &outputs) = 0;
232
234 virtual Amount getRequiredFee(unsigned int tx_bytes) = 0;
235
237 virtual Amount getMinimumFee(unsigned int tx_bytes,
238 const CCoinControl &coin_control) = 0;
239
240 // Return whether HD enabled.
241 virtual bool hdEnabled() = 0;
242
243 // Return whether the wallet is blank.
244 virtual bool canGetAddresses() const = 0;
245
246 // Return whether private keys enabled.
247 virtual bool privateKeysDisabled() = 0;
248
249 // Get default address type.
251
254
255 // Remove wallet.
256 virtual void remove() = 0;
257
259 virtual bool isLegacy() = 0;
260
262 using UnloadFn = std::function<void()>;
263 virtual std::unique_ptr<Handler> handleUnload(UnloadFn fn) = 0;
264
267 std::function<void(const std::string &title, int progress)>;
268 virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
269
271 using StatusChangedFn = std::function<void()>;
272 virtual std::unique_ptr<Handler>
274
276 using AddressBookChangedFn = std::function<void(
277 const CTxDestination &address, const std::string &label, bool is_mine,
278 const std::string &purpose, ChangeType status)>;
279 virtual std::unique_ptr<Handler>
281
284 std::function<void(const TxId &txid, ChangeType status)>;
285 virtual std::unique_ptr<Handler>
287
289 using WatchOnlyChangedFn = std::function<void(bool have_watch_only)>;
290 virtual std::unique_ptr<Handler>
292
294 using CanGetAddressesChangedFn = std::function<void()>;
295 virtual std::unique_ptr<Handler>
297
299 virtual CWallet *wallet() { return nullptr; }
300};
301
305class WalletClient : public ChainClient {
306public:
309 createWallet(const std::string &name, const SecureString &passphrase,
310 uint64_t wallet_creation_flags,
311 std::vector<bilingual_str> &warnings) = 0;
312
315 loadWallet(const std::string &name,
316 std::vector<bilingual_str> &warnings) = 0;
317
319 virtual std::string getWalletDir() = 0;
320
323 restoreWallet(const fs::path &backup_file, const std::string &wallet_name,
324 std::vector<bilingual_str> &warnings) = 0;
325
327 virtual std::vector<std::string> listWalletDir() = 0;
328
330 virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
331
335 using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
336 virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;
337
339 virtual WalletContext *context() { return nullptr; }
340};
341
346 std::string name;
347 std::string purpose;
348
350 std::string nameIn, std::string purposeIn)
351 : dest(std::move(destIn)), is_mine(isMineIn), name(std::move(nameIn)),
352 purpose(std::move(purposeIn)) {}
353};
354
360 bool have_watch_only = false;
364
365 bool balanceChanged(const WalletBalances &prev) const {
366 return balance != prev.balance ||
373 }
374};
375
376// Wallet transaction information.
377struct WalletTx {
379 std::vector<isminetype> txin_is_mine;
380 std::vector<isminetype> txout_is_mine;
381 std::vector<CTxDestination> txout_address;
382 std::vector<isminetype> txout_address_is_mine;
386 int64_t time;
387 std::map<std::string, std::string> value_map;
389};
390
396 unsigned int time_received;
397 uint32_t lock_time;
402};
403
407 int64_t time;
409 bool is_spent = false;
410};
411
414std::unique_ptr<Wallet> MakeWallet(WalletContext &context,
415 const std::shared_ptr<CWallet> &wallet);
416
419std::unique_ptr<WalletClient> MakeWalletClient(Chain &chain, ArgsManager &args);
420
421} // namespace interfaces
422
423#endif // BITCOIN_INTERFACES_WALLET_H
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
Definition: addresstype.h:54
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
Definition: chainparams.h:86
Coin Control Features.
Definition: coincontrol.h:20
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:270
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:305
virtual WalletContext * context()
Return pointer to internal context, useful for testing.
Definition: wallet.h:339
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:335
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:63
std::function< void(const TxId &txid, ChangeType status)> TransactionChangedFn
Register handler for transaction changed messages.
Definition: wallet.h:284
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:289
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:262
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:299
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 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:278
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:267
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:226
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 ~Wallet()=default
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:271
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:294
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:46
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:59
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:60
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:1078
const char * name
Definition: rest.cpp:46
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:55
SigningResult
Definition: signmessage.h:47
Definition: amount.h:23
static constexpr Amount zero() noexcept
Definition: amount.h:36
A BlockHash is a unqiue identifier for a block.
Definition: blockhash.h:13
A version of CTransaction with the PSBT format.
Definition: psbt.h:336
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:343
CTxDestination dest
Definition: wallet.h:344
std::string purpose
Definition: wallet.h:347
WalletAddress(CTxDestination destIn, isminetype isMineIn, std::string nameIn, std::string purposeIn)
Definition: wallet.h:349
Collection of wallet balances.
Definition: wallet.h:356
Amount immature_watch_only_balance
Definition: wallet.h:363
Amount unconfirmed_watch_only_balance
Definition: wallet.h:362
bool balanceChanged(const WalletBalances &prev) const
Definition: wallet.h:365
std::vector< CTxDestination > txout_address
Definition: wallet.h:381
std::vector< isminetype > txout_is_mine
Definition: wallet.h:380
CTransactionRef tx
Definition: wallet.h:378
std::map< std::string, std::string > value_map
Definition: wallet.h:387
std::vector< isminetype > txout_address_is_mine
Definition: wallet.h:382
std::vector< isminetype > txin_is_mine
Definition: wallet.h:379
Wallet transaction output.
Definition: wallet.h:405
Updated transaction status.
Definition: wallet.h:392
unsigned int time_received
Definition: wallet.h:396
ChangeType
General change type (added, updated, removed).
Definition: ui_change_type.h:9