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 
14 #include <functional>
15 #include <map>
16 #include <memory>
17 #include <psbt.h>
18 #include <stdint.h>
19 #include <string>
20 #include <tuple>
21 #include <utility>
22 #include <vector>
23 
24 class CCoinControl;
25 class CFeeRate;
26 class CKey;
27 class CWallet;
28 enum isminetype : unsigned int;
29 enum class FeeReason;
30 typedef uint8_t isminefilter;
31 
32 enum class OutputType;
33 struct CRecipient;
34 
35 namespace interfaces {
36 
37 class Handler;
38 struct WalletAddress;
39 struct WalletBalances;
40 struct WalletTx;
41 struct WalletTxOut;
42 struct WalletTxStatus;
43 
44 using WalletOrderForm = std::vector<std::pair<std::string, std::string>>;
45 using WalletValueMap = std::map<std::string, std::string>;
46 
48 class Wallet
49 {
50 public:
51  virtual ~Wallet() {}
52 
54  virtual bool encryptWallet(const SecureString& wallet_passphrase) = 0;
55 
57  virtual bool isCrypted() = 0;
58 
60  virtual bool lock() = 0;
61 
63  virtual bool unlock(const SecureString& wallet_passphrase) = 0;
64 
66  virtual bool isLocked() = 0;
67 
69  virtual bool changeWalletPassphrase(const SecureString& old_wallet_passphrase,
70  const SecureString& new_wallet_passphrase) = 0;
71 
73  virtual void abortRescan() = 0;
74 
76  virtual bool backupWallet(const std::string& filename) = 0;
77 
79  virtual std::string getWalletName() = 0;
80 
81  // Get a new address.
82  virtual bool getNewDestination(const OutputType type, const std::string label, CTxDestination& dest) = 0;
83 
85  virtual bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) = 0;
86 
88  virtual bool getPrivKey(const CScript& script, const CKeyID& address, CKey& key) = 0;
89 
91  virtual bool isSpendable(const CTxDestination& dest) = 0;
92 
94  virtual bool haveWatchOnly() = 0;
95 
97  virtual bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::string& purpose) = 0;
98 
99  // Remove address.
100  virtual bool delAddressBook(const CTxDestination& dest) = 0;
101 
103  virtual bool getAddress(const CTxDestination& dest,
104  std::string* name,
105  isminetype* is_mine,
106  std::string* purpose) = 0;
107 
109  virtual std::vector<WalletAddress> getAddresses() = 0;
110 
112  virtual bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) = 0;
113 
115  virtual bool eraseDestData(const CTxDestination& dest, const std::string& key) = 0;
116 
118  virtual std::vector<std::string> getDestValues(const std::string& prefix) = 0;
119 
121  virtual void lockCoin(const COutPoint& output) = 0;
122 
124  virtual void unlockCoin(const COutPoint& output) = 0;
125 
127  virtual bool isLockedCoin(const COutPoint& output) = 0;
128 
130  virtual void listLockedCoins(std::vector<COutPoint>& outputs) = 0;
131 
133  virtual CTransactionRef createTransaction(const std::vector<CRecipient>& recipients,
134  const CCoinControl& coin_control,
135  bool sign,
136  int& change_pos,
137  CAmount& fee,
138  std::string& fail_reason) = 0;
139 
141  virtual void commitTransaction(CTransactionRef tx,
142  WalletValueMap value_map,
143  WalletOrderForm order_form) = 0;
144 
146  virtual bool transactionCanBeAbandoned(const uint256& txid) = 0;
147 
149  virtual bool abandonTransaction(const uint256& txid) = 0;
150 
152  virtual bool transactionCanBeBumped(const uint256& txid) = 0;
153 
155  virtual bool createBumpTransaction(const uint256& txid,
156  const CCoinControl& coin_control,
157  CAmount total_fee,
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 
196  bool& complete,
197  int sighash_type = 1 /* SIGHASH_ALL */,
198  bool sign = true,
199  bool bip32derivs = false) = 0;
200 
202  virtual WalletBalances getBalances() = 0;
203 
205  virtual bool tryGetBalances(WalletBalances& balances, int& num_blocks) = 0;
206 
208  virtual CAmount getBalance() = 0;
209 
211  virtual CAmount getAvailableBalance(const CCoinControl& coin_control) = 0;
212 
214  virtual isminetype txinIsMine(const CTxIn& txin) = 0;
215 
217  virtual isminetype txoutIsMine(const CTxOut& txout) = 0;
218 
220  virtual CAmount getDebit(const CTxIn& txin, isminefilter filter) = 0;
221 
223  virtual CAmount getCredit(const CTxOut& txout, isminefilter filter) = 0;
224 
227  using CoinsList = std::map<CTxDestination, std::vector<std::tuple<COutPoint, WalletTxOut>>>;
228  virtual CoinsList listCoins() = 0;
229 
231  virtual std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) = 0;
232 
234  virtual CAmount getRequiredFee(unsigned int tx_bytes) = 0;
235 
237  virtual CAmount getMinimumFee(unsigned int tx_bytes,
238  const CCoinControl& coin_control,
239  int* returned_target,
240  FeeReason* reason) = 0;
241 
243  virtual unsigned int getConfirmTarget() = 0;
244 
245  // Return whether HD enabled.
246  virtual bool hdEnabled() = 0;
247 
248  // Return whether the wallet is blank.
249  virtual bool canGetAddresses() = 0;
250 
251  // check if a certain wallet flag is set.
252  virtual bool IsWalletFlagSet(uint64_t flag) = 0;
253 
254  // Get default address type.
255  virtual OutputType getDefaultAddressType() = 0;
256 
257  // Get default change type.
258  virtual OutputType getDefaultChangeType() = 0;
259 
261  virtual CAmount getDefaultMaxTxFee() = 0;
262 
263  // Remove wallet.
264  virtual void remove() = 0;
265 
267  using UnloadFn = std::function<void()>;
268  virtual std::unique_ptr<Handler> handleUnload(UnloadFn fn) = 0;
269 
271  using ShowProgressFn = std::function<void(const std::string& title, int progress)>;
272  virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
273 
275  using StatusChangedFn = std::function<void()>;
276  virtual std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) = 0;
277 
279  using AddressBookChangedFn = std::function<void(const CTxDestination& address,
280  const std::string& label,
281  bool is_mine,
282  const std::string& purpose,
283  ChangeType status)>;
284  virtual std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) = 0;
285 
287  using TransactionChangedFn = std::function<void(const uint256& txid, ChangeType status)>;
288  virtual std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) = 0;
289 
291  using WatchOnlyChangedFn = std::function<void(bool have_watch_only)>;
292  virtual std::unique_ptr<Handler> handleWatchOnlyChanged(WatchOnlyChangedFn fn) = 0;
293 
295  using CanGetAddressesChangedFn = std::function<void()>;
296  virtual std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) = 0;
297 };
298 
301 {
304  std::string name;
305  std::string purpose;
306 
307  WalletAddress(CTxDestination dest, isminetype is_mine, std::string name, std::string purpose)
308  : dest(std::move(dest)), is_mine(is_mine), name(std::move(name)), purpose(std::move(purpose))
309  {
310  }
311 };
312 
315 {
317  CAmount unconfirmed_balance = 0;
318  CAmount immature_balance = 0;
319  bool have_watch_only = false;
320  CAmount watch_only_balance = 0;
321  CAmount unconfirmed_watch_only_balance = 0;
322  CAmount immature_watch_only_balance = 0;
323 
324  bool balanceChanged(const WalletBalances& prev) const
325  {
326  return balance != prev.balance || unconfirmed_balance != prev.unconfirmed_balance ||
327  immature_balance != prev.immature_balance || watch_only_balance != prev.watch_only_balance ||
328  unconfirmed_watch_only_balance != prev.unconfirmed_watch_only_balance ||
329  immature_watch_only_balance != prev.immature_watch_only_balance;
330  }
331 };
332 
333 // Wallet transaction information.
334 struct WalletTx
335 {
337  std::vector<isminetype> txin_is_mine;
338  std::vector<isminetype> txout_is_mine;
339  std::vector<CTxDestination> txout_address;
340  std::vector<isminetype> txout_address_is_mine;
344  int64_t time;
345  std::map<std::string, std::string> value_map;
347 };
348 
351 {
355  unsigned int time_received;
356  uint32_t lock_time;
357  bool is_final;
362 };
363 
366 {
368  int64_t time;
369  int depth_in_main_chain = -1;
370  bool is_spent = false;
371 };
372 
375 std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet>& wallet);
376 
377 } // namespace interfaces
378 
379 #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 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:307
std::vector< CTxDestination > txout_address
Definition: wallet.h:339
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:355
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:291
std::vector< isminetype > txin_is_mine
Definition: wallet.h:337
virtual std::unique_ptr< Handler > handleAddressBookChanged(AddressBookChangedFn fn)=0
std::vector< isminetype > txout_is_mine
Definition: wallet.h:338
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:267
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:295
virtual void lockCoin(const COutPoint &output)=0
Lock coin.
CTransactionRef tx
Definition: wallet.h:336
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:45
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 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:275
virtual WalletTx getWalletTx(const uint256 &txid)=0
Get transaction information.
virtual isminetype txinIsMine(const CTxIn &txin)=0
Return whether transaction input belongs to wallet.
virtual bool createBumpTransaction(const uint256 &txid, const CCoinControl &coin_control, CAmount total_fee, std::vector< std::string > &errors, CAmount &old_fee, CAmount &new_fee, CMutableTransaction &mtx)=0
Create bump transaction.
virtual bool IsWalletFlagSet(uint64_t flag)=0
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:227
virtual std::unique_ptr< Handler > handleUnload(UnloadFn fn)=0
CTxDestination dest
Definition: wallet.h:302
std::string purpose
Definition: wallet.h:305
virtual std::unique_ptr< Handler > handleTransactionChanged(TransactionChangedFn fn)=0
virtual bool hdEnabled()=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:283
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:314
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:48
virtual bool encryptWallet(const SecureString &wallet_passphrase)=0
Encrypt wallet.
uint8_t isminefilter
Definition: wallet.h:29
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:322
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:287
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 TransactionError fillPSBT(PartiallySignedTransaction &psbtx, bool &complete, int sighash_type=1, bool sign=true, bool bip32derivs=false)=0
Fill PSBT.
virtual CAmount getDebit(const CTxIn &txin, isminefilter filter)=0
Return debit amount if transaction input belongs to wallet.
virtual ~Wallet()
Definition: wallet.h:51
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:340
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:321
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.
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:271
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:606
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:44
Information about one wallet address.
Definition: wallet.h:300
virtual bool getPrivKey(const CScript &script, const CKeyID &address, CKey &key)=0
Get private key.
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:345
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:365
virtual CAmount getRequiredFee(unsigned int tx_bytes)=0
Get required fee.
bool balanceChanged(const WalletBalances &prev) const
Definition: wallet.h:324
virtual std::string getWalletName()=0
Get wallet name.
Updated transaction status.
Definition: wallet.h:350
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 bool tryGetBalances(WalletBalances &balances, int &num_blocks)=0
Get balances if possible without blocking.
virtual std::vector< WalletAddress > getAddresses()=0
Get wallet address list.