Skip to content

Commit

Permalink
bitcoin: create new wrapper type bitcoin_txid, log backward endianness.
Browse files Browse the repository at this point in the history
It's just a sha256_double, but importantly when we convert it to a
string (in type_to_string, which is used in logging) we use
bitcoin_txid_to_hex() so it's reversed as people expect.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Dec 21, 2017
1 parent ed2158c commit 0237e0b
Show file tree
Hide file tree
Showing 46 changed files with 181 additions and 171 deletions.
10 changes: 8 additions & 2 deletions bitcoin/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,17 @@ struct bitcoin_block *bitcoin_block_from_hex(const tal_t *ctx,
bool bitcoin_blkid_from_hex(const char *hexstr, size_t hexstr_len,
struct sha256_double *blockid)
{
return bitcoin_txid_from_hex(hexstr, hexstr_len, blockid);
struct bitcoin_txid fake_txid;
if (!bitcoin_txid_from_hex(hexstr, hexstr_len, &fake_txid))
return false;
*blockid = fake_txid.shad;
return true;
}

bool bitcoin_blkid_to_hex(const struct sha256_double *blockid,
char *hexstr, size_t hexstr_len)
{
return bitcoin_txid_to_hex(blockid, hexstr, hexstr_len);
struct bitcoin_txid fake_txid;
fake_txid.shad = *blockid;
return bitcoin_txid_to_hex(&fake_txid, hexstr, hexstr_len);
}
3 changes: 2 additions & 1 deletion bitcoin/test/run-tx-encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ int main(void)
assert(tal_count(tx->input) == 1);
assert(tal_count(tx->output) == 1);

reverse_bytes(tx->input[0].txid.sha.u.u8, sizeof(tx->input[0].txid));
reverse_bytes(tx->input[0].txid.shad.sha.u.u8,
sizeof(tx->input[0].txid));
hexeq(&tx->input[0].txid, sizeof(tx->input[0].txid),
"1db36e1306dfc810ea63f0cf866d475cce4e9261a5e8d1581f0d1dc485f4beb5");
assert(tx->input[0].index == 0);
Expand Down
27 changes: 17 additions & 10 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,13 @@ size_t measure_tx_cost(const struct bitcoin_tx *tx)
return non_witness_len * 4 + witness_len;
}

void bitcoin_txid(const struct bitcoin_tx *tx, struct sha256_double *txid)
void bitcoin_txid(const struct bitcoin_tx *tx, struct bitcoin_txid *txid)
{
struct sha256_ctx ctx = SHA256_INIT;

/* For TXID, we never use extended form. */
push_tx(tx, push_sha, &ctx, false);
sha256_double_done(&ctx, txid);
sha256_double_done(&ctx, &txid->shad);
}

struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count,
Expand Down Expand Up @@ -336,7 +336,7 @@ static void pull_input(const tal_t *ctx, const u8 **cursor, size_t *max,
struct bitcoin_tx_input *input)
{
u64 script_len;
pull_sha256_double(cursor, max, &input->txid);
pull_sha256_double(cursor, max, &input->txid.shad);
input->index = pull_le32(cursor, max);
script_len = pull_length(cursor, max);
if (script_len)
Expand Down Expand Up @@ -460,9 +460,7 @@ struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex,
return NULL;
}

/* <sigh>. Bitcoind represents hashes as little-endian for RPC. This didn't
* stick for blockids (everyone else uses big-endian, eg. block explorers),
* but it did stick for txids. */
/* <sigh>. Bitcoind represents hashes as little-endian for RPC. */
static void reverse_bytes(u8 *arr, size_t len)
{
unsigned int i;
Expand All @@ -475,18 +473,18 @@ static void reverse_bytes(u8 *arr, size_t len)
}

bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len,
struct sha256_double *txid)
struct bitcoin_txid *txid)
{
if (!hex_decode(hexstr, hexstr_len, txid, sizeof(*txid)))
return false;
reverse_bytes(txid->sha.u.u8, sizeof(txid->sha.u.u8));
reverse_bytes(txid->shad.sha.u.u8, sizeof(txid->shad.sha.u.u8));
return true;
}

