From 58b14292ad61ce816052a56408ce6c834ab64a77 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 11 Apr 2016 16:43:53 +0930 Subject: [PATCH] bitcoin/tx: (optional) input amount. We need this for signing segwitness txs. Unfortunately, we don't have it for transactions we received as hex, only ones we created; to make this safe we use a pointer which is NULL if we don't know, and those will crash if we try to sign or check their sigs. Signed-off-by: Rusty Russell --- bitcoin/tx.c | 1 + bitcoin/tx.h | 3 +++ close_tx.c | 1 + commit_tx.c | 1 + daemon/peer.c | 2 ++ 5 files changed, 8 insertions(+) diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 9717ac855..1daf853ca 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -226,6 +226,7 @@ struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count, /* We assume NULL is a zero bitmap */ assert(tx->input[i].script == NULL); tx->input[i].sequence_number = 0xFFFFFFFF; + tx->input[i].amount = NULL; tx->input[i].witness = NULL; } tx->lock_time = 0; diff --git a/bitcoin/tx.h b/bitcoin/tx.h index 2051c167a..75afaf9b5 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -31,6 +31,9 @@ struct bitcoin_tx_input { u8 *script; u32 sequence_number; + /* Value of the output we're spending (NULL if unknown). */ + u64 *amount; + /* Only if BIP141 used. */ u8 **witness; }; diff --git a/close_tx.c b/close_tx.c index ff8b5b72a..f20b3c34a 100644 --- a/close_tx.c +++ b/close_tx.c @@ -24,6 +24,7 @@ struct bitcoin_tx *create_close_tx(secp256k1_context *secpctx, /* Our input spends the anchor tx output. */ tx->input[0].txid = *anchor_txid; tx->input[0].index = anchor_index; + tx->input[0].amount = tal_dup(tx->input, u64, &anchor_satoshis); /* One output is to us. */ tx->output[0].amount = to_us; diff --git a/commit_tx.c b/commit_tx.c index f46e9544e..7adcd9994 100644 --- a/commit_tx.c +++ b/commit_tx.c @@ -56,6 +56,7 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx, /* Our input spends the anchor tx output. */ tx->input[0].txid = *anchor_txid; tx->input[0].index = anchor_index; + tx->input[0].amount = tal_dup(tx->input, u64, &anchor_satoshis); /* First output is a P2SH to a complex redeem script (usu. for me) */ redeemscript = bitcoin_redeem_secret_or_delay(tx, our_final, diff --git a/daemon/peer.c b/daemon/peer.c index 1351b59b6..7bae33d41 100644 --- a/daemon/peer.c +++ b/daemon/peer.c @@ -1125,6 +1125,8 @@ const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer) p2sh_out = find_p2sh_out(commit, redeemscript); tx->input[0].index = p2sh_out; tx->input[0].sequence_number = bitcoin_nsequence(&peer->them.locktime); + tx->input[0].amount = tal_dup(tx->input, u64, + &commit->output[p2sh_out].amount); tx->output[0].amount = commit->output[p2sh_out].amount; tx->output[0].script = scriptpubkey_p2sh(tx,