/bitcoin/src/wallet/interfaces.cpp
Line | Count | Source |
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 | | #include <interfaces/wallet.h> |
6 | | |
7 | | #include <common/args.h> |
8 | | #include <consensus/amount.h> |
9 | | #include <interfaces/chain.h> |
10 | | #include <interfaces/handler.h> |
11 | | #include <node/types.h> |
12 | | #include <policy/fees.h> |
13 | | #include <primitives/transaction.h> |
14 | | #include <rpc/server.h> |
15 | | #include <scheduler.h> |
16 | | #include <support/allocators/secure.h> |
17 | | #include <sync.h> |
18 | | #include <uint256.h> |
19 | | #include <util/check.h> |
20 | | #include <util/translation.h> |
21 | | #include <util/ui_change_type.h> |
22 | | #include <wallet/coincontrol.h> |
23 | | #include <wallet/context.h> |
24 | | #include <wallet/feebumper.h> |
25 | | #include <wallet/fees.h> |
26 | | #include <wallet/types.h> |
27 | | #include <wallet/load.h> |
28 | | #include <wallet/receive.h> |
29 | | #include <wallet/rpc/wallet.h> |
30 | | #include <wallet/spend.h> |
31 | | #include <wallet/wallet.h> |
32 | | |
33 | | #include <memory> |
34 | | #include <string> |
35 | | #include <utility> |
36 | | #include <vector> |
37 | | |
38 | | using common::PSBTError; |
39 | | using interfaces::Chain; |
40 | | using interfaces::FoundBlock; |
41 | | using interfaces::Handler; |
42 | | using interfaces::MakeSignalHandler; |
43 | | using interfaces::Wallet; |
44 | | using interfaces::WalletAddress; |
45 | | using interfaces::WalletBalances; |
46 | | using interfaces::WalletLoader; |
47 | | using interfaces::WalletMigrationResult; |
48 | | using interfaces::WalletOrderForm; |
49 | | using interfaces::WalletTx; |
50 | | using interfaces::WalletTxOut; |
51 | | using interfaces::WalletTxStatus; |
52 | | using interfaces::WalletValueMap; |
53 | | |
54 | | namespace wallet { |
55 | | // All members of the classes in this namespace are intentionally public, as the |
56 | | // classes themselves are private. |
57 | | namespace { |
58 | | //! Construct wallet tx struct. |
59 | | WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx) |
60 | 0 | { |
61 | 0 | LOCK(wallet.cs_wallet); |
62 | 0 | WalletTx result; |
63 | 0 | result.tx = wtx.tx; |
64 | 0 | result.txin_is_mine.reserve(wtx.tx->vin.size()); |
65 | 0 | for (const auto& txin : wtx.tx->vin) { Branch (65:27): [True: 0, False: 0]
|
66 | 0 | result.txin_is_mine.emplace_back(InputIsMine(wallet, txin)); |
67 | 0 | } |
68 | 0 | result.txout_is_mine.reserve(wtx.tx->vout.size()); |
69 | 0 | result.txout_address.reserve(wtx.tx->vout.size()); |
70 | 0 | result.txout_address_is_mine.reserve(wtx.tx->vout.size()); |
71 | 0 | for (const auto& txout : wtx.tx->vout) { Branch (71:28): [True: 0, False: 0]
|
72 | 0 | result.txout_is_mine.emplace_back(wallet.IsMine(txout)); |
73 | 0 | result.txout_is_change.push_back(OutputIsChange(wallet, txout)); |
74 | 0 | result.txout_address.emplace_back(); |
75 | 0 | result.txout_address_is_mine.emplace_back(ExtractDestination(txout.scriptPubKey, result.txout_address.back()) ? Branch (75:51): [True: 0, False: 0]
|
76 | 0 | wallet.IsMine(result.txout_address.back()) : |
77 | 0 | ISMINE_NO); |
78 | 0 | } |
79 | 0 | result.credit = CachedTxGetCredit(wallet, wtx, ISMINE_ALL); |
80 | 0 | result.debit = CachedTxGetDebit(wallet, wtx, ISMINE_ALL); |
81 | 0 | result.change = CachedTxGetChange(wallet, wtx); |
82 | 0 | result.time = wtx.GetTxTime(); |
83 | 0 | result.value_map = wtx.mapValue; |
84 | 0 | result.is_coinbase = wtx.IsCoinBase(); |
85 | 0 | return result; |
86 | 0 | } |
87 | | |
88 | | //! Construct wallet tx status struct. |
89 | | WalletTxStatus MakeWalletTxStatus(const CWallet& wallet, const CWalletTx& wtx) |
90 | | EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) |
91 | 0 | { |
92 | 0 | AssertLockHeld(wallet.cs_wallet); |
93 | |
|
94 | 0 | WalletTxStatus result; |
95 | 0 | result.block_height = |
96 | 0 | wtx.state<TxStateConfirmed>() ? wtx.state<TxStateConfirmed>()->confirmed_block_height : Branch (96:9): [True: 0, False: 0]
|
97 | 0 | wtx.state<TxStateBlockConflicted>() ? wtx.state<TxStateBlockConflicted>()->conflicting_block_height : Branch (97:9): [True: 0, False: 0]
|
98 | 0 | std::numeric_limits<int>::max(); |
99 | 0 | result.blocks_to_maturity = wallet.GetTxBlocksToMaturity(wtx); |
100 | 0 | result.depth_in_main_chain = wallet.GetTxDepthInMainChain(wtx); |
101 | 0 | result.time_received = wtx.nTimeReceived; |
102 | 0 | result.lock_time = wtx.tx->nLockTime; |
103 | 0 | result.is_trusted = CachedTxIsTrusted(wallet, wtx); |
104 | 0 | result.is_abandoned = wtx.isAbandoned(); |
105 | 0 | result.is_coinbase = wtx.IsCoinBase(); |
106 | 0 | result.is_in_main_chain = wtx.isConfirmed(); |
107 | 0 | return result; |
108 | 0 | } |
109 | | |
110 | | //! Construct wallet TxOut struct. |
111 | | WalletTxOut MakeWalletTxOut(const CWallet& wallet, |
112 | | const CWalletTx& wtx, |
113 | | int n, |
114 | | int depth) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) |
115 | 0 | { |
116 | 0 | WalletTxOut result; |
117 | 0 | result.txout = wtx.tx->vout[n]; |
118 | 0 | result.time = wtx.GetTxTime(); |
119 | 0 | result.depth_in_main_chain = depth; |
120 | 0 | result.is_spent = wallet.IsSpent(COutPoint(wtx.GetHash(), n)); |
121 | 0 | return result; |
122 | 0 | } |
123 | | |
124 | | WalletTxOut MakeWalletTxOut(const CWallet& wallet, |
125 | | const COutput& output) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) |
126 | 0 | { |
127 | 0 | WalletTxOut result; |
128 | 0 | result.txout = output.txout; |
129 | 0 | result.time = output.time; |
130 | 0 | result.depth_in_main_chain = output.depth; |
131 | 0 | result.is_spent = wallet.IsSpent(output.outpoint); |
132 | 0 | return result; |
133 | 0 | } |
134 | | |
135 | | class WalletImpl : public Wallet |
136 | | { |
137 | | public: |
138 | 0 | explicit WalletImpl(WalletContext& context, const std::shared_ptr<CWallet>& wallet) : m_context(context), m_wallet(wallet) {} |
139 | | |
140 | | bool encryptWallet(const SecureString& wallet_passphrase) override |
141 | 0 | { |
142 | 0 | return m_wallet->EncryptWallet(wallet_passphrase); |
143 | 0 | } |
144 | 0 | bool isCrypted() override { return m_wallet->IsCrypted(); } |
145 | 0 | bool lock() override { return m_wallet->Lock(); } |
146 | 0 | bool unlock(const SecureString& wallet_passphrase) override { return m_wallet->Unlock(wallet_passphrase); } |
147 | 0 | bool isLocked() override { return m_wallet->IsLocked(); } |
148 | | bool changeWalletPassphrase(const SecureString& old_wallet_passphrase, |
149 | | const SecureString& new_wallet_passphrase) override |
150 | 0 | { |
151 | 0 | return m_wallet->ChangeWalletPassphrase(old_wallet_passphrase, new_wallet_passphrase); |
152 | 0 | } |
153 | 0 | void abortRescan() override { m_wallet->AbortRescan(); } |
154 | 0 | bool backupWallet(const std::string& filename) override { return m_wallet->BackupWallet(filename); } |
155 | 0 | std::string getWalletName() override { return m_wallet->GetName(); } |
156 | | util::Result<CTxDestination> getNewDestination(const OutputType type, const std::string& label) override |
157 | 0 | { |
158 | 0 | LOCK(m_wallet->cs_wallet); |
159 | 0 | return m_wallet->GetNewDestination(type, label); |
160 | 0 | } |
161 | | bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) override |
162 | 0 | { |
163 | 0 | std::unique_ptr<SigningProvider> provider = m_wallet->GetSolvingProvider(script); |
164 | 0 | if (provider) { Branch (164:13): [True: 0, False: 0]
|
165 | 0 | return provider->GetPubKey(address, pub_key); |
166 | 0 | } |
167 | 0 | return false; |
168 | 0 | } |
169 | | SigningResult signMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) override |
170 | 0 | { |
171 | 0 | return m_wallet->SignMessage(message, pkhash, str_sig); |
172 | 0 | } |
173 | | bool isSpendable(const CTxDestination& dest) override |
174 | 0 | { |
175 | 0 | LOCK(m_wallet->cs_wallet); |
176 | 0 | return m_wallet->IsMine(dest) & ISMINE_SPENDABLE; |
177 | 0 | } |
178 | | bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::optional<AddressPurpose>& purpose) override |
179 | 0 | { |
180 | 0 | return m_wallet->SetAddressBook(dest, name, purpose); |
181 | 0 | } |
182 | | bool delAddressBook(const CTxDestination& dest) override |
183 | 0 | { |
184 | 0 | return m_wallet->DelAddressBook(dest); |
185 | 0 | } |
186 | | bool getAddress(const CTxDestination& dest, |
187 | | std::string* name, |
188 | | isminetype* is_mine, |
189 | | AddressPurpose* purpose) override |
190 | 0 | { |
191 | 0 | LOCK(m_wallet->cs_wallet); |
192 | 0 | const auto& entry = m_wallet->FindAddressBookEntry(dest, /*allow_change=*/false); |
193 | 0 | if (!entry) return false; // addr not found Branch (193:13): [True: 0, False: 0]
|
194 | 0 | if (name) { Branch (194:13): [True: 0, False: 0]
|
195 | 0 | *name = entry->GetLabel(); |
196 | 0 | } |
197 | 0 | std::optional<isminetype> dest_is_mine; |
198 | 0 | if (is_mine || purpose) { Branch (198:13): [True: 0, False: 0]
Branch (198:24): [True: 0, False: 0]
|
199 | 0 | dest_is_mine = m_wallet->IsMine(dest); |
200 | 0 | } |
201 | 0 | if (is_mine) { Branch (201:13): [True: 0, False: 0]
|
202 | 0 | *is_mine = *dest_is_mine; |
203 | 0 | } |
204 | 0 | if (purpose) { Branch (204:13): [True: 0, False: 0]
|
205 | | // In very old wallets, address purpose may not be recorded so we derive it from IsMine |
206 | 0 | *purpose = entry->purpose.value_or(*dest_is_mine ? AddressPurpose::RECEIVE : AddressPurpose::SEND); Branch (206:48): [True: 0, False: 0]
|
207 | 0 | } |
208 | 0 | return true; |
209 | 0 | } |
210 | | std::vector<WalletAddress> getAddresses() override |
211 | 0 | { |
212 | 0 | LOCK(m_wallet->cs_wallet); |
213 | 0 | std::vector<WalletAddress> result; |
214 | 0 | m_wallet->ForEachAddrBookEntry([&](const CTxDestination& dest, const std::string& label, bool is_change, const std::optional<AddressPurpose>& purpose) EXCLUSIVE_LOCKS_REQUIRED(m_wallet->cs_wallet) { |
215 | 0 | if (is_change) return; Branch (215:17): [True: 0, False: 0]
|
216 | 0 | isminetype is_mine = m_wallet->IsMine(dest); |
217 | | // In very old wallets, address purpose may not be recorded so we derive it from IsMine |
218 | 0 | result.emplace_back(dest, is_mine, purpose.value_or(is_mine ? AddressPurpose::RECEIVE : AddressPurpose::SEND), label); Branch (218:65): [True: 0, False: 0]
|
219 | 0 | }); |
220 | 0 | return result; |
221 | 0 | } |
222 | 0 | std::vector<std::string> getAddressReceiveRequests() override { |
223 | 0 | LOCK(m_wallet->cs_wallet); |
224 | 0 | return m_wallet->GetAddressReceiveRequests(); |
225 | 0 | } |
226 | 0 | bool setAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& value) override { |
227 | | // Note: The setAddressReceiveRequest interface used by the GUI to store |
228 | | // receive requests is a little awkward and could be improved in the |
229 | | // future: |
230 | | // |
231 | | // - The same method is used to save requests and erase them, but |
232 | | // having separate methods could be clearer and prevent bugs. |
233 | | // |
234 | | // - Request ids are passed as strings even though they are generated as |
235 | | // integers. |
236 | | // |
237 | | // - Multiple requests can be stored for the same address, but it might |
238 | | // be better to only allow one request or only keep the current one. |
239 | 0 | LOCK(m_wallet->cs_wallet); |
240 | 0 | WalletBatch batch{m_wallet->GetDatabase()}; |
241 | 0 | return value.empty() ? m_wallet->EraseAddressReceiveRequest(batch, dest, id) Branch (241:16): [True: 0, False: 0]
|
242 | 0 | : m_wallet->SetAddressReceiveRequest(batch, dest, id, value); |
243 | 0 | } |
244 | | util::Result<void> displayAddress(const CTxDestination& dest) override |
245 | 0 | { |
246 | 0 | LOCK(m_wallet->cs_wallet); |
247 | 0 | return m_wallet->DisplayAddress(dest); |
248 | 0 | } |
249 | | bool lockCoin(const COutPoint& output, const bool write_to_db) override |
250 | 0 | { |
251 | 0 | LOCK(m_wallet->cs_wallet); |
252 | 0 | std::unique_ptr<WalletBatch> batch = write_to_db ? std::make_unique<WalletBatch>(m_wallet->GetDatabase()) : nullptr; Branch (252:46): [True: 0, False: 0]
|
253 | 0 | return m_wallet->LockCoin(output, batch.get()); |
254 | 0 | } |
255 | | bool unlockCoin(const COutPoint& output) override |
256 | 0 | { |
257 | 0 | LOCK(m_wallet->cs_wallet); |
258 | 0 | std::unique_ptr<WalletBatch> batch = std::make_unique<WalletBatch>(m_wallet->GetDatabase()); |
259 | 0 | return m_wallet->UnlockCoin(output, batch.get()); |
260 | 0 | } |
261 | | bool isLockedCoin(const COutPoint& output) override |
262 | 0 | { |
263 | 0 | LOCK(m_wallet->cs_wallet); |
264 | 0 | return m_wallet->IsLockedCoin(output); |
265 | 0 | } |
266 | | void listLockedCoins(std::vector<COutPoint>& outputs) override |
267 | 0 | { |
268 | 0 | LOCK(m_wallet->cs_wallet); |
269 | 0 | return m_wallet->ListLockedCoins(outputs); |
270 | 0 | } |
271 | | util::Result<CTransactionRef> createTransaction(const std::vector<CRecipient>& recipients, |
272 | | const CCoinControl& coin_control, |
273 | | bool sign, |
274 | | int& change_pos, |
275 | | CAmount& fee) override |
276 | 0 | { |
277 | 0 | LOCK(m_wallet->cs_wallet); |
278 | 0 | auto res = CreateTransaction(*m_wallet, recipients, change_pos == -1 ? std::nullopt : std::make_optional(change_pos), Branch (278:61): [True: 0, False: 0]
|
279 | 0 | coin_control, sign); |
280 | 0 | if (!res) return util::Error{util::ErrorString(res)}; Branch (280:13): [True: 0, False: 0]
|
281 | 0 | const auto& txr = *res; |
282 | 0 | fee = txr.fee; |
283 | 0 | change_pos = txr.change_pos ? int(*txr.change_pos) : -1; Branch (283:22): [True: 0, False: 0]
|
284 | |
|
285 | 0 | return txr.tx; |
286 | 0 | } |
287 | | void commitTransaction(CTransactionRef tx, |
288 | | WalletValueMap value_map, |
289 | | WalletOrderForm order_form) override |
290 | 0 | { |
291 | 0 | LOCK(m_wallet->cs_wallet); |
292 | 0 | m_wallet->CommitTransaction(std::move(tx), std::move(value_map), std::move(order_form)); |
293 | 0 | } |
294 | 0 | bool transactionCanBeAbandoned(const Txid& txid) override { return m_wallet->TransactionCanBeAbandoned(txid); } |
295 | | bool abandonTransaction(const Txid& txid) override |
296 | 0 | { |
297 | 0 | LOCK(m_wallet->cs_wallet); |
298 | 0 | return m_wallet->AbandonTransaction(txid); |
299 | 0 | } |
300 | | bool transactionCanBeBumped(const Txid& txid) override |
301 | 0 | { |
302 | 0 | return feebumper::TransactionCanBeBumped(*m_wallet.get(), txid); |
303 | 0 | } |
304 | | bool createBumpTransaction(const Txid& txid, |
305 | | const CCoinControl& coin_control, |
306 | | std::vector<bilingual_str>& errors, |
307 | | CAmount& old_fee, |
308 | | CAmount& new_fee, |
309 | | CMutableTransaction& mtx) override |
310 | 0 | { |
311 | 0 | std::vector<CTxOut> outputs; // just an empty list of new recipients for now |
312 | 0 | return feebumper::CreateRateBumpTransaction(*m_wallet.get(), txid, coin_control, errors, old_fee, new_fee, mtx, /* require_mine= */ true, outputs) == feebumper::Result::OK; |
313 | 0 | } |
314 | 0 | bool signBumpTransaction(CMutableTransaction& mtx) override { return feebumper::SignTransaction(*m_wallet.get(), mtx); } |
315 | | bool commitBumpTransaction(const Txid& txid, |
316 | | CMutableTransaction&& mtx, |
317 | | std::vector<bilingual_str>& errors, |
318 | | Txid& bumped_txid) override |
319 | 0 | { |
320 | 0 | return feebumper::CommitTransaction(*m_wallet.get(), txid, std::move(mtx), errors, bumped_txid) == |
321 | 0 | feebumper::Result::OK; |
322 | 0 | } |
323 | | CTransactionRef getTx(const Txid& txid) override |
324 | 0 | { |
325 | 0 | LOCK(m_wallet->cs_wallet); |
326 | 0 | auto mi = m_wallet->mapWallet.find(txid); |
327 | 0 | if (mi != m_wallet->mapWallet.end()) { Branch (327:13): [True: 0, False: 0]
|
328 | 0 | return mi->second.tx; |
329 | 0 | } |
330 | 0 | return {}; |
331 | 0 | } |
332 | | WalletTx getWalletTx(const Txid& txid) override |
333 | 0 | { |
334 | 0 | LOCK(m_wallet->cs_wallet); |
335 | 0 | auto mi = m_wallet->mapWallet.find(txid); |
336 | 0 | if (mi != m_wallet->mapWallet.end()) { Branch (336:13): [True: 0, False: 0]
|
337 | 0 | return MakeWalletTx(*m_wallet, mi->second); |
338 | 0 | } |
339 | 0 | return {}; |
340 | 0 | } |
341 | | std::set<WalletTx> getWalletTxs() override |
342 | 0 | { |
343 | 0 | LOCK(m_wallet->cs_wallet); |
344 | 0 | std::set<WalletTx> result; |
345 | 0 | for (const auto& entry : m_wallet->mapWallet) { Branch (345:32): [True: 0, False: 0]
|
346 | 0 | result.emplace(MakeWalletTx(*m_wallet, entry.second)); |
347 | 0 | } |
348 | 0 | return result; |
349 | 0 | } |
350 | | bool tryGetTxStatus(const Txid& txid, |
351 | | interfaces::WalletTxStatus& tx_status, |
352 | | int& num_blocks, |
353 | | int64_t& block_time) override |
354 | 0 | { |
355 | 0 | TRY_LOCK(m_wallet->cs_wallet, locked_wallet); |
356 | 0 | if (!locked_wallet) { Branch (356:13): [True: 0, False: 0]
|
357 | 0 | return false; |
358 | 0 | } |
359 | 0 | auto mi = m_wallet->mapWallet.find(txid); |
360 | 0 | if (mi == m_wallet->mapWallet.end()) { Branch (360:13): [True: 0, False: 0]
|
361 | 0 | return false; |
362 | 0 | } |
363 | 0 | num_blocks = m_wallet->GetLastBlockHeight(); |
364 | 0 | block_time = -1; |
365 | 0 | CHECK_NONFATAL(m_wallet->chain().findBlock(m_wallet->GetLastBlockHash(), FoundBlock().time(block_time))); |
366 | 0 | tx_status = MakeWalletTxStatus(*m_wallet, mi->second); |
367 | 0 | return true; |
368 | 0 | } |
369 | | WalletTx getWalletTxDetails(const Txid& txid, |
370 | | WalletTxStatus& tx_status, |
371 | | WalletOrderForm& order_form, |
372 | | bool& in_mempool, |
373 | | int& num_blocks) override |
374 | 0 | { |
375 | 0 | LOCK(m_wallet->cs_wallet); |
376 | 0 | auto mi = m_wallet->mapWallet.find(txid); |
377 | 0 | if (mi != m_wallet->mapWallet.end()) { Branch (377:13): [True: 0, False: 0]
|
378 | 0 | num_blocks = m_wallet->GetLastBlockHeight(); |
379 | 0 | in_mempool = mi->second.InMempool(); |
380 | 0 | order_form = mi->second.vOrderForm; |
381 | 0 | tx_status = MakeWalletTxStatus(*m_wallet, mi->second); |
382 | 0 | return MakeWalletTx(*m_wallet, mi->second); |
383 | 0 | } |
384 | 0 | return {}; |
385 | 0 | } |
386 | | std::optional<PSBTError> fillPSBT(std::optional<int> sighash_type, |
387 | | bool sign, |
388 | | bool bip32derivs, |
389 | | size_t* n_signed, |
390 | | PartiallySignedTransaction& psbtx, |
391 | | bool& complete) override |
392 | 0 | { |
393 | 0 | return m_wallet->FillPSBT(psbtx, complete, sighash_type, sign, bip32derivs, n_signed); |
394 | 0 | } |
395 | | WalletBalances getBalances() override |
396 | 0 | { |
397 | 0 | const auto bal = GetBalance(*m_wallet); |
398 | 0 | WalletBalances result; |
399 | 0 | result.balance = bal.m_mine_trusted; |
400 | 0 | result.unconfirmed_balance = bal.m_mine_untrusted_pending; |
401 | 0 | result.immature_balance = bal.m_mine_immature; |
402 | 0 | return result; |
403 | 0 | } |
404 | | bool tryGetBalances(WalletBalances& balances, uint256& block_hash) override |
405 | 0 | { |
406 | 0 | TRY_LOCK(m_wallet->cs_wallet, locked_wallet); |
407 | 0 | if (!locked_wallet) { Branch (407:13): [True: 0, False: 0]
|
408 | 0 | return false; |
409 | 0 | } |
410 | 0 | block_hash = m_wallet->GetLastBlockHash(); |
411 | 0 | balances = getBalances(); |
412 | 0 | return true; |
413 | 0 | } |
414 | 0 | CAmount getBalance() override { return GetBalance(*m_wallet).m_mine_trusted; } |
415 | | CAmount getAvailableBalance(const CCoinControl& coin_control) override |
416 | 0 | { |
417 | 0 | LOCK(m_wallet->cs_wallet); |
418 | 0 | CAmount total_amount = 0; |
419 | | // Fetch selected coins total amount |
420 | 0 | if (coin_control.HasSelected()) { Branch (420:13): [True: 0, False: 0]
|
421 | 0 | FastRandomContext rng{}; |
422 | 0 | CoinSelectionParams params(rng); |
423 | | // Note: for now, swallow any error. |
424 | 0 | if (auto res = FetchSelectedInputs(*m_wallet, coin_control, params)) { Branch (424:22): [True: 0, False: 0]
|
425 | 0 | total_amount += res->total_amount; |
426 | 0 | } |
427 | 0 | } |
428 | | |
429 | | // And fetch the wallet available coins |
430 | 0 | if (coin_control.m_allow_other_inputs) { Branch (430:13): [True: 0, False: 0]
|
431 | 0 | total_amount += AvailableCoins(*m_wallet, &coin_control).GetTotalAmount(); |
432 | 0 | } |
433 | |
|
434 | 0 | return total_amount; |
435 | 0 | } |
436 | | isminetype txinIsMine(const CTxIn& txin) override |
437 | 0 | { |
438 | 0 | LOCK(m_wallet->cs_wallet); |
439 | 0 | return InputIsMine(*m_wallet, txin); |
440 | 0 | } |
441 | | isminetype txoutIsMine(const CTxOut& txout) override |
442 | 0 | { |
443 | 0 | LOCK(m_wallet->cs_wallet); |
444 | 0 | return m_wallet->IsMine(txout); |
445 | 0 | } |
446 | | CAmount getDebit(const CTxIn& txin, isminefilter filter) override |
447 | 0 | { |
448 | 0 | LOCK(m_wallet->cs_wallet); |
449 | 0 | return m_wallet->GetDebit(txin, filter); |
450 | 0 | } |
451 | | CAmount getCredit(const CTxOut& txout, isminefilter filter) override |
452 | 0 | { |
453 | 0 | LOCK(m_wallet->cs_wallet); |
454 | 0 | return OutputGetCredit(*m_wallet, txout, filter); |
455 | 0 | } |
456 | | CoinsList listCoins() override |
457 | 0 | { |
458 | 0 | LOCK(m_wallet->cs_wallet); |
459 | 0 | CoinsList result; |
460 | 0 | for (const auto& entry : ListCoins(*m_wallet)) { Branch (460:32): [True: 0, False: 0]
|
461 | 0 | auto& group = result[entry.first]; |
462 | 0 | for (const auto& coin : entry.second) { Branch (462:35): [True: 0, False: 0]
|
463 | 0 | group.emplace_back(coin.outpoint, |
464 | 0 | MakeWalletTxOut(*m_wallet, coin)); |
465 | 0 | } |
466 | 0 | } |
467 | 0 | return result; |
468 | 0 | } |
469 | | std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) override |
470 | 0 | { |
471 | 0 | LOCK(m_wallet->cs_wallet); |
472 | 0 | std::vector<WalletTxOut> result; |
473 | 0 | result.reserve(outputs.size()); |
474 | 0 | for (const auto& output : outputs) { Branch (474:33): [True: 0, False: 0]
|
475 | 0 | result.emplace_back(); |
476 | 0 | auto it = m_wallet->mapWallet.find(output.hash); |
477 | 0 | if (it != m_wallet->mapWallet.end()) { Branch (477:17): [True: 0, False: 0]
|
478 | 0 | int depth = m_wallet->GetTxDepthInMainChain(it->second); |
479 | 0 | if (depth >= 0) { Branch (479:21): [True: 0, False: 0]
|
480 | 0 | result.back() = MakeWalletTxOut(*m_wallet, it->second, output.n, depth); |
481 | 0 | } |
482 | 0 | } |
483 | 0 | } |
484 | 0 | return result; |
485 | 0 | } |
486 | 0 | CAmount getRequiredFee(unsigned int tx_bytes) override { return GetRequiredFee(*m_wallet, tx_bytes); } |
487 | | CAmount getMinimumFee(unsigned int tx_bytes, |
488 | | const CCoinControl& coin_control, |
489 | | int* returned_target, |
490 | | FeeReason* reason) override |
491 | 0 | { |
492 | 0 | FeeCalculation fee_calc; |
493 | 0 | CAmount result; |
494 | 0 | result = GetMinimumFee(*m_wallet, tx_bytes, coin_control, &fee_calc); |
495 | 0 | if (returned_target) *returned_target = fee_calc.returnedTarget; Branch (495:13): [True: 0, False: 0]
|
496 | 0 | if (reason) *reason = fee_calc.reason; Branch (496:13): [True: 0, False: 0]
|
497 | 0 | return result; |
498 | 0 | } |
499 | 0 | unsigned int getConfirmTarget() override { return m_wallet->m_confirm_target; } |
500 | 0 | bool hdEnabled() override { return m_wallet->IsHDEnabled(); } |
501 | 0 | bool canGetAddresses() override { return m_wallet->CanGetAddresses(); } |
502 | 0 | bool hasExternalSigner() override { return m_wallet->IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER); } |
503 | 0 | bool privateKeysDisabled() override { return m_wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS); } |
504 | 0 | bool taprootEnabled() override { |
505 | 0 | auto spk_man = m_wallet->GetScriptPubKeyMan(OutputType::BECH32M, /*internal=*/false); |
506 | 0 | return spk_man != nullptr; |
507 | 0 | } |
508 | 0 | OutputType getDefaultAddressType() override { return m_wallet->m_default_address_type; } |
509 | 0 | CAmount getDefaultMaxTxFee() override { return m_wallet->m_default_max_tx_fee; } |
510 | | void remove() override |
511 | 0 | { |
512 | 0 | RemoveWallet(m_context, m_wallet, /*load_on_start=*/false); |
513 | 0 | } |
514 | | std::unique_ptr<Handler> handleUnload(UnloadFn fn) override |
515 | 0 | { |
516 | 0 | return MakeSignalHandler(m_wallet->NotifyUnload.connect(fn)); |
517 | 0 | } |
518 | | std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override |
519 | 0 | { |
520 | 0 | return MakeSignalHandler(m_wallet->ShowProgress.connect(fn)); |
521 | 0 | } |
522 | | std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) override |
523 | 0 | { |
524 | 0 | return MakeSignalHandler(m_wallet->NotifyStatusChanged.connect([fn](CWallet*) { fn(); })); |
525 | 0 | } |
526 | | std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) override |
527 | 0 | { |
528 | 0 | return MakeSignalHandler(m_wallet->NotifyAddressBookChanged.connect( |
529 | 0 | [fn](const CTxDestination& address, const std::string& label, bool is_mine, |
530 | 0 | AddressPurpose purpose, ChangeType status) { fn(address, label, is_mine, purpose, status); })); |
531 | 0 | } |
532 | | std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) override |
533 | 0 | { |
534 | 0 | return MakeSignalHandler(m_wallet->NotifyTransactionChanged.connect( |
535 | 0 | [fn](const Txid& txid, ChangeType status) { fn(txid, status); })); |
536 | 0 | } |
537 | | std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) override |
538 | 0 | { |
539 | 0 | return MakeSignalHandler(m_wallet->NotifyCanGetAddressesChanged.connect(fn)); |
540 | 0 | } |
541 | 0 | CWallet* wallet() override { return m_wallet.get(); } |
542 | | |
543 | | WalletContext& m_context; |
544 | | std::shared_ptr<CWallet> m_wallet; |
545 | | }; |
546 | | |
547 | | class WalletLoaderImpl : public WalletLoader |
548 | | { |
549 | | public: |
550 | | WalletLoaderImpl(Chain& chain, ArgsManager& args) |
551 | 11.0k | { |
552 | 11.0k | m_context.chain = &chain; |
553 | 11.0k | m_context.args = &args; |
554 | 11.0k | } |
555 | 11.0k | ~WalletLoaderImpl() override { stop(); } |
556 | | |
557 | | //! ChainClient methods |
558 | | void registerRpcs() override |
559 | 11.0k | { |
560 | 654k | for (const CRPCCommand& command : GetWalletRPCCommands()) { Branch (560:41): [True: 654k, False: 11.0k]
|
561 | 654k | m_rpc_commands.emplace_back(command.category, command.name, [this, &command](const JSONRPCRequest& request, UniValue& result, bool last_handler) { |
562 | 11.0k | JSONRPCRequest wallet_request = request; |
563 | 11.0k | wallet_request.context = &m_context; |
564 | 11.0k | return command.actor(wallet_request, result, last_handler); |
565 | 11.0k | }, command.argNames, command.unique_id); |
566 | 654k | m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back())); |
567 | 654k | } |
568 | 11.0k | } |
569 | 11.0k | bool verify() override { return VerifyWallets(m_context); } |
570 | 11.0k | bool load() override { return LoadWallets(m_context); } |
571 | | void start(CScheduler& scheduler) override |
572 | 11.0k | { |
573 | 11.0k | m_context.scheduler = &scheduler; |
574 | 11.0k | return StartWallets(m_context); |
575 | 11.0k | } |
576 | 22.1k | void stop() override { return UnloadWallets(m_context); } |
577 | 2.25M | void setMockTime(int64_t time) override { return SetMockTime(time); } |
578 | 0 | void schedulerMockForward(std::chrono::seconds delta) override { Assert(m_context.scheduler)->MockForward(delta); } |
579 | | |
580 | | //! WalletLoader methods |
581 | | util::Result<std::unique_ptr<Wallet>> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, std::vector<bilingual_str>& warnings) override |
582 | 0 | { |
583 | 0 | DatabaseOptions options; |
584 | 0 | DatabaseStatus status; |
585 | 0 | ReadDatabaseArgs(*m_context.args, options); |
586 | 0 | options.require_create = true; |
587 | 0 | options.create_flags = wallet_creation_flags; |
588 | 0 | options.create_passphrase = passphrase; |
589 | 0 | bilingual_str error; |
590 | 0 | std::unique_ptr<Wallet> wallet{MakeWallet(m_context, CreateWallet(m_context, name, /*load_on_start=*/true, options, status, error, warnings))}; |
591 | 0 | if (wallet) { Branch (591:13): [True: 0, False: 0]
|
592 | 0 | return wallet; |
593 | 0 | } else { |
594 | 0 | return util::Error{error}; |
595 | 0 | } |
596 | 0 | } |
597 | | util::Result<std::unique_ptr<Wallet>> loadWallet(const std::string& name, std::vector<bilingual_str>& warnings) override |
598 | 0 | { |
599 | 0 | DatabaseOptions options; |
600 | 0 | DatabaseStatus status; |
601 | 0 | ReadDatabaseArgs(*m_context.args, options); |
602 | 0 | options.require_existing = true; |
603 | 0 | bilingual_str error; |
604 | 0 | std::unique_ptr<Wallet> wallet{MakeWallet(m_context, LoadWallet(m_context, name, /*load_on_start=*/true, options, status, error, warnings))}; |
605 | 0 | if (wallet) { Branch (605:13): [True: 0, False: 0]
|
606 | 0 | return wallet; |
607 | 0 | } else { |
608 | 0 | return util::Error{error}; |
609 | 0 | } |
610 | 0 | } |
611 | | util::Result<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) override |
612 | 0 | { |
613 | 0 | DatabaseStatus status; |
614 | 0 | bilingual_str error; |
615 | 0 | std::unique_ptr<Wallet> wallet{MakeWallet(m_context, RestoreWallet(m_context, backup_file, wallet_name, /*load_on_start=*/true, status, error, warnings))}; |
616 | 0 | if (wallet) { Branch (616:13): [True: 0, False: 0]
|
617 | 0 | return wallet; |
618 | 0 | } else { |
619 | 0 | return util::Error{error}; |
620 | 0 | } |
621 | 0 | } |
622 | | util::Result<WalletMigrationResult> migrateWallet(const std::string& name, const SecureString& passphrase) override |
623 | 0 | { |
624 | 0 | auto res = wallet::MigrateLegacyToDescriptor(name, passphrase, m_context); |
625 | 0 | if (!res) return util::Error{util::ErrorString(res)}; Branch (625:13): [True: 0, False: 0]
|
626 | 0 | WalletMigrationResult out{ |
627 | 0 | .wallet = MakeWallet(m_context, res->wallet), |
628 | 0 | .watchonly_wallet_name = res->watchonly_wallet ? std::make_optional(res->watchonly_wallet->GetName()) : std::nullopt, Branch (628:38): [True: 0, False: 0]
|
629 | 0 | .solvables_wallet_name = res->solvables_wallet ? std::make_optional(res->solvables_wallet->GetName()) : std::nullopt, Branch (629:38): [True: 0, False: 0]
|
630 | 0 | .backup_path = res->backup_path, |
631 | 0 | }; |
632 | 0 | return out; |
633 | 0 | } |
634 | | bool isEncrypted(const std::string& wallet_name) override |
635 | 0 | { |
636 | 0 | auto wallets{GetWallets(m_context)}; |
637 | 0 | auto it = std::find_if(wallets.begin(), wallets.end(), [&](std::shared_ptr<CWallet> w){ return w->GetName() == wallet_name; }); |
638 | 0 | if (it != wallets.end()) return (*it)->IsCrypted(); Branch (638:13): [True: 0, False: 0]
|
639 | | |
640 | | // Unloaded wallet, read db |
641 | 0 | DatabaseOptions options; |
642 | 0 | options.require_existing = true; |
643 | 0 | DatabaseStatus status; |
644 | 0 | bilingual_str error; |
645 | 0 | auto db = MakeWalletDatabase(wallet_name, options, status, error); |
646 | 0 | if (!db) return false; Branch (646:13): [True: 0, False: 0]
|
647 | 0 | return WalletBatch(*db).IsEncrypted(); |
648 | 0 | } |
649 | | std::string getWalletDir() override |
650 | 0 | { |
651 | 0 | return fs::PathToString(GetWalletDir()); |
652 | 0 | } |
653 | | std::vector<std::pair<std::string, std::string>> listWalletDir() override |
654 | 0 | { |
655 | 0 | std::vector<std::pair<std::string, std::string>> paths; |
656 | 0 | for (auto& [path, format] : ListDatabases(GetWalletDir())) { Branch (656:35): [True: 0, False: 0]
|
657 | 0 | paths.emplace_back(fs::PathToString(path), format); |
658 | 0 | } |
659 | 0 | return paths; |
660 | 0 | } |
661 | | std::vector<std::unique_ptr<Wallet>> getWallets() override |
662 | 0 | { |
663 | 0 | std::vector<std::unique_ptr<Wallet>> wallets; |
664 | 0 | for (const auto& wallet : GetWallets(m_context)) { Branch (664:33): [True: 0, False: 0]
|
665 | 0 | wallets.emplace_back(MakeWallet(m_context, wallet)); |
666 | 0 | } |
667 | 0 | return wallets; |
668 | 0 | } |
669 | | std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) override |
670 | 0 | { |
671 | 0 | return HandleLoadWallet(m_context, std::move(fn)); |
672 | 0 | } |
673 | 0 | WalletContext* context() override { return &m_context; } |
674 | | |
675 | | WalletContext m_context; |
676 | | const std::vector<std::string> m_wallet_filenames; |
677 | | std::vector<std::unique_ptr<Handler>> m_rpc_handlers; |
678 | | std::list<CRPCCommand> m_rpc_commands; |
679 | | }; |
680 | | } // namespace |
681 | | } // namespace wallet |
682 | | |
683 | | namespace interfaces { |
684 | 0 | std::unique_ptr<Wallet> MakeWallet(wallet::WalletContext& context, const std::shared_ptr<wallet::CWallet>& wallet) { return wallet ? std::make_unique<wallet::WalletImpl>(context, wallet) : nullptr; } Branch (684:125): [True: 0, False: 0]
|
685 | | |
686 | | std::unique_ptr<WalletLoader> MakeWalletLoader(Chain& chain, ArgsManager& args) |
687 | 11.0k | { |
688 | 11.0k | return std::make_unique<wallet::WalletLoaderImpl>(chain, args); |
689 | 11.0k | } |
690 | | } // namespace interfaces |