From f9edbcb4ec2a7c6a4e5951b9570fbd01f2c1ae7f Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 26 Oct 2017 13:25:19 +1030 Subject: [PATCH] script: add p2sh scriptpubkey helper to create from hash. Signed-off-by: Rusty Russell --- bitcoin/script.c | 16 +++++++++++----- bitcoin/script.h | 3 +++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/bitcoin/script.c b/bitcoin/script.c index 397aab536..675340471 100644 --- a/bitcoin/script.c +++ b/bitcoin/script.c @@ -182,17 +182,23 @@ u8 *bitcoin_redeem_2of2(const tal_t *ctx, return script; } +u8 *scriptpubkey_p2sh_hash(const tal_t *ctx, const struct ripemd160 *redeemhash) +{ + u8 *script = tal_arr(ctx, u8, 0); + + add_op(&script, OP_HASH160); + add_push_bytes(&script, redeemhash->u.u8, sizeof(redeemhash->u.u8)); + add_op(&script, OP_EQUAL); + return script; +} + /* Create p2sh for this redeem script. */ u8 *scriptpubkey_p2sh(const tal_t *ctx, const u8 *redeemscript) { struct ripemd160 redeemhash; - u8 *script = tal_arr(ctx, u8, 0); - add_op(&script, OP_HASH160); hash160(&redeemhash, redeemscript, tal_count(redeemscript)); - add_push_bytes(&script, redeemhash.u.u8, sizeof(redeemhash.u.u8)); - add_op(&script, OP_EQUAL); - return script; + return scriptpubkey_p2sh_hash(ctx, &redeemhash); } /* Create an output script using p2pkh */ diff --git a/bitcoin/script.h b/bitcoin/script.h index 8a11783d8..5752bc9f6 100644 --- a/bitcoin/script.h +++ b/bitcoin/script.h @@ -23,6 +23,9 @@ u8 *bitcoin_redeem_2of2(const tal_t *ctx, /* Create an output script using p2sh for this redeem script. */ u8 *scriptpubkey_p2sh(const tal_t *ctx, const u8 *redeemscript); +/* Create an output script using p2sh for this hash. */ +u8 *scriptpubkey_p2sh_hash(const tal_t *ctx, const struct ripemd160 *redeemhash); + /* Create an output script using p2pkh */ u8 *scriptpubkey_p2pkh(const tal_t *ctx, const struct bitcoin_address *addr);