From 15e4e922c91bf36f23a2803a92132937798e33de Mon Sep 17 00:00:00 2001 From: lisa neigut Date: Wed, 1 Apr 2020 21:17:49 -0500 Subject: [PATCH] wire: serialize the amounts for a bitcoin tx over the wire we'll need this for calculating fees etc in onchaind --- wire/fromwire.c | 19 ++++++++++++++++++- wire/towire.c | 17 +++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/wire/fromwire.c b/wire/fromwire.c index e18075749..2938bcc0f 100644 --- a/wire/fromwire.c +++ b/wire/fromwire.c @@ -368,7 +368,24 @@ void derive_channel_id(struct channel_id *channel_id, struct bitcoin_tx *fromwire_bitcoin_tx(const tal_t *ctx, const u8 **cursor, size_t *max) { - return pull_bitcoin_tx(ctx, cursor, max); + struct bitcoin_tx *tx; + u16 input_amts_len; + size_t i; + + tx = pull_bitcoin_tx(ctx, cursor, max); + input_amts_len = fromwire_u16(cursor, max); + /* We don't serialize the amounts if they're not *all* populated */ + if (input_amts_len != tal_count(tx->input_amounts)) + return tx; + + for (i = 0; i < input_amts_len; i++) { + struct amount_sat sat; + sat = fromwire_amount_sat(cursor, max); + tx->input_amounts[i] = + tal_dup(tx, struct amount_sat, &sat); + } + + return tx; } void fromwire_siphash_seed(const u8 **cursor, size_t *max, diff --git a/wire/towire.c b/wire/towire.c index a35073c78..889aae8c4 100644 --- a/wire/towire.c +++ b/wire/towire.c @@ -226,8 +226,25 @@ void towire_wirestring(u8 **pptr, const char *str) void towire_bitcoin_tx(u8 **pptr, const struct bitcoin_tx *tx) { + size_t i; u8 *lin = linearize_tx(tmpctx, tx); towire_u8_array(pptr, lin, tal_count(lin)); + + /* We only want to 'save' the amounts if every amount + * has been populated */ + for (i = 0; i < tal_count(tx->input_amounts); i++) { + if (!tx->input_amounts[i]) { + towire_u16(pptr, 0); + return; + } + } + + /* Otherwise, we include the input amount set */ + towire_u16(pptr, tal_count(tx->input_amounts)); + for (i = 0; i < tal_count(tx->input_amounts); i++) { + assert(tx->input_amounts[i]); + towire_amount_sat(pptr, *tx->input_amounts[i]); + } } void towire_siphash_seed(u8 **pptr, const struct siphash_seed *seed)