/root/bitcoin/src/wallet/interfaces.cpp
Line | Count | Source (jump to first uncovered line) |
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) { |
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) { |
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()) ? |
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 : |
97 | 0 | wtx.state<TxStateBlockConflicted>() ? wtx.state<TxStateBlockConflicted>()->conflicting_block_height : |
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) { |
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 haveWatchOnly() override |
179 | 0 | { |
180 | 0 | auto spk_man = m_wallet->GetLegacyScriptPubKeyMan(); |
181 | 0 | if (spk_man) { |
182 | 0 | return spk_man->HaveWatchOnly(); |
183 | 0 | } |
184 | 0 | return false; |
185 | 0 | }; |
186 | | bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::optional<AddressPurpose>& purpose) override |
187 | 0 | { |
188 | 0 | return m_wallet->SetAddressBook(dest, name, purpose); |
189 | 0 | } |
190 | | bool delAddressBook(const CTxDestination& dest) override |
191 | 0 | { |
192 | 0 | return m_wallet->DelAddressBook(dest); |
193 | 0 | } |
194 | | bool getAddress(const CTxDestination& dest, |
195 | | std::string* name, |
196 | | isminetype* is_mine, |
197 | | AddressPurpose* purpose) override |
198 | 0 | { |
199 | 0 | LOCK(m_wallet->cs_wallet); |
200 | 0 | const auto& entry = m_wallet->FindAddressBookEntry(dest, /*allow_change=*/false); |
201 | 0 | if (!entry) return false; // addr not found |
202 | 0 | if (name) { |
203 | 0 | *name = entry->GetLabel(); |
204 | 0 | } |
205 | 0 | std::optional<isminetype> dest_is_mine; |
206 | 0 | if (is_mine || purpose) { |
207 | 0 | dest_is_mine = m_wallet->IsMine(dest); |
208 | 0 | } |
209 | 0 | if (is_mine) { |
210 | 0 | *is_mine = *dest_is_mine; |
211 | 0 | } |
212 | 0 | if (purpose) { |
213 | | // In very old wallets, address purpose may not be recorded so we derive it from IsMine |
214 | 0 | *purpose = entry->purpose.value_or(*dest_is_mine ? AddressPurpose::RECEIVE : AddressPurpose::SEND); |
215 | 0 | } |
216 | 0 | return true; |
217 | 0 | } |
218 | | std::vector<WalletAddress> getAddresses() override |
219 | 0 | { |
220 | 0 | LOCK(m_wallet->cs_wallet); |
221 | 0 | std::vector<WalletAddress> result; |
222 | 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) { |
223 | 0 | if (is_change) return; |
224 | 0 | isminetype is_mine = m_wallet->IsMine(dest); |
225 | | // In very old wallets, address purpose may not be recorded so we derive it from IsMine |
226 | 0 | result.emplace_back(dest, is_mine, purpose.value_or(is_mine ? AddressPurpose::RECEIVE : AddressPurpose::SEND), label); |
227 | 0 | }); |
228 | 0 | return result; |
229 | 0 | } |
230 | 0 | std::vector<std::string> getAddressReceiveRequests() override { |
231 | 0 | LOCK(m_wallet->cs_wallet); |
232 | 0 | return m_wallet->GetAddressReceiveRequests(); |
233 | 0 | } |
234 | 0 | bool setAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& value) override { |
235 | | // Note: The setAddressReceiveRequest interface used by the GUI to store |
236 | | // receive requests is a little awkward and could be improved in the |
237 | | // future: |
238 | | // |
239 | | // - The same method is used to save requests and erase them, but |
240 | | // having separate methods could be clearer and prevent bugs. |
241 | | // |
242 | | // - Request ids are passed as strings even though they are generated as |
243 | | // integers. |
244 | | // |
245 | | // - Multiple requests can be stored for the same address, but it might |
246 | | // be better to only allow one request or only keep the current one. |
247 | 0 | LOCK(m_wallet->cs_wallet); |
248 | 0 | WalletBatch batch{m_wallet->GetDatabase()}; |
249 | 0 | return value.empty() ? m_wallet->EraseAddressReceiveRequest(batch, dest, id) |
250 | 0 | : m_wallet->SetAddressReceiveRequest(batch, dest, id, value); |
251 | 0 | } |
252 | | util::Result<void> displayAddress(const CTxDestination& dest) override |
253 | 0 | { |
254 | 0 | LOCK(m_wallet->cs_wallet); |
255 | 0 | return m_wallet->DisplayAddress(dest); |
256 | 0 | } |
257 | | bool lockCoin(const COutPoint& output, const bool write_to_db) override |
258 | 0 | { |
259 | 0 | LOCK(m_wallet->cs_wallet); |
260 | 0 | std::unique_ptr<WalletBatch> batch = write_to_db ? std::make_unique<WalletBatch>(m_wallet->GetDatabase()) : nullptr; |
261 | 0 | return m_wallet->LockCoin(output, batch.get()); |
262 | 0 | } |
263 | | bool unlockCoin(const COutPoint& output) override |
264 | 0 | { |
265 | 0 | LOCK(m_wallet->cs_wallet); |
266 | 0 | std::unique_ptr<WalletBatch> batch = std::make_unique<WalletBatch>(m_wallet->GetDatabase()); |
267 | 0 | return m_wallet->UnlockCoin(output, batch.get()); |
268 | 0 | } |
269 | | bool isLockedCoin(const COutPoint& output) override |
270 | 0 | { |
271 | 0 | LOCK(m_wallet->cs_wallet); |
272 | 0 | return m_wallet->IsLockedCoin(output); |
273 | 0 | } |
274 | | void listLockedCoins(std::vector<COutPoint>& outputs) override |
275 | 0 | { |
276 | 0 | LOCK(m_wallet->cs_wallet); |
277 | 0 | return m_wallet->ListLockedCoins(outputs); |
278 | 0 | } |
279 | | util::Result<CTransactionRef> createTransaction(const std::vector<CRecipient>& recipients, |
280 | | const CCoinControl& coin_control, |
281 | | bool sign, |
282 | | int& change_pos, |
283 | | CAmount& fee) override |
284 | 0 | { |
285 | 0 | LOCK(m_wallet->cs_wallet); |
286 | 0 | auto res = CreateTransaction(*m_wallet, recipients, change_pos == -1 ? std::nullopt : std::make_optional(change_pos), |
287 | 0 | coin_control, sign); |
288 | 0 | if (!res) return util::Error{util::ErrorString(res)}; |
289 | 0 | const auto& txr = *res; |
290 | 0 | fee = txr.fee; |
291 | 0 | change_pos = txr.change_pos ? int(*txr.change_pos) : -1; |
292 | |
|
293 | 0 | return txr.tx; |
294 | 0 | } |
295 | | void commitTransaction(CTransactionRef tx, |
296 | | WalletValueMap value_map, |
297 | | WalletOrderForm order_form) override |
298 | 0 | { |
299 | 0 | LOCK(m_wallet->cs_wallet); |
300 | 0 | m_wallet->CommitTransaction(std::move(tx), std::move(value_map), std::move(order_form)); |
301 | 0 | } |
302 | 0 | bool transactionCanBeAbandoned(const uint256& txid) override { return m_wallet->TransactionCanBeAbandoned(txid); } |
303 | | bool abandonTransaction(const uint256& txid) override |
304 | 0 | { |
305 | 0 | LOCK(m_wallet->cs_wallet); |
306 | 0 | return m_wallet->AbandonTransaction(txid); |
307 | 0 | } |
308 | | bool transactionCanBeBumped(const uint256& txid) override |
309 | 0 | { |
310 | 0 | return feebumper::TransactionCanBeBumped(*m_wallet.get(), txid); |
311 | 0 | } |
312 | | bool createBumpTransaction(const uint256& txid, |
313 | | const CCoinControl& coin_control, |
314 | | std::vector<bilingual_str>& errors, |
315 | | CAmount& old_fee, |
316 | | CAmount& new_fee, |
317 | | CMutableTransaction& mtx) override |
318 | 0 | { |
319 | 0 | std::vector<CTxOut> outputs; // just an empty list of new recipients for now |
320 | 0 | return feebumper::CreateRateBumpTransaction(*m_wallet.get(), txid, coin_control, errors, old_fee, new_fee, mtx, /* require_mine= */ true, outputs) == feebumper::Result::OK; |
321 | 0 | } |
322 | 0 | bool signBumpTransaction(CMutableTransaction& mtx) override { return feebumper::SignTransaction(*m_wallet.get(), mtx); } |
323 | | bool commitBumpTransaction(const uint256& txid, |
324 | | CMutableTransaction&& mtx, |
325 | | std::vector<bilingual_str>& errors, |
326 | | uint256& bumped_txid) override |
327 | 0 | { |
328 | 0 | return feebumper::CommitTransaction(*m_wallet.get(), txid, std::move(mtx), errors, bumped_txid) == |
329 | 0 | feebumper::Result::OK; |
330 | 0 | } |
331 | | CTransactionRef getTx(const uint256& txid) override |
332 | 0 | { |
333 | 0 | LOCK(m_wallet->cs_wallet); |
334 | 0 | auto mi = m_wallet->mapWallet.find(txid); |
335 | 0 | if (mi != m_wallet->mapWallet.end()) { |
336 | 0 | return mi->second.tx; |
337 | 0 | } |
338 | 0 | return {}; |
339 | 0 | } |
340 | | WalletTx getWalletTx(const uint256& txid) override |
341 | 0 | { |
342 | 0 | LOCK(m_wallet->cs_wallet); |
343 | 0 | auto mi = m_wallet->mapWallet.find(txid); |
344 | 0 | if (mi != m_wallet->mapWallet.end()) { |
345 | 0 | return MakeWalletTx(*m_wallet, mi->second); |
346 | 0 | } |
347 | 0 | return {}; |
348 | 0 | } |
349 | | std::set<WalletTx> getWalletTxs() override |
350 | 0 | { |
351 | 0 | LOCK(m_wallet->cs_wallet); |
352 | 0 | std::set<WalletTx> result; |
353 | 0 | for (const auto& entry : m_wallet->mapWallet) { |
354 | 0 | result.emplace(MakeWalletTx(*m_wallet, entry.second)); |
355 | 0 | } |
356 | 0 | return result; |
357 | 0 | } |
358 | | bool tryGetTxStatus(const uint256& txid, |
359 | | interfaces::WalletTxStatus& tx_status, |
360 | | int& num_blocks, |
361 | | int64_t& block_time) override |
362 | 0 | { |
363 | 0 | TRY_LOCK(m_wallet->cs_wallet, locked_wallet); |
364 | 0 | if (!locked_wallet) { |
365 | 0 | return false; |
366 | 0 | } |
367 | 0 | auto mi = m_wallet->mapWallet.find(txid); |
368 | 0 | if (mi == m_wallet->mapWallet.end()) { |
369 | 0 | return false; |
370 | 0 | } |
371 | 0 | num_blocks = m_wallet->GetLastBlockHeight(); |
372 | 0 | block_time = -1; |
373 | 0 | CHECK_NONFATAL(m_wallet->chain().findBlock(m_wallet->GetLastBlockHash(), FoundBlock().time(block_time))); |
374 | 0 | tx_status = MakeWalletTxStatus(*m_wallet, mi->second); |
375 | 0 | return true; |
376 | 0 | } |
377 | | WalletTx getWalletTxDetails(const uint256& txid, |
378 | | WalletTxStatus& tx_status, |
379 | | WalletOrderForm& order_form, |
380 | | bool& in_mempool, |
381 | | int& num_blocks) override |
382 | 0 | { |
383 | 0 | LOCK(m_wallet->cs_wallet); |
384 | 0 | auto mi = m_wallet->mapWallet.find(txid); |
385 | 0 | if (mi != m_wallet->mapWallet.end()) { |
386 | 0 | num_blocks = m_wallet->GetLastBlockHeight(); |
387 | 0 | in_mempool = mi->second.InMempool(); |
388 | 0 | order_form = mi->second.vOrderForm; |
389 | 0 | tx_status = MakeWalletTxStatus(*m_wallet, mi->second); |
390 | 0 | return MakeWalletTx(*m_wallet, mi->second); |
391 | 0 | } |
392 | 0 | return {}; |
393 | 0 | } |
394 | | std::optional<PSBTError> fillPSBT(int sighash_type, |
395 | | bool sign, |
396 | | bool bip32derivs, |
397 | | size_t* n_signed, |
398 | | PartiallySignedTransaction& psbtx, |
399 | | bool& complete) override |
400 | 0 | { |
401 | 0 | return m_wallet->FillPSBT(psbtx, complete, sighash_type, sign, bip32derivs, n_signed); |
402 | 0 | } |
403 | | WalletBalances getBalances() override |
404 | 0 | { |
405 | 0 | const auto bal = GetBalance(*m_wallet); |
406 | 0 | WalletBalances result; |
407 | 0 | result.balance = bal.m_mine_trusted; |
408 | 0 | result.unconfirmed_balance = bal.m_mine_untrusted_pending; |
409 | 0 | result.immature_balance = bal.m_mine_immature; |
410 | 0 | result.have_watch_only = haveWatchOnly(); |
411 | 0 | if (result.have_watch_only) { |
412 | 0 | result.watch_only_balance = bal.m_watchonly_trusted; |
413 | 0 | result.unconfirmed_watch_only_balance = bal.m_watchonly_untrusted_pending; |
414 | 0 | result.immature_watch_only_balance = bal.m_watchonly_immature; |
415 | 0 | } |
416 | 0 | return result; |
417 | 0 | } |
418 | | bool tryGetBalances(WalletBalances& balances, uint256& block_hash) override |
419 | 0 | { |
420 | 0 | TRY_LOCK(m_wallet->cs_wallet, locked_wallet); |
421 | 0 | if (!locked_wallet) { |
422 | 0 | return false; |
423 | 0 | } |
424 | 0 | block_hash = m_wallet->GetLastBlockHash(); |
425 | 0 | balances = getBalances(); |
426 | 0 | return true; |
427 | 0 | } |
428 | 0 | CAmount getBalance() override { return GetBalance(*m_wallet).m_mine_trusted; } |
429 | | CAmount getAvailableBalance(const CCoinControl& coin_control) override |
430 | 0 | { |
431 | 0 | LOCK(m_wallet->cs_wallet); |
432 | 0 | CAmount total_amount = 0; |
433 | | // Fetch selected coins total amount |
434 | 0 | if (coin_control.HasSelected()) { |
435 | 0 | FastRandomContext rng{}; |
436 | 0 | CoinSelectionParams params(rng); |
437 | | // Note: for now, swallow any error. |
438 | 0 | if (auto res = FetchSelectedInputs(*m_wallet, coin_control, params)) { |
439 | 0 | total_amount += res->total_amount; |
440 | 0 | } |
441 | 0 | } |
442 | | |
443 | | // And fetch the wallet available coins |
444 | 0 | if (coin_control.m_allow_other_inputs) { |
445 | 0 | total_amount += AvailableCoins(*m_wallet, &coin_control).GetTotalAmount(); |
446 | 0 | } |
447 | |
|
448 | 0 | return total_amount; |
449 | 0 | } |
450 | | isminetype txinIsMine(const CTxIn& txin) override |
451 | 0 | { |
452 | 0 | LOCK(m_wallet->cs_wallet); |
453 | 0 | return InputIsMine(*m_wallet, txin); |
454 | 0 | } |
455 | | isminetype txoutIsMine(const CTxOut& txout) override |
456 | 0 | { |
457 | 0 | LOCK(m_wallet->cs_wallet); |
458 | 0 | return m_wallet->IsMine(txout); |
459 | 0 | } |
460 | | CAmount getDebit(const CTxIn& txin, isminefilter filter) override |
461 | 0 | { |
462 | 0 | LOCK(m_wallet->cs_wallet); |
463 | 0 | return m_wallet->GetDebit(txin, filter); |
464 | 0 | } |
465 | | CAmount getCredit(const CTxOut& txout, isminefilter filter) override |
466 | 0 | { |
467 | 0 | LOCK(m_wallet->cs_wallet); |
468 | 0 | return OutputGetCredit(*m_wallet, txout, filter); |
469 | 0 | } |
470 | | CoinsList listCoins() override |
471 | 0 | { |
472 | 0 | LOCK(m_wallet->cs_wallet); |
473 | 0 | CoinsList result; |
474 | 0 | for (const auto& entry : ListCoins(*m_wallet)) { |
475 | 0 | auto& group = result[entry.first]; |
476 | 0 | for (const auto& coin : entry.second) { |
477 | 0 | group.emplace_back(coin.outpoint, |
478 | 0 | MakeWalletTxOut(*m_wallet, coin)); |
479 | 0 | } |
480 | 0 | } |
481 | 0 | return result; |
482 | 0 | } |
483 | | std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) override |
484 | 0 | { |
485 | 0 | LOCK(m_wallet->cs_wallet); |
486 | 0 | std::vector<WalletTxOut> result; |
487 | 0 | result.reserve(outputs.size()); |
488 | 0 | for (const auto& output : outputs) { |
489 | 0 | result.emplace_back(); |
490 | 0 | auto it = m_wallet->mapWallet.find(output.hash); |
491 | 0 | if (it != m_wallet->mapWallet.end()) { |
492 | 0 | int depth = m_wallet->GetTxDepthInMainChain(it->second); |
493 | 0 | if (depth >= 0) { |
494 | 0 | result.back() = MakeWalletTxOut(*m_wallet, it->second, output.n, depth); |
495 | 0 | } |
496 | 0 | } |
497 | 0 | } |
498 | 0 | return result; |
499 | 0 | } |
500 | 0 | CAmount getRequiredFee(unsigned int tx_bytes) override { return GetRequiredFee(*m_wallet, tx_bytes); } |
501 | | CAmount getMinimumFee(unsigned int tx_bytes, |
502 | | const CCoinControl& coin_control, |
503 | | int* returned_target, |
504 | | FeeReason* reason) override |
505 | 0 | { |
506 | 0 | FeeCalculation fee_calc; |
507 | 0 | CAmount result; |
508 | 0 | result = GetMinimumFee(*m_wallet, tx_bytes, coin_control, &fee_calc); |
509 | 0 | if (returned_target) *returned_target = fee_calc.returnedTarget; |
510 | 0 | if (reason) *reason = fee_calc.reason; |
511 | 0 | return result; |
512 | 0 | } |
513 | 0 | unsigned int getConfirmTarget() override { return m_wallet->m_confirm_target; } |
514 | 0 | bool hdEnabled() override { return m_wallet->IsHDEnabled(); } |
515 | 0 | bool canGetAddresses() override { return m_wallet->CanGetAddresses(); } |
516 | 0 | bool hasExternalSigner() override { return m_wallet->IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER); } |
517 | 0 | bool privateKeysDisabled() override { return m_wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS); } |
518 | 0 | bool taprootEnabled() override { |
519 | 0 | if (m_wallet->IsLegacy()) return false; |
520 | 0 | auto spk_man = m_wallet->GetScriptPubKeyMan(OutputType::BECH32M, /*internal=*/false); |
521 | 0 | return spk_man != nullptr; |
522 | 0 | } |
523 | 0 | OutputType getDefaultAddressType() override { return m_wallet->m_default_address_type; } |
524 | 0 | CAmount getDefaultMaxTxFee() override { return m_wallet->m_default_max_tx_fee; } |
525 | | void remove() override |
526 | 0 | { |
527 | 0 | RemoveWallet(m_context, m_wallet, /*load_on_start=*/false); |
528 | 0 | } |
529 | 0 | bool isLegacy() override { return m_wallet->IsLegacy(); } |
530 | | std::unique_ptr<Handler> handleUnload(UnloadFn fn) override |
531 | 0 | { |
532 | 0 | return MakeSignalHandler(m_wallet->NotifyUnload.connect(fn)); |
533 | 0 | } |
534 | | std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override |
535 | 0 | { |
536 | 0 | return MakeSignalHandler(m_wallet->ShowProgress.connect(fn)); |
537 | 0 | } |
538 | | std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) override |
539 | 0 | { |
540 | 0 | return MakeSignalHandler(m_wallet->NotifyStatusChanged.connect([fn](CWallet*) { fn(); })); |
541 | 0 | } |
542 | | std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) override |
543 | 0 | { |
544 | 0 | return MakeSignalHandler(m_wallet->NotifyAddressBookChanged.connect( |
545 | 0 | [fn](const CTxDestination& address, const std::string& label, bool is_mine, |
546 | 0 | AddressPurpose purpose, ChangeType status) { fn(address, label, is_mine, purpose, status); })); |
547 | 0 | } |
548 | | std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) override |
549 | 0 | { |
550 | 0 | return MakeSignalHandler(m_wallet->NotifyTransactionChanged.connect( |
551 | 0 | [fn](const uint256& txid, ChangeType status) { fn(txid, status); })); |
552 | 0 | } |
553 | | std::unique_ptr<Handler> handleWatchOnlyChanged(WatchOnlyChangedFn fn) override |
554 | 0 | { |
555 | 0 | return MakeSignalHandler(m_wallet->NotifyWatchonlyChanged.connect(fn)); |
556 | 0 | } |
557 | | std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) override |
558 | 0 | { |
559 | 0 | return MakeSignalHandler(m_wallet->NotifyCanGetAddressesChanged.connect(fn)); |
560 | 0 | } |
561 | 0 | CWallet* wallet() override { return m_wallet.get(); } |
562 | | |
563 | | WalletContext& m_context; |
564 | | std::shared_ptr<CWallet> m_wallet; |
565 | | }; |
566 | | |
567 | | class WalletLoaderImpl : public WalletLoader |
568 | | { |
569 | | public: |
570 | | WalletLoaderImpl(Chain& chain, ArgsManager& args) |
571 | 0 | { |
572 | 0 | m_context.chain = &chain; |
573 | 0 | m_context.args = &args; |
574 | 0 | } |
575 | 0 | ~WalletLoaderImpl() override { UnloadWallets(m_context); } |
576 | | |
577 | | //! ChainClient methods |
578 | | void registerRpcs() override |
579 | 0 | { |
580 | 0 | for (const CRPCCommand& command : GetWalletRPCCommands()) { |
581 | 0 | m_rpc_commands.emplace_back(command.category, command.name, [this, &command](const JSONRPCRequest& request, UniValue& result, bool last_handler) { |
582 | 0 | JSONRPCRequest wallet_request = request; |
583 | 0 | wallet_request.context = &m_context; |
584 | 0 | return command.actor(wallet_request, result, last_handler); |
585 | 0 | }, command.argNames, command.unique_id); |
586 | 0 | m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back())); |
587 | 0 | } |
588 | 0 | } |
589 | 0 | bool verify() override { return VerifyWallets(m_context); } |
590 | 0 | bool load() override { return LoadWallets(m_context); } |
591 | | void start(CScheduler& scheduler) override |
592 | 0 | { |
593 | 0 | m_context.scheduler = &scheduler; |
594 | 0 | return StartWallets(m_context); |
595 | 0 | } |
596 | 0 | void flush() override { return FlushWallets(m_context); } |
597 | 0 | void stop() override { return StopWallets(m_context); } |
598 | 0 | void setMockTime(int64_t time) override { return SetMockTime(time); } |
599 | 0 | void schedulerMockForward(std::chrono::seconds delta) override { Assert(m_context.scheduler)->MockForward(delta); } |
600 | | |
601 | | //! WalletLoader methods |
602 | | 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 |
603 | 0 | { |
604 | 0 | DatabaseOptions options; |
605 | 0 | DatabaseStatus status; |
606 | 0 | ReadDatabaseArgs(*m_context.args, options); |
607 | 0 | options.require_create = true; |
608 | 0 | options.create_flags = wallet_creation_flags; |
609 | 0 | options.create_passphrase = passphrase; |
610 | 0 | bilingual_str error; |
611 | 0 | std::unique_ptr<Wallet> wallet{MakeWallet(m_context, CreateWallet(m_context, name, /*load_on_start=*/true, options, status, error, warnings))}; |
612 | 0 | if (wallet) { |
613 | 0 | return wallet; |
614 | 0 | } else { |
615 | 0 | return util::Error{error}; |
616 | 0 | } |
617 | 0 | } |
618 | | util::Result<std::unique_ptr<Wallet>> loadWallet(const std::string& name, std::vector<bilingual_str>& warnings) override |
619 | 0 | { |
620 | 0 | DatabaseOptions options; |
621 | 0 | DatabaseStatus status; |
622 | 0 | ReadDatabaseArgs(*m_context.args, options); |
623 | 0 | options.require_existing = true; |
624 | 0 | bilingual_str error; |
625 | 0 | std::unique_ptr<Wallet> wallet{MakeWallet(m_context, LoadWallet(m_context, name, /*load_on_start=*/true, options, status, error, warnings))}; |
626 | 0 | if (wallet) { |
627 | 0 | return wallet; |
628 | 0 | } else { |
629 | 0 | return util::Error{error}; |
630 | 0 | } |
631 | 0 | } |
632 | | util::Result<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) override |
633 | 0 | { |
634 | 0 | DatabaseStatus status; |
635 | 0 | bilingual_str error; |
636 | 0 | std::unique_ptr<Wallet> wallet{MakeWallet(m_context, RestoreWallet(m_context, backup_file, wallet_name, /*load_on_start=*/true, status, error, warnings))}; |
637 | 0 | if (wallet) { |
638 | 0 | return wallet; |
639 | 0 | } else { |
640 | 0 | return util::Error{error}; |
641 | 0 | } |
642 | 0 | } |
643 | | util::Result<WalletMigrationResult> migrateWallet(const std::string& name, const SecureString& passphrase) override |
644 | 0 | { |
645 | 0 | auto res = wallet::MigrateLegacyToDescriptor(name, passphrase, m_context); |
646 | 0 | if (!res) return util::Error{util::ErrorString(res)}; |
647 | 0 | WalletMigrationResult out{ |
648 | 0 | .wallet = MakeWallet(m_context, res->wallet), |
649 | 0 | .watchonly_wallet_name = res->watchonly_wallet ? std::make_optional(res->watchonly_wallet->GetName()) : std::nullopt, |
650 | 0 | .solvables_wallet_name = res->solvables_wallet ? std::make_optional(res->solvables_wallet->GetName()) : std::nullopt, |
651 | 0 | .backup_path = res->backup_path, |
652 | 0 | }; |
653 | 0 | return out; |
654 | 0 | } |
655 | | bool isEncrypted(const std::string& wallet_name) override |
656 | 0 | { |
657 | 0 | auto wallets{GetWallets(m_context)}; |
658 | 0 | auto it = std::find_if(wallets.begin(), wallets.end(), [&](std::shared_ptr<CWallet> w){ return w->GetName() == wallet_name; }); |
659 | 0 | if (it != wallets.end()) return (*it)->IsCrypted(); |
660 | | |
661 | | // Unloaded wallet, read db |
662 | 0 | DatabaseOptions options; |
663 | 0 | options.require_existing = true; |
664 | 0 | DatabaseStatus status; |
665 | 0 | bilingual_str error; |
666 | 0 | auto db = MakeWalletDatabase(wallet_name, options, status, error); |
667 | 0 | if (!db) return false; |
668 | 0 | return WalletBatch(*db).IsEncrypted(); |
669 | 0 | } |
670 | | std::string getWalletDir() override |
671 | 0 | { |
672 | 0 | return fs::PathToString(GetWalletDir()); |
673 | 0 | } |
674 | | std::vector<std::pair<std::string, std::string>> listWalletDir() override |
675 | 0 | { |
676 | 0 | std::vector<std::pair<std::string, std::string>> paths; |
677 | 0 | for (auto& [path, format] : ListDatabases(GetWalletDir())) { |
678 | 0 | paths.emplace_back(fs::PathToString(path), format); |
679 | 0 | } |
680 | 0 | return paths; |
681 | 0 | } |
682 | | std::vector<std::unique_ptr<Wallet>> getWallets() override |
683 | 0 | { |
684 | 0 | std::vector<std::unique_ptr<Wallet>> wallets; |
685 | 0 | for (const auto& wallet : GetWallets(m_context)) { |
686 | 0 | wallets.emplace_back(MakeWallet(m_context, wallet)); |
687 | 0 | } |
688 | 0 | return wallets; |
689 | 0 | } |
690 | | std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) override |
691 | 0 | { |
692 | 0 | return HandleLoadWallet(m_context, std::move(fn)); |
693 | 0 | } |
694 | 0 | WalletContext* context() override { return &m_context; } |
695 | | |
696 | | WalletContext m_context; |
697 | | const std::vector<std::string> m_wallet_filenames; |
698 | | std::vector<std::unique_ptr<Handler>> m_rpc_handlers; |
699 | | std::list<CRPCCommand> m_rpc_commands; |
700 | | }; |
701 | | } // namespace |
702 | | } // namespace wallet |
703 | | |
704 | | namespace interfaces { |
705 | 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; } |
706 | | |
707 | | std::unique_ptr<WalletLoader> MakeWalletLoader(Chain& chain, ArgsManager& args) |
708 | 0 | { |
709 | 0 | return std::make_unique<wallet::WalletLoaderImpl>(chain, args); |
710 | 0 | } |
711 | | } // namespace interfaces |