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/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 <stdint.h>
18 #include <string>
19 #include <tuple>
20 #include <utility>
21 #include <vector>
22 
23 class CCoinControl;
24 class CFeeRate;
25 class CKey;
26 class CWallet;
27 enum isminetype : unsigned int;
28 enum class FeeReason;
29 typedef uint8_t isminefilter;
30 
31 enum class OutputType;
32 struct CRecipient;
33 
34 namespace interfaces {
35 
36 class Handler;
37 struct WalletAddress;
38 struct WalletBalances;
39 struct WalletTx;
40 struct WalletTxOut;
41 struct WalletTxStatus;
42 
43 using WalletOrderForm = std::vector<std::pair<std::string, std::string>>;
44 using WalletValueMap = std::map<std::string, std::string>;
45 
47 class Wallet
48 {
49 public:
50  virtual ~Wallet() {}
51 
53  virtual bool encryptWallet(const SecureString& wallet_passphrase) = 0;
54 
56  virtual bool isCrypted() = 0;
57 
59  virtual bool lock() = 0;
60 
62  virtual bool unlock(const SecureString& wallet_passphrase) = 0;
63 
65  virtual bool isLocked() = 0;
66 
68  virtual bool changeWalletPassphrase(const SecureString& old_wallet_passphrase,
69  const SecureString& new_wallet_passphrase) = 0;
70 
72  virtual void abortRescan() = 0;
73 
75  virtual bool backupWallet(const std::string& filename) = 0;
76 
78  virtual std::string getWalletName() = 0;
79 
80  // Get a new address.
81  virtual bool getNewDestination(const OutputType type, const std::string label, CTxDestination& dest) = 0;
82 
84  virtual bool getPubKey(const CKeyID& address, CPubKey& pub_key) = 0;
85 
87  virtual bool getPrivKey(const CKeyID& address, CKey& key) = 0;
88 
90  virtual bool isSpendable(const CTxDestination& dest) = 0;
91 
93  virtual bool haveWatchOnly() = 0;
94 
96  virtual bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::string& purpose) = 0;
97 
98  // Remove address.
99  virtual bool delAddressBook(const CTxDestination& dest) = 0;
100 
102  virtual bool getAddress(const CTxDestination& dest,
103  std::string* name,
104  isminetype* is_mine,
105  std::string* purpose) = 0;
106 
108  virtual std::vector<WalletAddress> getAddresses() = 0;
109 
112  virtual void learnRelatedScripts(const CPubKey& key, OutputType type) = 0;
113 
115  virtual bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) = 0;
116 
118  virtual bool eraseDestData(const CTxDestination& dest, const std::string& key) = 0;
119 
121  virtual std::vector<std::string> getDestValues(const std::string& prefix) = 0;
122 
124  virtual void lockCoin(const COutPoint& output) = 0;
125 
127  virtual void unlockCoin(const COutPoint& output) = 0;
128 
130  virtual bool isLockedCoin(const COutPoint& output) = 0;
131 
133  virtual void listLockedCoins(std::vector<COutPoint>& outputs) = 0;
134 
136  virtual CTransactionRef createTransaction(const std::vector<CRecipient>& recipients,
137  const CCoinControl& coin_control,
138  bool sign,
139  int& change_pos,
140  CAmount& fee,
141  std::string& fail_reason) = 0;
142 
144  virtual bool commitTransaction(CTransactionRef tx,
145  WalletValueMap value_map,
146  WalletOrderForm order_form,
147  std::string& reject_reason) = 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  CAmount total_fee,
162  std::vector<std::string>& errors,
163  CAmount& old_fee,
164  CAmount& new_fee,
165  CMutableTransaction& mtx) = 0;
166 
168  virtual bool signBumpTransaction(CMutableTransaction& mtx) = 0;
169 
171  virtual bool commitBumpTransaction(const uint256& txid,
172  CMutableTransaction&& mtx,
173  std::vector<std::string>& errors,
174  uint256& bumped_txid) = 0;
175 
177  virtual CTransactionRef getTx(const uint256& txid) = 0;
178 
180  virtual WalletTx getWalletTx(const uint256& txid) = 0;
181 
183  virtual std::vector<WalletTx> getWalletTxs() = 0;
184 
186  virtual bool tryGetTxStatus(const uint256& txid,
187  WalletTxStatus& tx_status,
188  int& num_blocks,
189  int64_t& block_time) = 0;
190 
192  virtual WalletTx getWalletTxDetails(const uint256& txid,
193  WalletTxStatus& tx_status,
194  WalletOrderForm& order_form,
195  bool& in_mempool,
196  int& num_blocks) = 0;
197 
199  virtual WalletBalances getBalances() = 0;
200 
202  virtual bool tryGetBalances(WalletBalances& balances, int& num_blocks) = 0;
203 
205  virtual CAmount getBalance() = 0;
206 
208  virtual CAmount getAvailableBalance(const CCoinControl& coin_control) = 0;
209 
211  virtual isminetype txinIsMine(const CTxIn& txin) = 0;
212 
214  virtual isminetype txoutIsMine(const CTxOut& txout) = 0;
215 
217  virtual CAmount getDebit(const CTxIn& txin, isminefilter filter) = 0;
218 
220  virtual CAmount getCredit(const CTxOut& txout, isminefilter filter) = 0;
221 
224  using CoinsList = std::map<CTxDestination, std::vector<std::tuple<COutPoint, WalletTxOut>>>;
225  virtual CoinsList listCoins() = 0;
226 
228  virtual std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) = 0;
229 
231  virtual CAmount getRequiredFee(unsigned int tx_bytes) = 0;
232 
234  virtual CAmount getMinimumFee(unsigned int tx_bytes,
235  const CCoinControl& coin_control,
236  int* returned_target,
237  FeeReason* reason) = 0;
238 
240  virtual unsigned int getConfirmTarget() = 0;
241 
242  // Return whether HD enabled.
243  virtual bool hdEnabled() = 0;
244 
245  // Return whether the wallet is blank.
246  virtual bool canGetAddresses() = 0;
247 
248  // check if a certain wallet flag is set.
249  virtual bool IsWalletFlagSet(uint64_t flag) = 0;
250 
251  // Get default address type.
252  virtual OutputType getDefaultAddressType() = 0;
253 
254  // Get default change type.
255  virtual OutputType getDefaultChangeType() = 0;
256 
258  virtual CAmount getDefaultMaxTxFee() = 0;
259 
260  // Remove wallet.
261  virtual void remove() = 0;
262 
264  using UnloadFn = std::function<void()>;
265  virtual std::unique_ptr<Handler> handleUnload(UnloadFn fn) = 0;
266 
268  using ShowProgressFn = std::function<void(const std::string& title, int progress)>;
269  virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
270 
272  using StatusChangedFn = std::function<void()>;
273  virtual std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) = 0;
274 
276  using AddressBookChangedFn = std::function<void(const CTxDestination& address,
277  const std::string& label,
278  bool is_mine,
279  const std::string& purpose,
280  ChangeType status)>;
281  virtual std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) = 0;
282 
284  using TransactionChangedFn = std::function<void(const uint256& txid, ChangeType status)>;
285  virtual std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) = 0;
286 
288  using WatchOnlyChangedFn = std::function<void(bool have_watch_only)>;
289  virtual std::unique_ptr<Handler> handleWatchOnlyChanged(WatchOnlyChangedFn fn) = 0;
290 
292  using CanGetAddressesChangedFn = std::function<void()>;
293  virtual std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) = 0;
294 };
295 
298 {
301  std::string name;
302  std::string purpose;
303 
304  WalletAddress(CTxDestination dest, isminetype is_mine, std::string name, std::string purpose)
305  : dest(std::move(dest)), is_mine(is_mine), name(std::move(name)), purpose(std::move(purpose))
306  {
307  }
308 };
309 
312 {
314  CAmount unconfirmed_balance = 0;
315  CAmount immature_balance = 0;
316  bool have_watch_only = false;
317  CAmount watch_only_balance = 0;
318  CAmount unconfirmed_watch_only_balance = 0;
319  CAmount immature_watch_only_balance = 0;
320 
321  bool balanceChanged(const WalletBalances& prev) const
322  {
323  return balance != prev.balance || unconfirmed_balance != prev.unconfirmed_balance ||
324  immature_balance != prev.immature_balance || watch_only_balance != prev.watch_only_balance ||
325  unconfirmed_watch_only_balance != prev.unconfirmed_watch_only_balance ||
326  immature_watch_only_balance != prev.immature_watch_only_balance;
327  }
328 };
329 
330 // Wallet transaction information.
331 struct WalletTx
332 {
334  std::vector<isminetype> txin_is_mine;
335  std::vector<isminetype> txout_is_mine;
336  std::vector<CTxDestination> txout_address;
337  std::vector<isminetype> txout_address_is_mine;
341  int64_t time;
342  std::map<std::string, std::string> value_map;
344 };
345 
348 {
352  unsigned int time_received;
353  uint32_t lock_time;
354  bool is_final;
359 };
360 
363 {
365  int64_t time;
366  int depth_in_main_chain = -1;
367  bool is_spent = false;
368 };
369 
372 std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet>& wallet);
373 
374 } // namespace interfaces
375 
376 #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:304
std::vector< CTxDestination > txout_address
Definition: wallet.h:336
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:352
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:288
std::vector< isminetype > txin_is_mine
Definition: wallet.h:334
virtual std::unique_ptr< Handler > handleAddressBookChanged(AddressBookChangedFn fn)=0
std::vector< isminetype > txout_is_mine
Definition: wallet.h:335
virtual CTransactionRef getTx(const uint256 &txid)=0
Get a transaction.
const char * prefix
Definition: rest.cpp:626
virtual CAmount getDefaultMaxTxFee()=0
Get max tx fee.
std::function< void()> UnloadFn
Register handler for unload message.
Definition: wallet.h:264
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:292
virtual void lockCoin(const COutPoint &output)=0
Lock coin.
CTransactionRef tx
Definition: wallet.h:333
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:44
virtual bool eraseDestData(const CTxDestination &dest, const std::string &key)=0
Erase dest data.
virtual bool commitTransaction(CTransactionRef tx, WalletValueMap value_map, WalletOrderForm order_form, std::string &reject_reason)=0
Commit transaction.
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:19
std::function< void()> StatusChangedFn
Register handler for status changed messages.
Definition: wallet.h:272
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:224
virtual std::unique_ptr< Handler > handleUnload(UnloadFn fn)=0
CTxDestination dest
Definition: wallet.h:299
std::string purpose
Definition: wallet.h:302
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:280
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:26
Collection of wallet balances.
Definition: wallet.h:311
An input of a transaction.
Definition: transaction.h:63
const char * name
Definition: rest.cpp:39
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:47
virtual bool encryptWallet(const SecureString &wallet_passphrase)=0
Encrypt wallet.
uint8_t isminefilter
Definition: wallet.h:28
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:319
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
std::function< void(const uint256 &txid, ChangeType status)> TransactionChangedFn
Register handler for transaction changed messages.
Definition: wallet.h:284
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:50
256-bit opaque blob.
Definition: uint256.h:121
virtual bool getNewDestination(const OutputType type, const std::string label, CTxDestination &dest)=0
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:337
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:318
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:268
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:680
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:43
Information about one wallet address.
Definition: wallet.h:297
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:342
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 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:362
virtual CAmount getRequiredFee(unsigned int tx_bytes)=0
Get required fee.
bool balanceChanged(const WalletBalances &prev) const
Definition: wallet.h:321
virtual std::string getWalletName()=0
Get wallet name.
Updated transaction status.
Definition: wallet.h:347
virtual bool getPrivKey(const CKeyID &address, CKey &key)=0
Get private key.
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.