2016-01-21 21:08:08 +01:00
|
|
|
#include <assert.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <bitcoin/block.h>
|
|
|
|
#include <bitcoin/pullpush.h>
|
|
|
|
#include <bitcoin/tx.h>
|
2016-01-21 21:11:46 +01:00
|
|
|
#include <ccan/cast/cast.h>
|
2015-05-28 23:38:27 +02:00
|
|
|
#include <ccan/crypto/sha256/sha256.h>
|
2015-06-12 04:56:59 +02:00
|
|
|
#include <ccan/endian/endian.h>
|
2015-10-26 11:35:28 +01:00
|
|
|
#include <ccan/mem/mem.h>
|
2015-07-01 06:14:31 +02:00
|
|
|
#include <ccan/read_write_all/read_write_all.h>
|
2015-06-04 06:18:11 +02:00
|
|
|
#include <ccan/str/hex/hex.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/type_to_string.h>
|
2016-01-21 21:11:46 +01:00
|
|
|
#include <stdio.h>
|
2015-05-28 23:38:27 +02:00
|
|
|
|
2016-04-11 09:09:53 +02:00
|
|
|
#define SEGREGATED_WITNESS_FLAG 0x1
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
static void push_tx_input(const struct bitcoin_tx_input *input,
|
|
|
|
void (*push)(const void *, size_t, void *), void *pushp)
|
2015-05-28 23:38:27 +02:00
|
|
|
{
|
2016-08-18 06:53:45 +02:00
|
|
|
push(&input->txid, sizeof(input->txid), pushp);
|
|
|
|
push_le32(input->index, push, pushp);
|
2017-01-25 00:35:43 +01:00
|
|
|
push_varint_blob(input->script, push, pushp);
|
2016-08-18 06:53:45 +02:00
|
|
|
push_le32(input->sequence_number, push, pushp);
|
2015-05-28 23:38:27 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
static void push_tx_output(const struct bitcoin_tx_output *output,
|
|
|
|
void (*push)(const void *, size_t, void *), void *pushp)
|
2015-05-28 23:38:27 +02:00
|
|
|
{
|
2016-08-18 06:53:45 +02:00
|
|
|
push_le64(output->amount, push, pushp);
|
2017-01-25 00:35:43 +01:00
|
|
|
push_varint_blob(output->script, push, pushp);
|
2015-06-02 05:46:05 +02:00
|
|
|
}
|
|
|
|
|
2016-04-11 09:09:53 +02:00
|
|
|
/* BIP 141:
|
|
|
|
* It is followed by stack items, with each item starts with a var_int
|
|
|
|
* to indicate the length. */
|
2016-11-11 00:02:04 +01:00
|
|
|
static void push_witness(const u8 *witness,
|
2016-08-18 06:53:45 +02:00
|
|
|
void (*push)(const void *, size_t, void *), void *pushp)
|
2016-04-11 09:09:53 +02:00
|
|
|
{
|
2017-01-25 00:35:43 +01:00
|
|
|
push_varint_blob(witness, push, pushp);
|
2016-04-11 09:09:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* BIP144:
|
|
|
|
* If the witness is empty, the old serialization format should be used. */
|
|
|
|
static bool uses_witness(const struct bitcoin_tx *tx)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2017-01-25 00:35:43 +01:00
|
|
|
for (i = 0; i < tal_count(tx->input); i++) {
|
2016-04-11 09:09:53 +02:00
|
|
|
if (tx->input[i].witness)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:23:35 +02:00
|
|
|
/* BIP 141: The witness is a serialization of all witness data of the
|
|
|
|
* transaction. Each txin is associated with a witness field. A
|
|
|
|
* witness field starts with a var_int to indicate the number of stack
|
|
|
|
* items for the txin. */
|
2016-08-18 06:53:45 +02:00
|
|
|
static void push_witnesses(const struct bitcoin_tx *tx,
|
|
|
|
void (*push)(const void *, size_t, void *), void *pushp)
|
2016-04-24 12:23:35 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
2017-01-25 00:35:43 +01:00
|
|
|
for (i = 0; i < tal_count(tx->input); i++) {
|
2016-04-24 12:23:35 +02:00
|
|
|
size_t j, elements;
|
|
|
|
|
|
|
|
/* Not every input needs a witness. */
|
|
|
|
if (!tx->input[i].witness) {
|
2016-08-18 06:53:45 +02:00
|
|
|
push_varint(0, push, pushp);
|
2016-04-24 12:23:35 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
elements = tal_count(tx->input[i].witness);
|
2016-08-18 06:53:45 +02:00
|
|
|
push_varint(elements, push, pushp);
|
2016-04-24 12:23:35 +02:00
|
|
|
for (j = 0;
|
|
|
|
j < tal_count(tx->input[i].witness);
|
|
|
|
j++) {
|
2016-08-18 06:53:45 +02:00
|
|
|
push_witness(tx->input[i].witness[j],
|
|
|
|
push, pushp);
|
2016-04-24 12:23:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
static void push_tx(const struct bitcoin_tx *tx,
|
|
|
|
void (*push)(const void *, size_t, void *), void *pushp,
|
2018-02-01 22:18:46 +01:00
|
|
|
bool bip144)
|
2015-05-28 23:38:27 +02:00
|
|
|
{
|
|
|
|
varint_t i;
|
2016-04-11 09:09:53 +02:00
|
|
|
u8 flag = 0;
|
2015-05-28 23:38:27 +02:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
push_le32(tx->version, push, pushp);
|
2016-04-11 09:09:53 +02:00
|
|
|
|
2018-02-01 22:18:46 +01:00
|
|
|
if (bip144 && uses_witness(tx))
|
|
|
|
flag |= SEGREGATED_WITNESS_FLAG;
|
|
|
|
|
|
|
|
/* BIP 141: The flag MUST be a 1-byte non-zero value. */
|
|
|
|
/* ie. if no flags set, we fallback to pre-BIP144-style */
|
|
|
|
if (flag) {
|
|
|
|
u8 marker = 0;
|
2016-04-11 09:09:53 +02:00
|
|
|
/* BIP 144 */
|
|
|
|
/* marker char Must be zero */
|
|
|
|
/* flag char Must be nonzero */
|
2016-08-18 06:53:45 +02:00
|
|
|
push(&marker, 1, pushp);
|
|
|
|
push(&flag, 1, pushp);
|
2016-04-11 09:09:53 +02:00
|
|
|
}
|
|
|
|
|
2017-01-25 00:35:43 +01:00
|
|
|
push_varint(tal_count(tx->input), push, pushp);
|
|
|
|
for (i = 0; i < tal_count(tx->input); i++)
|
2016-08-18 06:53:45 +02:00
|
|
|
push_tx_input(&tx->input[i], push, pushp);
|
2015-06-25 06:20:16 +02:00
|
|
|
|
2017-01-25 00:35:43 +01:00
|
|
|
push_varint(tal_count(tx->output), push, pushp);
|
|
|
|
for (i = 0; i < tal_count(tx->output); i++)
|
2016-08-18 06:53:45 +02:00
|
|
|
push_tx_output(&tx->output[i], push, pushp);
|
2016-04-11 09:09:53 +02:00
|
|
|
|
2016-04-24 12:23:35 +02:00
|
|
|
if (flag & SEGREGATED_WITNESS_FLAG)
|
2016-08-18 06:53:45 +02:00
|
|
|
push_witnesses(tx, push, pushp);
|
2016-04-24 12:23:35 +02:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
push_le32(tx->lock_time, push, pushp);
|
2015-06-02 05:46:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
static void push_sha(const void *data, size_t len, void *shactx_)
|
2015-06-02 05:46:05 +02:00
|
|
|
{
|
|
|
|
struct sha256_ctx *ctx = shactx_;
|
2015-10-26 11:35:28 +01:00
|
|
|
sha256_update(ctx, memcheck(data, len), len);
|
2015-06-02 05:46:05 +02:00
|
|
|
}
|
|
|
|
|
2016-04-12 05:35:51 +02:00
|
|
|
static void hash_prevouts(struct sha256_double *h, const struct bitcoin_tx *tx)
|
|
|
|
{
|
|
|
|
struct sha256_ctx ctx;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* BIP143: If the ANYONECANPAY flag is not set, hashPrevouts is the
|
|
|
|
* double SHA256 of the serialization of all input
|
|
|
|
* outpoints */
|
|
|
|
sha256_init(&ctx);
|
2017-01-25 00:35:43 +01:00
|
|
|
for (i = 0; i < tal_count(tx->input); i++) {
|
2016-08-18 06:53:45 +02:00
|
|
|
push_sha(&tx->input[i].txid, sizeof(tx->input[i].txid), &ctx);
|
|
|
|
push_le32(tx->input[i].index, push_sha, &ctx);
|
2016-04-12 05:35:51 +02:00
|
|
|
}
|
|
|
|
sha256_double_done(&ctx, h);
|
|
|
|
}
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2016-04-12 05:35:51 +02:00
|
|
|
static void hash_sequence(struct sha256_double *h, const struct bitcoin_tx *tx)
|
|
|
|
{
|
|
|
|
struct sha256_ctx ctx;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* BIP143: If none of the ANYONECANPAY, SINGLE, NONE sighash type
|
|
|
|
* is set, hashSequence is the double SHA256 of the serialization
|
|
|
|
* of nSequence of all inputs */
|
|
|
|
sha256_init(&ctx);
|
2017-01-25 00:35:43 +01:00
|
|
|
for (i = 0; i < tal_count(tx->input); i++)
|
2016-08-18 06:53:45 +02:00
|
|
|
push_le32(tx->input[i].sequence_number, push_sha, &ctx);
|
2016-04-12 05:35:51 +02:00
|
|
|
|
|
|
|
sha256_double_done(&ctx, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the sighash type is neither SINGLE nor NONE, hashOutputs is the
|
|
|
|
* double SHA256 of the serialization of all output value (8-byte
|
|
|
|
* little endian) with scriptPubKey (varInt for the length +
|
|
|
|
* script); */
|
|
|
|
static void hash_outputs(struct sha256_double *h, const struct bitcoin_tx *tx)
|
|
|
|
{
|
|
|
|
struct sha256_ctx ctx;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
sha256_init(&ctx);
|
2017-01-25 00:35:43 +01:00
|
|
|
for (i = 0; i < tal_count(tx->output); i++) {
|
2016-08-18 06:53:45 +02:00
|
|
|
push_le64(tx->output[i].amount, push_sha, &ctx);
|
2017-01-25 00:35:43 +01:00
|
|
|
push_varint_blob(tx->output[i].script, push_sha, &ctx);
|
2016-04-12 05:35:51 +02:00
|
|
|
}
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2016-04-12 05:35:51 +02:00
|
|
|
sha256_double_done(&ctx, h);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hash_for_segwit(struct sha256_ctx *ctx,
|
|
|
|
const struct bitcoin_tx *tx,
|
|
|
|
unsigned int input_num,
|
|
|
|
const u8 *witness_script)
|
|
|
|
{
|
|
|
|
struct sha256_double h;
|
|
|
|
|
|
|
|
/* BIP143:
|
|
|
|
*
|
|
|
|
* Double SHA256 of the serialization of:
|
|
|
|
* 1. nVersion of the transaction (4-byte little endian)
|
|
|
|
*/
|
2016-08-18 06:53:45 +02:00
|
|
|
push_le32(tx->version, push_sha, ctx);
|
2016-04-12 05:35:51 +02:00
|
|
|
|
|
|
|
/* 2. hashPrevouts (32-byte hash) */
|
|
|
|
hash_prevouts(&h, tx);
|
2016-08-18 06:53:45 +02:00
|
|
|
push_sha(&h, sizeof(h), ctx);
|
2016-04-12 05:35:51 +02:00
|
|
|
|
|
|
|
/* 3. hashSequence (32-byte hash) */
|
|
|
|
hash_sequence(&h, tx);
|
2016-08-18 06:53:45 +02:00
|
|
|
push_sha(&h, sizeof(h), ctx);
|
2016-04-12 05:35:51 +02:00
|
|
|
|
|
|
|
/* 4. outpoint (32-byte hash + 4-byte little endian) */
|
2016-08-18 06:53:45 +02:00
|
|
|
push_sha(&tx->input[input_num].txid, sizeof(tx->input[input_num].txid),
|
2016-04-12 05:35:51 +02:00
|
|
|
ctx);
|
2016-08-18 06:53:45 +02:00
|
|
|
push_le32(tx->input[input_num].index, push_sha, ctx);
|
2016-04-12 05:35:51 +02:00
|
|
|
|
|
|
|
/* 5. scriptCode of the input (varInt for the length + script) */
|
2017-01-25 00:35:43 +01:00
|
|
|
push_varint_blob(witness_script, push_sha, ctx);
|
2016-04-12 05:35:51 +02:00
|
|
|
|
|
|
|
/* 6. value of the output spent by this input (8-byte little end) */
|
2016-08-18 06:53:45 +02:00
|
|
|
push_le64(*tx->input[input_num].amount, push_sha, ctx);
|
2016-04-12 05:35:51 +02:00
|
|
|
|
|
|
|
/* 7. nSequence of the input (4-byte little endian) */
|
2016-08-18 06:53:45 +02:00
|
|
|
push_le32(tx->input[input_num].sequence_number, push_sha, ctx);
|
2016-04-12 05:35:51 +02:00
|
|
|
|
|
|
|
/* 8. hashOutputs (32-byte hash) */
|
|
|
|
hash_outputs(&h, tx);
|
2016-08-18 06:53:45 +02:00
|
|
|
push_sha(&h, sizeof(h), ctx);
|
2016-04-12 05:35:51 +02:00
|
|
|
|
|
|
|
/* 9. nLocktime of the transaction (4-byte little endian) */
|
2016-08-18 06:53:45 +02:00
|
|
|
push_le32(tx->lock_time, push_sha, ctx);
|
2016-04-12 05:35:51 +02:00
|
|
|
}
|
|
|
|
|
2016-04-11 09:15:53 +02:00
|
|
|
void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx,
|
2017-01-25 00:34:23 +01:00
|
|
|
unsigned int input_num,
|
2016-04-12 05:35:51 +02:00
|
|
|
const u8 *witness_script)
|
2015-06-02 05:46:05 +02:00
|
|
|
{
|
2015-07-24 08:30:10 +02:00
|
|
|
size_t i;
|
2016-04-11 09:15:53 +02:00
|
|
|
struct sha256_ctx ctx = SHA256_INIT;
|
|
|
|
|
2015-07-24 08:30:10 +02:00
|
|
|
/* Caller should zero-out other scripts for signing! */
|
2017-01-25 00:35:43 +01:00
|
|
|
assert(input_num < tal_count(tx->input));
|
|
|
|
for (i = 0; i < tal_count(tx->input); i++)
|
2015-07-24 08:30:10 +02:00
|
|
|
if (i != input_num)
|
2017-01-25 00:35:43 +01:00
|
|
|
assert(!tx->input[i].script);
|
2016-04-12 05:35:51 +02:00
|
|
|
|
|
|
|
if (witness_script) {
|
|
|
|
/* BIP143 hashing if OP_CHECKSIG is inside witness. */
|
|
|
|
hash_for_segwit(&ctx, tx, input_num, witness_script);
|
|
|
|
} else {
|
|
|
|
/* Otherwise signature hashing never includes witness. */
|
2016-08-18 06:53:45 +02:00
|
|
|
push_tx(tx, push_sha, &ctx, false);
|
2016-04-12 05:35:51 +02:00
|
|
|
}
|
2016-04-11 09:15:53 +02:00
|
|
|
|
2017-01-25 00:34:23 +01:00
|
|
|
sha256_le32(&ctx, SIGHASH_ALL);
|
2016-04-11 09:15:53 +02:00
|
|
|
sha256_double_done(&ctx, h);
|
2015-06-02 05:46:05 +02:00
|
|
|
}
|
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
static void push_linearize(const void *data, size_t len, void *pptr_)
|
2015-06-02 05:46:05 +02:00
|
|
|
{
|
|
|
|
u8 **pptr = pptr_;
|
|
|
|
size_t oldsize = tal_count(*pptr);
|
|
|
|
|
|
|
|
tal_resize(pptr, oldsize + len);
|
2015-10-26 11:35:28 +01:00
|
|
|
memcpy(*pptr + oldsize, memcheck(data, len), len);
|
2015-06-02 05:46:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u8 *linearize_tx(const tal_t *ctx, const struct bitcoin_tx *tx)
|
|
|
|
{
|
|
|
|
u8 *arr = tal_arr(ctx, u8, 0);
|
2018-02-01 22:18:46 +01:00
|
|
|
push_tx(tx, push_linearize, &arr, true);
|
2015-06-02 05:46:05 +02:00
|
|
|
return arr;
|
2015-05-28 23:38:27 +02:00
|
|
|
}
|
2015-05-29 04:03:18 +02:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
static void push_measure(const void *data, size_t len, void *lenp)
|
2016-04-12 05:37:04 +02:00
|
|
|
{
|
|
|
|
*(size_t *)lenp += len;
|
|
|
|
}
|
|
|
|
|
2016-04-24 12:23:35 +02:00
|
|
|
size_t measure_tx_cost(const struct bitcoin_tx *tx)
|
|
|
|
{
|
|
|
|
size_t non_witness_len = 0, witness_len = 0;
|
2016-08-18 06:53:45 +02:00
|
|
|
push_tx(tx, push_measure, &non_witness_len, false);
|
2016-04-24 12:23:35 +02:00
|
|
|
if (uses_witness(tx))
|
2016-08-18 06:53:45 +02:00
|
|
|
push_witnesses(tx, push_measure, &witness_len);
|
2016-04-24 12:23:35 +02:00
|
|
|
|
2016-08-18 06:53:45 +02:00
|
|
|
/* Witness bytes only push 1/4 of normal bytes, for cost. */
|
2016-04-24 12:23:35 +02:00
|
|
|
return non_witness_len * 4 + witness_len;
|
|
|
|
}
|
|
|
|
|
2017-12-18 07:41:52 +01:00
|
|
|
void bitcoin_txid(const struct bitcoin_tx *tx, struct bitcoin_txid *txid)
|
2015-06-02 05:29:13 +02:00
|
|
|
{
|
|
|
|
struct sha256_ctx ctx = SHA256_INIT;
|
|
|
|
|
2016-04-11 09:09:53 +02:00
|
|
|
/* For TXID, we never use extended form. */
|
2016-08-18 06:53:45 +02:00
|
|
|
push_tx(tx, push_sha, &ctx, false);
|
2017-12-18 07:41:52 +01:00
|
|
|
sha256_double_done(&ctx, &txid->shad);
|
2015-06-02 05:29:13 +02:00
|
|
|
}
|
|
|
|
|
2015-05-29 04:03:18 +02:00
|
|
|
struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count,
|
|
|
|
varint_t output_count)
|
|
|
|
{
|
|
|
|
struct bitcoin_tx *tx = tal(ctx, struct bitcoin_tx);
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
tx->output = tal_arrz(tx, struct bitcoin_tx_output, output_count);
|
|
|
|
tx->input = tal_arrz(tx, struct bitcoin_tx_input, input_count);
|
2017-01-25 00:35:43 +01:00
|
|
|
for (i = 0; i < tal_count(tx->input); i++) {
|
2015-05-29 04:03:18 +02:00
|
|
|
/* We assume NULL is a zero bitmap */
|
|
|
|
assert(tx->input[i].script == NULL);
|
|
|
|
tx->input[i].sequence_number = 0xFFFFFFFF;
|
2016-04-11 09:13:53 +02:00
|
|
|
tx->input[i].amount = NULL;
|
2016-04-11 09:09:53 +02:00
|
|
|
tx->input[i].witness = NULL;
|
2015-05-29 04:03:18 +02:00
|
|
|
}
|
2015-07-02 22:38:59 +02:00
|
|
|
tx->lock_time = 0;
|
2015-09-30 03:24:11 +02:00
|
|
|
tx->version = 2;
|
2015-05-29 04:03:18 +02:00
|
|
|
return tx;
|
|
|
|
}
|
2015-06-04 06:18:11 +02:00
|
|
|
|
|
|
|
static bool pull_sha256_double(const u8 **cursor, size_t *max,
|
|
|
|
struct sha256_double *h)
|
|
|
|
{
|
|
|
|
return pull(cursor, max, h, sizeof(*h));
|
|
|
|
}
|
|
|
|
|
2015-06-25 06:20:16 +02:00
|
|
|
static u64 pull_value(const u8 **cursor, size_t *max)
|
|
|
|
{
|
|
|
|
u64 amount;
|
|
|
|
|
2016-04-11 09:02:43 +02:00
|
|
|
amount = pull_le64(cursor, max);
|
2015-06-25 06:20:16 +02:00
|
|
|
return amount;
|
|
|
|
}
|
|
|
|
|
2016-04-11 09:06:29 +02:00
|
|
|
/* Pulls a varint which specifies a data length: ensures basic sanity to
|
|
|
|
* avoid trivial OOM */
|
|
|
|
static u64 pull_length(const u8 **cursor, size_t *max)
|
|
|
|
{
|
|
|
|
u64 v = pull_varint(cursor, max);
|
|
|
|
if (v > *max) {
|
|
|
|
*cursor = NULL;
|
|
|
|
*max = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2015-06-04 06:18:11 +02:00
|
|
|
static void pull_input(const tal_t *ctx, const u8 **cursor, size_t *max,
|
|
|
|
struct bitcoin_tx_input *input)
|
|
|
|
{
|
2017-08-18 06:43:52 +02:00
|
|
|
u64 script_len;
|
2017-12-18 07:41:52 +01:00
|
|
|
pull_sha256_double(cursor, max, &input->txid.shad);
|
2015-06-04 06:18:11 +02:00
|
|
|
input->index = pull_le32(cursor, max);
|
2017-08-18 06:43:52 +02:00
|
|
|
script_len = pull_length(cursor, max);
|
|
|
|
if (script_len)
|
|
|
|
input->script = tal_arr(ctx, u8, script_len);
|
|
|
|
else
|
|
|
|
input->script = NULL;
|
2017-01-25 00:35:43 +01:00
|
|
|
pull(cursor, max, input->script, tal_len(input->script));
|
2015-06-04 06:18:11 +02:00
|
|
|
input->sequence_number = pull_le32(cursor, max);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pull_output(const tal_t *ctx, const u8 **cursor, size_t *max,
|
|
|
|
struct bitcoin_tx_output *output)
|
|
|
|
{
|
2015-06-25 06:20:16 +02:00
|
|
|
output->amount = pull_value(cursor, max);
|
2017-01-25 00:35:43 +01:00
|
|
|
output->script = tal_arr(ctx, u8, pull_length(cursor, max));
|
|
|
|
pull(cursor, max, output->script, tal_len(output->script));
|
2015-06-04 06:18:11 +02:00
|
|
|
}
|
|
|
|
|
2016-04-11 09:09:53 +02:00
|
|
|
static u8 *pull_witness_item(const tal_t *ctx, const u8 **cursor, size_t *max)
|
|
|
|
{
|
|
|
|
uint64_t len = pull_length(cursor, max);
|
|
|
|
u8 *item;
|
|
|
|
|
|
|
|
item = tal_arr(ctx, u8, len);
|
|
|
|
pull(cursor, max, item, len);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pull_witness(struct bitcoin_tx_input *inputs, size_t i,
|
|
|
|
const u8 **cursor, size_t *max)
|
|
|
|
{
|
|
|
|
uint64_t j, num = pull_length(cursor, max);
|
|
|
|
|
|
|
|
/* 0 means not using witness. */
|
|
|
|
if (num == 0) {
|
|
|
|
inputs[i].witness = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
inputs[i].witness = tal_arr(inputs, u8 *, num);
|
|
|
|
for (j = 0; j < num; j++) {
|
2016-11-11 00:02:04 +01:00
|
|
|
inputs[i].witness[j] = pull_witness_item(inputs[i].witness,
|
2016-04-11 09:09:53 +02:00
|
|
|
cursor, max);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 18:16:56 +01:00
|
|
|
struct bitcoin_tx *pull_bitcoin_tx_onto(const tal_t *ctx, const u8 **cursor,
|
|
|
|
size_t *max, struct bitcoin_tx *tx)
|
2015-06-04 06:18:11 +02:00
|
|
|
{
|
|
|
|
size_t i;
|
2017-01-25 00:35:43 +01:00
|
|
|
u64 count;
|
2016-04-11 09:09:53 +02:00
|
|
|
u8 flag = 0;
|
2015-06-04 06:18:11 +02:00
|
|
|
|
|
|
|
tx->version = pull_le32(cursor, max);
|
2017-01-25 00:35:43 +01:00
|
|
|
count = pull_length(cursor, max);
|
2016-04-11 09:09:53 +02:00
|
|
|
/* BIP 144 marker is 0 (impossible to have tx with 0 inputs) */
|
2017-01-25 00:35:43 +01:00
|
|
|
if (count == 0) {
|
2016-04-11 09:09:53 +02:00
|
|
|
pull(cursor, max, &flag, 1);
|
|
|
|
if (flag != SEGREGATED_WITNESS_FLAG)
|
|
|
|
return tal_free(tx);
|
2017-01-25 00:35:43 +01:00
|
|
|
count = pull_length(cursor, max);
|
2016-04-11 09:09:53 +02:00
|
|
|
}
|
|
|
|
|
2017-01-25 00:35:43 +01:00
|
|
|
tx->input = tal_arr(tx, struct bitcoin_tx_input, count);
|
|
|
|
for (i = 0; i < tal_count(tx->input); i++)
|
2015-06-04 06:18:11 +02:00
|
|
|
pull_input(tx, cursor, max, tx->input + i);
|
2015-06-25 06:20:16 +02:00
|
|
|
|
2017-01-25 00:35:43 +01:00
|
|
|
count = pull_length(cursor, max);
|
|
|
|
tx->output = tal_arr(tx, struct bitcoin_tx_output, count);
|
|
|
|
for (i = 0; i < tal_count(tx->output); i++)
|
2015-06-04 06:18:11 +02:00
|
|
|
pull_output(tx, cursor, max, tx->output + i);
|
2016-04-11 09:09:53 +02:00
|
|
|
|
|
|
|
if (flag & SEGREGATED_WITNESS_FLAG) {
|
2017-01-25 00:35:43 +01:00
|
|
|
for (i = 0; i < tal_count(tx->input); i++)
|
2016-04-11 09:09:53 +02:00
|
|
|
pull_witness(tx->input, i, cursor, max);
|
|
|
|
} else {
|
2017-01-25 00:35:43 +01:00
|
|
|
for (i = 0; i < tal_count(tx->input); i++)
|
2016-04-11 09:09:53 +02:00
|
|
|
tx->input[i].witness = NULL;
|
|
|
|
}
|
2015-06-04 06:18:11 +02:00
|
|
|
tx->lock_time = pull_le32(cursor, max);
|
|
|
|
|
2016-04-24 12:10:29 +02:00
|
|
|
/* If we ran short, fail. */
|
|
|
|
if (!*cursor)
|
2015-06-04 06:18:11 +02:00
|
|
|
tx = tal_free(tx);
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
|
2018-01-04 18:16:56 +01:00
|
|
|
struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx,
|
|
|
|
const u8 **cursor, size_t *max)
|
|
|
|
{
|
|
|
|
struct bitcoin_tx *tx = tal(ctx, struct bitcoin_tx);
|
|
|
|
return pull_bitcoin_tx_onto(ctx, cursor, max, tx);
|
|
|
|
}
|
|
|
|
|
2016-01-21 21:11:47 +01:00
|
|
|
struct bitcoin_tx *bitcoin_tx_from_hex(const tal_t *ctx, const char *hex,
|
|
|
|
size_t hexlen)
|
2015-06-04 06:18:11 +02:00
|
|
|
{
|
2016-07-01 03:57:57 +02:00
|
|
|
const char *end;
|
2015-06-04 06:18:11 +02:00
|
|
|
u8 *linear_tx;
|
|
|
|
const u8 *p;
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
size_t len;
|
|
|
|
|
2016-04-11 09:02:43 +02:00
|
|
|
end = memchr(hex, '\n', hexlen);
|
|
|
|
if (!end)
|
2016-07-01 03:57:57 +02:00
|
|
|
end = hex + hexlen;
|
2016-01-21 21:11:46 +01:00
|
|
|
|
2015-07-01 06:14:31 +02:00
|
|
|
len = hex_data_size(end - hex);
|
2016-01-21 21:11:46 +01:00
|
|
|
p = linear_tx = tal_arr(ctx, u8, len);
|
2015-07-01 06:14:31 +02:00
|
|
|
if (!hex_decode(hex, end - hex, linear_tx, len))
|
2016-01-21 21:11:46 +01:00
|
|
|
goto fail;
|
2015-06-04 06:18:11 +02:00
|
|
|
|
|
|
|
tx = pull_bitcoin_tx(ctx, &p, &len);
|
|
|
|
if (!tx)
|
2016-01-21 21:11:46 +01:00
|
|
|
goto fail;
|
2015-07-01 06:14:31 +02:00
|
|
|
|
2016-04-24 12:10:29 +02:00
|
|
|
if (len)
|
|
|
|
goto fail_free_tx;
|
2016-11-11 00:02:04 +01:00
|
|
|
|
2016-01-21 21:11:46 +01:00
|
|
|
tal_free(linear_tx);
|
2015-06-04 06:18:11 +02:00
|
|
|
return tx;
|
2016-01-21 21:11:46 +01:00
|
|
|
|
|
|
|
fail_free_tx:
|
|
|
|
tal_free(tx);
|
|
|
|
fail:
|
|
|
|
tal_free(linear_tx);
|
|
|
|
return NULL;
|
2015-06-04 06:18:11 +02:00
|
|
|
}
|
2015-06-05 05:05:46 +02:00
|
|
|
|
2017-12-18 07:41:52 +01:00
|
|
|
/* <sigh>. Bitcoind represents hashes as little-endian for RPC. */
|
2015-06-05 05:05:46 +02:00
|
|
|
static void reverse_bytes(u8 *arr, size_t len)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < len / 2; i++) {
|
|
|
|
unsigned char tmp = arr[i];
|
|
|
|
arr[i] = arr[len - 1 - i];
|
|
|
|
arr[len - 1 - i] = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool bitcoin_txid_from_hex(const char *hexstr, size_t hexstr_len,
|
2017-12-18 07:41:52 +01:00
|
|
|
struct bitcoin_txid *txid)
|
2015-06-05 05:05:46 +02:00
|
|
|
{
|
|
|
|
if (!hex_decode(hexstr, hexstr_len, txid, sizeof(*txid)))
|
|
|
|
return false;
|
2017-12-18 07:41:52 +01:00
|
|
|
reverse_bytes(txid->shad.sha.u.u8, sizeof(txid->shad.sha.u.u8));
|
2015-06-05 05:05:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
2015-07-01 06:14:31 +02:00
|
|
|
|
2017-12-18 07:41:52 +01:00
|
|
|
bool bitcoin_txid_to_hex(const struct bitcoin_txid *txid,
|
2015-07-24 08:30:10 +02:00
|
|
|
char *hexstr, size_t hexstr_len)
|
|
|
|
{
|
2017-12-18 07:41:52 +01:00
|
|
|
struct sha256_double rev = txid->shad;
|
2015-07-24 08:30:10 +02:00
|
|
|
reverse_bytes(rev.sha.u.u8, sizeof(rev.sha.u.u8));
|
|
|
|
return hex_encode(&rev, sizeof(rev), hexstr, hexstr_len);
|
|
|
|
}
|
2017-01-04 04:37:15 +01:00
|
|
|
|
|
|
|
static char *fmt_bitcoin_tx(const tal_t *ctx, const struct bitcoin_tx *tx)
|
|
|
|
{
|
|
|
|
u8 *lin = linearize_tx(ctx, tx);
|
2017-01-10 05:49:25 +01:00
|
|
|
char *s = tal_hex(ctx, lin);
|
2017-01-04 04:37:15 +01:00
|
|
|
tal_free(lin);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-12-18 07:41:52 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-01-04 04:37:15 +01:00
|
|
|
REGISTER_TYPE_TO_STRING(bitcoin_tx, fmt_bitcoin_tx);
|
2017-12-18 07:41:52 +01:00
|
|
|
REGISTER_TYPE_TO_STRING(bitcoin_txid, fmt_bitcoin_txid);
|