Branch data Line data Source code
1 : : // Copyright (c) 2018-2022 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 <addresstype.h>
9 : : #include <common/signmessage.h>
10 : : #include <consensus/amount.h>
11 : : #include <interfaces/chain.h>
12 : : #include <pubkey.h>
13 : : #include <script/script.h>
14 : : #include <support/allocators/secure.h>
15 : : #include <util/fs.h>
16 : : #include <util/result.h>
17 : : #include <util/transaction_identifier.h>
18 : : #include <util/ui_change_type.h>
19 : :
20 : : #include <cstdint>
21 : : #include <functional>
22 : : #include <map>
23 : : #include <memory>
24 : : #include <string>
25 : : #include <tuple>
26 : : #include <type_traits>
27 : : #include <utility>
28 : : #include <vector>
29 : :
30 : : class CFeeRate;
31 : : class CKey;
32 : : enum class FeeReason;
33 : : enum class OutputType;
34 : : struct PartiallySignedTransaction;
35 : : struct bilingual_str;
36 : : namespace common {
37 : : enum class PSBTError;
38 : : } // namespace common
39 : : namespace node {
40 : : enum class TransactionError;
41 : : } // namespace node
42 : : namespace wallet {
43 : : class CCoinControl;
44 : : class CWallet;
45 : : enum class AddressPurpose;
46 : : enum isminetype : unsigned int;
47 : : struct CRecipient;
48 : : struct WalletContext;
49 : : using isminefilter = std::underlying_type_t<isminetype>;
50 : : } // namespace wallet
51 : :
52 : : namespace interfaces {
53 : :
54 : : class Handler;
55 : : struct WalletAddress;
56 : : struct WalletBalances;
57 : : struct WalletTx;
58 : : struct WalletTxOut;
59 : : struct WalletTxStatus;
60 : : struct WalletMigrationResult;
61 : :
62 : : using WalletOrderForm = std::vector<std::pair<std::string, std::string>>;
63 : : using WalletValueMap = std::map<std::string, std::string>;
64 : :
65 : : //! Interface for accessing a wallet.
66 [ + - ]: 1 : class Wallet
67 : : {
68 : : public:
69 : : virtual ~Wallet() = default;
70 : :
71 : : //! Encrypt wallet.
72 : : virtual bool encryptWallet(const SecureString& wallet_passphrase) = 0;
73 : :
74 : : //! Return whether wallet is encrypted.
75 : : virtual bool isCrypted() = 0;
76 : :
77 : : //! Lock wallet.
78 : : virtual bool lock() = 0;
79 : :
80 : : //! Unlock wallet.
81 : : virtual bool unlock(const SecureString& wallet_passphrase) = 0;
82 : :
83 : : //! Return whether wallet is locked.
84 : : virtual bool isLocked() = 0;
85 : :
86 : : //! Change wallet passphrase.
87 : : virtual bool changeWalletPassphrase(const SecureString& old_wallet_passphrase,
88 : : const SecureString& new_wallet_passphrase) = 0;
89 : :
90 : : //! Abort a rescan.
91 : : virtual void abortRescan() = 0;
92 : :
93 : : //! Back up wallet.
94 : : virtual bool backupWallet(const std::string& filename) = 0;
95 : :
96 : : //! Get wallet name.
97 : : virtual std::string getWalletName() = 0;
98 : :
99 : : // Get a new address.
100 : : virtual util::Result<CTxDestination> getNewDestination(const OutputType type, const std::string& label) = 0;
101 : :
102 : : //! Get public key.
103 : : virtual bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) = 0;
104 : :
105 : : //! Sign message
106 : : virtual SigningResult signMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) = 0;
107 : :
108 : : //! Return whether wallet has private key.
109 : : virtual bool isSpendable(const CTxDestination& dest) = 0;
110 : :
111 : : //! Add or update address.
112 : : virtual bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::optional<wallet::AddressPurpose>& purpose) = 0;
113 : :
114 : : // Remove address.
115 : : virtual bool delAddressBook(const CTxDestination& dest) = 0;
116 : :
117 : : //! Look up address in wallet, return whether exists.
118 : : virtual bool getAddress(const CTxDestination& dest,
119 : : std::string* name,
120 : : wallet::isminetype* is_mine,
121 : : wallet::AddressPurpose* purpose) = 0;
122 : :
123 : : //! Get wallet address list.
124 : : virtual std::vector<WalletAddress> getAddresses() = 0;
125 : :
126 : : //! Get receive requests.
127 : : virtual std::vector<std::string> getAddressReceiveRequests() = 0;
128 : :
129 : : //! Save or remove receive request.
130 : : virtual bool setAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& value) = 0;
131 : :
132 : : //! Display address on external signer
133 : : virtual util::Result<void> displayAddress(const CTxDestination& dest) = 0;
134 : :
135 : : //! Lock coin.
136 : : virtual bool lockCoin(const COutPoint& output, const bool write_to_db) = 0;
137 : :
138 : : //! Unlock coin.
139 : : virtual bool unlockCoin(const COutPoint& output) = 0;
140 : :
141 : : //! Return whether coin is locked.
142 : : virtual bool isLockedCoin(const COutPoint& output) = 0;
143 : :
144 : : //! List locked coins.
145 : : virtual void listLockedCoins(std::vector<COutPoint>& outputs) = 0;
146 : :
147 : : //! Create transaction.
148 : : virtual util::Result<CTransactionRef> createTransaction(const std::vector<wallet::CRecipient>& recipients,
149 : : const wallet::CCoinControl& coin_control,
150 : : bool sign,
151 : : int& change_pos,
152 : : CAmount& fee) = 0;
153 : :
154 : : //! Commit transaction.
155 : : virtual void commitTransaction(CTransactionRef tx,
156 : : WalletValueMap value_map,
157 : : WalletOrderForm order_form) = 0;
158 : :
159 : : //! Return whether transaction can be abandoned.
160 : : virtual bool transactionCanBeAbandoned(const Txid& txid) = 0;
161 : :
162 : : //! Abandon transaction.
163 : : virtual bool abandonTransaction(const Txid& txid) = 0;
164 : :
165 : : //! Return whether transaction can be bumped.
166 : : virtual bool transactionCanBeBumped(const Txid& txid) = 0;
167 : :
168 : : //! Create bump transaction.
169 : : virtual bool createBumpTransaction(const Txid& txid,
170 : : const wallet::CCoinControl& coin_control,
171 : : std::vector<bilingual_str>& errors,
172 : : CAmount& old_fee,
173 : : CAmount& new_fee,
174 : : CMutableTransaction& mtx) = 0;
175 : :
176 : : //! Sign bump transaction.
177 : : virtual bool signBumpTransaction(CMutableTransaction& mtx) = 0;
178 : :
179 : : //! Commit bump transaction.
180 : : virtual bool commitBumpTransaction(const Txid& txid,
181 : : CMutableTransaction&& mtx,
182 : : std::vector<bilingual_str>& errors,
183 : : Txid& bumped_txid) = 0;
184 : :
185 : : //! Get a transaction.
186 : : virtual CTransactionRef getTx(const Txid& txid) = 0;
187 : :
188 : : //! Get transaction information.
189 : : virtual WalletTx getWalletTx(const Txid& txid) = 0;
190 : :
191 : : //! Get list of all wallet transactions.
192 : : virtual std::set<WalletTx> getWalletTxs() = 0;
193 : :
194 : : //! Try to get updated status for a particular transaction, if possible without blocking.
195 : : virtual bool tryGetTxStatus(const Txid& txid,
196 : : WalletTxStatus& tx_status,
197 : : int& num_blocks,
198 : : int64_t& block_time) = 0;
199 : :
200 : : //! Get transaction details.
201 : : virtual WalletTx getWalletTxDetails(const Txid& txid,
202 : : WalletTxStatus& tx_status,
203 : : WalletOrderForm& order_form,
204 : : bool& in_mempool,
205 : : int& num_blocks) = 0;
206 : :
207 : : //! Fill PSBT.
208 : : virtual std::optional<common::PSBTError> fillPSBT(std::optional<int> sighash_type,
209 : : bool sign,
210 : : bool bip32derivs,
211 : : size_t* n_signed,
212 : : PartiallySignedTransaction& psbtx,
213 : : bool& complete) = 0;
214 : :
215 : : //! Get balances.
216 : : virtual WalletBalances getBalances() = 0;
217 : :
218 : : //! Get balances if possible without blocking.
219 : : virtual bool tryGetBalances(WalletBalances& balances, uint256& block_hash) = 0;
220 : :
221 : : //! Get balance.
222 : : virtual CAmount getBalance() = 0;
223 : :
224 : : //! Get available balance.
225 : : virtual CAmount getAvailableBalance(const wallet::CCoinControl& coin_control) = 0;
226 : :
227 : : //! Return whether transaction input belongs to wallet.
228 : : virtual wallet::isminetype txinIsMine(const CTxIn& txin) = 0;
229 : :
230 : : //! Return whether transaction output belongs to wallet.
231 : : virtual wallet::isminetype txoutIsMine(const CTxOut& txout) = 0;
232 : :
233 : : //! Return debit amount if transaction input belongs to wallet.
234 : : virtual CAmount getDebit(const CTxIn& txin, wallet::isminefilter filter) = 0;
235 : :
236 : : //! Return credit amount if transaction input belongs to wallet.
237 : : virtual CAmount getCredit(const CTxOut& txout, wallet::isminefilter filter) = 0;
238 : :
239 : : //! Return AvailableCoins + LockedCoins grouped by wallet address.
240 : : //! (put change in one group with wallet address)
241 : : using CoinsList = std::map<CTxDestination, std::vector<std::tuple<COutPoint, WalletTxOut>>>;
242 : : virtual CoinsList listCoins() = 0;
243 : :
244 : : //! Return wallet transaction output information.
245 : : virtual std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) = 0;
246 : :
247 : : //! Get required fee.
248 : : virtual CAmount getRequiredFee(unsigned int tx_bytes) = 0;
249 : :
250 : : //! Get minimum fee.
251 : : virtual CAmount getMinimumFee(unsigned int tx_bytes,
252 : : const wallet::CCoinControl& coin_control,
253 : : int* returned_target,
254 : : FeeReason* reason) = 0;
255 : :
256 : : //! Get tx confirm target.
257 : : virtual unsigned int getConfirmTarget() = 0;
258 : :
259 : : // Return whether HD enabled.
260 : : virtual bool hdEnabled() = 0;
261 : :
262 : : // Return whether the wallet is blank.
263 : : virtual bool canGetAddresses() = 0;
264 : :
265 : : // Return whether private keys enabled.
266 : : virtual bool privateKeysDisabled() = 0;
267 : :
268 : : // Return whether the wallet contains a Taproot scriptPubKeyMan
269 : : virtual bool taprootEnabled() = 0;
270 : :
271 : : // Return whether wallet uses an external signer.
272 : : virtual bool hasExternalSigner() = 0;
273 : :
274 : : // Get default address type.
275 : : virtual OutputType getDefaultAddressType() = 0;
276 : :
277 : : //! Get max tx fee.
278 : : virtual CAmount getDefaultMaxTxFee() = 0;
279 : :
280 : : // Remove wallet.
281 : : virtual void remove() = 0;
282 : :
283 : : //! Register handler for unload message.
284 : : using UnloadFn = std::function<void()>;
285 : : virtual std::unique_ptr<Handler> handleUnload(UnloadFn fn) = 0;
286 : :
287 : : //! Register handler for show progress messages.
288 : : using ShowProgressFn = std::function<void(const std::string& title, int progress)>;
289 : : virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
290 : :
291 : : //! Register handler for status changed messages.
292 : : using StatusChangedFn = std::function<void()>;
293 : : virtual std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) = 0;
294 : :
295 : : //! Register handler for address book changed messages.
296 : : using AddressBookChangedFn = std::function<void(const CTxDestination& address,
297 : : const std::string& label,
298 : : bool is_mine,
299 : : wallet::AddressPurpose purpose,
300 : : ChangeType status)>;
301 : : virtual std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) = 0;
302 : :
303 : : //! Register handler for transaction changed messages.
304 : : using TransactionChangedFn = std::function<void(const Txid& txid, ChangeType status)>;
305 : : virtual std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) = 0;
306 : :
307 : : //! Register handler for keypool changed messages.
308 : : using CanGetAddressesChangedFn = std::function<void()>;
309 : : virtual std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) = 0;
310 : :
311 : : //! Return pointer to internal wallet class, useful for testing.
312 : 0 : virtual wallet::CWallet* wallet() { return nullptr; }
313 : : };
314 : :
315 : : //! Wallet chain client that in addition to having chain client methods for
316 : : //! starting up, shutting down, and registering RPCs, also has additional
317 : : //! methods (called by the GUI) to load and create wallets.
318 [ + - ]: 24 : class WalletLoader : public ChainClient
319 : : {
320 : : public:
321 : : //! Create new wallet.
322 : : virtual util::Result<std::unique_ptr<Wallet>> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, std::vector<bilingual_str>& warnings) = 0;
323 : :
324 : : //! Load existing wallet.
325 : : virtual util::Result<std::unique_ptr<Wallet>> loadWallet(const std::string& name, std::vector<bilingual_str>& warnings) = 0;
326 : :
327 : : //! Return default wallet directory.
328 : : virtual std::string getWalletDir() = 0;
329 : :
330 : : //! Restore backup wallet
331 : : virtual util::Result<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) = 0;
332 : :
333 : : //! Migrate a wallet
334 : : virtual util::Result<WalletMigrationResult> migrateWallet(const std::string& name, const SecureString& passphrase) = 0;
335 : :
336 : : //! Returns true if wallet stores encryption keys
337 : : virtual bool isEncrypted(const std::string& wallet_name) = 0;
338 : :
339 : : //! Return available wallets in wallet directory.
340 : : virtual std::vector<std::pair<std::string, std::string>> listWalletDir() = 0;
341 : :
342 : : //! Return interfaces for accessing wallets (if any).
343 : : virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
344 : :
345 : : //! Register handler for load wallet messages. This callback is triggered by
346 : : //! createWallet and loadWallet above, and also triggered when wallets are
347 : : //! loaded at startup or by RPC.
348 : : using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
349 : : virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;
350 : :
351 : : //! Return pointer to internal context, useful for testing.
352 : 0 : virtual wallet::WalletContext* context() { return nullptr; }
353 : : };
354 : :
355 : : //! Information about one wallet address.
356 : 0 : struct WalletAddress
357 : : {
358 : : CTxDestination dest;
359 : : wallet::isminetype is_mine;
360 : : wallet::AddressPurpose purpose;
361 : : std::string name;
362 : :
363 : 0 : WalletAddress(CTxDestination dest, wallet::isminetype is_mine, wallet::AddressPurpose purpose, std::string name)
364 : 0 : : dest(std::move(dest)), is_mine(is_mine), purpose(std::move(purpose)), name(std::move(name))
365 : : {
366 : 0 : }
367 : : };
368 : :
369 : : //! Collection of wallet balances.
370 : : struct WalletBalances
371 : : {
372 : : CAmount balance = 0;
373 : : CAmount unconfirmed_balance = 0;
374 : : CAmount immature_balance = 0;
375 : :
376 : : bool balanceChanged(const WalletBalances& prev) const
377 : : {
378 : : return balance != prev.balance || unconfirmed_balance != prev.unconfirmed_balance ||
379 : : immature_balance != prev.immature_balance;
380 : : }
381 : : };
382 : :
383 : : // Wallet transaction information.
384 : : struct WalletTx
385 : : {
386 : : CTransactionRef tx;
387 : : std::vector<wallet::isminetype> txin_is_mine;
388 : : std::vector<wallet::isminetype> txout_is_mine;
389 : : std::vector<bool> txout_is_change;
390 : : std::vector<CTxDestination> txout_address;
391 : : std::vector<wallet::isminetype> txout_address_is_mine;
392 : : CAmount credit;
393 : : CAmount debit;
394 : : CAmount change;
395 : : int64_t time;
396 : : std::map<std::string, std::string> value_map;
397 : : bool is_coinbase;
398 : :
399 [ # # # # : 0 : bool operator<(const WalletTx& a) const { return tx->GetHash() < a.tx->GetHash(); }
# # ]
400 : : };
401 : :
402 : : //! Updated transaction status.
403 : : struct WalletTxStatus
404 : : {
405 : : int block_height;
406 : : int blocks_to_maturity;
407 : : int depth_in_main_chain;
408 : : unsigned int time_received;
409 : : uint32_t lock_time;
410 : : bool is_trusted;
411 : : bool is_abandoned;
412 : : bool is_coinbase;
413 : : bool is_in_main_chain;
414 : : };
415 : :
416 : : //! Wallet transaction output.
417 : 0 : struct WalletTxOut
418 : : {
419 : : CTxOut txout;
420 : : int64_t time;
421 : : int depth_in_main_chain = -1;
422 : : bool is_spent = false;
423 : : };
424 : :
425 : : //! Migrated wallet info
426 : : struct WalletMigrationResult
427 : : {
428 : : std::unique_ptr<Wallet> wallet;
429 : : std::optional<std::string> watchonly_wallet_name;
430 : : std::optional<std::string> solvables_wallet_name;
431 : : fs::path backup_path;
432 : : };
433 : :
434 : : //! Return implementation of Wallet interface. This function is defined in
435 : : //! dummywallet.cpp and throws if the wallet component is not compiled.
436 : : std::unique_ptr<Wallet> MakeWallet(wallet::WalletContext& context, const std::shared_ptr<wallet::CWallet>& wallet);
437 : :
438 : : //! Return implementation of ChainClient interface for a wallet loader. This
439 : : //! function will be undefined in builds where ENABLE_WALLET is false.
440 : : std::unique_ptr<WalletLoader> MakeWalletLoader(Chain& chain, ArgsManager& args);
441 : :
442 : : } // namespace interfaces
443 : :
444 : : #endif // BITCOIN_INTERFACES_WALLET_H
|