From de39752d0588c503d6db795dad331c8fd775b6fe Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 7 Mar 2017 16:12:18 +1030 Subject: [PATCH] bitcoin/script: add internal hash160_key helper. Signed-off-by: Rusty Russell --- bitcoin/script.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/bitcoin/script.c b/bitcoin/script.c index c0ce4dbc7..f00ac32af 100644 --- a/bitcoin/script.c +++ b/bitcoin/script.c @@ -47,6 +47,13 @@ static void hash160(struct ripemd160 *redeemhash, const void *mem, size_t len) ripemd160(redeemhash, h.u.u8, sizeof(h)); } +static void hash160_key(struct ripemd160 *khash, const struct pubkey *key) +{ + u8 der[PUBKEY_DER_LEN]; + pubkey_to_der(der, key); + hash160(khash, der, sizeof(der)); +} + static void add(u8 **scriptp, const void *mem, size_t len) { size_t oldlen = tal_count(*scriptp); @@ -218,11 +225,9 @@ u8 *scriptpubkey_p2sh(const tal_t *ctx, const u8 *redeemscript) u8 *scriptpubkey_p2pkh(const tal_t *ctx, const struct pubkey *pubkey) { struct bitcoin_address addr; - u8 der[PUBKEY_DER_LEN]; u8 *script = tal_arr(ctx, u8, 0); - pubkey_to_der(der, pubkey); - hash160(&addr.addr, der, sizeof(der)); + hash160_key(&addr.addr, pubkey); add_op(&script, OP_DUP); add_op(&script, OP_HASH160); add_push_bytes(&script, &addr.addr, sizeof(addr.addr)); @@ -247,14 +252,12 @@ u8 *bitcoin_redeem_p2pkh(const tal_t *ctx, const struct pubkey *pubkey, u8 *bitcoin_redeem_p2sh_p2wpkh(const tal_t *ctx, const struct pubkey *key) { struct ripemd160 keyhash; - u8 der[PUBKEY_DER_LEN]; u8 *script = tal_arr(ctx, u8, 0); /* BIP141: BIP16 redeemScript pushed in the scriptSig is exactly a * push of a version byte plus a push of a witness program. */ add_number(&script, 0); - pubkey_to_der(der, key); - hash160(&keyhash, der, sizeof(der)); + hash160_key(&keyhash, key); add_push_bytes(&script, &keyhash, sizeof(keyhash)); return script; } @@ -319,12 +322,10 @@ u8 *scriptpubkey_p2wsh(const tal_t *ctx, const u8 *witnessscript) u8 *scriptpubkey_p2wpkh(const tal_t *ctx, const struct pubkey *key) { struct ripemd160 h; - u8 der[PUBKEY_DER_LEN]; u8 *script = tal_arr(ctx, u8, 0); add_op(&script, OP_0); - pubkey_to_der(der, key); - hash160(&h, der, sizeof(der)); + hash160_key(&h, key); add_push_bytes(&script, &h, sizeof(h)); return script; } @@ -479,14 +480,10 @@ u8 *bitcoin_redeem_htlc_recv(const tal_t *ctx, /* Create scriptcode (fake witness, basically) for P2WPKH */ u8 *p2wpkh_scriptcode(const tal_t *ctx, const struct pubkey *key) { - struct sha256 h; struct ripemd160 pkhash; - u8 der[PUBKEY_DER_LEN]; u8 *script = tal_arr(ctx, u8, 0); - pubkey_to_der(der, key); - sha256(&h, der, sizeof(der)); - ripemd160(&pkhash, h.u.u8, sizeof(h)); + hash160_key(&pkhash, key); /* BIP143: * * For P2WPKH witness program, the scriptCode is