From 639713b547d759a313fef7a28c111a3b63010a54 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Sat, 13 Apr 2019 19:14:07 +0200 Subject: [PATCH] elements: Fix transaction handling for elements transactions Skipping coinbase transactions and ensuring that the transaction is serialized correctly when sending it onwards. Signed-off-by: Christian Decker --- bitcoin/tx.c | 8 +++++++- lightningd/chaintopology.c | 3 +++ wallet/txfilter.c | 3 +++ wallet/wallet.c | 8 +++++--- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/bitcoin/tx.c b/bitcoin/tx.c index fb2a84942..10eff9fb3 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -97,6 +97,9 @@ const u8 *bitcoin_tx_output_get_script(const tal_t *ctx, u8 *res; assert(outnum < tx->wtx->num_outputs); output = &tx->wtx->outputs[outnum]; + if (output->features & WALLY_TX_IS_COINBASE) + return NULL; + res = tal_arr(ctx, u8, output->script_len); memcpy(res, output->script, output->script_len); return res; @@ -220,7 +223,10 @@ static void push_tx(const struct bitcoin_tx *tx, if (bip144 && uses_witness(tx)) flag |= WALLY_TX_FLAG_USE_WITNESS; - res = wally_tx_get_length(tx->wtx, flag, &len); + if (is_elements) + flag |= WALLY_TX_FLAG_USE_ELEMENTS; + + res = wally_tx_get_length(tx->wtx, flag & WALLY_TX_FLAG_USE_WITNESS, &len); assert(res == WALLY_OK); serialized = tal_arr(tmpctx, u8, len); diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index ded3bccf0..4838243c2 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -654,6 +654,9 @@ static void topo_add_utxos(struct chain_topology *topo, struct block *b) for (size_t i = 0; i < tal_count(b->full_txs); i++) { const struct bitcoin_tx *tx = b->full_txs[i]; for (size_t j = 0; j < tx->wtx->num_outputs; j++) { + if (tx->wtx->outputs[j].features & WALLY_TX_IS_COINBASE) + continue; + const u8 *script = bitcoin_tx_output_get_script(tmpctx, tx, j); struct amount_sat amt = bitcoin_tx_output_get_amount(tx, j); diff --git a/wallet/txfilter.c b/wallet/txfilter.c index d9e66a7e7..8eae0c9b9 100644 --- a/wallet/txfilter.c +++ b/wallet/txfilter.c @@ -103,6 +103,9 @@ bool txfilter_match(const struct txfilter *filter, const struct bitcoin_tx *tx) for (size_t i = 0; i < tx->wtx->num_outputs; i++) { const u8 *oscript = bitcoin_tx_output_get_script(tmpctx, tx, i); + if (!oscript) + continue; + if (scriptpubkeyset_get(&filter->scriptpubkeyset, oscript)) return true; } diff --git a/wallet/wallet.c b/wallet/wallet.c index eb95f35a4..befd162c7 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1498,11 +1498,13 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct bitcoin_tx *tx, struct utxo *utxo; u32 index; bool is_p2sh; - const u8 *script = bitcoin_tx_output_get_script(tmpctx, tx, output); + const u8 *script; + script = bitcoin_tx_output_get_script(tmpctx, tx, output); + if (!script) + continue; - if (!wallet_can_spend(w, script, &index, - &is_p2sh)) + if (!wallet_can_spend(w, script, &index, &is_p2sh)) continue; utxo = tal(w, struct utxo);