From 998a0ca6e297825472ac5396be4b62ad0c1dfb28 Mon Sep 17 00:00:00 2001 From: Luca Vaccaro Date: Wed, 7 Feb 2018 12:56:16 +0100 Subject: [PATCH 1/4] Add dev-listaddrs command to get the list of generated addresses --- wallet/walletrpc.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 3a651cd7ac43..1375ef42a48c 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -19,6 +19,7 @@ #include #include #include +#include struct withdrawal { u64 amount, changesatoshi; @@ -395,6 +396,84 @@ 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); + jsmntok_t *bip32tok; + u64 bip32_max_index; + const tal_t *tmpctx = tal_tmpctx(cmd); + + 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"); + + s64 keyidx; + for (keyidx = 0; keyidx < bip32_max_index; keyidx++) { + + struct ext_key ext; + struct sha256 h; + struct ripemd160 p2sh; + struct pubkey pubkey; + u8 *redeemscript; + + if(keyidx == BIP32_INITIAL_HARDENED_CHILD){ + command_fail(cmd, "Keys exhausted "); + } + + 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; + } + + redeemscript = bitcoin_redeem_p2sh_p2wpkh(cmd, &pubkey); + sha256(&h, redeemscript, tal_count(redeemscript)); + ripemd160(&p2sh, h.u.u8, sizeof(h)); + + json_object_start(response, NULL); + json_add_u64(response, "keyidx", keyidx); + json_add_string(response, "address", + p2sh_to_base58(cmd, get_chainparams(cmd->ld)->testnet, + &p2sh)); + + { + char *hex = pubkey_to_hexstr(tmpctx, &pubkey); + json_add_string(response, "pubkey", hex); + } + { + int len = sizeof(struct ripemd160) + 1; + char *hex = tal_arr(NULL, char, hex_str_size(len)); + hex_encode(redeemscript, len, hex, hex_str_size(len)); + json_add_string(response, "redeemscript", hex); + } + 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, + "Get list of generated p2sh addresses" +}; +AUTODATA(json_command, &listaddrs_command); + static void json_listfunds(struct command *cmd, const char *buffer, const jsmntok_t *params) { From 7ce5d2705274b705d2ae3cd294063b2055e50434 Mon Sep 17 00:00:00 2001 From: Luca Vaccaro Date: Wed, 7 Feb 2018 13:11:24 +0100 Subject: [PATCH 2/4] Change description --- wallet/walletrpc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 1375ef42a48c..b13b1a09d68e 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -470,7 +470,7 @@ static void json_listaddrs(struct command *cmd, static const struct json_command listaddrs_command = { "dev-listaddrs", json_listaddrs, - "Get list of generated p2sh addresses" + "Show addresses list up to derivation {index} (default is the last bip32 index)" }; AUTODATA(json_command, &listaddrs_command); From ed09dd28ed4fd50636980a11bf5420f8be7ceefe Mon Sep 17 00:00:00 2001 From: Luca Vaccaro Date: Wed, 7 Feb 2018 16:41:25 +0100 Subject: [PATCH 3/4] Free tal_tmpctx context --- wallet/walletrpc.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index b13b1a09d68e..5337db449e50 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -402,10 +402,11 @@ static void json_listaddrs(struct command *cmd, struct json_result *response = new_json_result(cmd); jsmntok_t *bip32tok; u64 bip32_max_index; - const tal_t *tmpctx = tal_tmpctx(cmd); + const tal_t *tmpctx = tal_tmpctx(NULL); if (!json_get_params(cmd, buffer, params, "?bip32_max_index", &bip32tok, NULL)) { + tal_free(tmpctx); return; } @@ -426,17 +427,21 @@ static void json_listaddrs(struct command *cmd, if(keyidx == BIP32_INITIAL_HARDENED_CHILD){ command_fail(cmd, "Keys exhausted "); + tal_free(tmpctx); + return; } if (bip32_key_from_parent(cmd->ld->wallet->bip32_base, keyidx, BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) { command_fail(cmd, "Keys generation failure"); + tal_free(tmpctx); return; } if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey.pubkey, ext.pub_key, sizeof(ext.pub_key))) { command_fail(cmd, "Key parsing failure"); + tal_free(tmpctx); return; } @@ -465,6 +470,7 @@ static void json_listaddrs(struct command *cmd, json_array_end(response); json_object_end(response); command_success(cmd, response); + tal_free(tmpctx); } static const struct json_command listaddrs_command = { From 5399e0653722439c1a44fa04b69f071fb4930724 Mon Sep 17 00:00:00 2001 From: Luca Vaccaro Date: Wed, 14 Feb 2018 14:29:35 +0100 Subject: [PATCH 4/4] Show bech32 & p2sh address --- wallet/walletrpc.c | 72 ++++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index c2966f8aa22c..3647f22dabd1 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -397,71 +397,87 @@ 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; - const tal_t *tmpctx = tal_tmpctx(cmd); + const tal_t *tmpctx = tal_tmpctx(NULL); - if (!json_get_params(cmd, buffer, params, - "?bip32_max_index", &bip32tok, NULL)) { - return; - } + if (!json_get_params(cmd, buffer, params, + "?bip32_max_index", &bip32tok, NULL)) { + tal_free(tmpctx); + 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); + bip32_max_index = db_get_intvar(cmd->ld->wallet->db, "bip32_max_index", 0); } json_object_start(response, NULL); json_array_start(response, "addresses"); - s64 keyidx; - for (keyidx = 0; keyidx < bip32_max_index; keyidx++) { - - struct ext_key ext; - struct sha256 h; - struct ripemd160 p2sh; - struct pubkey pubkey; - u8 *redeemscript; + for (s64 keyidx = 0; keyidx < bip32_max_index; keyidx++) { if(keyidx == BIP32_INITIAL_HARDENED_CHILD){ command_fail(cmd, "Keys exhausted "); + tal_free(tmpctx); + return; } if (bip32_key_from_parent(cmd->ld->wallet->bip32_base, keyidx, BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) { command_fail(cmd, "Keys generation failure"); + tal_free(tmpctx); return; } if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey.pubkey, ext.pub_key, sizeof(ext.pub_key))) { command_fail(cmd, "Key parsing failure"); + tal_free(tmpctx); return; } + // p2sh redeemscript = bitcoin_redeem_p2sh_p2wpkh(cmd, &pubkey); sha256(&h, redeemscript, tal_count(redeemscript)); - ripemd160(&p2sh, h.u.u8, sizeof(h)); + 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."); + tal_free(tmpctx); + return; + } + + // outputs json_object_start(response, NULL); json_add_u64(response, "keyidx", keyidx); - json_add_string(response, "address", - p2sh_to_base58(cmd, get_chainparams(cmd->ld)->testnet, - &p2sh)); + json_add_pubkey(response, "pubkey", &pubkey); + json_add_string(response, "p2sh", out_p2sh); + json_add_hex(response, "p2sh_redeemscript", redeemscript, sizeof(struct ripemd160) + 1); + json_add_string(response, "bech32", out_p2wpkh); + json_add_hex(response, "bech32_redeemscript", &h160.u.u8, sizeof(struct ripemd160)); - { - char *hex = pubkey_to_hexstr(tmpctx, &pubkey); - json_add_string(response, "pubkey", hex); - } - { - int len = sizeof(struct ripemd160) + 1; - char *hex = tal_arr(NULL, char, hex_str_size(len)); - hex_encode(redeemscript, len, hex, hex_str_size(len)); - json_add_string(response, "redeemscript", hex); - } json_object_end(response); } json_array_end(response); json_object_end(response); command_success(cmd, response); + tal_free(tmpctx); } static const struct json_command listaddrs_command = {