Bitcoin Core  0.19.99
P2P Digital Currency
wallet.h
Go to the documentation of this file.
1 // Copyright (c) 2018-2019 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 <amount.h> // For CAmount
9 #include <pubkey.h> // For CKeyID and CScriptID (definitions needed in CTxDestination instantiation)
10 #include <script/standard.h> // For CTxDestination
11 #include <support/allocators/secure.h> // For SecureString
12 #include <ui_interface.h> // For ChangeType
13 #include <util/message.h>
14 
15 #include <functional>
16 #include <map>
17 #include <memory>
18 #include <psbt.h>
19 #include <stdint.h>
20 #include <string>
21 #include <tuple>
22 #include <utility>
23 #include <vector>
24 
25 class CCoinControl;
26 class CFeeRate;
27 class CKey;
28 class CWallet;
29 enum isminetype : unsigned int;
30 enum class FeeReason;
31 typedef uint8_t isminefilter;
32 
33 enum class OutputType;
34 struct CRecipient;
35 
36 namespace interfaces {
37 
38 class Handler;
39 struct WalletAddress;
40 struct WalletBalances;
41 struct WalletTx;
42 struct WalletTxOut;
43 struct WalletTxStatus;
44 
45 using WalletOrderForm = std::vector<std::pair<std::string, std::string>>;
46 using WalletValueMap = std::map<std::string, std::string>;
47 
49 class Wallet
50 {
51 public:
52  virtual ~Wallet() {}
53 
55  virtual bool encryptWallet(const SecureString& wallet_passphrase) = 0;
56 
58  virtual bool isCrypted() = 0;
59 
61  virtual bool lock() = 0;
62 
64  virtual bool unlock(const SecureString& wallet_passphrase) = 0;
65 
67  virtual bool isLocked() = 0;
68 
70  virtual bool changeWalletPassphrase(const SecureString& old_wallet_passphrase,
71  const SecureString& new_wallet_passphrase) = 0;
72 
74  virtual void abortRescan() = 0;
75 
77  virtual bool backupWallet(const std::string& filename) = 0;
78 
80  virtual std::string getWalletName() = 0;
81 
82  // Get a new address.
83  virtual bool getNewDestination(const OutputType type, const std::string label, CTxDestination& dest) = 0;
84 
86  virtual bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) = 0;
87 
89  virtual SigningResult signMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) = 0;
90 
92  virtual bool isSpendable(const CTxDestination& dest) = 0;
93 
95  virtual bool haveWatchOnly() = 0;
96 
98  virtual bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::string& purpose) = 0;
99 
100  // Remove address.
101  virtual bool delAddressBook(const CTxDestination& dest) = 0;
102 
104  virtual bool getAddress(const CTxDestination& dest,
105  std::string* name,
106  isminetype* is_mine,
107  std::string* purpose) = 0;
108 
110  virtual std::vector<WalletAddress> getAddresses() = 0;
111 
113  virtual bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) = 0;
114 
116  virtual bool eraseDestData(const CTxDestination& dest, const std::string& key) = 0;
117 
119  virtual std::vector<std::string> getDestValues(const std::string& prefix) = 0;
120 
122  virtual void lockCoin(const COutPoint& output) = 0;
123 
125  virtual void unlockCoin(const COutPoint& output) = 0;
126 
128  virtual bool isLockedCoin(const COutPoint& output) = 0;
129 
131  virtual void listLockedCoins(std::vector<COutPoint>& outputs) = 0;
132 
134  virtual CTransactionRef createTransaction(const std::vector<CRecipient>& recipients,
135  const CCoinControl& coin_control,
136  bool sign,
137  int& change_pos,
138  CAmount& fee,
139  std::string& fail_reason) = 0;
140 
142  virtual void commitTransaction(CTransactionRef tx,
143  WalletValueMap value_map,
144  WalletOrderForm order_form) = 0;
145 
147  virtual bool transactionCanBeAbandoned(const uint256& txid) = 0;
148 
150  virtual bool abandonTransaction(const uint256& txid) = 0;
151 
153  virtual bool transactionCanBeBumped(const uint256& txid) = 0;
154 
156  virtual bool createBumpTransaction(const uint256& txid,
157  const CCoinControl& coin_control,
158  std::vector<std::string>& errors,
159  CAmount& old_fee,
160  CAmount& new_fee,
161  CMutableTransaction& mtx) = 0;
162 
164  virtual bool signBumpTransaction(CMutableTransaction& mtx) = 0;
165 
167  virtual bool commitBumpTransaction(const uint256& txid,
168  CMutableTransaction&& mtx,
169  std::vector<std::string>& errors,
170  uint256& bumped_txid) = 0;
171 
173  virtual CTransactionRef getTx(const uint256& txid) = 0;
174 
176  virtual WalletTx getWalletTx(const uint256& txid) = 0;
177 
179  virtual std::vector<WalletTx> getWalletTxs() = 0;
180 
182  virtual bool tryGetTxStatus(const uint256& txid,
183  WalletTxStatus& tx_status,
184  int& num_blocks,
185  int64_t& block_time) = 0;
186 
188  virtual WalletTx getWalletTxDetails(const uint256& txid,
189  WalletTxStatus& tx_status,
190  WalletOrderForm& order_form,
191  bool& in_mempool,
192  int& num_blocks) = 0;
193 
195  virtual TransactionError fillPSBT(int sighash_type,
196  bool sign,
197  bool bip32derivs,
199  bool& complete) = 0;
200 
202  virtual WalletBalances getBalances() = 0;
203 
205  virtual bool tryGetBalances(WalletBalances& balances,
206  int& num_blocks,
207  bool force,
208  int cached_num_blocks) = 0;
209 
211  virtual CAmount getBalance() = 0;
212 
214  virtual CAmount getAvailableBalance(const CCoinControl& coin_control) = 0;
215 
217  virtual isminetype txinIsMine(const CTxIn& txin) = 0;
218 
220  virtual isminetype txoutIsMine(const CTxOut& txout) = 0;
221 
223  virtual CAmount getDebit(const CTxIn& txin, isminefilter filter) = 0;
224 
226  virtual CAmount getCredit(const CTxOut& txout, isminefilter filter) = 0;
227 
230  using CoinsList = std::map<CTxDestination, std::vector<std::tuple<COutPoint, WalletTxOut>>>;
231  virtual CoinsList listCoins() = 0;
232 
234  virtual std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) = 0;
235 
237  virtual CAmount getRequiredFee(unsigned int tx_bytes) = 0;
238 
240  virtual CAmount getMinimumFee(unsigned int tx_bytes,
241  const CCoinControl& coin_control,
242  int* returned_target,
243  FeeReason* reason) = 0;
244 
246  virtual unsigned int getConfirmTarget() = 0;
247 
248  // Return whether HD enabled.
249  virtual bool hdEnabled() = 0;
250 
251  // Return whether the wallet is blank.
252  virtual bool canGetAddresses() = 0;
253 
254  // Return whether private keys enabled.
255  virtual bool privateKeysDisabled() = 0;
256 
257  // Get default address type.
258  virtual OutputType getDefaultAddressType() = 0;
259 
260  // Get default change type.
261  virtual OutputType getDefaultChangeType() = 0;
262 
264  virtual CAmount getDefaultMaxTxFee() = 0;
265 
266  // Remove wallet.
267  virtual void remove() = 0;
268 
270  using UnloadFn = std::function<void()>;
271  virtual std::unique_ptr<Handler> handleUnload(UnloadFn fn) = 0;
272 
274  using ShowProgressFn = std::function<void(const std::string& title, int progress)>;
275  virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
276 
278  using StatusChangedFn = std::function<void()>;
279  virtual std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) = 0;
280 
282  using AddressBookChangedFn = std::function<void(const CTxDestination& address,
283  const std::string& label,
284  bool is_mine,
285  const std::string& purpose,
286  ChangeType status)>;
287  virtual std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) = 0;
288 
290  using TransactionChangedFn = std::function<void(const uint256& txid, ChangeType status)>;
291  virtual std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) = 0;
292 
294  using WatchOnlyChangedFn = std::function<void(bool have_watch_only)>;
295  virtual std::unique_ptr<Handler> handleWatchOnlyChanged(WatchOnlyChangedFn fn) = 0;
296 
298  using CanGetAddressesChangedFn = std::function<void()>;
299  virtual std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) = 0;
300 };
301 
304 {
307  std::string name;
308  std::string purpose;
309 
310  WalletAddress(CTxDestination dest, isminetype is_mine, std::string name, std::string purpose)
311  : dest(std::move(dest)), is_mine(is_mine), name(std::move(name)), purpose(std::move(purpose))
312  {
313  }
314 };
315 
318 {
320  CAmount unconfirmed_balance = 0;
321  CAmount immature_balance = 0;
322  bool have_watch_only = false;
323  CAmount watch_only_balance = 0;
324  CAmount unconfirmed_watch_only_balance = 0;
325  CAmount immature_watch_only_balance = 0;
326 
327  bool balanceChanged(const WalletBalances& prev) const
328  {
329  return balance != prev.balance || unconfirmed_balance != prev.unconfirmed_balance ||
330  immature_balance != prev.immature_balance || watch_only_balance != prev.watch_only_balance ||
331  unconfirmed_watch_only_balance != prev.unconfirmed_watch_only_balance ||
332  immature_watch_only_balance != prev.immature_watch_only_balance;
333  }
334 };
335 
336 // Wallet transaction information.
337 struct WalletTx
338 {
340  std::vector<isminetype> txin_is_mine;
341  std::vector<isminetype> txout_is_mine;
342  std::vector<CTxDestination> txout_address;
343  std::vector<isminetype> txout_address_is_mine;
347  int64_t time;
348  std::map<std::string, std::string> value_map;
350 };
351 
354 {
358  unsigned int time_received;
359  uint32_t lock_time;
360  bool is_final;
365 };
366 
369 {
371  int64_t time;
372  int depth_in_main_chain = -1;
373  bool is_spent = false;
374 };
375 
378 std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet>& wallet);
379 
380 } // namespace interfaces
381 
382 #endif // BITCOIN_INTERFACES_WALLET_H
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:408
virtual bool isCrypted()=0
Return whether wallet is encrypted.
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 CoinsList listCoins()=0
virtual bool canGetAddresses()=0
WalletAddress(CTxDestination dest, isminetype is_mine, std::string name, std::string purpose)
Definition: wallet.h:310
std::vector< CTxDestination > txout_address
Definition: wallet.h:342
virtual WalletTx getWalletTxDetails(const uint256 &txid, WalletTxStatus &tx_status, WalletOrderForm &order_form, bool &in_mempool, int &num_blocks)=0
Get transaction details.
virtual bool commitBumpTransaction(const uint256 &txid, CMutableTransaction &&mtx, std::vector< std::string > &errors, uint256 &bumped_txid)=0
Commit bump transaction.
virtual bool isLocked()=0
Return whether wallet is locked.
virtual OutputType getDefaultAddressType()=0
unsigned int time_received
Definition: wallet.h:358
virtual isminetype txoutIsMine(const CTxOut &txout)=0
Return whether transaction output belongs to wallet.
std::function< void(bool have_watch_only)> WatchOnlyChangedFn
Register handler for watchonly changed messages.
Definition: wallet.h:294
std::vector< isminetype > txin_is_mine
Definition: wallet.h:340
SigningResult
Definition: message.h:42
virtual std::unique_ptr< Handler > handleAddressBookChanged(AddressBookChangedFn fn)=0
std::vector< isminetype > txout_is_mine
Definition: wallet.h:341
virtual CTransactionRef getTx(const uint256 &txid)=0
Get a transaction.
const char * prefix
Definition: rest.cpp:650
virtual bool getPubKey(const CScript &script, const CKeyID &address, CPubKey &pub_key)=0
Get public key.
virtual CAmount getDefaultMaxTxFee()=0
Get max tx fee.
std::function< void()> UnloadFn
Register handler for unload message.
Definition: wallet.h:270
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:60
virtual std::unique_ptr< Handler > handleWatchOnlyChanged(WatchOnlyChangedFn fn)=0
virtual bool backupWallet(const std::string &filename)=0
Back up wallet.
std::function< void()> CanGetAddressesChangedFn
Register handler for keypool changed messages.
Definition: wallet.h:298
virtual void lockCoin(const COutPoint &output)=0
Lock coin.
CTransactionRef tx
Definition: wallet.h:339
A version of CTransaction with the PSBT format.
Definition: psbt.h:388
virtual CTransactionRef createTransaction(const std::vector< CRecipient > &recipients, const CCoinControl &coin_control, bool sign, int &change_pos, CAmount &fee, std::string &fail_reason)=0
Create transaction.
std::map< std::string, std::string > WalletValueMap
Definition: wallet.h:46
virtual SigningResult signMessage(const std::string &message, const PKHash &pkhash, std::string &str_sig)=0
Sign message.
virtual bool eraseDestData(const CTxDestination &dest, const std::string &key)=0
Erase dest data.
virtual bool changeWalletPassphrase(const SecureString &old_wallet_passphrase, const SecureString &new_wallet_passphrase)=0
Change wallet passphrase.
virtual bool createBumpTransaction(const uint256 &txid, const CCoinControl &coin_control, std::vector< std::string > &errors, CAmount &old_fee, CAmount &new_fee, CMutableTransaction &mtx)=0
Create bump transaction.
virtual bool isSpendable(const CTxDestination &dest)=0
Return whether wallet has private key.
virtual OutputType getDefaultChangeType()=0
OutputType
Definition: outputtype.h:17
Coin Control Features.
Definition: coincontrol.h:22
std::function< void()> StatusChangedFn
Register handler for status changed messages.
Definition: wallet.h:278
virtual WalletTx getWalletTx(const uint256 &txid)=0
Get transaction information.
virtual isminetype txinIsMine(const CTxIn &txin)=0
Return whether transaction input belongs to wallet.
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
std::map< CTxDestination, std::vector< std::tuple< COutPoint, WalletTxOut > >> CoinsList
Return AvailableCoins + LockedCoins grouped by wallet address.
Definition: wallet.h:230
virtual std::unique_ptr< Handler > handleUnload(UnloadFn fn)=0
CTxDestination dest
Definition: wallet.h:305
std::string purpose
Definition: wallet.h:308
virtual std::unique_ptr< Handler > handleTransactionChanged(TransactionChangedFn fn)=0
virtual bool hdEnabled()=0
virtual bool tryGetBalances(WalletBalances &balances, int &num_blocks, bool force, int cached_num_blocks)=0
Get balances if possible without waiting for chain and wallet locks.
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:286
virtual CAmount getCredit(const CTxOut &txout, isminefilter filter)=0
Return credit amount if transaction input belongs to wallet.
virtual std::unique_ptr< Handler > handleShowProgress(ShowProgressFn fn)=0
static CAmount balance
virtual std::unique_ptr< Handler > handleCanGetAddressesChanged(CanGetAddressesChangedFn fn)=0
virtual std::unique_ptr< Handler > handleStatusChanged(StatusChangedFn fn)=0
virtual void unlockCoin(const COutPoint &output)=0
Unlock coin.
ChangeType
General change type (added, updated, removed).
Definition: ui_interface.h:21
Collection of wallet balances.
Definition: wallet.h:317
An input of a transaction.
Definition: transaction.h:63
const char * name
Definition: rest.cpp:40
An encapsulated public key.
Definition: pubkey.h:30
virtual bool transactionCanBeAbandoned(const uint256 &txid)=0
Return whether transaction can be abandoned.
Interface for accessing a wallet.
Definition: wallet.h:49
virtual bool encryptWallet(const SecureString &wallet_passphrase)=0
Encrypt wallet.
uint8_t isminefilter
Definition: wallet.h:30
virtual CAmount getMinimumFee(unsigned int tx_bytes, const CCoinControl &coin_control, int *returned_target, FeeReason *reason)=0
Get minimum fee.
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.
isminetype
IsMine() return codes.
Definition: ismine.h:18
An output of a transaction.
Definition: transaction.h:133
CAmount immature_watch_only_balance
Definition: wallet.h:325
FeeReason
Definition: fees.h:36
virtual bool transactionCanBeBumped(const uint256 &txid)=0
Return whether transaction can be bumped.
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
std::unique_ptr< Wallet > MakeWallet(const std::shared_ptr< CWallet > &wallet)
Return implementation of Wallet interface.
Definition: dummywallet.cpp:93
std::function< void(const uint256 &txid, ChangeType status)> TransactionChangedFn
Register handler for transaction changed messages.
Definition: wallet.h:290
virtual std::vector< WalletTxOut > getCoins(const std::vector< COutPoint > &outputs)=0
Return wallet transaction output information.
virtual bool addDestData(const CTxDestination &dest, const std::string &key, const std::string &value)=0
Add dest data.
virtual CAmount getDebit(const CTxIn &txin, isminefilter filter)=0
Return debit amount if transaction input belongs to wallet.
virtual ~Wallet()
Definition: wallet.h:52
256-bit opaque blob.
Definition: uint256.h:120
virtual bool getNewDestination(const OutputType type, const std::string label, CTxDestination &dest)=0
virtual bool signBumpTransaction(CMutableTransaction &mtx)=0
Sign bump transaction.
std::vector< isminetype > txout_address_is_mine
Definition: wallet.h:343
virtual std::vector< WalletTx > getWalletTxs()=0
Get list of all wallet transactions.
virtual bool delAddressBook(const CTxDestination &dest)=0
CAmount unconfirmed_watch_only_balance
Definition: wallet.h:324
virtual void listLockedCoins(std::vector< COutPoint > &outputs)=0
List locked coins.
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
virtual bool setAddressBook(const CTxDestination &dest, const std::string &name, const std::string &purpose)=0
Add or update address.
virtual TransactionError fillPSBT(int sighash_type, bool sign, bool bip32derivs, PartiallySignedTransaction &psbtx, bool &complete)=0
Fill PSBT.
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
virtual bool haveWatchOnly()=0
Return whether wallet has watch only keys.
std::function< void(const std::string &title, int progress)> ShowProgressFn
Register handler for show progress messages.
Definition: wallet.h:274
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:609
TransactionError
Definition: error.h:22
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
virtual bool isLockedCoin(const COutPoint &output)=0
Return whether coin is locked.
std::vector< std::pair< std::string, std::string > > WalletOrderForm
Definition: wallet.h:45
Information about one wallet address.
Definition: wallet.h:303
virtual bool lock()=0
Lock wallet.
A mutable version of CTransaction.
Definition: transaction.h:366
virtual unsigned int getConfirmTarget()=0
Get tx confirm target.
virtual CAmount getBalance()=0
Get balance.
std::map< std::string, std::string > value_map
Definition: wallet.h:348
An encapsulated private key.
Definition: key.h:27
virtual void abortRescan()=0
Abort a rescan.
virtual CAmount getAvailableBalance(const CCoinControl &coin_control)=0
Get available balance.
virtual void commitTransaction(CTransactionRef tx, WalletValueMap value_map, WalletOrderForm order_form)=0
Commit transaction.
virtual WalletBalances getBalances()=0
Get balances.
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:143
Wallet transaction output.
Definition: wallet.h:368
virtual CAmount getRequiredFee(unsigned int tx_bytes)=0
Get required fee.
bool balanceChanged(const WalletBalances &prev) const
Definition: wallet.h:327
virtual std::string getWalletName()=0
Get wallet name.
Updated transaction status.
Definition: wallet.h:353
virtual bool abandonTransaction(const uint256 &txid)=0
Abandon transaction.
virtual std::vector< std::string > getDestValues(const std::string &prefix)=0
Get dest values with prefix.
virtual bool unlock(const SecureString &wallet_passphrase)=0
Unlock wallet.
virtual std::vector< WalletAddress > getAddresses()=0
Get wallet address list.