43 result.txin_is_mine.reserve(wtx.
tx->vin.size());
44 for (
const auto& txin : wtx.
tx->vin) {
45 result.txin_is_mine.emplace_back(wallet.
IsMine(txin));
47 result.txout_is_mine.reserve(wtx.
tx->vout.size());
48 result.txout_address.reserve(wtx.
tx->vout.size());
49 result.txout_address_is_mine.reserve(wtx.
tx->vout.size());
50 for (
const auto& txout : wtx.
tx->vout) {
51 result.txout_is_mine.emplace_back(wallet.
IsMine(txout));
52 result.txout_address.emplace_back();
53 result.txout_address_is_mine.emplace_back(
ExtractDestination(txout.scriptPubKey, result.txout_address.back()) ?
54 wallet.
IsMine(result.txout_address.back()) :
69 WalletTxStatus result;
74 result.lock_time = wtx.
tx->nLockTime;
84 WalletTxOut MakeWalletTxOut(
CWallet& wallet,
90 result.txout = wtx.
tx->vout[n];
92 result.depth_in_main_chain = depth;
97 class WalletImpl :
public Wallet
100 explicit WalletImpl(
const std::shared_ptr<CWallet>& wallet) :
m_wallet(wallet) {}
102 bool encryptWallet(
const SecureString& wallet_passphrase)
override
104 return m_wallet->EncryptWallet(wallet_passphrase);
106 bool isCrypted()
override {
return m_wallet->IsCrypted(); }
107 bool lock()
override {
return m_wallet->Lock(); }
108 bool unlock(
const SecureString& wallet_passphrase)
override {
return m_wallet->Unlock(wallet_passphrase); }
109 bool isLocked()
override {
return m_wallet->IsLocked(); }
110 bool changeWalletPassphrase(
const SecureString& old_wallet_passphrase,
113 return m_wallet->ChangeWalletPassphrase(old_wallet_passphrase, new_wallet_passphrase);
115 void abortRescan()
override {
m_wallet->AbortRescan(); }
116 bool backupWallet(
const std::string& filename)
override {
return m_wallet->BackupWallet(filename); }
117 std::string getWalletName()
override {
return m_wallet->GetName(); }
122 return m_wallet->GetNewDestination(type, label, dest,
error);
126 std::unique_ptr<SigningProvider> provider =
m_wallet->GetSolvingProvider(script);
128 return provider->GetPubKey(address, pub_key);
132 SigningResult signMessage(
const std::string& message,
const PKHash& pkhash, std::string& str_sig)
override
134 return m_wallet->SignMessage(message, pkhash, str_sig);
141 bool haveWatchOnly()
override
143 auto spk_man =
m_wallet->GetLegacyScriptPubKeyMan();
145 return spk_man->HaveWatchOnly();
149 bool setAddressBook(
const CTxDestination& dest,
const std::string&
name,
const std::string& purpose)
override
155 return m_wallet->DelAddressBook(dest);
160 std::string* purpose)
override
163 auto it =
m_wallet->m_address_book.find(dest);
164 if (
it ==
m_wallet->m_address_book.end() ||
it->second.IsChange()) {
168 *
name =
it->second.GetLabel();
174 *purpose =
it->second.purpose;
178 std::vector<WalletAddress> getAddresses()
override
181 std::vector<WalletAddress> result;
182 for (
const auto& item :
m_wallet->m_address_book) {
183 if (item.second.IsChange())
continue;
184 result.emplace_back(item.first,
m_wallet->IsMine(item.first), item.second.GetLabel(), item.second.purpose);
188 bool addDestData(
const CTxDestination& dest,
const std::string& key,
const std::string& value)
override
192 return m_wallet->AddDestData(batch, dest, key, value);
194 bool eraseDestData(
const CTxDestination& dest,
const std::string& key)
override
198 return m_wallet->EraseDestData(batch, dest, key);
200 std::vector<std::string> getDestValues(
const std::string&
prefix)
override
205 void lockCoin(
const COutPoint& output)
override
210 void unlockCoin(
const COutPoint& output)
override
213 return m_wallet->UnlockCoin(output);
215 bool isLockedCoin(
const COutPoint& output)
override
220 void listLockedCoins(std::vector<COutPoint>& outputs)
override
223 return m_wallet->ListLockedCoins(outputs);
225 CTransactionRef createTransaction(
const std::vector<CRecipient>& recipients,
235 if (!
m_wallet->CreateTransaction(recipients, tx, fee, change_pos,
236 fail_reason, coin_control, fee_calc_out, sign)) {
246 m_wallet->CommitTransaction(std::move(tx), std::move(value_map), std::move(order_form));
248 bool transactionCanBeAbandoned(
const uint256& txid)
override {
return m_wallet->TransactionCanBeAbandoned(txid); }
249 bool abandonTransaction(
const uint256& txid)
override
252 return m_wallet->AbandonTransaction(txid);
254 bool transactionCanBeBumped(
const uint256& txid)
override
258 bool createBumpTransaction(
const uint256& txid,
260 std::vector<bilingual_str>& errors,
268 bool commitBumpTransaction(
const uint256& txid,
270 std::vector<bilingual_str>& errors,
279 auto mi =
m_wallet->mapWallet.find(txid);
280 if (mi !=
m_wallet->mapWallet.end()) {
281 return mi->second.tx;
285 WalletTx getWalletTx(
const uint256& txid)
override
288 auto mi =
m_wallet->mapWallet.find(txid);
289 if (mi !=
m_wallet->mapWallet.end()) {
290 return MakeWalletTx(*
m_wallet, mi->second);
294 std::vector<WalletTx> getWalletTxs()
override
297 std::vector<WalletTx> result;
298 result.reserve(
m_wallet->mapWallet.size());
299 for (
const auto& entry :
m_wallet->mapWallet) {
300 result.emplace_back(MakeWalletTx(*
m_wallet, entry.second));
304 bool tryGetTxStatus(
const uint256& txid,
307 int64_t& block_time)
override
310 if (!locked_wallet) {
313 auto mi =
m_wallet->mapWallet.find(txid);
314 if (mi ==
m_wallet->mapWallet.end()) {
317 num_blocks =
m_wallet->GetLastBlockHeight();
320 tx_status = MakeWalletTxStatus(*
m_wallet, mi->second);
323 WalletTx getWalletTxDetails(
const uint256& txid,
324 WalletTxStatus& tx_status,
327 int& num_blocks)
override
330 auto mi =
m_wallet->mapWallet.find(txid);
331 if (mi !=
m_wallet->mapWallet.end()) {
332 num_blocks =
m_wallet->GetLastBlockHeight();
333 in_mempool = mi->second.InMempool();
334 order_form = mi->second.vOrderForm;
335 tx_status = MakeWalletTxStatus(*
m_wallet, mi->second);
336 return MakeWalletTx(*
m_wallet, mi->second);
345 size_t* n_signed)
override
347 return m_wallet->FillPSBT(psbtx, complete, sighash_type, sign, bip32derivs, n_signed);
349 WalletBalances getBalances()
override
351 const auto bal =
m_wallet->GetBalance();
352 WalletBalances result;
353 result.balance = bal.m_mine_trusted;
354 result.unconfirmed_balance = bal.m_mine_untrusted_pending;
355 result.immature_balance = bal.m_mine_immature;
356 result.have_watch_only = haveWatchOnly();
357 if (result.have_watch_only) {
358 result.watch_only_balance = bal.m_watchonly_trusted;
359 result.unconfirmed_watch_only_balance = bal.m_watchonly_untrusted_pending;
360 result.immature_watch_only_balance = bal.m_watchonly_immature;
364 bool tryGetBalances(WalletBalances& balances,
uint256& block_hash)
override
367 if (!locked_wallet) {
370 block_hash =
m_wallet->GetLastBlockHash();
371 balances = getBalances();
374 CAmount getBalance()
override {
return m_wallet->GetBalance().m_mine_trusted; }
377 return m_wallet->GetAvailableBalance(&coin_control);
392 return m_wallet->GetDebit(txin, filter);
397 return m_wallet->GetCredit(txout, filter);
399 CoinsList listCoins()
override
403 for (
const auto& entry :
m_wallet->ListCoins()) {
404 auto& group = result[entry.first];
405 for (
const auto& coin : entry.second) {
406 group.emplace_back(
COutPoint(coin.tx->GetHash(), coin.i),
407 MakeWalletTxOut(*
m_wallet, *coin.tx, coin.i, coin.nDepth));
412 std::vector<WalletTxOut> getCoins(
const std::vector<COutPoint>& outputs)
override
415 std::vector<WalletTxOut> result;
416 result.reserve(outputs.size());
417 for (
const auto& output : outputs) {
418 result.emplace_back();
421 int depth =
it->second.GetDepthInMainChain();
423 result.back() = MakeWalletTxOut(*
m_wallet,
it->second, output.
n, depth);
430 CAmount getMinimumFee(
unsigned int tx_bytes,
432 int* returned_target,
439 if (reason) *reason = fee_calc.
reason;
442 unsigned int getConfirmTarget()
override {
return m_wallet->m_confirm_target; }
443 bool hdEnabled()
override {
return m_wallet->IsHDEnabled(); }
444 bool canGetAddresses()
override {
return m_wallet->CanGetAddresses(); }
446 OutputType getDefaultAddressType()
override {
return m_wallet->m_default_address_type; }
447 CAmount getDefaultMaxTxFee()
override {
return m_wallet->m_default_max_tx_fee; }
448 void remove()
override
452 bool isLegacy()
override {
return m_wallet->IsLegacy(); }
453 std::unique_ptr<Handler> handleUnload(UnloadFn fn)
override
457 std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn)
override
461 std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn)
override
465 std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn)
override
469 const std::string& purpose,
ChangeType status) { fn(address, label, is_mine, purpose, status); }));
471 std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn)
override
476 std::unique_ptr<Handler> handleWatchOnlyChanged(WatchOnlyChangedFn fn)
override
480 std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn)
override
489 class WalletClientImpl :
public WalletClient
500 void registerRpcs()
override
504 return command.actor({request, m_context}, result, last_handler);
505 }, command.argNames, command.unique_id);
514 void setMockTime(int64_t time)
override {
return SetMockTime(time); }
517 std::unique_ptr<Wallet> createWallet(
const std::string&
name,
const SecureString& passphrase, uint64_t wallet_creation_flags,
bilingual_str&
error, std::vector<bilingual_str>& warnings)
override
519 std::shared_ptr<CWallet> wallet;
527 std::unique_ptr<Wallet> loadWallet(
const std::string&
name,
bilingual_str&
error, std::vector<bilingual_str>& warnings)
override
534 std::string getWalletDir()
override
538 std::vector<std::string> listWalletDir()
override
540 std::vector<std::string> paths;
542 paths.push_back(path.string());
546 std::vector<std::unique_ptr<Wallet>> getWallets()
override
548 std::vector<std::unique_ptr<Wallet>> wallets;
554 std::unique_ptr<Handler> handleLoadWallet(
LoadWalletFn fn)
override
567 std::unique_ptr<Wallet>
MakeWallet(
const std::shared_ptr<CWallet>& wallet) {
return wallet ? MakeUnique<WalletImpl>(wallet) :
nullptr; }
571 return MakeUnique<WalletClientImpl>(chain, args);
int64_t CAmount
Amount in satoshis (Can be negative)
#define CHECK_NONFATAL(condition)
Throw a NonFatalCheckError when the condition evaluates to false.
#define Assert(val)
Identity function.
A reference to a CKey: the Hash160 of its serialized public key.
An outpoint - a combination of a transaction hash and an index n into its vout.
An encapsulated public key.
Simple class for background tasks that should be run periodically or once "after a while".
Serialized script, used inside transaction inputs and outputs.
An input of a transaction.
An output of a transaction.
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
interfaces::Chain & chain() const
Interface for accessing chain state.
A transaction with a bunch of additional info that only the owner cares about.
mapValue_t mapValue
Key/value map with information about the transaction.
int GetDepthInMainChain() const NO_THREAD_SAFETY_ANALYSIS
Return depth of transaction in blockchain: <0 : conflicts with a transaction this deep in the blockch...
int GetBlocksToMaturity() const
bool IsInMainChain() const
const uint256 & GetHash() const
unsigned int nTimeReceived
time received by this node
Access to the wallet database.
Interface giving clients (wallet processes, maybe other analysis tools in the future) ability to acce...
virtual std::unique_ptr< Handler > handleRpc(const CRPCCommand &command)=0
Register handler for RPC.
virtual bool checkFinalTx(const CTransaction &tx)=0
Check if transaction will be final given chain height current time.
CAmount GetCredit(const isminefilter &filter) const
isminetype IsMine(const CTxDestination &dest) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
CAmount GetChange() const
bool IsSpent(const uint256 &hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Outpoint is spent if any non-conflicted transaction spends it:
CAmount GetDebit(const isminefilter &filter) const
filter decides which addresses will count towards the debit
int64_t GetTxTime() const
std::list< CRPCCommand > m_rpc_commands
std::shared_ptr< CWallet > m_wallet
std::vector< std::unique_ptr< Handler > > m_rpc_handlers
const std::vector< std::string > m_wallet_filenames
isminetype
IsMine() return codes.
void StartWallets(CScheduler &scheduler, const ArgsManager &args)
Complete startup of wallets.
bool VerifyWallets(interfaces::Chain &chain)
Responsible for reading and validating the -wallet arguments and verifying the wallet database.
void UnloadWallets()
Close all wallets.
void FlushWallets()
Flush all wallets in preparation for shutdown.
void StopWallets()
Stop all wallets. Wallets will be flushed first.
bool LoadWallets(interfaces::Chain &chain)
Load wallet databases.
Result CommitTransaction(CWallet &wallet, const uint256 &txid, CMutableTransaction &&mtx, std::vector< bilingual_str > &errors, uint256 &bumped_txid)
Commit the bumpfee transaction.
Result CreateRateBumpTransaction(CWallet &wallet, const uint256 &txid, const CCoinControl &coin_control, std::vector< bilingual_str > &errors, CAmount &old_fee, CAmount &new_fee, CMutableTransaction &mtx)
Create bumpfee transaction based on feerate estimates.
bool TransactionCanBeBumped(const CWallet &wallet, const uint256 &txid)
Return whether transaction can be bumped.
bool SignTransaction(CWallet &wallet, CMutableTransaction &mtx)
Sign the new transaction,.
std::unique_ptr< Wallet > MakeWallet(const std::shared_ptr< CWallet > &wallet)
Return implementation of Wallet interface.
std::unique_ptr< Handler > MakeHandler(boost::signals2::connection connection)
Return handler wrapping a boost signal connection.
std::vector< std::pair< std::string, std::string > > WalletOrderForm
std::unique_ptr< WalletClient > MakeWalletClient(Chain &chain, ArgsManager &args)
Return implementation of ChainClient interface for a wallet client.
std::map< std::string, std::string > WalletValueMap
std::deque< CInv >::iterator it
std::shared_ptr< const CTransaction > CTransactionRef
Span< const CRPCCommand > GetWalletRPCCommands()
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a standard scriptPubKey for the destination address.
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
A mutable version of CTransaction.
SecureString create_passphrase
A version of CTransaction with the PSBT format.
WalletContext struct containing references to state shared between CWallet instances,...
interfaces::Chain * chain
Updated transaction status.
#define TRY_LOCK(cs, name)
bool error(const char *fmt, const Args &... args)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
void SetMockTime(int64_t nMockTimeIn)
For testing.
ChangeType
General change type (added, updated, removed).
CAmount GetRequiredFee(const CWallet &wallet, unsigned int nTxBytes)
Return the minimum required absolute fee for this size based on the required fee rate.
CAmount GetMinimumFee(const CWallet &wallet, unsigned int nTxBytes, const CCoinControl &coin_control, FeeCalculation *feeCalc)
Estimate the minimum fee considering user set parameters and the required fee.
std::unique_ptr< interfaces::Handler > HandleLoadWallet(LoadWalletFn load_wallet)
bool RemoveWallet(const std::shared_ptr< CWallet > &wallet, Optional< bool > load_on_start, std::vector< bilingual_str > &warnings)
std::shared_ptr< CWallet > CreateWallet(interfaces::Chain &chain, const std::string &name, Optional< bool > load_on_start, DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error, std::vector< bilingual_str > &warnings)
std::vector< std::shared_ptr< CWallet > > GetWallets()
std::shared_ptr< CWallet > LoadWallet(interfaces::Chain &chain, const std::string &name, Optional< bool > load_on_start, const DatabaseOptions &options, DatabaseStatus &status, bilingual_str &error, std::vector< bilingual_str > &warnings)
std::function< void(std::unique_ptr< interfaces::Wallet > wallet)> LoadWalletFn
std::vector< fs::path > ListWalletDir()
Get wallets in wallet directory.
fs::path GetWalletDir()
Get the path of the wallet directory.
@ WALLET_FLAG_DISABLE_PRIVATE_KEYS