Skip to content

Commit fd27cda

Browse files
ShahanaFarooquirustyrussell
authored andcommitted
rpc: Added listaddresses command
Changelog-Added: New rpc `listaddresses` to list issued addresses from the node.
1 parent af2f960 commit fd27cda

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

wallet/walletrpc.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,93 @@ static const struct json_command newaddr_command = {
177177
};
178178
AUTODATA(json_command, &newaddr_command);
179179

180+
static void json_add_address_details(struct json_stream *response,
181+
const u64 keyidx,
182+
const char *out_p2wpkh,
183+
const char *out_p2tr)
184+
{
185+
json_object_start(response, NULL);
186+
json_add_u64(response, "keyidx", keyidx);
187+
if (!streq(out_p2wpkh, "")) {
188+
json_add_string(response, "bech32", out_p2wpkh);
189+
}
190+
if (!streq(out_p2tr,"")) {
191+
json_add_string(response, "p2tr", out_p2tr);
192+
}
193+
json_object_end(response);
194+
}
195+
196+
static struct command_result *json_listaddresses(struct command *cmd,
197+
const char *buffer,
198+
const jsmntok_t *obj UNNEEDED,
199+
const jsmntok_t *params)
200+
{
201+
struct json_stream *response;
202+
struct pubkey pubkey;
203+
const u8 *scriptpubkey;
204+
u64 *liststart;
205+
u32 *listlimit;
206+
char *addr = NULL;
207+
208+
if (!param(cmd, buffer, params,
209+
p_opt("address", param_bitcoin_address, &scriptpubkey),
210+
p_opt_def("start", param_u64, &liststart, 1),
211+
p_opt("limit", param_u32, &listlimit),
212+
NULL))
213+
return command_param_failed();
214+
215+
addr = encode_scriptpubkey_to_addr(tmpctx, chainparams, scriptpubkey);
216+
217+
if (*liststart == 0) {
218+
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
219+
"Starting keyidx is 1; Cannot {start} with 0");
220+
}
221+
struct issued_address_type *listaddrtypes = wallet_list_addresses(tmpctx, cmd->ld->wallet, *liststart, listlimit);
222+
response = json_stream_success(cmd);
223+
json_array_start(response, "addresses");
224+
for (size_t i = 0; i < tal_count(listaddrtypes); i++) {
225+
if (listaddrtypes[i].keyidx == BIP32_INITIAL_HARDENED_CHILD){
226+
break;
227+
}
228+
bip32_pubkey(cmd->ld, &pubkey, listaddrtypes[i].keyidx);
229+
char *out_p2wpkh = "";
230+
char *out_p2tr = "";
231+
if (listaddrtypes[i].addrtype == ADDR_BECH32 || listaddrtypes[i].addrtype == ADDR_ALL) {
232+
u8 *redeemscript_p2wpkh;
233+
out_p2wpkh = encode_pubkey_to_addr(cmd,
234+
&pubkey,
235+
ADDR_BECH32,
236+
&redeemscript_p2wpkh);
237+
if (!out_p2wpkh) {
238+
abort();
239+
}
240+
}
241+
if (listaddrtypes[i].addrtype == ADDR_P2TR || listaddrtypes[i].addrtype == ADDR_ALL) {
242+
out_p2tr = encode_pubkey_to_addr(cmd,
243+
&pubkey,
244+
ADDR_P2TR,
245+
/* out_redeemscript */ NULL);
246+
if (!out_p2tr) {
247+
abort();
248+
}
249+
}
250+
if (!addr || streq(addr, out_p2wpkh) || streq(addr, out_p2tr)) {
251+
json_add_address_details(response, listaddrtypes[i].keyidx, out_p2wpkh, out_p2tr);
252+
if (addr) {
253+
break;
254+
}
255+
}
256+
}
257+
json_array_end(response);
258+
return command_success(cmd, response);
259+
}
260+
261+
static const struct json_command listaddresses_command = {
262+
"listaddresses",
263+
json_listaddresses
264+
};
265+
AUTODATA(json_command, &listaddresses_command);
266+
180267
static struct command_result *json_listaddrs(struct command *cmd,
181268
const char *buffer,
182269
const jsmntok_t *obj UNNEEDED,

0 commit comments

Comments
 (0)