MCPcopy Create free account
hub / github.com/ElementsProject/lightning / json_newaddr

Function json_newaddr

wallet/walletrpc.c:106–151  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

104}
105
106static struct command_result *json_newaddr(struct command *cmd,
107 const char *buffer,
108 const jsmntok_t *obj UNNEEDED,
109 const jsmntok_t *params)
110{
111 struct json_stream *response;
112 struct pubkey pubkey;
113 enum addrtype *addrtype;
114 s64 keyidx;
115 char *p2sh, *bech32;
116 u8 *b32script;
117
118 if (!param(cmd, buffer, params,
119 p_opt_def("addresstype", param_newaddr, &addrtype, ADDR_BECH32),
120 NULL))
121 return command_param_failed();
122
123 keyidx = wallet_get_newindex(cmd->ld);
124 if (keyidx < 0) {
125 return command_fail(cmd, LIGHTNINGD, "Keys exhausted ");
126 }
127
128 if (!bip32_pubkey(cmd->ld->wallet->bip32_base, &pubkey, keyidx))
129 return command_fail(cmd, LIGHTNINGD, "Keys generation failure");
130
131 b32script = scriptpubkey_p2wpkh(tmpctx, &pubkey);
132 if (*addrtype & ADDR_BECH32)
133 txfilter_add_scriptpubkey(cmd->ld->owned_txfilter, b32script);
134 if (*addrtype & ADDR_P2SH_SEGWIT)
135 txfilter_add_scriptpubkey(cmd->ld->owned_txfilter,
136 scriptpubkey_p2sh(tmpctx, b32script));
137
138 p2sh = encode_pubkey_to_addr(cmd, &pubkey, true, NULL);
139 bech32 = encode_pubkey_to_addr(cmd, &pubkey, false, NULL);
140 if (!p2sh || !bech32) {
141 return command_fail(cmd, LIGHTNINGD,
142 "p2wpkh address encoding failure.");
143 }
144
145 response = json_stream_success(cmd);
146 if (*addrtype & ADDR_BECH32)
147 json_add_string(response, "bech32", bech32);
148 if (*addrtype & ADDR_P2SH_SEGWIT)
149 json_add_string(response, "p2sh-segwit", p2sh);
150 return command_success(cmd, response);
151}
152
153static const struct json_command newaddr_command = {
154 "newaddr",

Callers

nothing calls this directly

Calls 12

wallet_get_newindexFunction · 0.85
bip32_pubkeyFunction · 0.85
scriptpubkey_p2wpkhFunction · 0.85
scriptpubkey_p2shFunction · 0.85
encode_pubkey_to_addrFunction · 0.85
paramFunction · 0.50
command_param_failedFunction · 0.50
command_failFunction · 0.50
json_stream_successFunction · 0.50
json_add_stringFunction · 0.50
command_successFunction · 0.50

Tested by

no test coverage detected