/tmp/bitcoin/src/wallet/interfaces.cpp
Line | Count | Source |
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 | | #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 <primitives/transaction.h> |
13 | | #include <pubkey.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/export.h> |
25 | | #include <wallet/feebumper.h> |
26 | | #include <wallet/fees.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/scan.h> |
32 | | #include <wallet/wallet.h> |
33 | | |
34 | | #include <memory> |
35 | | #include <optional> |
36 | | #include <string> |
37 | | #include <utility> |
38 | | #include <vector> |
39 | | |
40 | | using common::PSBTError; |
41 | | using interfaces::Chain; |
42 | | using interfaces::FoundBlock; |
43 | | using interfaces::Handler; |
44 | | using interfaces::MakeSignalHandler; |
45 | | using interfaces::Wallet; |
46 | | using interfaces::WalletAddress; |
47 | | using interfaces::WalletBalances; |
48 | | using interfaces::WalletLoader; |
49 | | using interfaces::WalletMigrationResult; |
50 | | using interfaces::WalletTx; |
51 | | using interfaces::WalletTxOut; |
52 | | using interfaces::WalletTxStatus; |
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.GetTx(); |
64 | 0 | result.txin_is_mine.reserve(result.tx->vin.size()); |
65 | 0 | for (const auto& txin : result.tx->vin) { |
66 | 0 | result.txin_is_mine.emplace_back(InputIsMine(wallet, txin)); |
67 | 0 | } |
68 | 0 | result.txout_is_mine.reserve(result.tx->vout.size()); |
69 | 0 | result.txout_address.reserve(result.tx->vout.size()); |
70 | 0 | result.txout_address_is_mine.reserve(result.tx->vout.size()); |
71 | 0 | for (const auto& txout : result.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 | false); |
78 | 0 | } |
79 | 0 | result.credit = CachedTxGetCredit(wallet, wtx, /*avoid_reuse=*/true); |
80 | 0 | result.debit = CachedTxGetDebit(wallet, wtx, /*avoid_reuse=*/true); |
81 | 0 | result.change = CachedTxGetChange(wallet, wtx); |
82 | 0 | result.time = wtx.GetTxTime(); |
83 | 0 | result.from = wtx.m_from; |
84 | 0 | result.message = wtx.m_message; |
85 | 0 | result.comment = wtx.m_comment; |
86 | 0 | result.comment_to = wtx.m_comment_to; |
87 | 0 | result.is_coinbase = wtx.IsCoinBase(); |
88 | 0 | return result; |
89 | 0 | } |
90 | | |
91 | | //! Construct wallet tx status struct. |
92 | | WalletTxStatus MakeWalletTxStatus(const CWallet& wallet, const CWalletTx& wtx) |
93 | | EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) |
94 | 0 | { |
95 | 0 | AssertLockHeld(wallet.cs_wallet); |
96 | |
|
97 | 0 | WalletTxStatus result; |
98 | 0 | result.block_height = |
99 | 0 | wtx.state<TxStateConfirmed>() ? wtx.state<TxStateConfirmed>()->confirmed_block_height : |
100 | 0 | wtx.state<TxStateBlockConflicted>() ? wtx.state<TxStateBlockConflicted>()->conflicting_block_height : |
101 | 0 | std::numeric_limits<int>::max(); |
102 | 0 | result.blocks_to_maturity = wallet.GetTxBlocksToMaturity(wtx); |
103 | 0 | result.depth_in_main_chain = wallet.GetTxDepthInMainChain(wtx); |
104 | 0 | result.time_received = wtx.nTimeReceived; |
105 | 0 | result.lock_time = wtx.GetTx()->nLockTime; |
106 | 0 | result.is_trusted = CachedTxIsTrusted(wallet, wtx); |
107 | 0 | result.is_abandoned = wtx.isAbandoned(); |
108 | 0 | result.is_coinbase = wtx.IsCoinBase(); |
109 | 0 | result.is_in_main_chain = wtx.isConfirmed(); |
110 | 0 | return result; |
111 | 0 | } |
112 | | |
113 | | //! Construct wallet TxOut struct. |
114 | | WalletTxOut MakeWalletTxOut(const CWallet& wallet, |
115 | | const CWalletTx& wtx, |
116 | | int n, |
117 | | int depth) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) |
118 | 0 | { |
119 | 0 | WalletTxOut result; |
120 | 0 | result.txout = wtx.GetTx()->vout[n]; |
121 | 0 | result.time = wtx.GetTxTime(); |
122 | 0 | result.depth_in_main_chain = depth; |
123 | 0 | result.is_spent = wallet.IsSpent(COutPoint(wtx.GetHash(), n)); |
124 | 0 | return result; |
125 | 0 | } |
126 | | |
127 | | WalletTxOut MakeWalletTxOut(const CWallet& wallet, |
128 | | const COutput& output) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) |
129 | 0 | { |
130 | 0 | WalletTxOut result; |
131 | 0 | result.txout = output.txout; |
132 | 0 | result.time = output.time; |
133 | 0 | result.depth_in_main_chain = output.depth; |
134 | 0 | result.is_spent = wallet.IsSpent(output.outpoint); |
135 | 0 | return result; |
136 | 0 | } |
137 | | |
138 | | class WalletImpl : public Wallet |
139 | | { |
140 | | public: |
141 | 5 | explicit WalletImpl(WalletContext& context, const std::shared_ptr<CWallet>& wallet) : m_context(context), m_wallet(wallet) {} |
142 | | |
143 | | bool encryptWallet(const SecureString& wallet_passphrase) override |
144 | 0 | { |
145 | 0 | return m_wallet->EncryptWallet(wallet_passphrase); |
146 | 0 | } |
147 | 0 | bool isCrypted() override { return m_wallet->HasEncryptionKeys(); } |
148 | 0 | bool lock() override { return m_wallet->Lock(); } |
149 | 0 | bool unlock(const SecureString& wallet_passphrase) override { return m_wallet->Unlock(wallet_passphrase); } |
150 | 0 | bool isLocked() override { return m_wallet->IsLocked(); } |
151 | | bool changeWalletPassphrase(const SecureString& old_wallet_passphrase, |
152 | | const SecureString& new_wallet_passphrase) override |
153 | 0 | { |
154 | 0 | return m_wallet->ChangeWalletPassphrase(old_wallet_passphrase, new_wallet_passphrase); |
155 | 0 | } |
156 | 0 | void abortRescan() override { m_wallet->Scanner().Abort(); } |
157 | 0 | bool backupWallet(const std::string& filename) override { return m_wallet->BackupWallet(filename); } |
158 | 0 | std::string getWalletName() override { return m_wallet->GetName(); } |
159 | | util::Result<CTxDestination> getNewDestination(const OutputType type, const std::string& label) override |
160 | 0 | { |
161 | 0 | LOCK(m_wallet->cs_wallet); |
162 | 0 | return m_wallet->GetNewDestination(type, label); |
163 | 0 | } |
164 | | bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) override |
165 | 0 | { |
166 | 0 | std::unique_ptr<SigningProvider> provider = m_wallet->GetSolvingProvider(script); |
167 | 0 | if (provider) { |
168 | 0 | return provider->GetPubKey(address, pub_key); |
169 | 0 | } |
170 | 0 | return false; |
171 | 0 | } |
172 | | util::Expected<CExtPubKey, wallet::WalletError> addHDKey(const std::optional<CExtKey>& key) override |
173 | 4 | { |
174 | 4 | return m_wallet->AddHDKey(key); |
175 | 4 | } |
176 | | |
177 | | SigningResult signMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) override |
178 | 0 | { |
179 | 0 | return m_wallet->SignMessage(message, pkhash, str_sig); |
180 | 0 | } |
181 | | bool isSpendable(const CTxDestination& dest) override |
182 | 0 | { |
183 | 0 | LOCK(m_wallet->cs_wallet); |
184 | 0 | return m_wallet->IsMine(dest); |
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 | | AddressPurpose* purpose) override |
197 | 0 | { |
198 | 0 | LOCK(m_wallet->cs_wallet); |
199 | 0 | const auto& entry = m_wallet->FindAddressBookEntry(dest, /*allow_change=*/false); |
200 | 0 | if (!entry) return false; // addr not found |
201 | 0 | if (name) { |
202 | 0 | *name = entry->GetLabel(); |
203 | 0 | } |
204 | 0 | if (purpose) { |
205 | | // In very old wallets, address purpose may not be recorded so we derive it from IsMine |
206 | 0 | *purpose = entry->purpose.value_or(m_wallet->IsMine(dest) ? AddressPurpose::RECEIVE : AddressPurpose::SEND); |
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; |
216 | 0 | bool 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); |
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) |
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 | return m_wallet->LockCoin(output, write_to_db); |
253 | 0 | } |
254 | | bool unlockCoin(const COutPoint& output) override |
255 | 0 | { |
256 | 0 | LOCK(m_wallet->cs_wallet); |
257 | 0 | return m_wallet->UnlockCoin(output); |
258 | 0 | } |
259 | | bool isLockedCoin(const COutPoint& output) override |
260 | 0 | { |
261 | 0 | LOCK(m_wallet->cs_wallet); |
262 | 0 | return m_wallet->IsLockedCoin(output); |
263 | 0 | } |
264 | | void listLockedCoins(std::vector<COutPoint>& outputs) override |
265 | 0 | { |
266 | 0 | LOCK(m_wallet->cs_wallet); |
267 | 0 | return m_wallet->ListLockedCoins(outputs); |
268 | 0 | } |
269 | | util::Result<wallet::CreatedTransactionResult> createTransaction(const std::vector<CRecipient>& recipients, |
270 | | const CCoinControl& coin_control, |
271 | | bool sign, |
272 | | std::optional<unsigned int> change_pos) override |
273 | 0 | { |
274 | 0 | LOCK(m_wallet->cs_wallet); |
275 | 0 | return CreateTransaction(*m_wallet, recipients, change_pos, coin_control, sign); |
276 | 0 | } |
277 | | void commitTransaction(CTransactionRef tx, const std::vector<std::string>& messages) override |
278 | 0 | { |
279 | 0 | LOCK(m_wallet->cs_wallet); |
280 | 0 | m_wallet->CommitTransaction(std::move(tx), /*replaces_txid=*/std::nullopt, /*comment=*/std::nullopt, /*comment_to=*/std::nullopt, messages); |
281 | 0 | } |
282 | 0 | bool transactionCanBeAbandoned(const Txid& txid) override { return m_wallet->TransactionCanBeAbandoned(txid); } |
283 | | bool abandonTransaction(const Txid& txid) override |
284 | 0 | { |
285 | 0 | LOCK(m_wallet->cs_wallet); |
286 | 0 | return m_wallet->AbandonTransaction(txid); |
287 | 0 | } |
288 | | bool transactionCanBeBumped(const Txid& txid) override |
289 | 0 | { |
290 | 0 | return feebumper::TransactionCanBeBumped(*m_wallet.get(), txid); |
291 | 0 | } |
292 | | bool createBumpTransaction(const Txid& txid, |
293 | | const CCoinControl& coin_control, |
294 | | std::vector<bilingual_str>& errors, |
295 | | CAmount& old_fee, |
296 | | CAmount& new_fee, |
297 | | CMutableTransaction& mtx) override |
298 | 0 | { |
299 | 0 | std::vector<CTxOut> outputs; // just an empty list of new recipients for now |
300 | 0 | return feebumper::CreateRateBumpTransaction(*m_wallet.get(), txid, coin_control, errors, old_fee, new_fee, mtx, /* require_mine= */ true, outputs) == feebumper::Result::OK; |
301 | 0 | } |
302 | 0 | bool signBumpTransaction(CMutableTransaction& mtx) override { return feebumper::SignTransaction(*m_wallet.get(), mtx); } |
303 | | bool commitBumpTransaction(const Txid& txid, |
304 | | CMutableTransaction&& mtx, |
305 | | std::vector<bilingual_str>& errors, |
306 | | Txid& bumped_txid) override |
307 | 0 | { |
308 | 0 | return feebumper::CommitTransaction(*m_wallet.get(), txid, std::move(mtx), errors, bumped_txid) == |
309 | 0 | feebumper::Result::OK; |
310 | 0 | } |
311 | | CTransactionRef getTx(const Txid& txid) override |
312 | 0 | { |
313 | 0 | LOCK(m_wallet->cs_wallet); |
314 | 0 | auto mi = m_wallet->mapWallet.find(txid); |
315 | 0 | if (mi != m_wallet->mapWallet.end()) { |
316 | 0 | return mi->second.GetTx(); |
317 | 0 | } |
318 | 0 | return {}; |
319 | 0 | } |
320 | | WalletTx getWalletTx(const Txid& txid) override |
321 | 0 | { |
322 | 0 | LOCK(m_wallet->cs_wallet); |
323 | 0 | auto mi = m_wallet->mapWallet.find(txid); |
324 | 0 | if (mi != m_wallet->mapWallet.end()) { |
325 | 0 | return MakeWalletTx(*m_wallet, mi->second); |
326 | 0 | } |
327 | 0 | return {}; |
328 | 0 | } |
329 | | std::set<WalletTx> getWalletTxs() override |
330 | 0 | { |
331 | 0 | LOCK(m_wallet->cs_wallet); |
332 | 0 | std::set<WalletTx> result; |
333 | 0 | for (const auto& entry : m_wallet->mapWallet) { |
334 | 0 | result.emplace(MakeWalletTx(*m_wallet, entry.second)); |
335 | 0 | } |
336 | 0 | return result; |
337 | 0 | } |
338 | | bool tryGetTxStatus(const Txid& txid, |
339 | | interfaces::WalletTxStatus& tx_status, |
340 | | int& num_blocks, |
341 | | int64_t& block_time) override |
342 | 0 | { |
343 | 0 | TRY_LOCK(m_wallet->cs_wallet, locked_wallet); |
344 | 0 | if (!locked_wallet) { |
345 | 0 | return false; |
346 | 0 | } |
347 | 0 | auto mi = m_wallet->mapWallet.find(txid); |
348 | 0 | if (mi == m_wallet->mapWallet.end()) { |
349 | 0 | return false; |
350 | 0 | } |
351 | 0 | num_blocks = m_wallet->GetLastBlockHeight(); |
352 | 0 | block_time = -1; |
353 | 0 | CHECK_NONFATAL(m_wallet->chain().findBlock(m_wallet->GetLastBlockHash(), FoundBlock().time(block_time))); |
354 | 0 | tx_status = MakeWalletTxStatus(*m_wallet, mi->second); |
355 | 0 | return true; |
356 | 0 | } |
357 | | WalletTx getWalletTxDetails(const Txid& txid, |
358 | | WalletTxStatus& tx_status, |
359 | | std::vector<std::string>& messages, |
360 | | std::vector<std::string>& payment_requests, |
361 | | bool& in_mempool, |
362 | | int& num_blocks) override |
363 | 0 | { |
364 | 0 | LOCK(m_wallet->cs_wallet); |
365 | 0 | auto mi = m_wallet->mapWallet.find(txid); |
366 | 0 | if (mi != m_wallet->mapWallet.end()) { |
367 | 0 | num_blocks = m_wallet->GetLastBlockHeight(); |
368 | 0 | in_mempool = mi->second.InMempool(); |
369 | 0 | messages = mi->second.m_messages; |
370 | 0 | payment_requests = mi->second.m_payment_requests; |
371 | 0 | tx_status = MakeWalletTxStatus(*m_wallet, mi->second); |
372 | 0 | return MakeWalletTx(*m_wallet, mi->second); |
373 | 0 | } |
374 | 0 | return {}; |
375 | 0 | } |
376 | | std::optional<PSBTError> fillPSBT(const common::PSBTFillOptions& options, |
377 | | size_t* n_signed, |
378 | | PartiallySignedTransaction& psbtx, |
379 | | bool& complete) override |
380 | 0 | { |
381 | 0 | return m_wallet->FillPSBT(psbtx, options, complete, n_signed); |
382 | 0 | } |
383 | | WalletBalances getBalances() override |
384 | 0 | { |
385 | 0 | const auto bal = GetBalance(*m_wallet); |
386 | 0 | WalletBalances result; |
387 | 0 | result.balance = bal.m_mine_trusted; |
388 | 0 | result.unconfirmed_balance = bal.m_mine_untrusted_pending; |
389 | 0 | result.immature_balance = bal.m_mine_immature; |
390 | 0 | result.used_balance = bal.m_mine_used; |
391 | 0 | result.nonmempool_balance = bal.m_mine_nonmempool; |
392 | 0 | return result; |
393 | 0 | } |
394 | | bool tryGetBalances(WalletBalances& balances, uint256& block_hash) override |
395 | 0 | { |
396 | 0 | TRY_LOCK(m_wallet->cs_wallet, locked_wallet); |
397 | 0 | if (!locked_wallet) { |
398 | 0 | return false; |
399 | 0 | } |
400 | 0 | block_hash = m_wallet->GetLastBlockHash(); |
401 | 0 | balances = getBalances(); |
402 | 0 | return true; |
403 | 0 | } |
404 | 0 | CAmount getBalance() override { return GetBalance(*m_wallet).m_mine_trusted; } |
405 | | CAmount getAvailableBalance(const CCoinControl& coin_control) override |
406 | 0 | { |
407 | 0 | LOCK(m_wallet->cs_wallet); |
408 | 0 | CAmount total_amount = 0; |
409 | | // Fetch selected coins total amount |
410 | 0 | if (coin_control.HasSelected()) { |
411 | 0 | FastRandomContext rng{}; |
412 | 0 | CoinSelectionParams params(rng); |
413 | | // Note: for now, swallow any error. |
414 | 0 | if (auto res = FetchSelectedInputs(*m_wallet, coin_control, params)) { |
415 | 0 | total_amount += res->GetTotalAmount(); |
416 | 0 | } |
417 | 0 | } |
418 | | |
419 | | // And fetch the wallet available coins |
420 | 0 | if (coin_control.m_allow_other_inputs) { |
421 | 0 | total_amount += AvailableCoins(*m_wallet, &coin_control).GetTotalAmount(); |
422 | 0 | } |
423 | |
|
424 | 0 | return total_amount; |
425 | 0 | } |
426 | | bool txinIsMine(const CTxIn& txin) override |
427 | 0 | { |
428 | 0 | LOCK(m_wallet->cs_wallet); |
429 | 0 | return InputIsMine(*m_wallet, txin); |
430 | 0 | } |
431 | | bool txoutIsMine(const CTxOut& txout) override |
432 | 0 | { |
433 | 0 | LOCK(m_wallet->cs_wallet); |
434 | 0 | return m_wallet->IsMine(txout); |
435 | 0 | } |
436 | | CAmount getDebit(const CTxIn& txin) override |
437 | 0 | { |
438 | 0 | LOCK(m_wallet->cs_wallet); |
439 | 0 | return m_wallet->GetDebit(txin); |
440 | 0 | } |
441 | | CAmount getCredit(const CTxOut& txout) override |
442 | 0 | { |
443 | 0 | LOCK(m_wallet->cs_wallet); |
444 | 0 | return OutputGetCredit(*m_wallet, txout); |
445 | 0 | } |
446 | | CoinsList listCoins() override |
447 | 0 | { |
448 | 0 | LOCK(m_wallet->cs_wallet); |
449 | 0 | CoinsList result; |
450 | 0 | for (const auto& entry : ListCoins(*m_wallet)) { |
451 | 0 | auto& group = result[entry.first]; |
452 | 0 | for (const auto& coin : entry.second) { |
453 | 0 | group.emplace_back(coin.outpoint, |
454 | 0 | MakeWalletTxOut(*m_wallet, coin)); |
455 | 0 | } |
456 | 0 | } |
457 | 0 | return result; |
458 | 0 | } |
459 | | std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) override |
460 | 0 | { |
461 | 0 | LOCK(m_wallet->cs_wallet); |
462 | 0 | std::vector<WalletTxOut> result; |
463 | 0 | result.reserve(outputs.size()); |
464 | 0 | for (const auto& output : outputs) { |
465 | 0 | result.emplace_back(); |
466 | 0 | auto it = m_wallet->mapWallet.find(output.hash); |
467 | 0 | if (it != m_wallet->mapWallet.end()) { |
468 | 0 | int depth = m_wallet->GetTxDepthInMainChain(it->second); |
469 | 0 | if (depth >= 0) { |
470 | 0 | result.back() = MakeWalletTxOut(*m_wallet, it->second, output.n, depth); |
471 | 0 | } |
472 | 0 | } |
473 | 0 | } |
474 | 0 | return result; |
475 | 0 | } |
476 | 0 | CAmount getRequiredFee(unsigned int tx_bytes) override { return GetRequiredFee(*m_wallet, tx_bytes); } |
477 | | CAmount getMinimumFee(unsigned int tx_bytes, |
478 | | const CCoinControl& coin_control, |
479 | | std::optional<int>* returned_target, |
480 | | FeeReason* reason) override |
481 | 0 | { |
482 | 0 | auto min_fee_rate{GetMinimumFeeRate(*m_wallet, coin_control)}; |
483 | 0 | auto result = GetMinimumFee(min_fee_rate, tx_bytes); |
484 | 0 | if (returned_target) *returned_target = min_fee_rate.returned_target; |
485 | 0 | if (reason) *reason = min_fee_rate.fee_reason; |
486 | 0 | return result; |
487 | 0 | } |
488 | 0 | unsigned int getConfirmTarget() override { return m_wallet->m_confirm_target; } |
489 | 0 | bool hdEnabled() override { return m_wallet->IsHDEnabled(); } |
490 | 0 | bool canGetAddresses() override { return m_wallet->CanGetAddresses(); } |
491 | 0 | bool hasExternalSigner() override { return m_wallet->IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER); } |
492 | 0 | bool privateKeysDisabled() override { return m_wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS); } |
493 | 0 | bool taprootEnabled() override { |
494 | 0 | auto spk_man = m_wallet->GetScriptPubKeyMan(OutputType::BECH32M, /*internal=*/false); |
495 | 0 | return spk_man != nullptr; |
496 | 0 | } |
497 | 0 | OutputType getDefaultAddressType() override { return m_wallet->m_default_address_type; } |
498 | 0 | CAmount getDefaultMaxTxFee() override { return m_wallet->m_default_max_tx_fee; } |
499 | | void remove() override |
500 | 0 | { |
501 | 0 | RemoveWallet(m_context, m_wallet, /*load_on_start=*/false); |
502 | 0 | } |
503 | | std::unique_ptr<Handler> handleUnload(UnloadFn fn) override |
504 | 0 | { |
505 | 0 | return MakeSignalHandler(m_wallet->NotifyUnload.connect(fn)); |
506 | 0 | } |
507 | | std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override |
508 | 0 | { |
509 | 0 | return MakeSignalHandler(m_wallet->ShowProgress.connect(fn)); |
510 | 0 | } |
511 | | std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) override |
512 | 0 | { |
513 | 0 | return MakeSignalHandler(m_wallet->NotifyStatusChanged.connect([fn](CWallet*) { fn(); })); |
514 | 0 | } |
515 | | std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) override |
516 | 0 | { |
517 | 0 | return MakeSignalHandler(m_wallet->NotifyAddressBookChanged.connect( |
518 | 0 | [fn](const CTxDestination& address, const std::string& label, bool is_mine, |
519 | 0 | AddressPurpose purpose, ChangeType status) { fn(address, label, is_mine, purpose, status); })); |
520 | 0 | } |
521 | | std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) override |
522 | 0 | { |
523 | 0 | return MakeSignalHandler(m_wallet->NotifyTransactionChanged.connect( |
524 | 0 | [fn](const Txid& txid, ChangeType status) { fn(txid, status); })); |
525 | 0 | } |
526 | | std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) override |
527 | 0 | { |
528 | 0 | return MakeSignalHandler(m_wallet->NotifyCanGetAddressesChanged.connect(fn)); |
529 | 0 | } |
530 | 0 | CWallet* wallet() override { return m_wallet.get(); } |
531 | | |
532 | 0 | util::Result<std::string> exportWatchOnlyWallet(const fs::path& destination) override { |
533 | 0 | LOCK(m_wallet->cs_wallet); |
534 | 0 | m_wallet->TopUpKeyPool(); |
535 | 0 | return ExportWatchOnlyWallet(*m_wallet, destination, m_context); |
536 | 0 | } |
537 | | |
538 | | WalletContext& m_context; |
539 | | std::shared_ptr<CWallet> m_wallet; |
540 | | }; |
541 | | |
542 | | class WalletLoaderImpl : public WalletLoader |
543 | | { |
544 | | public: |
545 | | WalletLoaderImpl(Chain& chain, ArgsManager& args) |
546 | 438 | { |
547 | 438 | m_context.chain = &chain; |
548 | 438 | m_context.args = &args; |
549 | 438 | } |
550 | 438 | ~WalletLoaderImpl() override { stop(); } |
551 | | |
552 | | //! ChainClient methods |
553 | | void registerRpcs() override |
554 | 431 | { |
555 | 25.4k | for (const CRPCCommand& command : GetWalletRPCCommands()) { |
556 | 25.4k | m_rpc_commands.emplace_back(command.category, command.name, [this, &command](const JSONRPCRequest& request, UniValue& result, bool last_handler) { |
557 | 22.1k | JSONRPCRequest wallet_request = request; |
558 | 22.1k | wallet_request.context = &m_context; |
559 | 22.1k | return command.actor(wallet_request, result, last_handler); |
560 | 22.1k | }, command.argNames, command.unique_id); |
561 | 25.4k | m_rpc_commands.back().metadata_fn = command.metadata_fn; |
562 | 25.4k | m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back())); |
563 | 25.4k | } |
564 | 431 | } |
565 | 421 | bool verify() override { return VerifyWallets(m_context); } |
566 | 363 | bool load() override { return LoadWallets(m_context); } |
567 | | void start(CScheduler& scheduler) override |
568 | 360 | { |
569 | 360 | m_context.scheduler = &scheduler; |
570 | 360 | return StartWallets(m_context); |
571 | 360 | } |
572 | 852 | void stop() override { return UnloadWallets(m_context); } |
573 | 191 | void setMockTime(int64_t time) override { return SetMockTime(std::chrono::seconds{time}); } |
574 | 25 | void schedulerMockForward(std::chrono::seconds delta) override { Assert(m_context.scheduler)->MockForward(delta); } |
575 | | |
576 | | //! WalletLoader methods |
577 | | 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 |
578 | 0 | { |
579 | 0 | DatabaseOptions options; |
580 | 0 | DatabaseStatus status; |
581 | 0 | ReadDatabaseArgs(*m_context.args, options); |
582 | 0 | options.require_create = true; |
583 | 0 | options.create_flags = wallet_creation_flags; |
584 | 0 | options.create_passphrase = passphrase; |
585 | 0 | bilingual_str error; |
586 | 0 | std::unique_ptr<Wallet> wallet{MakeWallet(m_context, CreateWallet(m_context, name, /*load_on_start=*/true, options, status, error, warnings))}; |
587 | 0 | if (wallet) { |
588 | 0 | return wallet; |
589 | 0 | } else { |
590 | 0 | return util::Error{error}; |
591 | 0 | } |
592 | 0 | } |
593 | | util::Result<std::unique_ptr<Wallet>> loadWallet(const std::string& name, std::vector<bilingual_str>& warnings) override |
594 | 0 | { |
595 | 0 | DatabaseOptions options; |
596 | 0 | DatabaseStatus status; |
597 | 0 | ReadDatabaseArgs(*m_context.args, options); |
598 | 0 | options.require_existing = true; |
599 | 0 | bilingual_str error; |
600 | 0 | std::unique_ptr<Wallet> wallet{MakeWallet(m_context, LoadWallet(m_context, name, /*load_on_start=*/true, options, status, error, warnings))}; |
601 | 0 | if (wallet) { |
602 | 0 | return wallet; |
603 | 0 | } else { |
604 | 0 | return util::Error{error}; |
605 | 0 | } |
606 | 0 | } |
607 | | 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) override |
608 | 0 | { |
609 | 0 | DatabaseStatus status; |
610 | 0 | bilingual_str error; |
611 | 0 | std::unique_ptr<Wallet> wallet{MakeWallet(m_context, RestoreWallet(m_context, backup_file, wallet_name, /*load_on_start=*/true, status, error, warnings, load_after_restore))}; |
612 | 0 | if (!error.empty()) { |
613 | 0 | return util::Error{error}; |
614 | 0 | } |
615 | 0 | return wallet; |
616 | 0 | } |
617 | | util::Result<WalletMigrationResult> migrateWallet(const std::string& name, const SecureString& passphrase, bool load_wallet) override |
618 | 0 | { |
619 | 0 | auto res = wallet::MigrateLegacyToDescriptor(name, passphrase, m_context, load_wallet); |
620 | 0 | if (!res) return util::Error{util::ErrorString(res)}; |
621 | 0 | WalletMigrationResult out{ |
622 | 0 | .wallet = MakeWallet(m_context, res->wallet), |
623 | 0 | .watchonly_wallet_name = res->watchonly_wallet_name, |
624 | 0 | .solvables_wallet_name = res->solvables_wallet_name, |
625 | 0 | .backup_path = res->backup_path, |
626 | 0 | }; |
627 | 0 | return out; |
628 | 0 | } |
629 | | bool isEncrypted(const std::string& wallet_name) override |
630 | 0 | { |
631 | 0 | auto wallets{GetWallets(m_context)}; |
632 | 0 | auto it = std::find_if(wallets.begin(), wallets.end(), [&](std::shared_ptr<CWallet> w){ return w->GetName() == wallet_name; }); |
633 | 0 | if (it != wallets.end()) return (*it)->HasEncryptionKeys(); |
634 | | |
635 | | // Unloaded wallet, read db |
636 | 0 | DatabaseOptions options; |
637 | 0 | options.require_existing = true; |
638 | 0 | DatabaseStatus status; |
639 | 0 | bilingual_str error; |
640 | 0 | auto db = MakeWalletDatabase(wallet_name, options, status, error); |
641 | 0 | if (!db && status == wallet::DatabaseStatus::FAILED_LEGACY_DISABLED) { |
642 | 0 | options.require_format = wallet::DatabaseFormat::BERKELEY_RO; |
643 | 0 | db = MakeWalletDatabase(wallet_name, options, status, error); |
644 | 0 | } |
645 | 0 | if (!db) return false; |
646 | 0 | return WalletBatch(*db).IsEncrypted(); |
647 | 0 | } |
648 | | std::string getWalletDir() override |
649 | 0 | { |
650 | 0 | return fs::PathToString(GetWalletDir()); |
651 | 0 | } |
652 | | std::vector<std::pair<std::string, std::string>> listWalletDir() override |
653 | 0 | { |
654 | 0 | std::vector<std::pair<std::string, std::string>> paths; |
655 | 0 | for (auto& [path, format] : ListDatabases(GetWalletDir())) { |
656 | 0 | paths.emplace_back(fs::PathToString(path), format); |
657 | 0 | } |
658 | 0 | return paths; |
659 | 0 | } |
660 | | std::vector<std::unique_ptr<Wallet>> getWallets() override |
661 | 0 | { |
662 | 0 | std::vector<std::unique_ptr<Wallet>> wallets; |
663 | 0 | for (const auto& wallet : GetWallets(m_context)) { |
664 | 0 | wallets.emplace_back(MakeWallet(m_context, wallet)); |
665 | 0 | } |
666 | 0 | return wallets; |
667 | 0 | } |
668 | | std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) override |
669 | 0 | { |
670 | 0 | return HandleLoadWallet(m_context, std::move(fn)); |
671 | 0 | } |
672 | 0 | WalletContext* context() override { return &m_context; } |
673 | | |
674 | | WalletContext m_context; |
675 | | const std::vector<std::string> m_wallet_filenames; |
676 | | std::vector<std::unique_ptr<Handler>> m_rpc_handlers; |
677 | | std::list<CRPCCommand> m_rpc_commands; |
678 | | }; |
679 | | } // namespace |
680 | | } // namespace wallet |
681 | | |
682 | | namespace interfaces { |
683 | 5 | 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; } |
684 | | |
685 | | std::unique_ptr<WalletLoader> MakeWalletLoader(Chain& chain, ArgsManager& args) |
686 | 438 | { |
687 | 438 | return std::make_unique<wallet::WalletLoaderImpl>(chain, args); |
688 | 438 | } |
689 | | } // namespace interfaces |