From 16f72cb1604d919c40eff5c7de6f66cc54fe5a9d Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 5 Mar 2019 14:28:29 +0100 Subject: [PATCH] wally: Migrate version and locktime to libwally tx Signed-off-by: Christian Decker --- bitcoin/tx.c | 18 +++++++++--------- bitcoin/tx.h | 4 ++-- channeld/commit_tx.c | 4 ++-- common/htlc_tx.c | 4 ++-- common/initial_commit_tx.c | 4 ++-- onchaind/onchaind.c | 4 ++-- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/bitcoin/tx.c b/bitcoin/tx.c index 6f1099ed5..a5d5fa336 100644 --- a/bitcoin/tx.c +++ b/bitcoin/tx.c @@ -98,7 +98,7 @@ static void push_tx(const struct bitcoin_tx *tx, varint_t i; u8 flag = 0; - push_le32(tx->version, push, pushp); + push_le32(tx->wtx->version, push, pushp); if (bip144 && uses_witness(tx)) flag |= SEGREGATED_WITNESS_FLAG; @@ -133,7 +133,7 @@ static void push_tx(const struct bitcoin_tx *tx, if (flag & SEGREGATED_WITNESS_FLAG) push_witnesses(tx, push, pushp); - push_le32(tx->lock_time, push, pushp); + push_le32(tx->wtx->locktime, push, pushp); } static void push_sha(const void *data, size_t len, void *shactx_) @@ -224,7 +224,7 @@ static void hash_for_segwit(struct sha256_ctx *ctx, * Double SHA256 of the serialization of: * 1. nVersion of the transaction (4-byte little endian) */ - push_le32(tx->version, push_sha, ctx); + push_le32(tx->wtx->version, push_sha, ctx); /* 2. hashPrevouts (32-byte hash) */ hash_prevouts(&h, tx, sighash_type); @@ -253,7 +253,7 @@ static void hash_for_segwit(struct sha256_ctx *ctx, push_sha(&h, sizeof(h), ctx); /* 9. nLocktime of the transaction (4-byte little endian) */ - push_le32(tx->lock_time, push_sha, ctx); + push_le32(tx->wtx->locktime, push_sha, ctx); } void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx, @@ -349,12 +349,12 @@ struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count, for (i = 0; i < tal_count(tx->input); i++) { /* We assume NULL is a zero bitmap */ assert(tx->input[i].script == NULL); - tx->input[i].sequence_number = 0xFFFFFFFF; + tx->input[i].sequence_number = BITCOIN_TX_DEFAULT_SEQUENCE; tx->input[i].amount = NULL; tx->input[i].witness = NULL; } - tx->lock_time = 0; - tx->version = 2; + tx->wtx->locktime = 0; + tx->wtx->version = 2; return tx; } @@ -457,7 +457,7 @@ struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx, const u8 **cursor, } tal_add_destructor(tx, bitcoin_tx_destroy); - tx->version = pull_le32(cursor, max); + assert(pull_le32(cursor, max) == tx->wtx->version); count = pull_length(cursor, max, 32 + 4 + 4 + 1); /* BIP 144 marker is 0 (impossible to have tx with 0 inputs) */ if (count == 0) { @@ -483,7 +483,7 @@ struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx, const u8 **cursor, for (i = 0; i < tal_count(tx->input); i++) tx->input[i].witness = NULL; } - tx->lock_time = pull_le32(cursor, max); + assert(pull_le32(cursor, max) == tx->wtx->locktime); /* If we ran short, fail. */ if (!*cursor) diff --git a/bitcoin/tx.h b/bitcoin/tx.h index b44e14355..9f1a772dd 100644 --- a/bitcoin/tx.h +++ b/bitcoin/tx.h @@ -10,6 +10,8 @@ #include #include +#define BITCOIN_TX_DEFAULT_SEQUENCE 0xFFFFFFFF + struct bitcoin_txid { struct sha256_double shad; }; @@ -17,10 +19,8 @@ struct bitcoin_txid { STRUCTEQ_DEF(bitcoin_txid, 0, shad.sha.u); struct bitcoin_tx { - u32 version; struct bitcoin_tx_input *input; struct bitcoin_tx_output *output; - u32 lock_time; struct wally_tx *wtx; }; diff --git a/channeld/commit_tx.c b/channeld/commit_tx.c index 30a50a01c..7fd114149 100644 --- a/channeld/commit_tx.c +++ b/channeld/commit_tx.c @@ -280,13 +280,13 @@ struct bitcoin_tx *commit_tx(const tal_t *ctx, * * * version: 2 */ - assert(tx->version == 2); + assert(tx->wtx->version == 2); /* BOLT #3: * * * locktime: upper 8 bits are 0x20, lower 24 bits are the lower 24 bits of the obscured commitment number */ - tx->lock_time + tx->wtx->locktime = (0x20000000 | (obscured_commitment_number & 0xFFFFFF)); /* BOLT #3: diff --git a/common/htlc_tx.c b/common/htlc_tx.c index 9ed0a504c..e5d93a4b9 100644 --- a/common/htlc_tx.c +++ b/common/htlc_tx.c @@ -31,12 +31,12 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx, /* BOLT #3: * * version: 2 */ - assert(tx->version == 2); + assert(tx->wtx->version == 2); /* BOLT #3: * * locktime: `0` for HTLC-success, `cltv_expiry` for HTLC-timeout */ - tx->lock_time = locktime; + tx->wtx->locktime = locktime; /* BOLT #3: * * txin count: 1 diff --git a/common/initial_commit_tx.c b/common/initial_commit_tx.c index 6a66536a8..78639b4b2 100644 --- a/common/initial_commit_tx.c +++ b/common/initial_commit_tx.c @@ -207,14 +207,14 @@ struct bitcoin_tx *initial_commit_tx(const tal_t *ctx, * * * version: 2 */ - assert(tx->version == 2); + assert(tx->wtx->version == 2); /* BOLT #3: * * * locktime: upper 8 bits are 0x20, lower 24 bits are the * lower 24 bits of the obscured commitment number */ - tx->lock_time + tx->wtx->locktime = (0x20000000 | (obscured_commitment_number & 0xFFFFFF)); /* BOLT #3: diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index 3fc432d8a..c47fd7599 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -298,7 +298,7 @@ static struct bitcoin_tx *tx_to_us(const tal_t *ctx, u8 *msg; tx = bitcoin_tx(ctx, 1, 1); - tx->lock_time = locktime; + tx->wtx->locktime = locktime; tx->input[0].sequence_number = to_self_delay; tx->input[0].txid = out->txid; tx->input[0].index = out->outnum; @@ -647,7 +647,7 @@ static u64 unmask_commit_number(const struct bitcoin_tx *tx, *... * * `txin[0]` sequence: upper 8 bits are 0x80, lower 24 bits are upper 24 bits of the obscured commitment number */ - return ((tx->lock_time & 0x00FFFFFF) + return ((tx->wtx->locktime & 0x00FFFFFF) | (tx->input[0].sequence_number & (u64)0x00FFFFFF) << 24) ^ obscurer; }