5#include <bitcoin-build-config.h>
24 "\nReturns a new Bitcoin address for receiving payments.\n"
25 "If 'label' is specified, it is added to the address book \n"
26 "so payments received with the address will be associated with 'label'.\n",
28 {
"label",
RPCArg::Type::STR,
RPCArg::Default{
""},
"The label name for the address to be linked to. It can also be set to the empty string \"\" to represent the default label. The label does not need to exist, it will be created if there is no label by the given name."},
29 {
"address_type",
RPCArg::Type::STR,
RPCArg::DefaultHint{
"set by -addresstype"},
"The address type to use. Options are \"legacy\", \"p2sh-segwit\", \"bech32\", and \"bech32m\"."},
43 LOCK(pwallet->cs_wallet);
45 if (!pwallet->CanGetAddresses()) {
52 OutputType output_type = pwallet->m_default_address_type;
53 if (!request.params[1].isNull()) {
54 std::optional<OutputType> parsed =
ParseOutputType(request.params[1].get_str());
60 output_type = parsed.value();
63 auto op_dest = pwallet->GetNewDestination(output_type, label);
76 "\nReturns a new Bitcoin address, for receiving change.\n"
77 "This is for use with raw transactions, NOT normal use.\n",
79 {
"address_type",
RPCArg::Type::STR,
RPCArg::DefaultHint{
"set by -changetype"},
"The address type to use. Options are \"legacy\", \"p2sh-segwit\", \"bech32\", and \"bech32m\"."},
93 LOCK(pwallet->cs_wallet);
95 if (!pwallet->CanGetAddresses(
true)) {
99 OutputType output_type = pwallet->m_default_change_type.value_or(pwallet->m_default_address_type);
100 if (!request.params[0].isNull()) {
101 std::optional<OutputType> parsed =
ParseOutputType(request.params[0].get_str());
107 output_type = parsed.value();
110 auto op_dest = pwallet->GetNewChangeDestination(output_type);
123 "\nSets the label associated with the given address.\n",
138 LOCK(pwallet->cs_wallet);
147 if (pwallet->IsMine(dest)) {
161 "\nLists groups of addresses which have had their common ownership\n"
162 "made public by common use as inputs or as the resulting change\n"
163 "in past transactions\n",
190 pwallet->BlockUntilSyncedToCurrentChain();
192 LOCK(pwallet->cs_wallet);
204 const auto* address_book_entry = pwallet->FindAddressBookEntry(address);
205 if (address_book_entry) {
206 addressInfo.
push_back(address_book_entry->GetLabel());
209 jsonGrouping.
push_back(std::move(addressInfo));
211 jsonGroupings.
push_back(std::move(jsonGrouping));
213 return jsonGroupings;
221 "\nAdd an nrequired-to-sign multisignature address to the wallet. Requires a new wallet backup.\n"
222 "Each key is a Bitcoin address or hex-encoded public key.\n"
223 "This functionality is only intended for use with non-watchonly addresses.\n"
224 "See `importaddress` for watchonly p2sh address support.\n"
225 "If 'label' is specified, assign address to that label.\n"
226 "Note: This command is only compatible with legacy wallets.\n",
243 {
RPCResult::Type::ARR,
"warnings",
true,
"Any warnings resulting from the creation of this multisig",
250 "\nAdd a multisig address from 2 addresses\n"
252 "\nAs a JSON-RPC call\n"
266 int required = request.params[0].getInt<
int>();
270 std::vector<CPubKey> pubkeys;
271 for (
unsigned int i = 0; i < keys_or_addrs.size(); ++i) {
272 if (
IsHex(keys_or_addrs[i].get_str()) && (keys_or_addrs[i].get_str().length() == 66 || keys_or_addrs[i].get_str().length() == 130)) {
275 pubkeys.push_back(
AddrToPubKey(spk_man, keys_or_addrs[i].get_str()));
279 OutputType output_type = pwallet->m_default_address_type;
280 if (!request.params[3].isNull()) {
281 std::optional<OutputType> parsed =
ParseOutputType(request.params[3].get_str());
287 output_type = parsed.value();
322 result.
pushKV(
"descriptor", descriptor->ToString());
325 if (descriptor->GetOutputType() != output_type) {
327 warnings.
push_back(
"Unable to make chosen address type, please ensure no uncompressed public keys are present.");
339 "\nFills the keypool."+
358 LOCK(pwallet->cs_wallet);
361 unsigned int kpSize = 0;
362 if (!request.params[0].isNull()) {
363 if (request.params[0].getInt<
int>() < 0)
365 kpSize = (
unsigned int)request.params[0].getInt<
int>();
369 pwallet->TopUpKeyPool(kpSize);
371 if (pwallet->GetKeyPoolSize() < kpSize) {
383 "\nEntirely clears and refills the keypool.\n"
384 "WARNING: On non-HD wallets, this will require a new backup immediately, to include the new keys.\n"
385 "When restoring a backup of an HD wallet created before the newkeypool command is run, funds received to\n"
386 "new addresses may not appear automatically. They have not been lost, but the wallet may not find them.\n"
387 "This can be fixed by running the newkeypool command on the backup and then rescanning, so the wallet\n"
388 "re-generates the required keys." +
401 LOCK(pwallet->cs_wallet);
421 std::vector<std::vector<unsigned char>> solutions_data;
432 UniValue wallet_detail = std::visit(*
this, embedded);
433 subobj.
pushKVs(std::move(wallet_detail));
437 if (subobj.
exists(
"pubkey")) obj.
pushKV(
"pubkey", subobj[
"pubkey"]);
438 obj.
pushKV(
"embedded", std::move(subobj));
441 obj.
pushKV(
"sigsrequired", solutions_data[0][0]);
443 for (
size_t i = 1; i < solutions_data.size() - 1; ++i) {
444 CPubKey key(solutions_data[i].begin(), solutions_data[i].end());
447 obj.
pushKV(
"pubkeys", std::move(pubkeys));
513 std::unique_ptr<SigningProvider> provider =
nullptr;
523 "\nReturn information about the given bitcoin address.\n"
524 "Some of the information will only be present if the address is in the active wallet.\n",
535 {
RPCResult::Type::BOOL,
"solvable",
"If we know how to spend coins sent to this address, ignoring the possible lack of private keys."},
536 {
RPCResult::Type::STR,
"desc",
true,
"A descriptor for spending coins sent to this address (only when solvable)."},
537 {
RPCResult::Type::STR,
"parent_desc",
true,
"The descriptor used to derive this address if this is a descriptor wallet"},
543 {
RPCResult::Type::STR,
"script",
true,
"The output script type. Only if isscript is true and the redeemscript is known. Possible\n"
544 "types: nonstandard, pubkey, pubkeyhash, scripthash, multisig, nulldata, witness_v0_keyhash,\n"
545 "witness_v0_scripthash, witness_unknown."},
547 {
RPCResult::Type::ARR,
"pubkeys",
true,
"Array of pubkeys associated with the known redeemscript (only if script is multisig).",
551 {
RPCResult::Type::NUM,
"sigsrequired",
true,
"The number of signatures required to spend multisig output (only if script is multisig)."},
552 {
RPCResult::Type::STR_HEX,
"pubkey",
true,
"The hex value of the raw public key for single-key addresses (possibly embedded in P2SH or P2WSH)."},
553 {
RPCResult::Type::OBJ,
"embedded",
true,
"Information about the address embedded in P2SH or P2WSH, if relevant and known.",
555 {
RPCResult::Type::ELISION,
"",
"Includes all getaddressinfo output fields for the embedded address, excluding metadata (timestamp, hdkeypath, hdseedid)\n"
556 "and relation to the wallet (ismine, iswatchonly)."},
563 {
RPCResult::Type::ARR,
"labels",
"Array of labels associated with the address. Currently limited to one label but returned\n"
564 "as an array to keep the API stable if multiple labels are enabled in the future.",
579 LOCK(pwallet->cs_wallet);
581 std::string error_msg;
587 if (error_msg.empty()) error_msg =
"Invalid address";
595 ret.pushKV(
"address", currentAddress);
598 ret.pushKV(
"scriptPubKey",
HexStr(scriptPubKey));
600 std::unique_ptr<SigningProvider> provider = pwallet->GetSolvingProvider(scriptPubKey);
607 bool solvable = inferred->IsSolvable();
608 ret.pushKV(
"solvable", solvable);
610 ret.pushKV(
"desc", inferred->ToString());
613 ret.pushKV(
"solvable",
false);
616 const auto& spk_mans = pwallet->GetScriptPubKeyMans(scriptPubKey);
619 if (spk_mans.size()) spk_man = *spk_mans.begin();
623 std::string desc_str;
625 ret.pushKV(
"parent_desc", desc_str);
637 if (
const std::unique_ptr<CKeyMetadata> meta = spk_man->GetMetadata(dest)) {
638 ret.pushKV(
"timestamp", meta->nCreateTime);
639 if (meta->has_key_origin) {
643 ret.pushKV(
"hdseedid", meta->hd_seed_id.GetHex());
644 ret.pushKV(
"hdmasterfingerprint",
HexStr(meta->key_origin.fingerprint));
655 const auto* address_book_entry = pwallet->FindAddressBookEntry(dest);
656 if (address_book_entry) {
657 labels.
push_back(address_book_entry->GetLabel());
659 ret.pushKV(
"labels", std::move(labels));
669 "\nReturns the list of addresses assigned the specified label.\n",
678 {
RPCResult::Type::STR,
"purpose",
"Purpose of address (\"send\" for sending address, \"receive\" for receiving address)"},
691 LOCK(pwallet->cs_wallet);
697 std::set<std::string> addresses;
698 pwallet->ForEachAddrBookEntry([&](
const CTxDestination& _dest,
const std::string& _label,
bool _is_change,
const std::optional<AddressPurpose>& _purpose) {
699 if (_is_change)
return;
700 if (_label == label) {
705 bool unique = addresses.emplace(address).second;
713 ret.pushKVEnd(address, std::move(value));
729 "\nReturns the list of all labels, or labels that are assigned to addresses with a specific purpose.\n",
740 "\nList all labels\n"
742 "\nList labels that have receiving addresses\n"
744 "\nList labels that have sending addresses\n"
746 "\nAs a JSON-RPC call\n"
754 LOCK(pwallet->cs_wallet);
756 std::optional<AddressPurpose> purpose;
757 if (!request.params[0].isNull()) {
758 std::string purpose_str = request.params[0].get_str();
759 if (!purpose_str.empty()) {
768 std::set<std::string> label_set = pwallet->ListAddrBookLabels(purpose);
771 for (
const std::string&
name : label_set) {
781#ifdef ENABLE_EXTERNAL_SIGNER
785 "walletdisplayaddress",
786 "Display address on an external signer for verification.",
816 result.
pushKV(
"address", request.params[0].get_str());
CScriptID ToScriptID(const ScriptHash &script_hash)
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Parse a scriptPubKey for the destination.
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination corresponds to one with an address.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
CKeyID ToKeyID(const PKHash &key_hash)
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
std::string WriteHDKeypath(const std::vector< uint32_t > &keypath, bool apostrophe)
Write HD keypaths as strings.
#define CHECK_NONFATAL(condition)
Identity function.
A reference to a CKey: the Hash160 of its serialized public key.
An encapsulated public key.
bool IsCompressed() const
Check whether this is a compressed public key.
A hasher class for RIPEMD-160.
CRIPEMD160 & Write(const unsigned char *data, size_t len)
void Finalize(unsigned char hash[OUTPUT_SIZE])
Serialized script, used inside transaction inputs and outputs.
A reference to a CScript: the Hash160 of its serialization.
virtual bool GetCScript(const CScriptID &hash, CScript &redeemScriptOut) const override
RecursiveMutex cs_KeyStore
An interface to be implemented by keystores that support signing.
virtual bool GetCScript(const CScriptID &scriptid, CScript &script) const
virtual bool GetPubKey(const CKeyID &address, CPubKey &pubkey) const
void push_back(UniValue val)
void pushKVs(UniValue obj)
const UniValue & get_array() const
bool exists(const std::string &key) const
void pushKV(std::string key, UniValue val)
constexpr unsigned char * begin()
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
RecursiveMutex cs_wallet
Main wallet lock.
UniValue operator()(const PayToAnchor &id) const
UniValue operator()(const PKHash &pkhash) const
UniValue operator()(const WitnessV0ScriptHash &id) const
UniValue operator()(const WitnessV1Taproot &id) const
const SigningProvider *const provider
UniValue operator()(const WitnessV0KeyHash &id) const
UniValue operator()(const WitnessUnknown &id) const
DescribeWalletAddressVisitor(const SigningProvider *_provider)
void ProcessSubScript(const CScript &subscript, UniValue &obj) const
UniValue operator()(const CNoDestination &dest) const
UniValue operator()(const ScriptHash &scripthash) const
UniValue operator()(const PubKeyDestination &dest) const
bool GetDescriptorString(std::string &out, const bool priv) const
bool AddCScript(const CScript &redeemScript) override
bool NewKeyPool()
Mark old keypool keys as used, and generate all new keys.
UniValue ValueFromAmount(const CAmount amount)
const std::string CURRENCY_UNIT
util::Result< void > DisplayAddress(const CTxDestination &dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Display address on an external signer.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
std::string EncodeDestination(const CTxDestination &dest)
bilingual_str ErrorString(const Result< T > &result)
std::shared_ptr< CWallet > GetWalletForJSONRPCRequest(const JSONRPCRequest &request)
Figures out what wallet, if any, to use for a JSONRPCRequest.
RPCHelpMan keypoolrefill()
bool ScriptIsChange(const CWallet &wallet, const CScript &script)
void EnsureWalletIsUnlocked(const CWallet &wallet)
const std::string HELP_REQUIRING_PASSPHRASE
RPCHelpMan walletdisplayaddress()
LegacyScriptPubKeyMan & EnsureLegacyScriptPubKeyMan(CWallet &wallet, bool also_create)
RPCHelpMan addmultisigaddress()
RPCHelpMan listaddressgroupings()
static const unsigned int DEFAULT_KEYPOOL_SIZE
Default for -keypool.
std::string LabelFromValue(const UniValue &value)
isminetype
IsMine() return codes, which depend on ScriptPubKeyMan implementation.
static UniValue DescribeWalletAddress(const CWallet &wallet, const CTxDestination &dest)
std::string PurposeToString(AddressPurpose p)
RPCHelpMan getrawchangeaddress()
RPCHelpMan getaddressinfo()
std::map< CTxDestination, CAmount > GetAddressBalances(const CWallet &wallet)
RPCHelpMan getaddressesbylabel()
@ WALLET_FLAG_DISABLE_PRIVATE_KEYS
std::optional< AddressPurpose > PurposeFromString(std::string_view s)
RPCHelpMan getnewaddress()
std::set< std::set< CTxDestination > > GetAddressGroupings(const CWallet &wallet)
std::optional< OutputType > ParseOutputType(const std::string &type)
UniValue JSONRPCError(int code, const std::string &message)
@ RPC_WALLET_INVALID_LABEL_NAME
Invalid label name.
@ RPC_MISC_ERROR
General application defined errors.
@ RPC_INVALID_PARAMETER
Invalid, missing or duplicate parameter.
@ RPC_WALLET_ERROR
Wallet errors.
@ RPC_WALLET_KEYPOOL_RAN_OUT
Keypool ran out, call keypoolrefill first.
@ RPC_INVALID_ADDRESS_OR_KEY
Invalid address or key.
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
void PushWarnings(const UniValue &warnings, UniValue &obj)
Push warning messages to an RPC "warnings" field as a JSON array of strings.
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
const std::string UNIX_EPOCH_TIME
String used to describe UNIX epoch time in documentation, factored out to a constant for consistency.
CPubKey HexToPubKey(const std::string &hex_in)
const std::string EXAMPLE_ADDRESS[2]
Example bech32 addresses for the RPCExamples help documentation.
CTxDestination AddAndGetMultisigDestination(const int required, const std::vector< CPubKey > &pubkeys, OutputType type, FlatSigningProvider &keystore, CScript &script_out)
CPubKey AddrToPubKey(const FillableSigningProvider &keystore, const std::string &addr_in)
UniValue DescribeAddress(const CTxDestination &dest)
std::unique_ptr< Descriptor > InferDescriptor(const CScript &script, const SigningProvider &provider)
Find a descriptor for the specified script, using information from provider where possible.
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
TxoutType Solver(const CScript &scriptPubKey, std::vector< std::vector< unsigned char > > &vSolutionsRet)
Parse a scriptPubKey and identify script type for standard scripts.
std::string GetTxnOutputType(TxoutType t)
Get the name of a TxoutType as a string.
std::map< CScriptID, CScript > scripts
std::string DefaultHint
Hint for default value.
@ OMITTED
Optional argument for which the default value is omitted from help text for one of two reasons:
@ ELISION
Special type to denote elision (...)
@ NUM_TIME
Special numeric to denote unix epoch time.
@ ARR_FIXED
Special array that has a fixed number of entries.
@ OBJ_DYN
Special dictionary with keys that are not literals.
@ STR_HEX
Special string with only hex chars.
@ STR_AMOUNT
Special string to represent a floating point amount.
CTxDestination subtype to encode any future Witness version.
bool IsHex(std::string_view str)