37 result.txin_is_mine.reserve(wtx.
tx->vin.size());
38 for (
const auto &txin : wtx.
tx->vin) {
39 result.txin_is_mine.emplace_back(wallet.
IsMine(txin));
41 result.txout_is_mine.reserve(wtx.
tx->vout.size());
42 result.txout_address.reserve(wtx.
tx->vout.size());
43 result.txout_address_is_mine.reserve(wtx.
tx->vout.size());
44 for (
const auto &txout : wtx.
tx->vout) {
45 result.txout_is_mine.emplace_back(wallet.
IsMine(txout));
46 result.txout_address.emplace_back();
47 result.txout_address_is_mine.emplace_back(
49 result.txout_address.back())
50 ? wallet.
IsMine(result.txout_address.back())
64 WalletTxStatus result;
67 : std::numeric_limits<int>::max();
71 result.lock_time = wtx.
tx->nLockTime;
88 result.txout = wtx.
tx->vout[n];
90 result.depth_in_main_chain = depth;
95 class WalletImpl :
public Wallet {
97 explicit WalletImpl(
const std::shared_ptr<CWallet> &wallet)
100 bool encryptWallet(
const SecureString &wallet_passphrase)
override {
101 return m_wallet->EncryptWallet(wallet_passphrase);
103 bool isCrypted()
override {
return m_wallet->IsCrypted(); }
104 bool lock()
override {
return m_wallet->Lock(); }
105 bool unlock(
const SecureString &wallet_passphrase)
override {
106 return m_wallet->Unlock(wallet_passphrase);
108 bool isLocked()
override {
return m_wallet->IsLocked(); }
109 bool changeWalletPassphrase(
112 return m_wallet->ChangeWalletPassphrase(old_wallet_passphrase,
113 new_wallet_passphrase);
115 void abortRescan()
override {
m_wallet->AbortRescan(); }
116 bool backupWallet(
const std::string &filename)
override {
117 return m_wallet->BackupWallet(filename);
119 std::string getWalletName()
override {
return m_wallet->GetName(); }
120 std::set<CTxDestination>
121 getLabelAddresses(
const std::string &label)
override {
122 return m_wallet->GetLabelAddresses(label);
124 bool getNewDestination(
const OutputType type,
const std::string label,
128 return m_wallet->GetNewDestination(type, label, dest, error);
135 std::unique_ptr<SigningProvider> provider =
136 m_wallet->GetSolvingProvider(script);
138 return provider->GetPubKey(address, pub_key);
144 std::string &str_sig)
override {
145 return m_wallet->SignMessage(message, pkhash, str_sig);
150 bool haveWatchOnly()
override {
151 auto spk_man =
m_wallet->GetLegacyScriptPubKeyMan();
153 return spk_man->HaveWatchOnly();
158 const std::string &purpose)
override {
159 return m_wallet->SetAddressBook(dest, name, purpose);
162 return m_wallet->DelAddressBook(dest);
165 isminetype *is_mine, std::string *purpose)
override {
167 auto it =
m_wallet->m_address_book.find(dest);
168 if (it ==
m_wallet->m_address_book.end() || it->second.IsChange()) {
172 *name = it->second.GetLabel();
178 *purpose = it->second.purpose;
182 std::vector<WalletAddress> getAddresses()
override {
184 std::vector<WalletAddress> result;
185 for (
const auto &item :
m_wallet->m_address_book) {
186 if (item.second.IsChange()) {
189 result.emplace_back(item.first,
m_wallet->IsMine(item.first),
190 item.second.GetLabel(),
191 item.second.purpose);
195 bool addDestData(
const CTxDestination &dest,
const std::string &key,
196 const std::string &value)
override {
199 return m_wallet->AddDestData(batch, dest, key, value);
202 const std::string &key)
override {
205 return m_wallet->EraseDestData(batch, dest, key);
207 std::vector<std::string>
208 getDestValues(
const std::string &
prefix)
override {
210 return m_wallet->GetDestValues(prefix);
212 void lockCoin(
const COutPoint &output)
override {
216 void unlockCoin(
const COutPoint &output)
override {
218 return m_wallet->UnlockCoin(output);
220 bool isLockedCoin(
const COutPoint &output)
override {
222 return m_wallet->IsLockedCoin(output);
224 void listLockedCoins(std::vector<COutPoint> &outputs)
override {
226 return m_wallet->ListLockedCoins(outputs);
229 createTransaction(
const std::vector<CRecipient> &recipients,
231 int &change_pos,
Amount &fee,
235 if (!
m_wallet->CreateTransaction(recipients, tx, fee, change_pos,
236 fail_reason, coin_control, sign)) {
244 m_wallet->CommitTransaction(std::move(tx), std::move(value_map),
245 std::move(order_form));
247 bool transactionCanBeAbandoned(
const TxId &txid)
override {
248 return m_wallet->TransactionCanBeAbandoned(txid);
250 bool abandonTransaction(
const TxId &txid)
override {
252 return m_wallet->AbandonTransaction(txid);
256 auto mi =
m_wallet->mapWallet.find(txid);
257 if (mi !=
m_wallet->mapWallet.end()) {
258 return mi->second.tx;
262 WalletTx getWalletTx(
const TxId &txid)
override {
264 auto mi =
m_wallet->mapWallet.find(txid);
265 if (mi !=
m_wallet->mapWallet.end()) {
266 return MakeWalletTx(*
m_wallet, mi->second);
270 std::vector<WalletTx> getWalletTxs()
override {
272 std::vector<WalletTx> result;
273 result.reserve(
m_wallet->mapWallet.size());
274 for (
const auto &entry :
m_wallet->mapWallet) {
275 result.emplace_back(MakeWalletTx(*
m_wallet, entry.second));
279 bool tryGetTxStatus(
const TxId &txid,
281 int &num_blocks, int64_t &block_time)
override {
283 if (!locked_wallet) {
286 auto mi =
m_wallet->mapWallet.find(txid);
287 if (mi ==
m_wallet->mapWallet.end()) {
290 num_blocks =
m_wallet->GetLastBlockHeight();
293 m_wallet->GetLastBlockHash(), FoundBlock().time(block_time)));
294 tx_status = MakeWalletTxStatus(*
m_wallet, mi->second);
297 WalletTx getWalletTxDetails(
const TxId &txid, WalletTxStatus &tx_status,
300 int &num_blocks)
override {
302 auto mi =
m_wallet->mapWallet.find(txid);
303 if (mi !=
m_wallet->mapWallet.end()) {
304 num_blocks =
m_wallet->GetLastBlockHeight();
305 in_mempool = mi->second.InMempool();
306 order_form = mi->second.vOrderForm;
307 tx_status = MakeWalletTxStatus(*
m_wallet, mi->second);
308 return MakeWalletTx(*
m_wallet, mi->second);
315 bool &complete)
const override {
316 return m_wallet->FillPSBT(psbtx, complete, sighash_type, sign,
319 WalletBalances getBalances()
override {
320 const auto bal =
m_wallet->GetBalance();
321 WalletBalances result;
322 result.balance = bal.m_mine_trusted;
323 result.unconfirmed_balance = bal.m_mine_untrusted_pending;
324 result.immature_balance = bal.m_mine_immature;
325 result.have_watch_only = haveWatchOnly();
326 if (result.have_watch_only) {
327 result.watch_only_balance = bal.m_watchonly_trusted;
328 result.unconfirmed_watch_only_balance =
329 bal.m_watchonly_untrusted_pending;
330 result.immature_watch_only_balance = bal.m_watchonly_immature;
334 bool tryGetBalances(WalletBalances &balances,
int &num_blocks,
335 bool force,
int cached_num_blocks)
override {
337 if (!locked_wallet) {
340 num_blocks =
m_wallet->GetLastBlockHeight();
341 if (!force && num_blocks == cached_num_blocks) {
344 balances = getBalances();
347 Amount getBalance()
override {
348 return m_wallet->GetBalance().m_mine_trusted;
351 return m_wallet->GetAvailableBalance(&coin_control);
363 return m_wallet->GetDebit(txin, filter);
367 return m_wallet->GetCredit(txout, filter);
369 CoinsList listCoins()
override {
372 for (
const auto &entry :
m_wallet->ListCoins()) {
373 auto &group = result[entry.first];
374 for (
const auto &coin : entry.second) {
375 group.emplace_back(
COutPoint(coin.tx->GetId(), coin.i),
376 MakeWalletTxOut(*
m_wallet, *coin.tx,
377 coin.i, coin.nDepth));
382 std::vector<WalletTxOut>
383 getCoins(
const std::vector<COutPoint> &outputs)
override {
385 std::vector<WalletTxOut> result;
386 result.reserve(outputs.size());
387 for (
const auto &output : outputs) {
388 result.emplace_back();
390 if (it !=
m_wallet->mapWallet.end()) {
391 int depth = it->second.GetDepthInMainChain();
393 result.back() = MakeWalletTxOut(*
m_wallet, it->second,
394 output.
GetN(), depth);
400 bool hdEnabled()
override {
return m_wallet->IsHDEnabled(); }
402 return m_wallet->m_default_address_type;
404 bool canGetAddresses()
const override {
407 bool privateKeysDisabled()
override {
411 return m_wallet->m_default_change_type;
413 Amount getDefaultMaxTxFee()
override {
414 return m_wallet->m_default_max_tx_fee;
417 bool isLegacy()
override {
return m_wallet->IsLegacy(); }
418 std::unique_ptr<Handler> handleUnload(UnloadFn fn)
override {
421 std::unique_ptr<Handler>
422 handleShowProgress(ShowProgressFn fn)
override {
425 std::unique_ptr<Handler>
426 handleStatusChanged(StatusChangedFn fn)
override {
430 std::unique_ptr<Handler>
431 handleAddressBookChanged(AddressBookChangedFn fn)
override {
434 const std::string &label,
bool is_mine,
435 const std::string &purpose,
ChangeType status) {
436 fn(address, label, is_mine, purpose, status);
439 std::unique_ptr<Handler>
440 handleTransactionChanged(TransactionChangedFn fn)
override {
446 std::unique_ptr<Handler>
447 handleWatchOnlyChanged(WatchOnlyChangedFn fn)
override {
450 std::unique_ptr<Handler>
451 handleCanGetAddressesChanged(CanGetAddressesChangedFn fn)
override {
453 m_wallet->NotifyCanGetAddressesChanged.connect(fn));
455 Amount getRequiredFee(
unsigned int tx_bytes)
override {
458 Amount getMinimumFee(
unsigned int tx_bytes,
467 class WalletClientImpl :
public ChainClient {
470 std::vector<std::string> wallet_filenames)
479 command.category, command.name,
480 [
this, &command](
Config &config,
482 UniValue &result,
bool last_handler) {
483 return command.actor(config, {request, m_context},
484 result, last_handler);
486 command.argNames, command.unique_id);
492 void registerRpcs()
override {
509 void setMockTime(int64_t time)
override {
return SetMockTime(time); }
510 std::vector<std::unique_ptr<Wallet>> getWallets()
override {
511 std::vector<std::unique_ptr<Wallet>> wallets;
527 std::unique_ptr<Wallet>
MakeWallet(
const std::shared_ptr<CWallet> &wallet) {
528 return wallet ? std::make_unique<WalletImpl>(wallet) :
nullptr;
531 std::unique_ptr<ChainClient>
533 std::vector<std::string> wallet_filenames) {
534 return std::make_unique<WalletClientImpl>(chain, args,
535 std::move(wallet_filenames));
std::shared_ptr< const CTransaction > CTransactionRef
std::shared_ptr< CWallet > m_wallet
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
#define TRY_LOCK(cs, name)
#define CHECK_NONFATAL(condition)
Throw a NonFatalCheckError when the condition evaluates to false.
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
bool IsInMainChain() const
const TxId & GetTxId() const
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
std::vector< std::shared_ptr< CWallet > > GetWallets()
A version of CTransaction with the PSBT format.
std::unique_ptr< Handler > MakeHandler(boost::signals2::connection connection)
Return handler wrapping a boost signal connection.
std::map< std::string, std::string > WalletValueMap
Amount GetRequiredFee(const CWallet &wallet, unsigned int nTxBytes)
Return the minimum required absolute fee for this size based on the required fee rate.
const std::vector< std::string > m_wallet_filenames
isminetype IsMine(const CTxDestination &dest) const
Access to the wallet database.
mapValue_t mapValue
Key/value map with information about the transaction.
interfaces::Chain & chain() const
Interface for accessing chain state.
void StopWallets()
Stop all wallets. Wallets will be flushed first.
void SetMockTime(int64_t nMockTimeIn)
For testing.
bool LoadWallets(const CChainParams &chainParams, interfaces::Chain &chain, const std::vector< std::string > &wallet_files)
Load wallet databases.
Amount GetCredit(const isminefilter &filter) const
An input of a transaction.
An encapsulated public key.
int GetBlocksToMaturity() const
virtual std::unique_ptr< Handler > handleRpc(const CRPCCommand &command)=0
Register handler for RPC.
isminetype
IsMine() return codes.
An output of a transaction.
Span< const CRPCCommand > GetWalletDumpRPCCommands()
An outpoint - a combination of a transaction hash and an index n into its vout.
bool VerifyWallets(const CChainParams &chainParams, interfaces::Chain &chain, const std::vector< std::string > &wallet_files)
Responsible for reading and validating the -wallet arguments and verifying the wallet database...
std::unique_ptr< Wallet > MakeWallet(const std::shared_ptr< CWallet > &wallet)
Return implementation of Wallet interface.
void FlushWallets()
Flush all wallets in preparation for shutdown.
A transaction with a bunch of additional info that only the owner cares about.
virtual bool contextualCheckTransactionForCurrentBlock(const CTransaction &tx, TxValidationState &state)=0
Check if transaction will be final given chain height current time.
Span< const CRPCCommand > GetWalletRPCCommands()
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Amount GetMinimumFee(const CWallet &wallet, unsigned int nTxBytes, const CCoinControl &coin_control)
Estimate the minimum fee considering user set parameters and the required fee.
interfaces::Chain * chain
int64_t GetTxTime() const
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
Amount GetDebit(const isminefilter &filter) const
filter decides which addresses will count towards the debit
Serialized script, used inside transaction inputs and outputs.
bool IsSpent(const COutPoint &outpoint) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Outpoint is spent if any non-conflicted transaction, spends it:
A TxId is the identifier of a transaction.
A reference to a CKey: the Hash160 of its serialized public key.
static UniValue stop(const Config &config, const JSONRPCRequest &jsonRequest)
int GetDepthInMainChain() const NO_THREAD_SAFETY_ANALYSIS
Return depth of transaction in blockchain: <0 : conflicts with a transaction this deep in the blockch...
bool RemoveWallet(const std::shared_ptr< CWallet > &wallet)
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
std::vector< std::pair< std::string, std::string > > WalletOrderForm
std::list< CRPCCommand > m_rpc_commands
WalletContext struct containing references to state shared between CWallet instances, like the reference to the chain interface, and the list of opened wallets.
unsigned int nTimeReceived
time received by this node
A Span is an object that can refer to a contiguous sequence of objects.
void UnloadWallets()
Close all wallets.
ChangeType
General change type (added, updated, removed).
std::unique_ptr< ChainClient > MakeWalletClient(Chain &chain, ArgsManager &args, std::vector< std::string > wallet_filenames)
Return implementation of ChainClient interface for a wallet client.
bool error(const char *fmt, const Args &... args)
Updated transaction status.
std::vector< std::unique_ptr< Handler > > m_rpc_handlers
boost::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
#define Assert(val)
Identity function.
void StartWallets(CScheduler &scheduler, const ArgsManager &args)
Complete startup of wallets.
Signature hash type wrapper class.