bool bitcoin_txid_to_hex(const struct sha256_double *txid,
bool bitcoin_txid_to_hex(const struct bitcoin_txid *txid,
char *hexstr, size_t hexstr_len)
{
struct sha256_double rev = *txid;
struct sha256_double rev = txid->shad;
reverse_bytes(rev.sha.u.u8, sizeof(rev.sha.u.u8));
return hex_encode(&rev, sizeof(rev), hexstr, hexstr_len);
}
Expand All @@ -499,4 +497,13 @@ static char *fmt_bitcoin_tx(const tal_t *ctx, const struct bitcoin_tx *tx)
return s;
}

static char *fmt_bitcoin_txid(const tal_t *ctx, const struct bitcoin_txid *txid)
{
char *hexstr = tal_arr(ctx, char, hex_str_size(sizeof(*txid)));

bitcoin_txid_to_hex(txid, hexstr, hex_str_size(sizeof(*txid)));
return hexstr;
}

REGISTER_TYPE_TO_STRING(bitcoin_tx, fmt_bitcoin_tx);
REGISTER_TYPE_TO_STRING(bitcoin_txid, fmt_bitcoin_txid);
12 changes: 8 additions & 4 deletions bitcoin/tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>

struct bitcoin_txid {
struct sha256_double shad;
};

