Skip to content

Instantly share code, notes, and snippets.

@rustyrussell
Created August 6, 2019 04:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rustyrussell/d7446e9f5bfafe1beb8184fe1f08fd13 to your computer and use it in GitHub Desktop.
Save rustyrussell/d7446e9f5bfafe1beb8184fe1f08fd13 to your computer and use it in GitHub Desktop.
#include "../lightning/external/libwally-core/include/wally_address.h"
#include "../lightning/external/libwally-core/include/wally_bip32.h"
#include "../lightning/external/libwally-core/src/secp256k1/include/secp256k1.h"
#include "../lightning/common/bech32.c"
#include <ccan/crypto/sha256/sha256.h>
#include <ccan/crypto/ripemd160/ripemd160.h>
#include <ccan/str/hex/hex.h>
#include <err.h>
#include <assert.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
unsigned char *seed;
size_t seed_len;
struct ext_key master_extkey, child_extkey, child2_extkey;
seed_len = hex_data_size(strlen(argv[1]));
seed = malloc(seed_len);
if (!hex_decode(argv[1], strlen(argv[1]), seed, seed_len))
errx(1, "Expected hex");
/* Generate BIP32 children m/0/0/i */
if (bip32_key_from_seed(seed, seed_len,
BIP32_VER_TEST_PRIVATE,
0, &master_extkey) != WALLY_OK)
errx(1, "bip32_key_from_seed failed");
if (bip32_key_from_parent(&master_extkey, 0, BIP32_FLAG_KEY_PRIVATE,
&child_extkey) != WALLY_OK)
errx(1, "bip32_key_from_parent/0 failed");
if (bip32_key_from_parent(&child_extkey, 0, BIP32_FLAG_KEY_PRIVATE,
&child2_extkey) != WALLY_OK)
errx(1, "bip32_key_from_parent/0/0 failed");
for (uint32_t i = 0; i < 10; i++) {
secp256k1_pubkey pubkey;
char *wif;
struct ripemd160 r160;
unsigned char key[33];
struct sha256 s256;
char swaddr[73 + strlen("bcrt")];
struct ext_key ext;
unsigned char priv_key[32];
char privkey_hex[65];
char pubkey_hex[67];
if (bip32_key_from_parent(&child2_extkey, i,
BIP32_FLAG_KEY_PRIVATE, &ext)
!= WALLY_OK)
errx(1, "bip32_key_from_parent/0/0/%u failed", i);
/* libwally says: The private key with prefix byte 0. */
memcpy(priv_key, ext.priv_key+1, sizeof(priv_key));
if (!hex_encode(priv_key, sizeof(priv_key),
privkey_hex, sizeof(privkey_hex)))
errx(1, "Cannot hex-encode privkey");
if (wally_wif_from_bytes(priv_key, sizeof(priv_key), 0xef,
WALLY_WIF_FLAG_COMPRESSED,
&wif) != WALLY_OK)
errx(1, "Cannot encode privkey seed/0/0/%u", i);
if (secp256k1_ec_pubkey_create(wally_get_secp_context(),
&pubkey, priv_key) != 1)
errx(1, "Cannot derive pubkey");
size_t outlen = 33;
if (!secp256k1_ec_pubkey_serialize(wally_get_secp_context(),
key, &outlen, &pubkey,
SECP256K1_EC_COMPRESSED))
errx(1, "Cannot serialize pubkey");
if (!hex_encode(key, outlen, pubkey_hex, sizeof(pubkey_hex)))
errx(1, "Cannot hex-encode pubkey");
printf("pubkey 0/0/%u: %s\n", i, pubkey_hex);
printf("privkey 0/0/%u: %s\n", i, privkey_hex);
printf("WIF 0/0/%u: %s\n", i, wif);
assert(outlen == 33);
sha256(&s256, key, sizeof(key));
ripemd160(&r160, s256.u.u8, sizeof(s256.u.u8));
if (segwit_addr_encode(swaddr, "bcrt", 0, r160.u.u8, sizeof(r160.u.u8)) != 1)
errx(1, "Cannot make segwit address");
printf("P2WPKH 0/0/%u: %s\n\n", i, swaddr);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment