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