struct bitcoin_tx {
u32 version;
struct bitcoin_tx_input *input;
Expand All @@ -20,7 +24,7 @@ struct bitcoin_tx_output {
};

struct bitcoin_tx_input {
struct sha256_double txid;
struct bitcoin_txid txid;
u32 index; /* output number referred to by above */
u8 *script;
u32 sequence_number;
Expand All @@ -34,7 +38,7 @@ struct bitcoin_tx_input {


/* SHA256^2 the tx: simpler than sha256_tx */
void bitcoin_txid(const struct bitcoin_tx *tx, struct sha256_double *txid);
void bitcoin_txid(const struct bitcoin_tx *tx, struct bitcoin_txid *txid);

/* Useful for signature code. */
void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx,
Expand All @@ -57,10 +61,10 @@ struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex,

/* Parse hex string to get txid (reversed, a-la bitcoind). */
bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len,
struct sha256_double *txid);
struct bitcoin_txid *txid);

/* Get hex string of txid (reversed, a-la bitcoind). */
bool bitcoin_txid_to_hex(const struct sha256_double *txid,
bool bitcoin_txid_to_hex(const struct bitcoin_txid *txid,
char *hexstr, size_t hexstr_len);

/* Internal de-linearization functions. */
Expand Down
2 changes: 1 addition & 1 deletion channeld/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -2351,7 +2351,7 @@ static void init_channel(struct peer *peer)
u16 funding_txout;
u64 local_msatoshi;
struct pubkey funding_pubkey[NUM_SIDES];
struct sha256_double funding_txid;
struct bitcoin_txid funding_txid;
enum side funder;
enum htlc_state *hstates;
struct fulfilled_htlc *fulfilled;
Expand Down
2 changes: 1 addition & 1 deletion channeld/channel_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ channel_normal_operation,11001
# Begin! (passes gossipd-client fd)
channel_init,1000
channel_init,,chain_hash,struct sha256_double
channel_init,,funding_txid,struct sha256_double
channel_init,,funding_txid,struct bitcoin_txid
channel_init,,funding_txout,u16
channel_init,,funding_satoshi,u64
channel_init,,our_config,struct channel_config
Expand Down
2 changes: 1 addition & 1 deletion channeld/commit_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ static void add_received_htlc_out(struct bitcoin_tx *tx, size_t n,
}

struct bitcoin_tx *commit_tx(const tal_t *ctx,
const struct sha256_double *funding_txid,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
u64 funding_satoshis,
enum side funder,
Expand Down
3 changes: 1 addition & 2 deletions channeld/commit_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <common/initial_commit_tx.h>

struct keyset;
struct sha256_double;

/**
* commit_tx_num_untrimmed: how many of these htlc outputs will commit tx have?
Expand Down Expand Up @@ -43,7 +42,7 @@ size_t commit_tx_num_untrimmed(const struct htlc **htlcs,
* transaction, so we carefully use the terms "self" and "other" here.
*/
struct bitcoin_tx *commit_tx(const tal_t *ctx,
const struct sha256_double *funding_txid,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
u64 funding_satoshis,
enum side funder,
Expand Down
4 changes: 2 additions & 2 deletions channeld/full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <string.h>

struct channel *new_channel(const tal_t *ctx,
const struct sha256_double *funding_txid,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
u64 funding_satoshis,
u64 local_msatoshi,
Expand Down Expand Up @@ -178,7 +178,7 @@ static void add_htlcs(struct bitcoin_tx ***txs,
enum side side)
{
size_t i, n;
struct sha256_double txid;
struct bitcoin_txid txid;
u32 feerate_per_kw = channel->view[side].feerate_per_kw;

/* Get txid of commitment transaction */
Expand Down
2 changes: 1 addition & 1 deletion channeld/full_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* Returns state, or NULL if malformed.
*/
struct channel *new_channel(const tal_t *ctx,
const struct sha256_double *funding_txid,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
u64 funding_satoshis,
u64 local_msatoshi,
Expand Down
24 changes: 6 additions & 18 deletions channeld/test/run-full_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,14 @@

const void *trc;

static struct sha256 sha256_from_hex(const char *hex)
{
struct sha256 sha256;
if (strstarts(hex, "0x"))
hex += 2;
if (!hex_decode(hex, strlen(hex), &sha256, sizeof(sha256)))
abort();
return sha256;
}

/* bitcoind loves its backwards txids! */
static struct sha256_double txid_from_hex(const char *hex)
static struct bitcoin_txid txid_from_hex(const char *hex)
{
struct sha256_double sha256;
struct sha256 rev = sha256_from_hex(hex);
size_t i;
struct bitcoin_txid txid;

for (i = 0; i < sizeof(rev); i++)
sha256.sha.u.u8[sizeof(sha256) - 1 - i] = rev.u.u8[i];
return sha256;
if (!bitcoin_txid_from_hex(hex, strlen(hex), &txid))
abort();
return txid;
}

static struct bitcoin_tx *tx_from_hex(const tal_t *ctx, const char *hex)
Expand Down Expand Up @@ -326,7 +314,7 @@ static void update_feerate(struct channel *channel, u32 feerate)
int main(void)
{
tal_t *tmpctx = tal_tmpctx(NULL);
struct sha256_double funding_txid;
struct bitcoin_txid funding_txid;
/* We test from both sides. */
struct channel *lchannel, *rchannel;
u64 funding_amount_satoshi;
Expand Down
4 changes: 2 additions & 2 deletions closingd/closing.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static struct bitcoin_tx *close_tx(const tal_t *ctx,
struct crypto_state *cs,
const struct channel_id *channel_id,
u8 *scriptpubkey[NUM_SIDES],
const struct sha256_double *funding_txid,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
u64 funding_satoshi,
const u64 satoshi_out[NUM_SIDES],
Expand Down Expand Up @@ -155,7 +155,7 @@ int main(int argc, char *argv[])
u8 *msg;
struct privkey seed;
struct pubkey funding_pubkey[NUM_SIDES];
struct sha256_double funding_txid;
struct bitcoin_txid funding_txid;
u16 funding_txout;
u64 funding_satoshi, satoshi_out[NUM_SIDES];
u64 our_dust_limit;
Expand Down
2 changes: 1 addition & 1 deletion closingd/closing_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ closing_init,2001
closing_init,,crypto_state,struct crypto_state
closing_init,,gossip_index,u64
closing_init,,seed,struct privkey
closing_init,,funding_txid,struct sha256_double
closing_init,,funding_txid,struct bitcoin_txid
closing_init,,funding_txout,u16
closing_init,,funding_satoshi,u64
closing_init,,remote_fundingkey,struct pubkey
Expand Down
2 changes: 1 addition & 1 deletion common/close_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
struct bitcoin_tx *create_close_tx(const tal_t *ctx,
const u8 *our_script,
const u8 *their_script,
const struct sha256_double *anchor_txid,
const struct bitcoin_txid *anchor_txid,
unsigned int anchor_index,
u64 anchor_satoshis,
uint64_t to_us, uint64_t to_them,
Expand Down
3 changes: 1 addition & 2 deletions common/close_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>

struct sha256_double;
struct pubkey;

/* Create close tx to spend the anchor tx output; doesn't fill in
* input scriptsig. */
struct bitcoin_tx *create_close_tx(const tal_t *ctx,
const u8 *our_script,
const u8 *their_script,
const struct sha256_double *anchor_txid,
const struct bitcoin_txid *anchor_txid,
unsigned int anchor_index,
u64 anchor_satoshis,
uint64_t to_us, uint64_t to_them,
Expand Down
1 change: 0 additions & 1 deletion common/funding_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ struct bitcoin_tx;
struct ext_key;
struct privkey;
struct pubkey;
struct sha256_double;
struct utxo;

/**
Expand Down
6 changes: 3 additions & 3 deletions common/htlc_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <common/keyset.h>

static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
const struct sha256_double *commit_txid,
const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number,
u64 msatoshi,
u16 to_self_delay,
Expand Down Expand Up @@ -72,7 +72,7 @@ static struct bitcoin_tx *htlc_tx(const tal_t *ctx,
}

struct bitcoin_tx *htlc_success_tx(const tal_t *ctx,
const struct sha256_double *commit_txid,
const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number,
u64 htlc_msatoshi,
u16 to_self_delay,
Expand Down Expand Up @@ -117,7 +117,7 @@ void htlc_success_tx_add_witness(struct bitcoin_tx *htlc_success,
}

struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
const struct sha256_double *commit_txid,
const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number,
u64 htlc_msatoshi,
u32 cltv_expiry,
Expand Down
5 changes: 2 additions & 3 deletions common/htlc_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
struct keyset;
struct preimage;
struct pubkey;
struct sha256_double;

static inline u64 htlc_timeout_fee(u32 feerate_per_kw)
{
Expand Down Expand Up @@ -35,7 +34,7 @@ static inline u64 htlc_success_fee(u32 feerate_per_kw)
/* Create HTLC-success tx to spend a received HTLC commitment tx
* output; doesn't fill in input witness. */
struct bitcoin_tx *htlc_success_tx(const tal_t *ctx,
const struct sha256_double *commit_txid,
const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number,
u64 htlc_msatoshi,
u16 to_self_delay,
Expand All @@ -55,7 +54,7 @@ void htlc_success_tx_add_witness(struct bitcoin_tx *htlc_success,
/* Create HTLC-timeout tx to spend an offered HTLC commitment tx
* output; doesn't fill in input witness. */
struct bitcoin_tx *htlc_timeout_tx(const tal_t *ctx,
const struct sha256_double *commit_txid,
const struct bitcoin_txid *commit_txid,
unsigned int commit_output_number,
u64 htlc_msatoshi,
u32 cltv_expiry,
Expand Down
2 changes: 1 addition & 1 deletion common/initial_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <inttypes.h>

struct channel *new_initial_channel(const tal_t *ctx,
const struct sha256_double *funding_txid,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
u64 funding_satoshis,
u64 local_msatoshi,
Expand Down
4 changes: 2 additions & 2 deletions common/initial_channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct channel_view {

struct channel {
/* Funding txid and output. */
struct sha256_double funding_txid;
struct bitcoin_txid funding_txid;
unsigned int funding_txout;

/* Keys used to spend funding tx. */
Expand Down Expand Up @@ -142,7 +142,7 @@ static inline u16 to_self_delay(const struct channel *channel, enum side side)
* Returns channel, or NULL if malformed.
*/
struct channel *new_initial_channel(const tal_t *ctx,
const struct sha256_double *funding_txid,
const struct bitcoin_txid *funding_txid,
unsigned int funding_txout,
u64 funding_satoshis,
u64 local_msatoshi,
Expand Down
Loading

0 comments on commit 0237e0b

Please sign in to comment.