Bitcoin Core  0.18.99
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 <amount.h> // For CAmount
9 #include <pubkey.h> // For CKeyID and CScriptID (definitions needed in CTxDestination instantiation)
10 #include <script/ismine.h> // For isminefilter, isminetype
11 #include <script/standard.h> // For CTxDestination
12 #include <support/allocators/secure.h> // For SecureString
13 #include <ui_interface.h> // For ChangeType
14 
15 #include <functional>
16 #include <map>
17 #include <memory>
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 class FeeReason;
29 enum class OutputType;
30 struct CRecipient;
31 
32 namespace interfaces {
33 
34 class Handler;
35 class PendingWalletTx;
36 struct WalletAddress;
37 struct WalletBalances;
38 struct WalletTx;
39 struct WalletTxOut;
40 struct WalletTxStatus;
41 
42 using WalletOrderForm = std::vector<std::pair<std::string, std::string>>;
43 using WalletValueMap = std::map<std::string, std::string>;
44 
46 class Wallet
47 {
48 public:
49  virtual ~Wallet() {}
50 
52  virtual bool encryptWallet(const SecureString& wallet_passphrase) = 0;
53 
55  virtual bool isCrypted() = 0;
56 
58  virtual bool lock() = 0;
59 
61  virtual bool unlock(const SecureString& wallet_passphrase) = 0;
62 
64  virtual bool isLocked() = 0;
65 
67  virtual bool changeWalletPassphrase(const SecureString& old_wallet_passphrase,
68  const SecureString& new_wallet_passphrase) = 0;
69 
71  virtual void abortRescan() = 0;
72 
74  virtual bool backupWallet(const std::string& filename) = 0;
75 
77  virtual std::string getWalletName() = 0;
78 
79  // Get key from pool.
80  virtual bool getKeyFromPool(bool internal, CPubKey& pub_key) = 0;
81 
83  virtual bool getPubKey(const CKeyID& address, CPubKey& pub_key) = 0;
84 
86  virtual bool getPrivKey(const CKeyID& address, CKey& key) = 0;
87 
89  virtual bool isSpendable(const CTxDestination& dest) = 0;
90 
92  virtual bool haveWatchOnly() = 0;
93 
95  virtual bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::string& purpose) = 0;
96 
97  // Remove address.
98  virtual bool delAddressBook(const CTxDestination& dest) = 0;
99 
101  virtual bool getAddress(const CTxDestination& dest,
102  std::string* name,
103  isminetype* is_mine,
104  std::string* purpose) = 0;
105 
107  virtual std::vector<WalletAddress> getAddresses() = 0;
108 
111  virtual void learnRelatedScripts(const CPubKey& key, OutputType type) = 0;
112 
114  virtual bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) = 0;
115 
117  virtual bool eraseDestData(const CTxDestination& dest, const std::string& key) = 0;
118 
120  virtual std::vector<std::string> getDestValues(const std::string& prefix) = 0;
121 
123  virtual void lockCoin(const COutPoint& output) = 0;
124 
126  virtual void unlockCoin(const COutPoint& output) = 0;
127 
129  virtual bool isLockedCoin(const COutPoint& output) = 0;
130 
132  virtual void listLockedCoins(std::vector<COutPoint>& outputs) = 0;
133 
135  virtual std::unique_ptr<PendingWalletTx> createTransaction(const std::vector<CRecipient>& recipients,
136  const CCoinControl& coin_control,
137  bool sign,
138  int& change_pos,
139  CAmount& fee,
140  std::string& fail_reason) = 0;
141 
143  virtual bool transactionCanBeAbandoned(const uint256& txid) = 0;
144 
146  virtual bool abandonTransaction(const uint256& txid) = 0;
147 
149  virtual bool transactionCanBeBumped(const uint256& txid) = 0;
150 
152  virtual bool createBumpTransaction(const uint256& txid,
153  const CCoinControl& coin_control,
154  CAmount total_fee,
155  std::vector<std::string>& errors,
156  CAmount& old_fee,
157  CAmount& new_fee,
158  CMutableTransaction& mtx) = 0;
159 
161  virtual bool signBumpTransaction(CMutableTransaction& mtx) = 0;
162 
164  virtual bool commitBumpTransaction(const uint256& txid,
165  CMutableTransaction&& mtx,
166  std::vector<std::string>& errors,
167  uint256& bumped_txid) = 0;
168 
170  virtual CTransactionRef getTx(const uint256& txid) = 0;
171 
173  virtual WalletTx getWalletTx(const uint256& txid) = 0;
174 
176  virtual std::vector<WalletTx> getWalletTxs() = 0;
177 
179  virtual bool tryGetTxStatus(const uint256& txid,
180  WalletTxStatus& tx_status,
181  int& num_blocks,
182  int64_t& block_time) = 0;
183 
185  virtual WalletTx getWalletTxDetails(const uint256& txid,
186  WalletTxStatus& tx_status,
187  WalletOrderForm& order_form,
188  bool& in_mempool,
189  int& num_blocks) = 0;
190 
192  virtual WalletBalances getBalances() = 0;
193 
195  virtual bool tryGetBalances(WalletBalances& balances, int& num_blocks) = 0;
196 
198  virtual CAmount getBalance() = 0;
199 
201  virtual CAmount getAvailableBalance(const CCoinControl& coin_control) = 0;
202 
204  virtual isminetype txinIsMine(const CTxIn& txin) = 0;
205 
207  virtual isminetype txoutIsMine(const CTxOut& txout) = 0;
208 
210  virtual CAmount getDebit(const CTxIn& txin, isminefilter filter) = 0;
211 
213  virtual CAmount getCredit(const CTxOut& txout, isminefilter filter) = 0;
214 
217  using CoinsList = std::map<CTxDestination, std::vector<std::tuple<COutPoint, WalletTxOut>>>;
218  virtual CoinsList listCoins() = 0;
219 
221  virtual std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) = 0;
222 
224  virtual CAmount getRequiredFee(unsigned int tx_bytes) = 0;
225 
227  virtual CAmount getMinimumFee(unsigned int tx_bytes,
228  const CCoinControl& coin_control,
229  int* returned_target,
230  FeeReason* reason) = 0;
231 
233  virtual unsigned int getConfirmTarget() = 0;
234 
235  // Return whether HD enabled.
236  virtual bool hdEnabled() = 0;
237 
238  // Return whether the wallet is blank.
239  virtual bool canGetAddresses() = 0;
240 
241  // check if a certain wallet flag is set.
242  virtual bool IsWalletFlagSet(uint64_t flag) = 0;
243 
244  // Get default address type.
245  virtual OutputType getDefaultAddressType() = 0;
246 
247  // Get default change type.
248  virtual OutputType getDefaultChangeType() = 0;
249 
251  virtual CAmount getDefaultMaxTxFee() = 0;
252 
253  // Remove wallet.
254  virtual void remove() = 0;
255 
257  using UnloadFn = std::function<void()>;
258  virtual std::unique_ptr<Handler> handleUnload(UnloadFn fn) = 0;
259 
261  using ShowProgressFn = std::function<void(const std::string& title, int progress)>;
262  virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
263 
265  using StatusChangedFn = std::function<void()>;
266  virtual std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) = 0;
267 
269  using AddressBookChangedFn = std::function<void(const CTxDestination& address,
270  const std::string& label,
271  bool is_mine,
272  const std::string& purpose,
273  ChangeType status)>;
274  virtual std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) = 0;
275 
277  using TransactionChangedFn = std::function<void(const uint256& txid, ChangeType status)>;
278  virtual std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) = 0;
279 
281  using WatchOnlyChangedFn = std::function<void(bool have_watch_only)>;
282  virtual std::unique_ptr<Handler> handleWatchOnlyChanged(WatchOnlyChangedFn fn) = 0;
283 
285  using CanGetAddressesChangedFn = std::function<void()>;
286  virtual std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) = 0;
287 };
288 
291 {
292 public:
293  virtual ~PendingWalletTx() {}
294 
296  virtual const CTransaction& get() = 0;
297 
299  virtual bool commit(WalletValueMap value_map,
300  WalletOrderForm order_form,
301  std::string& reject_reason) = 0;
302 };
303 
306 {
309  std::string name;
310  std::string purpose;
311 
312  WalletAddress(CTxDestination dest, isminetype is_mine, std::string name, std::string purpose)
313  : dest(std::move(dest)), is_mine(is_mine), name(std::move(name)), purpose(std::move(purpose))
314  {
315  }
316 };
317 
320 {
321  CAmount balance = 0;
322  CAmount unconfirmed_balance = 0;
323  CAmount immature_balance = 0;
324  bool have_watch_only = false;
325  CAmount watch_only_balance = 0;
326  CAmount unconfirmed_watch_only_balance = 0;
327  CAmount immature_watch_only_balance = 0;
328 
329  bool balanceChanged(const WalletBalances& prev) const
330  {
331  return balance != prev.balance || unconfirmed_balance != prev.unconfirmed_balance ||
332  immature_balance != prev.immature_balance || watch_only_balance != prev.watch_only_balance ||
333  unconfirmed_watch_only_balance != prev.unconfirmed_watch_only_balance ||
334  immature_watch_only_balance != prev.immature_watch_only_balance;
335  }
336 };
337 
338 // Wallet transaction information.
339 struct WalletTx
340 {
342  std::vector<isminetype> txin_is_mine;
343  std::vector<isminetype> txout_is_mine;
344  std::vector<CTxDestination> txout_address;
345  std::vector<isminetype> txout_address_is_mine;
349  int64_t time;
350  std::map<std::string, std::string> value_map;
352 };
353 
356 {
360  unsigned int time_received;
361  uint32_t lock_time;
362  bool is_final;
367 };
368 
371 {
373  int64_t time;
374  int depth_in_main_chain = -1;
375  bool is_spent = false;
376 };
377 
380 std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet>& wallet);
381 
382 } // namespace interfaces
383 
384 #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:312
std::vector< CTxDestination > txout_address
Definition: wallet.h:344
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:360
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:281
std::vector< isminetype > txin_is_mine
Definition: wallet.h:342
virtual std::unique_ptr< Handler > handleAddressBookChanged(AddressBookChangedFn fn)=0
std::vector< isminetype > txout_is_mine
Definition: wallet.h:343
virtual CTransactionRef getTx(const uint256 &txid)=0
Get a transaction.
const char * prefix
Definition: rest.cpp:625
virtual CAmount getDefaultMaxTxFee()=0
Get max tx fee.
std::function< void()> UnloadFn
Register handler for unload message.
Definition: wallet.h:257
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.
uint8_t isminefilter
used for bitflags of isminetype
Definition: ismine.h:27
std::function< void()> CanGetAddressesChangedFn
Register handler for keypool changed messages.
Definition: wallet.h:285
virtual void lockCoin(const COutPoint &output)=0
Lock coin.
CTransactionRef tx
Definition: wallet.h:341
std::map< std::string, std::string > WalletValueMap
Definition: wallet.h:43
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:16
Coin Control Features.
Definition: coincontrol.h:16
std::function< void()> StatusChangedFn
Register handler for status changed messages.
Definition: wallet.h:265
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:217
virtual std::unique_ptr< Handler > handleUnload(UnloadFn fn)=0
CTxDestination dest
Definition: wallet.h:307
std::string purpose
Definition: wallet.h:310
virtual bool getKeyFromPool(bool internal, CPubKey &pub_key)=0
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:273
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
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:26
virtual ~PendingWalletTx()
Definition: wallet.h:293
Collection of wallet balances.
Definition: wallet.h:319
isminetype
IsMine() return codes.
Definition: ismine.h:18
An input of a transaction.
Definition: transaction.h:63
const char * name
Definition: rest.cpp:38
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:46
virtual bool encryptWallet(const SecureString &wallet_passphrase)=0
Encrypt wallet.
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.
An output of a transaction.
Definition: transaction.h:133
CAmount immature_watch_only_balance
Definition: wallet.h:327
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:81
virtual std::unique_ptr< PendingWalletTx > 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::function< void(const uint256 &txid, ChangeType status)> TransactionChangedFn
Register handler for transaction changed messages.
Definition: wallet.h:277
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:49
256-bit opaque blob.
Definition: uint256.h:121
virtual void learnRelatedScripts(const CPubKey &key, OutputType type)=0
Add scripts to key store so old so software versions opening the wallet database can detect payments ...
virtual bool signBumpTransaction(CMutableTransaction &mtx)=0
Sign bump transaction.
std::vector< isminetype > txout_address_is_mine
Definition: wallet.h:345
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:326
virtual void listLockedCoins(std::vector< COutPoint > &outputs)=0
List locked coins.
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:261
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:692
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:19
virtual bool getPubKey(const CKeyID &address, CPubKey &pub_key)=0
Get public key.
virtual bool isLockedCoin(const COutPoint &output)=0
Return whether coin is locked.
std::vector< std::pair< std::string, std::string > > WalletOrderForm
Definition: wallet.h:42
Information about one wallet address.
Definition: wallet.h:305
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:350
An encapsulated private key.
Definition: key.h:27
virtual void abortRescan()=0
Abort a rescan.
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
virtual CAmount getAvailableBalance(const CCoinControl &coin_control)=0
Get available balance.
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:139
Wallet transaction output.
Definition: wallet.h:370
virtual CAmount getRequiredFee(unsigned int tx_bytes)=0
Get required fee.
bool balanceChanged(const WalletBalances &prev) const
Definition: wallet.h:329
virtual std::string getWalletName()=0
Get wallet name.
Updated transaction status.
Definition: wallet.h:355
virtual bool getPrivKey(const CKeyID &address, CKey &key)=0
Get private key.
Tracking object returned by CreateTransaction and passed to CommitTransaction.
Definition: wallet.h:290
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.