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