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