BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
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& adjusted_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,
190  int64_t& adjusted_time) = 0;
191 
193  virtual WalletBalances getBalances() = 0;
194 
196  virtual bool tryGetBalances(WalletBalances& balances, int& num_blocks) = 0;
197 
199  virtual CAmount getBalance() = 0;
200 
202  virtual CAmount getAvailableBalance(const CCoinControl& coin_control) = 0;
203 
205  virtual isminetype txinIsMine(const CTxIn& txin) = 0;
206 
208  virtual isminetype txoutIsMine(const CTxOut& txout) = 0;
209 
211  virtual CAmount getDebit(const CTxIn& txin, isminefilter filter) = 0;
212 
214  virtual CAmount getCredit(const CTxOut& txout, isminefilter filter) = 0;
215 
218  using CoinsList = std::map<CTxDestination, std::vector<std::tuple<COutPoint, WalletTxOut>>>;
219  virtual CoinsList listCoins() = 0;
220 
222  virtual std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) = 0;
223 
225  virtual CAmount getRequiredFee(unsigned int tx_bytes) = 0;
226 
228  virtual CAmount getMinimumFee(unsigned int tx_bytes,
229  const CCoinControl& coin_control,
230  int* returned_target,
231  FeeReason* reason) = 0;
232 
234  virtual unsigned int getConfirmTarget() = 0;
235 
236  // Return whether HD enabled.
237  virtual bool hdEnabled() = 0;
238 
239  // check if a certain wallet flag is set.
240  virtual bool IsWalletFlagSet(uint64_t flag) = 0;
241 
242  // Get default address type.
243  virtual OutputType getDefaultAddressType() = 0;
244 
245  // Get default change type.
246  virtual OutputType getDefaultChangeType() = 0;
247 
249  using UnloadFn = std::function<void()>;
250  virtual std::unique_ptr<Handler> handleUnload(UnloadFn fn) = 0;
251 
253  using ShowProgressFn = std::function<void(const std::string& title, int progress)>;
254  virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
255 
257  using StatusChangedFn = std::function<void()>;
258  virtual std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) = 0;
259 
261  using AddressBookChangedFn = std::function<void(const CTxDestination& address,
262  const std::string& label,
263  bool is_mine,
264  const std::string& purpose,
265  ChangeType status)>;
266  virtual std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) = 0;
267 
269  using TransactionChangedFn = std::function<void(const uint256& txid, ChangeType status)>;
270  virtual std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) = 0;
271 
273  using WatchOnlyChangedFn = std::function<void(bool have_watch_only)>;
274  virtual std::unique_ptr<Handler> handleWatchOnlyChanged(WatchOnlyChangedFn fn) = 0;
275 };
276 
279 {
280 public:
281  virtual ~PendingWalletTx() {}
282 
284  virtual const CTransaction& get() = 0;
285 
287  virtual int64_t getVirtualSize() = 0;
288 
290  virtual bool commit(WalletValueMap value_map,
291  WalletOrderForm order_form,
292  std::string& reject_reason) = 0;
293 };
294 
297 {
300  std::string name;
301  std::string purpose;
302 
304  : dest(std::move(dest)), is_mine(is_mine), name(std::move(name)), purpose(std::move(purpose))
305  {
306  }
307 };
308 
311 {
315  bool have_watch_only = false;
319 
320  bool balanceChanged(const WalletBalances& prev) const
321  {
322  return balance != prev.balance || unconfirmed_balance != prev.unconfirmed_balance ||
326  }
327 };
328 
329 // Wallet transaction information.
330 struct WalletTx
331 {
333  std::vector<isminetype> txin_is_mine;
334  std::vector<isminetype> txout_is_mine;
335  std::vector<CTxDestination> txout_address;
336  std::vector<isminetype> txout_address_is_mine;
340  int64_t time;
341  std::map<std::string, std::string> value_map;
343 };
344 
347 {
351  unsigned int time_received;
352  uint32_t lock_time;
353  bool is_final;
358 };
359 
362 {
364  int64_t time;
366  bool is_spent = false;
367 };
368 
371 std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet>& wallet);
372 
373 } // namespace interfaces
374 
375 #endif // BITCOIN_INTERFACES_WALLET_H
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 bool tryGetTxStatus(const uint256 &txid, WalletTxStatus &tx_status, int &num_blocks, int64_t &adjusted_time)=0
Try to get updated status for a particular transaction, if possible without blocking.
virtual CoinsList listCoins()=0
WalletAddress(CTxDestination dest, isminetype is_mine, std::string name, std::string purpose)
Definition: wallet.h:303
std::vector< CTxDestination > txout_address
Definition: wallet.h:335
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:351
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:273
std::vector< isminetype > txin_is_mine
Definition: wallet.h:333
virtual std::unique_ptr< Handler > handleAddressBookChanged(AddressBookChangedFn fn)=0
std::vector< isminetype > txout_is_mine
Definition: wallet.h:334
virtual CTransactionRef getTx(const uint256 &txid)=0
Get a transaction.
const char * prefix
Definition: rest.cpp:581
std::function< void()> UnloadFn
Register handler for unload message.
Definition: wallet.h:249
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:56
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:25
virtual void lockCoin(const COutPoint &output)=0
Lock coin.
virtual bool commit(WalletValueMap value_map, WalletOrderForm order_form, std::string &reject_reason)=0
Send pending transaction and commit to wallet.
CTransactionRef tx
Definition: wallet.h:332
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.
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:402
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:15
Coin Control Features.
Definition: coincontrol.h:16
std::function< void()> StatusChangedFn
Register handler for status changed messages.
Definition: wallet.h:257
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:218
virtual std::unique_ptr< Handler > handleUnload(UnloadFn fn)=0
CTxDestination dest
Definition: wallet.h:298
std::string purpose
Definition: wallet.h:301
virtual bool getKeyFromPool(bool internal, CPubKey &pub_key)=0
isminefilter filter
Definition: rpcwallet.cpp:1011
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:265
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 > handleStatusChanged(StatusChangedFn fn)=0
virtual void unlockCoin(const COutPoint &output)=0
Unlock coin.
ChangeType
General change type (added, updated, removed).
Definition: ui_interface.h:23
virtual ~PendingWalletTx()
Definition: wallet.h:281
Collection of wallet balances.
Definition: wallet.h:310
isminetype
IsMine() return codes.
Definition: ismine.h:17
An input of a transaction.
Definition: transaction.h:61
boost::variant< CNoDestination, CKeyID, CScriptID, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:123
const char * name
Definition: rest.cpp:37
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.
An output of a transaction.
Definition: transaction.h:131
CAmount immature_watch_only_balance
Definition: wallet.h:318
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
virtual int64_t getVirtualSize()=0
Get virtual transaction size.
std::unique_ptr< Wallet > MakeWallet(const std::shared_ptr< CWallet > &wallet)
Return implementation of Wallet interface.
Definition: dummywallet.cpp:56
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:269
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:122
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:336
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:317
virtual void listLockedCoins(std::vector< COutPoint > &outputs)=0
List locked coins.
virtual WalletTx getWalletTxDetails(const uint256 &txid, WalletTxStatus &tx_status, WalletOrderForm &order_form, bool &in_mempool, int &num_blocks, int64_t &adjusted_time)=0
Get transaction details.
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 Hash360 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:253
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:599
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:296
virtual bool lock()=0
Lock wallet.
A mutable version of CTransaction.
Definition: transaction.h:360
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:341
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:264
virtual CAmount getAvailableBalance(const CCoinControl &coin_control)=0
Get available balance.
virtual WalletBalances getBalances()=0
Get balances.
Wallet transaction output.
Definition: wallet.h:361
virtual CAmount getRequiredFee(unsigned int tx_bytes)=0
Get required fee.
bool balanceChanged(const WalletBalances &prev) const
Definition: wallet.h:320
virtual std::string getWalletName()=0
Get wallet name.
Updated transaction status.
Definition: wallet.h:346
virtual bool getPrivKey(const CKeyID &address, CKey &key)=0
Get private key.
Tracking object returned by CreateTransaction and passed to CommitTransaction.
Definition: wallet.h:278
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.