diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 96fc50074..75720c927 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -393,6 +393,93 @@ static const struct json_command newaddr_command = { }; AUTODATA(json_command, &newaddr_command); +static void json_listaddrs(struct command *cmd, + const char *buffer, const jsmntok_t *params) +{ + struct json_result *response = new_json_result(cmd); + struct ext_key ext; + struct sha256 h; + struct ripemd160 h160; + struct pubkey pubkey; + jsmntok_t *bip32tok; + u8 *redeemscript; + bool ok; + char *out_p2sh; + char *out_p2wpkh; + const char *hrp; + u64 bip32_max_index; + + if (!json_get_params(cmd, buffer, params, + "?bip32_max_index", &bip32tok, + NULL)) { + return; + } + + if (!bip32tok || !json_tok_u64(buffer, bip32tok, &bip32_max_index)) { + bip32_max_index = db_get_intvar(cmd->ld->wallet->db, "bip32_max_index", 0); + } + json_object_start(response, NULL); + json_array_start(response, "addresses"); + + for (s64 keyidx = 0; keyidx < bip32_max_index; keyidx++) { + + if(keyidx == BIP32_INITIAL_HARDENED_CHILD){ + break; + } + + if (bip32_key_from_parent(cmd->ld->wallet->bip32_base, keyidx, + BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) { + command_fail(cmd, "Keys generation failure"); + return; + } + + if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey.pubkey, + ext.pub_key, sizeof(ext.pub_key))) { + command_fail(cmd, "Key parsing failure"); + return; + } + + // p2sh + redeemscript = bitcoin_redeem_p2sh_p2wpkh(cmd, &pubkey); + sha256(&h, redeemscript, tal_count(redeemscript)); + ripemd160(&h160, h.u.u8, sizeof(h)); + out_p2sh = p2sh_to_base58(cmd, + get_chainparams(cmd->ld)->testnet, &h160); + + // bech32 : p2wpkh + hrp = get_chainparams(cmd->ld)->bip173_name; + /* out buffer is 73 + strlen(human readable part). see bech32.h */ + out_p2wpkh = tal_arr(cmd, char, 73 + strlen(hrp)); + pubkey_to_hash160(&pubkey, &h160); + ok = segwit_addr_encode(out_p2wpkh, hrp, 0, h160.u.u8, sizeof(h160.u.u8)); + if (!ok) { + command_fail(cmd, "p2wpkh address encoding failure."); + return; + } + + // outputs + json_object_start(response, NULL); + json_add_u64(response, "keyidx", keyidx); + json_add_pubkey(response, "pubkey", &pubkey); + json_add_string(response, "p2sh", out_p2sh); + json_add_hex(response, "p2sh_redeemscript", redeemscript, tal_count(redeemscript)); + json_add_string(response, "bech32", out_p2wpkh); + json_add_hex(response, "bech32_redeemscript", &h160.u.u8, sizeof(struct ripemd160)); + json_object_end(response); + } + json_array_end(response); + json_object_end(response); + command_success(cmd, response); +} + +static const struct json_command listaddrs_command = { + "dev-listaddrs", + json_listaddrs, + "Show addresses list up to derivation {index} (default is the last bip32 index)", false, + "Show addresses of your internal wallet. Use `newaddr` to generate a new address." +}; +AUTODATA(json_command, &listaddrs_command); + static void json_listfunds(struct command *cmd, const char *buffer UNUSED, const jsmntok_t *params UNUSED) {