MCPcopy Create free account
hub / github.com/ElementsProject/elements / getnewaddress

Function getnewaddress

src/wallet/rpc/addresses.cpp:17–82  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15
16namespace wallet {
17RPCHelpMan getnewaddress()
18{
19 return RPCHelpMan{"getnewaddress",
20 "\nReturns a new address for receiving payments.\n"
21 "If 'label' is specified, it is added to the address book \n"
22 "so payments received with the address will be associated with 'label'.\n"
23 "When the wallet doesn't give blinded addresses by default (-blindedaddresses=0), \n"
24 "the address type \"blech32\" can still be used to get a blinded address.\n",
25 {
26 {"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."},
27 {"address_type", RPCArg::Type::STR, RPCArg::DefaultHint{"set by -addresstype"}, "The address type to use. Options are \"legacy\", \"p2sh-segwit\", \"bech32\", and \"bech32m\"."},
28 },
29 RPCResult{
30 RPCResult::Type::STR, "address", "The new address"
31 },
32 RPCExamples{
33 HelpExampleCli("getnewaddress", "")
34 + HelpExampleRpc("getnewaddress", "")
35 },
36 [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
37{
38 std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
39 if (!pwallet) return NullUniValue;
40
41 LOCK(pwallet->cs_wallet);
42
43 if (!pwallet->CanGetAddresses()) {
44 throw JSONRPCError(RPC_WALLET_ERROR, "Error: This wallet has no available keys");
45 }
46
47 // Parse the label first so we don't generate a key if there's an error
48 std::string label;
49 if (!request.params[0].isNull())
50 label = LabelFromValue(request.params[0]);
51
52 OutputType output_type = pwallet->m_default_address_type;
53 // default blinding to the blindedaddresses setting
54 bool add_blinding_key = gArgs.GetBoolArg("-blindedaddresses", g_con_elementsmode);
55
56 if (!request.params[1].isNull()) {
57 std::optional<OutputType> parsed = ParseOutputType(request.params[1].get_str());
58 if (!parsed) {
59 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, strprintf("Unknown address type '%s'", request.params[1].get_str()));
60 } else if (parsed.value() == OutputType::BECH32M && pwallet->GetLegacyScriptPubKeyMan()) {
61 throw JSONRPCError(RPC_INVALID_PARAMETER, "Legacy wallets cannot provide bech32m addresses");
62 }
63 if (request.params[1].get_str() == "blech32") {
64 // always blind for "blech32" even if `-blindedaddresses=0` in the config.
65 add_blinding_key = true;
66 } else if (request.params[1].get_str() == "bech32") {
67 // never blind for "bech32"
68 add_blinding_key = false;
69 }
70 output_type = parsed.value();
71 }
72
73 CTxDestination dest;
74 bilingual_str error;

Callers

nothing calls this directly

Calls 13

HelpExampleCliFunction · 0.85
HelpExampleRpcFunction · 0.85
JSONRPCErrorFunction · 0.85
LabelFromValueFunction · 0.85
ParseOutputTypeFunction · 0.85
EncodeDestinationFunction · 0.85
GetBoolArgMethod · 0.80
CanGetAddressesMethod · 0.45
isNullMethod · 0.45
valueMethod · 0.45

Tested by

no test coverage detected