From 0ca6c3cc8445e780cca378049d231397b4ae70a6 Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 20 Oct 2020 20:05:16 -0500 Subject: [PATCH] script: break out redeemscript->scriptsig function We're going to finalze some redeemscripts here shortly, so break out the ability to render an arbitrary redeemscript -> scriptsig --- bitcoin/script.c | 20 ++++++++++++++++---- bitcoin/script.h | 4 ++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/bitcoin/script.c b/bitcoin/script.c index 0ba9dcdc3..d69751c24 100644 --- a/bitcoin/script.c +++ b/bitcoin/script.c @@ -261,18 +261,30 @@ u8 *bitcoin_redeem_p2sh_p2wpkh(const tal_t *ctx, const struct pubkey *key) return script; } -u8 *bitcoin_scriptsig_p2sh_p2wpkh(const tal_t *ctx, const struct pubkey *key) +u8 *bitcoin_scriptsig_redeem(const tal_t *ctx, + const u8 *redeemscript TAKES) { - u8 *redeemscript = bitcoin_redeem_p2sh_p2wpkh(ctx, key), *script; + u8 *script; /* BIP141: The scriptSig must be exactly a push of the BIP16 * redeemScript or validation fails. */ script = tal_arr(ctx, u8, 0); - script_push_bytes(&script, redeemscript, tal_count(redeemscript)); - tal_free(redeemscript); + script_push_bytes(&script, redeemscript, + tal_count(redeemscript)); + + if (taken(redeemscript)) + tal_free(redeemscript); + return script; } +u8 *bitcoin_scriptsig_p2sh_p2wpkh(const tal_t *ctx, const struct pubkey *key) +{ + u8 *redeemscript = + bitcoin_redeem_p2sh_p2wpkh(NULL, key); + return bitcoin_scriptsig_redeem(ctx, take(redeemscript)); +} + u8 **bitcoin_witness_p2wpkh(const tal_t *ctx, const struct bitcoin_signature *sig, const struct pubkey *key) diff --git a/bitcoin/script.h b/bitcoin/script.h index 30b4c3f6c..b424f45d7 100644 --- a/bitcoin/script.h +++ b/bitcoin/script.h @@ -43,6 +43,10 @@ u8 *bitcoin_redeem_p2pkh(const tal_t *ctx, const struct pubkey *pubkey, /* Create the redeemscript for a P2SH + P2WPKH. */ u8 *bitcoin_redeem_p2sh_p2wpkh(const tal_t *ctx, const struct pubkey *key); +/* Create the scriptsig for a redeemscript */ +u8 *bitcoin_scriptsig_redeem(const tal_t *ctx, + const u8 *redeemscript TAKES); + /* Create scriptsig for p2sh-p2wpkh */ u8 *bitcoin_scriptsig_p2sh_p2wpkh(const tal_t *ctx, const struct pubkey *key);