Skip to content

Commit

Permalink
bitcoin/tx: add helper to write blob of data.
Browse files Browse the repository at this point in the history
We do this a lot.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Apr 11, 2016
1 parent 58b1429 commit ee5f0e3
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions bitcoin/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,28 @@ static void add_le64(u64 v,
add(&l, sizeof(l), addp);
}

static void add_varint_blob(const void *blob, varint_t len,
void (*add)(const void *, size_t, void *),
void *addp)
{
add_varint(len, add, addp);
add(blob, len, addp);
}

static void add_tx_input(const struct bitcoin_tx_input *input,
void (*add)(const void *, size_t, void *), void *addp)
{
add(&input->txid, sizeof(input->txid), addp);
add_le32(input->index, add, addp);
add_varint(input->script_length, add, addp);
add(input->script, input->script_length, addp);
add_varint_blob(input->script, input->script_length, add, addp);
add_le32(input->sequence_number, add, addp);
}

static void add_tx_output(const struct bitcoin_tx_output *output,
void (*add)(const void *, size_t, void *), void *addp)
{
add_le64(output->amount, add, addp);
add_varint(output->script_length, add, addp);
add(output->script, output->script_length, addp);
add_varint_blob(output->script, output->script_length, add, addp);
}

/* BIP 141:
Expand All @@ -79,8 +85,7 @@ static void add_tx_output(const struct bitcoin_tx_output *output,
static void add_witness(const u8 *witness,
void (*add)(const void *, size_t, void *), void *addp)
{
add_varint(tal_count(witness), add, addp);
add(witness, tal_count(witness), addp);
add_varint_blob(witness, tal_count(witness), add, addp);
}

/* BIP144:
Expand Down

0 comments on commit ee5f0e3

Please sign in to comment.