From 0388fe6db4d21c060cf2834e8b427ed8e283b86f Mon Sep 17 00:00:00 2001 From: niftynei Date: Tue, 16 Jun 2020 13:40:13 -0500 Subject: [PATCH] tx: add helper for extracting script from a wally_tx the bitcoin_tx version is basically a wrapper for the wally_tx script extraction -- here we pull it apart so we can easily get a tal'd script for a wally_tx_output --- bitcoin/tx.c | 22 +++++++++++++--------- bitcoin/tx.h | 9 +++++++++ 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/bitcoin/tx.c b/bitcoin/tx.c index ca0a15973..57cc593fd 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -265,22 +265,26 @@ void bitcoin_tx_output_set_amount(struct bitcoin_tx *tx, int outnum, } } -const u8 *bitcoin_tx_output_get_script(const tal_t *ctx, - const struct bitcoin_tx *tx, int outnum) +const u8 *wally_tx_output_get_script(const tal_t *ctx, + const struct wally_tx_output *output) { - const struct wally_tx_output *output; - u8 *res; - assert(outnum < tx->wtx->num_outputs); - output = &tx->wtx->outputs[outnum]; - if (output->script == NULL) { /* This can happen for coinbase transactions and pegin * transactions */ return NULL; } - res = tal_dup_arr(ctx, u8, output->script, output->script_len, 0); - return res; + return tal_dup_arr(ctx, u8, output->script, output->script_len, 0); +} + +const u8 *bitcoin_tx_output_get_script(const tal_t *ctx, + const struct bitcoin_tx *tx, int outnum) +{ + const struct wally_tx_output *output; + assert(outnum < tx->wtx->num_outputs); + output = &tx->wtx->outputs[outnum]; + + return wally_tx_output_get_script(ctx, output); } u8 *bitcoin_tx_output_get_witscript(const tal_t *ctx, const struct bitcoin_tx *tx, diff --git a/bitcoin/tx.h b/bitcoin/tx.h index 982f6ff22..bdc2928e0 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -126,6 +126,15 @@ void bitcoin_tx_output_set_amount(struct bitcoin_tx *tx, int outnum, */ const u8 *bitcoin_tx_output_get_script(const tal_t *ctx, const struct bitcoin_tx *tx, int outnum); +/** + * Helper to get the script of a script's output as a tal_arr + * + * The script attached to a `wally_tx_output` is not a `tal_arr`, so in order to keep the + * comfort of being able to call `tal_bytelen` and similar on a script we just + * return a `tal_arr` clone of the original script. + */ +const u8 *wally_tx_output_get_script(const tal_t *ctx, + const struct wally_tx_output *output); /** * Helper to get a witness script for an output. */