From 6966cf99e1c36f30a8fcd503d45ccbbec2a3a919 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 7 Aug 2020 10:51:33 +0930 Subject: [PATCH] bitcoin: add wally_tx_output helper to create standalone output. In preparation for when we don't have a tx. Signed-off-by: Rusty Russell --- bitcoin/tx.c | 38 ++++++++++++++++++++++++++------------ bitcoin/tx.h | 7 +++++++ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/bitcoin/tx.c b/bitcoin/tx.c index fb8bade65..fb47428e4 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -26,19 +26,12 @@ struct bitcoin_tx_output *new_tx_output(const tal_t *ctx, return output; } -int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script, - u8 *wscript, struct amount_sat amount) +struct wally_tx_output *wally_tx_output(const u8 *script, + struct amount_sat amount) { - size_t i = tx->wtx->num_outputs; + u64 satoshis = amount.satoshis; /* Raw: wally API */ struct wally_tx_output *output; - struct wally_psbt_output *psbt_out; int ret; - u64 satoshis = amount.satoshis; /* Raw: low-level helper */ - const struct chainparams *chainparams = tx->chainparams; - assert(i < tx->wtx->outputs_allocation_len); - - assert(tx->wtx != NULL); - assert(chainparams); if (chainparams->is_elements) { u8 value[9]; @@ -48,15 +41,36 @@ int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script, ret = wally_tx_elements_output_init_alloc( script, tal_bytelen(script), chainparams->fee_asset_tag, 33, value, sizeof(value), NULL, 0, NULL, 0, NULL, 0, &output); - assert(ret == WALLY_OK); + if (ret != WALLY_OK) + return NULL; + /* Cheat a bit by also setting the numeric satoshi value, * otherwise we end up converting a number of times */ output->satoshi = satoshis; } else { ret = wally_tx_output_init_alloc(satoshis, script, tal_bytelen(script), &output); - assert(ret == WALLY_OK); + if (ret != WALLY_OK) + return NULL; } + return output; +} + +int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script, + u8 *wscript, struct amount_sat amount) +{ + size_t i = tx->wtx->num_outputs; + struct wally_tx_output *output; + struct wally_psbt_output *psbt_out; + int ret; + const struct chainparams *chainparams = tx->chainparams; + assert(i < tx->wtx->outputs_allocation_len); + + assert(tx->wtx != NULL); + assert(chainparams); + + output = wally_tx_output(script, amount); + assert(output); ret = wally_tx_add_output(tx->wtx, output); assert(ret == WALLY_OK); diff --git a/bitcoin/tx.h b/bitcoin/tx.h index eae2c47b2..3a56dc0e6 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -79,6 +79,13 @@ struct bitcoin_tx *bitcoin_tx_with_psbt(const tal_t *ctx, struct wally_psbt *psb /* Internal de-linearization functions. */ struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx, const u8 **cursor, size_t *max); + +/* Helper to create a wally_tx_output: make sure to wally_tx_output_free! + * Returns NULL if amount is extreme (wally doesn't like). + */ +struct wally_tx_output *wally_tx_output(const u8 *script, + struct amount_sat amount); + /* Add one output to tx. */ int bitcoin_tx_add_output(struct bitcoin_tx *tx, const u8 *script, u8 *wscript,