Browse Source

struct bitcoin_tx: remove explicit lengths, use tal_len()/tal_count()

They're always tal objects, so we can simply ask tal for the length,
simplifying the API.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 8 years ago
parent
commit
8522a5ea64
  1. 6
      bitcoin/pullpush.c
  2. 3
      bitcoin/pullpush.h
  3. 27
      bitcoin/script.c
  4. 10
      bitcoin/script.h
  5. 45
      bitcoin/signature.c
  6. 4
      bitcoin/signature.h
  7. 6
      bitcoin/test/run-tx-encode.c
  8. 71
      bitcoin/tx.c
  9. 5
      bitcoin/tx.h
  10. 2
      close_tx.c
  11. 2
      daemon/chaintopology.c
  12. 28
      daemon/commit_tx.c
  13. 8
      daemon/output_to_htlc.c
  14. 3
      daemon/output_to_htlc.h
  15. 18
      daemon/packets.c
  16. 72
      daemon/peer.c
  17. 14
      daemon/secrets.c
  18. 6
      daemon/wallet.c
  19. 9
      find_p2sh_out.c
  20. 14
      permute_tx.c

6
bitcoin/pullpush.c

@ -26,12 +26,12 @@ void push_le64(u64 v,
push(&l, sizeof(l), pushp); push(&l, sizeof(l), pushp);
} }
void push_varint_blob(const void *blob, varint_t len, void push_varint_blob(const tal_t *blob,
void (*push)(const void *, size_t, void *), void (*push)(const void *, size_t, void *),
void *pushp) void *pushp)
{ {
push_varint(len, push, pushp); push_varint(tal_len(blob), push, pushp);
push(blob, len, pushp); push(blob, tal_len(blob), pushp);
} }
void push(const void *data, size_t len, void *pptr_) void push(const void *data, size_t len, void *pptr_)

3
bitcoin/pullpush.h

@ -2,12 +2,13 @@
#define LIGHTNING_BITCOIN_PULLPUSH_H #define LIGHTNING_BITCOIN_PULLPUSH_H
#include "config.h" #include "config.h"
#include "bitcoin/varint.h" #include "bitcoin/varint.h"
#include <ccan/tal/tal.h>
void push_varint(varint_t v, void push_varint(varint_t v,
void (*push)(const void *, size_t, void *), void *pushp); void (*push)(const void *, size_t, void *), void *pushp);
void push_le32(u32 v, void (*push)(const void *, size_t, void *), void *pushp); void push_le32(u32 v, void (*push)(const void *, size_t, void *), void *pushp);
void push_le64(u64 v, void (*push)(const void *, size_t, void *), void *pushp); void push_le64(u64 v, void (*push)(const void *, size_t, void *), void *pushp);
void push_varint_blob(const void *blob, varint_t len, void push_varint_blob(const tal_t *blob,
void (*push)(const void *, size_t, void *), void (*push)(const void *, size_t, void *),
void *pushp); void *pushp);

27
bitcoin/script.c

@ -222,7 +222,6 @@ void bitcoin_witness_p2sh_p2wpkh(const tal_t *ctx,
* or validation fails. */ * or validation fails. */
input->script = tal_arr(ctx, u8, 0); input->script = tal_arr(ctx, u8, 0);
add_push_bytes(&input->script, redeemscript, tal_count(redeemscript)); add_push_bytes(&input->script, redeemscript, tal_count(redeemscript));
input->script_length = tal_count(input->script);
tal_free(redeemscript); tal_free(redeemscript);
/* BIP141: The witness must consist of exactly 2 items (≤ 520 /* BIP141: The witness must consist of exactly 2 items (≤ 520
@ -423,8 +422,10 @@ u8 *p2wpkh_scriptcode(const tal_t *ctx, const struct pubkey *key)
return script; return script;
} }
bool is_p2pkh(const u8 *script, size_t script_len) bool is_p2pkh(const u8 *script)
{ {
size_t script_len = tal_len(script);
if (script_len != 25) if (script_len != 25)
return false; return false;
if (script[0] != OP_DUP) if (script[0] != OP_DUP)
@ -440,8 +441,10 @@ bool is_p2pkh(const u8 *script, size_t script_len)
return true; return true;
} }
bool is_p2sh(const u8 *script, size_t script_len) bool is_p2sh(const u8 *script)
{ {
size_t script_len = tal_len(script);
if (script_len != 23) if (script_len != 23)
return false; return false;
if (script[0] != OP_HASH160) if (script[0] != OP_HASH160)
@ -453,8 +456,10 @@ bool is_p2sh(const u8 *script, size_t script_len)
return true; return true;
} }
bool is_p2wsh(const u8 *script, size_t script_len) bool is_p2wsh(const u8 *script)
{ {
size_t script_len = tal_len(script);
if (script_len != 1 + 1 + sizeof(struct sha256)) if (script_len != 1 + 1 + sizeof(struct sha256))
return false; return false;
if (script[0] != OP_0) if (script[0] != OP_0)
@ -464,8 +469,10 @@ bool is_p2wsh(const u8 *script, size_t script_len)
return true; return true;
} }
bool is_p2wpkh(const u8 *script, size_t script_len) bool is_p2wpkh(const u8 *script)
{ {
size_t script_len = tal_len(script);
if (script_len != 1 + 1 + sizeof(struct ripemd160)) if (script_len != 1 + 1 + sizeof(struct ripemd160))
return false; return false;
if (script[0] != OP_0) if (script[0] != OP_0)
@ -542,12 +549,12 @@ u8 **bitcoin_witness_htlc(const tal_t *ctx,
32, sig, witnessscript); 32, sig, witnessscript);
} }
bool scripteq(const u8 *s1, size_t s1len, const u8 *s2, size_t s2len) bool scripteq(const tal_t *s1, const tal_t *s2)
{ {
memcheck(s1, s1len); memcheck(s1, tal_len(s1));
memcheck(s2, s2len); memcheck(s2, tal_len(s2));
if (s1len != s2len) if (tal_len(s1) != tal_len(s2))
return false; return false;
return memcmp(s1, s2, s1len) == 0; return memcmp(s1, s2, tal_len(s1)) == 0;
} }

10
bitcoin/script.h

@ -90,18 +90,18 @@ u8 **bitcoin_witness_htlc(const tal_t *ctx,
const u8 *witnessscript); const u8 *witnessscript);
/* Is this a pay to pubkeu hash? */ /* Is this a pay to pubkeu hash? */
bool is_p2pkh(const u8 *script, size_t script_len); bool is_p2pkh(const u8 *script);
/* Is this a pay to script hash? */ /* Is this a pay to script hash? */
bool is_p2sh(const u8 *script, size_t script_len); bool is_p2sh(const u8 *script);
/* Is this (version 0) pay to witness script hash? */ /* Is this (version 0) pay to witness script hash? */
bool is_p2wsh(const u8 *script, size_t script_len); bool is_p2wsh(const u8 *script);
/* Is this (version 0) pay to witness pubkey hash? */ /* Is this (version 0) pay to witness pubkey hash? */
bool is_p2wpkh(const u8 *script, size_t script_len); bool is_p2wpkh(const u8 *script);
/* Are these two scripts equal? */ /* Are these two scripts equal? */
bool scripteq(const u8 *s1, size_t s1len, const u8 *s2, size_t s2len); bool scripteq(const tal_t *s1, const tal_t *s2);
#endif /* LIGHTNING_BITCOIN_SCRIPT_H */ #endif /* LIGHTNING_BITCOIN_SCRIPT_H */

45
bitcoin/signature.c

@ -26,29 +26,29 @@
static void dump_tx(const char *msg, static void dump_tx(const char *msg,
const struct bitcoin_tx *tx, size_t inputnum, const struct bitcoin_tx *tx, size_t inputnum,
const u8 *script, size_t script_len, const u8 *script,
const struct pubkey *key, const struct pubkey *key,
const struct sha256_double *h) const struct sha256_double *h)
{ {
size_t i, j; size_t i, j;
warnx("%s tx version %u locktime %#x:", warnx("%s tx version %u locktime %#x:",
msg, tx->version, tx->lock_time); msg, tx->version, tx->lock_time);
for (i = 0; i < tx->input_count; i++) { for (i = 0; i < tal_count(tx->input); i++) {
warnx("input[%zu].txid = "SHA_FMT, i, warnx("input[%zu].txid = "SHA_FMT, i,
SHA_VALS(tx->input[i].txid.sha.u.u8)); SHA_VALS(tx->input[i].txid.sha.u.u8));
warnx("input[%zu].index = %u", i, tx->input[i].index); warnx("input[%zu].index = %u", i, tx->input[i].index);
} }
for (i = 0; i < tx->output_count; i++) { for (i = 0; i < tal_count(tx->output); i++) {
warnx("output[%zu].amount = %llu", warnx("output[%zu].amount = %llu",
i, (long long)tx->output[i].amount); i, (long long)tx->output[i].amount);
warnx("output[%zu].script = %llu", warnx("output[%zu].script = %zu",
i, (long long)tx->output[i].script_length); i, tal_len(tx->output[i].script));
for (j = 0; j < tx->output[i].script_length; j++) for (j = 0; j < tal_len(tx->output[i].script); j++)
fprintf(stderr, "%02x", tx->output[i].script[j]); fprintf(stderr, "%02x", tx->output[i].script[j]);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
} }
warnx("input[%zu].script = %zu", inputnum, script_len); warnx("input[%zu].script = %zu", inputnum, tal_len(script));
for (i = 0; i < script_len; i++) for (i = 0; i < tal_len(script); i++)
fprintf(stderr, "%02x", script[i]); fprintf(stderr, "%02x", script[i]);
if (key) { if (key) {
fprintf(stderr, "\nPubkey: "); fprintf(stderr, "\nPubkey: ");
@ -66,7 +66,7 @@ static void dump_tx(const char *msg,
#else #else
static void dump_tx(const char *msg, static void dump_tx(const char *msg,
const struct bitcoin_tx *tx, size_t inputnum, const struct bitcoin_tx *tx, size_t inputnum,
const u8 *script, size_t script_len, const u8 *script,
const struct pubkey *key, const struct pubkey *key,
const struct sha256_double *h) const struct sha256_double *h)
{ {
@ -89,41 +89,38 @@ void sign_hash(const struct privkey *privkey,
/* Only does SIGHASH_ALL */ /* Only does SIGHASH_ALL */
static void sha256_tx_one_input(struct bitcoin_tx *tx, static void sha256_tx_one_input(struct bitcoin_tx *tx,
size_t input_num, size_t input_num,
const u8 *script, size_t script_len, const u8 *script,
const u8 *witness_script, const u8 *witness_script,
struct sha256_double *hash) struct sha256_double *hash)
{ {
size_t i; size_t i;
assert(input_num < tx->input_count); assert(input_num < tal_count(tx->input));
/* You must have all inputs zeroed to start. */ /* You must have all inputs zeroed to start. */
for (i = 0; i < tx->input_count; i++) for (i = 0; i < tal_count(tx->input); i++)
assert(tx->input[i].script_length == 0); assert(!tx->input[i].script);
tx->input[input_num].script_length = script_len;
tx->input[input_num].script = cast_const(u8 *, script); tx->input[input_num].script = cast_const(u8 *, script);
sha256_tx_for_sig(hash, tx, input_num, witness_script); sha256_tx_for_sig(hash, tx, input_num, witness_script);
/* Reset it for next time. */ /* Reset it for next time. */
tx->input[input_num].script_length = 0;
tx->input[input_num].script = NULL; tx->input[input_num].script = NULL;
} }
/* Only does SIGHASH_ALL */ /* Only does SIGHASH_ALL */
void sign_tx_input(struct bitcoin_tx *tx, void sign_tx_input(struct bitcoin_tx *tx,
unsigned int in, unsigned int in,
const u8 *subscript, size_t subscript_len, const u8 *subscript,
const u8 *witness_script, const u8 *witness_script,
const struct privkey *privkey, const struct pubkey *key, const struct privkey *privkey, const struct pubkey *key,
secp256k1_ecdsa_signature *sig) secp256k1_ecdsa_signature *sig)
{ {
struct sha256_double hash; struct sha256_double hash;
sha256_tx_one_input(tx, in, subscript, subscript_len, witness_script, sha256_tx_one_input(tx, in, subscript, witness_script, &hash);
&hash); dump_tx("Signing", tx, in, subscript, key, &hash);
dump_tx("Signing", tx, in, subscript, subscript_len, key, &hash);
sign_hash(privkey, &hash, sig); sign_hash(privkey, &hash, sig);
} }
@ -140,7 +137,7 @@ bool check_signed_hash(const struct sha256_double *hash,
} }
bool check_tx_sig(struct bitcoin_tx *tx, size_t input_num, bool check_tx_sig(struct bitcoin_tx *tx, size_t input_num,
const u8 *redeemscript, size_t redeemscript_len, const u8 *redeemscript,
const u8 *witness_script, const u8 *witness_script,
const struct pubkey *key, const struct pubkey *key,
const secp256k1_ecdsa_signature *sig) const secp256k1_ecdsa_signature *sig)
@ -148,15 +145,13 @@ bool check_tx_sig(struct bitcoin_tx *tx, size_t input_num,
struct sha256_double hash; struct sha256_double hash;
bool ret; bool ret;
assert(input_num < tx->input_count); assert(input_num < tal_count(tx->input));
sha256_tx_one_input(tx, input_num, redeemscript, redeemscript_len, sha256_tx_one_input(tx, input_num, redeemscript, witness_script, &hash);
witness_script, &hash);
ret = check_signed_hash(&hash, sig, key); ret = check_signed_hash(&hash, sig, key);
if (!ret) if (!ret)
dump_tx("Sig failed", tx, input_num, dump_tx("Sig failed", tx, input_num, redeemscript, key, &hash);
redeemscript, redeemscript_len, key, &hash);
return ret; return ret;
} }

4
bitcoin/signature.h

@ -29,14 +29,14 @@ bool check_signed_hash(const struct sha256_double *hash,
/* All tx input scripts must be set to 0 len. */ /* All tx input scripts must be set to 0 len. */
void sign_tx_input(struct bitcoin_tx *tx, void sign_tx_input(struct bitcoin_tx *tx,
unsigned int in, unsigned int in,
const u8 *subscript, size_t subscript_len, const u8 *subscript,
const u8 *witness, const u8 *witness,
const struct privkey *privkey, const struct pubkey *pubkey, const struct privkey *privkey, const struct pubkey *pubkey,
secp256k1_ecdsa_signature *sig); secp256k1_ecdsa_signature *sig);
/* Does this sig sign the tx with this input for this pubkey. */ /* Does this sig sign the tx with this input for this pubkey. */
bool check_tx_sig(struct bitcoin_tx *tx, size_t input_num, bool check_tx_sig(struct bitcoin_tx *tx, size_t input_num,
const u8 *redeemscript, size_t redeemscript_len, const u8 *redeemscript,
const u8 *witness, const u8 *witness,
const struct pubkey *key, const struct pubkey *key,
const secp256k1_ecdsa_signature *sig); const secp256k1_ecdsa_signature *sig);

6
bitcoin/test/run-tx-encode.c

@ -35,8 +35,8 @@ int main(void)
* http://n.bitcoin.ninja/checktx * http://n.bitcoin.ninja/checktx
* With much thanks! * With much thanks!
*/ */
assert(tx->input_count == 1); assert(tal_count(tx->input) == 1);
assert(tx->output_count == 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.sha.u.u8, sizeof(tx->input[0].txid));
hexeq(&tx->input[0].txid, sizeof(tx->input[0].txid), hexeq(&tx->input[0].txid, sizeof(tx->input[0].txid),
@ -45,7 +45,7 @@ int main(void)
/* This is a p2sh-p2wpkh: */ /* This is a p2sh-p2wpkh: */
/* ScriptSig is push of "version 0 + hash of pubkey" */ /* ScriptSig is push of "version 0 + hash of pubkey" */
hexeq(tx->input[0].script, tx->input[0].script_length, hexeq(tx->input[0].script, tal_len(tx->input[0].script),
"16" "00" "144aa38e396e1394fb45cbf83f48d1464fbc9f498f"); "16" "00" "144aa38e396e1394fb45cbf83f48d1464fbc9f498f");
/* Witness with 2 items */ /* Witness with 2 items */

71
bitcoin/tx.c

@ -18,7 +18,7 @@ static void push_tx_input(const struct bitcoin_tx_input *input,
{ {
push(&input->txid, sizeof(input->txid), pushp); push(&input->txid, sizeof(input->txid), pushp);
push_le32(input->index, push, pushp); push_le32(input->index, push, pushp);
push_varint_blob(input->script, input->script_length, push, pushp); push_varint_blob(input->script, push, pushp);
push_le32(input->sequence_number, push, pushp); push_le32(input->sequence_number, push, pushp);
} }
@ -26,7 +26,7 @@ static void push_tx_output(const struct bitcoin_tx_output *output,
void (*push)(const void *, size_t, void *), void *pushp) void (*push)(const void *, size_t, void *), void *pushp)
{ {
push_le64(output->amount, push, pushp); push_le64(output->amount, push, pushp);
push_varint_blob(output->script, output->script_length, push, pushp); push_varint_blob(output->script, push, pushp);
} }
/* BIP 141: /* BIP 141:
@ -35,7 +35,7 @@ static void push_tx_output(const struct bitcoin_tx_output *output,
static void push_witness(const u8 *witness, static void push_witness(const u8 *witness,
void (*push)(const void *, size_t, void *), void *pushp) void (*push)(const void *, size_t, void *), void *pushp)
{ {
push_varint_blob(witness, tal_count(witness), push, pushp); push_varint_blob(witness, push, pushp);
} }
/* BIP144: /* BIP144:
@ -44,7 +44,7 @@ static bool uses_witness(const struct bitcoin_tx *tx)
{ {
size_t i; size_t i;
for (i = 0; i < tx->input_count; i++) { for (i = 0; i < tal_count(tx->input); i++) {
if (tx->input[i].witness) if (tx->input[i].witness)
return true; return true;
} }
@ -59,7 +59,7 @@ static void push_witnesses(const struct bitcoin_tx *tx,
void (*push)(const void *, size_t, void *), void *pushp) void (*push)(const void *, size_t, void *), void *pushp)
{ {
size_t i; size_t i;
for (i = 0; i < tx->input_count; i++) { for (i = 0; i < tal_count(tx->input); i++) {
size_t j, elements; size_t j, elements;
/* Not every input needs a witness. */ /* Not every input needs a witness. */
@ -106,12 +106,12 @@ static void push_tx(const struct bitcoin_tx *tx,
push(&flag, 1, pushp); push(&flag, 1, pushp);
} }
push_varint(tx->input_count, push, pushp); push_varint(tal_count(tx->input), push, pushp);
for (i = 0; i < tx->input_count; i++) for (i = 0; i < tal_count(tx->input); i++)
push_tx_input(&tx->input[i], push, pushp); push_tx_input(&tx->input[i], push, pushp);
push_varint(tx->output_count, push, pushp); push_varint(tal_count(tx->output), push, pushp);
for (i = 0; i < tx->output_count; i++) for (i = 0; i < tal_count(tx->output); i++)
push_tx_output(&tx->output[i], push, pushp); push_tx_output(&tx->output[i], push, pushp);
if (flag & SEGREGATED_WITNESS_FLAG) if (flag & SEGREGATED_WITNESS_FLAG)
@ -135,7 +135,7 @@ static void hash_prevouts(struct sha256_double *h, const struct bitcoin_tx *tx)
* double SHA256 of the serialization of all input * double SHA256 of the serialization of all input
* outpoints */ * outpoints */
sha256_init(&ctx); sha256_init(&ctx);
for (i = 0; i < tx->input_count; i++) { for (i = 0; i < tal_count(tx->input); i++) {
push_sha(&tx->input[i].txid, sizeof(tx->input[i].txid), &ctx); push_sha(&tx->input[i].txid, sizeof(tx->input[i].txid), &ctx);
push_le32(tx->input[i].index, push_sha, &ctx); push_le32(tx->input[i].index, push_sha, &ctx);
} }
@ -151,7 +151,7 @@ static void hash_sequence(struct sha256_double *h, const struct bitcoin_tx *tx)
* is set, hashSequence is the double SHA256 of the serialization * is set, hashSequence is the double SHA256 of the serialization
* of nSequence of all inputs */ * of nSequence of all inputs */
sha256_init(&ctx); sha256_init(&ctx);
for (i = 0; i < tx->input_count; i++) for (i = 0; i < tal_count(tx->input); i++)
push_le32(tx->input[i].sequence_number, push_sha, &ctx); push_le32(tx->input[i].sequence_number, push_sha, &ctx);
sha256_double_done(&ctx, h); sha256_double_done(&ctx, h);
@ -167,11 +167,9 @@ static void hash_outputs(struct sha256_double *h, const struct bitcoin_tx *tx)
size_t i; size_t i;
sha256_init(&ctx); sha256_init(&ctx);
for (i = 0; i < tx->output_count; i++) { for (i = 0; i < tal_count(tx->output); i++) {
push_le64(tx->output[i].amount, push_sha, &ctx); push_le64(tx->output[i].amount, push_sha, &ctx);
push_varint_blob(tx->output[i].script, push_varint_blob(tx->output[i].script, push_sha, &ctx);
tx->output[i].script_length,
push_sha, &ctx);
} }
sha256_double_done(&ctx, h); sha256_double_done(&ctx, h);
@ -205,7 +203,7 @@ static void hash_for_segwit(struct sha256_ctx *ctx,
push_le32(tx->input[input_num].index, push_sha, ctx); push_le32(tx->input[input_num].index, push_sha, ctx);
/* 5. scriptCode of the input (varInt for the length + script) */ /* 5. scriptCode of the input (varInt for the length + script) */
push_varint_blob(witness_script, tal_count(witness_script), push_sha, ctx); push_varint_blob(witness_script, push_sha, ctx);
/* 6. value of the output spent by this input (8-byte little end) */ /* 6. value of the output spent by this input (8-byte little end) */
push_le64(*tx->input[input_num].amount, push_sha, ctx); push_le64(*tx->input[input_num].amount, push_sha, ctx);
@ -229,10 +227,10 @@ void sha256_tx_for_sig(struct sha256_double *h, const struct bitcoin_tx *tx,
struct sha256_ctx ctx = SHA256_INIT; struct sha256_ctx ctx = SHA256_INIT;
/* Caller should zero-out other scripts for signing! */ /* Caller should zero-out other scripts for signing! */
assert(input_num < tx->input_count); assert(input_num < tal_count(tx->input));
for (i = 0; i < tx->input_count; i++) for (i = 0; i < tal_count(tx->input); i++)
if (i != input_num) if (i != input_num)
assert(tx->input[i].script_length == 0); assert(!tx->input[i].script);
if (witness_script) { if (witness_script) {
/* BIP143 hashing if OP_CHECKSIG is inside witness. */ /* BIP143 hashing if OP_CHECKSIG is inside witness. */
@ -293,11 +291,9 @@ struct bitcoin_tx *bitcoin_tx(const tal_t *ctx, varint_t input_count,
struct bitcoin_tx *tx = tal(ctx, struct bitcoin_tx); struct bitcoin_tx *tx = tal(ctx, struct bitcoin_tx);
size_t i; size_t i;
tx->output_count = output_count;
tx->output = tal_arrz(tx, struct bitcoin_tx_output, output_count); tx->output = tal_arrz(tx, struct bitcoin_tx_output, output_count);
tx->input_count = input_count;
tx->input = tal_arrz(tx, struct bitcoin_tx_input, input_count); tx->input = tal_arrz(tx, struct bitcoin_tx_input, input_count);
for (i = 0; i < tx->input_count; i++) { for (i = 0; i < tal_count(tx->input); i++) {
/* We assume NULL is a zero bitmap */ /* We assume NULL is a zero bitmap */
assert(tx->input[i].script == NULL); assert(tx->input[i].script == NULL);
tx->input[i].sequence_number = 0xFFFFFFFF; tx->input[i].sequence_number = 0xFFFFFFFF;
@ -341,9 +337,8 @@ static void pull_input(const tal_t *ctx, const u8 **cursor, size_t *max,
{ {
pull_sha256_double(cursor, max, &input->txid); pull_sha256_double(cursor, max, &input->txid);
input->index = pull_le32(cursor, max); input->index = pull_le32(cursor, max);
input->script_length = pull_length(cursor, max); input->script = tal_arr(ctx, u8, pull_length(cursor, max));
input->script = tal_arr(ctx, u8, input->script_length); pull(cursor, max, input->script, tal_len(input->script));
pull(cursor, max, input->script, input->script_length);
input->sequence_number = pull_le32(cursor, max); input->sequence_number = pull_le32(cursor, max);
} }
@ -351,9 +346,8 @@ static void pull_output(const tal_t *ctx, const u8 **cursor, size_t *max,
struct bitcoin_tx_output *output) struct bitcoin_tx_output *output)
{ {
output->amount = pull_value(cursor, max); output->amount = pull_value(cursor, max);
output->script_length = pull_length(cursor, max); output->script = tal_arr(ctx, u8, pull_length(cursor, max));
output->script = tal_arr(ctx, u8, output->script_length); pull(cursor, max, output->script, tal_len(output->script));
pull(cursor, max, output->script, output->script_length);
} }
static u8 *pull_witness_item(const tal_t *ctx, const u8 **cursor, size_t *max) static u8 *pull_witness_item(const tal_t *ctx, const u8 **cursor, size_t *max)
@ -389,32 +383,33 @@ struct bitcoin_tx *pull_bitcoin_tx(const tal_t *ctx,
{ {
struct bitcoin_tx *tx = tal(ctx, struct bitcoin_tx); struct bitcoin_tx *tx = tal(ctx, struct bitcoin_tx);
size_t i; size_t i;
u64 count;
u8 flag = 0; u8 flag = 0;
tx->version = pull_le32(cursor, max); tx->version = pull_le32(cursor, max);
tx->input_count = pull_length(cursor, max); count = pull_length(cursor, max);
/* BIP 144 marker is 0 (impossible to have tx with 0 inputs) */ /* BIP 144 marker is 0 (impossible to have tx with 0 inputs) */
if (tx->input_count == 0) { if (count == 0) {
pull(cursor, max, &flag, 1); pull(cursor, max, &flag, 1);
if (flag != SEGREGATED_WITNESS_FLAG) if (flag != SEGREGATED_WITNESS_FLAG)
return tal_free(tx); return tal_free(tx);
tx->input_count = pull_length(cursor, max); count = pull_length(cursor, max);
} }
tx->input = tal_arr(tx, struct bitcoin_tx_input, tx->input_count); tx->input = tal_arr(tx, struct bitcoin_tx_input, count);
for (i = 0; i < tx->input_count; i++) for (i = 0; i < tal_count(tx->input); i++)
pull_input(tx, cursor, max, tx->input + i); pull_input(tx, cursor, max, tx->input + i);
tx->output_count = pull_length(cursor, max); count = pull_length(cursor, max);
tx->output = tal_arr(tx, struct bitcoin_tx_output, tx->output_count); tx->output = tal_arr(tx, struct bitcoin_tx_output, count);
for (i = 0; i < tx->output_count; i++) for (i = 0; i < tal_count(tx->output); i++)
pull_output(tx, cursor, max, tx->output + i); pull_output(tx, cursor, max, tx->output + i);
if (flag & SEGREGATED_WITNESS_FLAG) { if (flag & SEGREGATED_WITNESS_FLAG) {
for (i = 0; i < tx->input_count; i++) for (i = 0; i < tal_count(tx->input); i++)
pull_witness(tx->input, i, cursor, max); pull_witness(tx->input, i, cursor, max);
} else { } else {
for (i = 0; i < tx->input_count; i++) for (i = 0; i < tal_count(tx->input); i++)
tx->input[i].witness = NULL; tx->input[i].witness = NULL;
} }
tx->lock_time = pull_le32(cursor, max); tx->lock_time = pull_le32(cursor, max);

5
bitcoin/tx.h

@ -9,24 +9,19 @@
struct bitcoin_tx { struct bitcoin_tx {
u32 version; u32 version;
varint_t input_count;
struct bitcoin_tx_input *input; struct bitcoin_tx_input *input;
varint_t output_count;
struct bitcoin_tx_output *output; struct bitcoin_tx_output *output;
u32 lock_time; u32 lock_time;
}; };
struct bitcoin_tx_output { struct bitcoin_tx_output {
u64 amount; u64 amount;
varint_t script_length;
u8 *script; u8 *script;
}; };
struct bitcoin_tx_input { struct bitcoin_tx_input {
struct sha256_double txid; struct sha256_double txid;
u32 index; /* output number referred to by above */ u32 index; /* output number referred to by above */
varint_t script_length;
u8 *script; u8 *script;
u32 sequence_number; u32 sequence_number;

2
close_tx.c

@ -26,14 +26,12 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
tx->output[0].amount = to_us; tx->output[0].amount = to_us;
tx->output[0].script = tal_dup_arr(tx, u8, tx->output[0].script = tal_dup_arr(tx, u8,
our_script, tal_count(our_script), 0); our_script, tal_count(our_script), 0);
tx->output[0].script_length = tal_count(tx->output[0].script);
/* Other output is to them. */ /* Other output is to them. */
tx->output[1].amount = to_them; tx->output[1].amount = to_them;
tx->output[1].script = tal_dup_arr(tx, u8, tx->output[1].script = tal_dup_arr(tx, u8,
their_script, tal_count(their_script), their_script, tal_count(their_script),
0); 0);
tx->output[1].script_length = tal_count(tx->output[1].script);
assert(tx->output[0].amount + tx->output[1].amount <= anchor_satoshis); assert(tx->output[0].amount + tx->output[1].amount <= anchor_satoshis);

2
daemon/chaintopology.c

@ -162,7 +162,7 @@ static void connect_block(struct lightningd_state *dstate,
size_t j; size_t j;
/* Tell them if it spends a txo we care about. */ /* Tell them if it spends a txo we care about. */
for (j = 0; j < tx->input_count; j++) { for (j = 0; j < tal_count(tx->input); j++) {
struct txwatch_output out; struct txwatch_output out;
struct txowatch *txo; struct txowatch *txo;
out.txid = tx->input[j].txid; out.txid = tx->input[j].txid;

28
daemon/commit_tx.c

@ -112,15 +112,15 @@ u8 *commit_output_to_them(const tal_t *ctx,
/* Takes ownership of script. */ /* Takes ownership of script. */
static bool add_output(struct bitcoin_tx *tx, u8 *script, u64 amount, static bool add_output(struct bitcoin_tx *tx, u8 *script, u64 amount,
size_t *output_count,
u64 *total) u64 *total)
{ {
assert(tx->output_count < tal_count(tx->output)); assert(*output_count < tal_count(tx->output));
if (is_dust(amount)) if (is_dust(amount))
return false; return false;
tx->output[tx->output_count].script = tal_steal(tx, script); tx->output[*output_count].script = tal_steal(tx, script);
tx->output[tx->output_count].script_length = tal_count(script); tx->output[*output_count].amount = amount;
tx->output[tx->output_count].amount = amount; (*output_count)++;
tx->output_count++;
(*total) += amount; (*total) += amount;
return true; return true;
} }
@ -137,6 +137,7 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
uint64_t total = 0; uint64_t total = 0;
struct htlc_map_iter it; struct htlc_map_iter it;
struct htlc *h; struct htlc *h;
size_t output_count;
bool pays_to[2]; bool pays_to[2];
int committed_flag = HTLC_FLAG(side,HTLC_F_COMMITTED); int committed_flag = HTLC_FLAG(side,HTLC_F_COMMITTED);
@ -159,17 +160,16 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
tx->input[0].index = peer->anchor.index; tx->input[0].index = peer->anchor.index;
tx->input[0].amount = tal_dup(tx->input, u64, &peer->anchor.satoshis); tx->input[0].amount = tal_dup(tx->input, u64, &peer->anchor.satoshis);
tx->output_count = 0; output_count = 0;
pays_to[LOCAL] = add_output(tx, commit_output_to_us(tmpctx, peer, rhash, pays_to[LOCAL] = add_output(tx, commit_output_to_us(tmpctx, peer, rhash,
side, NULL), side, NULL),
cstate->side[LOCAL].pay_msat / 1000, cstate->side[LOCAL].pay_msat / 1000,
&output_count,
&total); &total);
if (pays_to[LOCAL]) if (pays_to[LOCAL])
log_debug(peer->log, "Pays %u to local: %s", log_debug(peer->log, "Pays %u to local: %s",
cstate->side[LOCAL].pay_msat / 1000, cstate->side[LOCAL].pay_msat / 1000,
tal_hexstr(tmpctx, tal_hex(tmpctx, tx->output[output_count-1].script));
tx->output[tx->output_count-1].script,
tx->output[tx->output_count-1].script_length));
else else
log_debug(peer->log, "DOES NOT pay %u to local", log_debug(peer->log, "DOES NOT pay %u to local",
cstate->side[LOCAL].pay_msat / 1000); cstate->side[LOCAL].pay_msat / 1000);
@ -177,13 +177,12 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
rhash, side, rhash, side,
NULL), NULL),
cstate->side[REMOTE].pay_msat / 1000, cstate->side[REMOTE].pay_msat / 1000,
&output_count,
&total); &total);
if (pays_to[REMOTE]) if (pays_to[REMOTE])
log_debug(peer->log, "Pays %u to remote: %s", log_debug(peer->log, "Pays %u to remote: %s",
cstate->side[REMOTE].pay_msat / 1000, cstate->side[REMOTE].pay_msat / 1000,
tal_hexstr(tmpctx, tal_hex(tmpctx, tx->output[output_count-1].script));
tx->output[tx->output_count-1].script,
tx->output[tx->output_count-1].script_length));
else else
log_debug(peer->log, "DOES NOT pay %u to remote", log_debug(peer->log, "DOES NOT pay %u to remote",
cstate->side[REMOTE].pay_msat / 1000); cstate->side[REMOTE].pay_msat / 1000);
@ -202,7 +201,7 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
wscript = wscript_for_htlc(tmpctx, peer, h, rhash, side); wscript = wscript_for_htlc(tmpctx, peer, h, rhash, side);
/* If we pay any HTLC, it's txout is not just to other side. */ /* If we pay any HTLC, it's txout is not just to other side. */
if (add_output(tx, scriptpubkey_p2wsh(tmpctx, wscript), if (add_output(tx, scriptpubkey_p2wsh(tmpctx, wscript),
h->msatoshi / 1000, &total)) { h->msatoshi / 1000, &output_count, &total)) {
*otherside_only = false; *otherside_only = false;
log_debug(peer->log, "Pays %"PRIu64" to htlc %"PRIu64, log_debug(peer->log, "Pays %"PRIu64" to htlc %"PRIu64,
h->msatoshi / 1000, h->id); h->msatoshi / 1000, h->id);
@ -218,7 +217,8 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
} }
assert(total <= peer->anchor.satoshis); assert(total <= peer->anchor.satoshis);
permute_outputs(tx->output, tx->output_count); tal_resize(&tx->output, output_count);
permute_outputs(tx->output, tal_count(tx->output));
tal_free(tmpctx); tal_free(tmpctx);
return tx; return tx;
} }

8
daemon/output_to_htlc.c

@ -49,11 +49,11 @@ struct htlc_output_map *get_htlc_output_map(const tal_t *ctx,
} }
static struct wscript_by_wpkh *get_wpkh(struct htlc_output_map *omap, static struct wscript_by_wpkh *get_wpkh(struct htlc_output_map *omap,
const u8 *script, size_t script_len) const u8 *script)
{ {
size_t i; size_t i;
if (!is_p2wsh(script, script_len)) if (!is_p2wsh(script))
return NULL; return NULL;
for (i = 0; i < tal_count(omap->wpkh); i++) { for (i = 0; i < tal_count(omap->wpkh); i++) {
@ -66,10 +66,10 @@ static struct wscript_by_wpkh *get_wpkh(struct htlc_output_map *omap,
/* Which wscript does this pay to? */ /* Which wscript does this pay to? */
struct htlc *txout_get_htlc(struct htlc_output_map *omap, struct htlc *txout_get_htlc(struct htlc_output_map *omap,
const u8 *script, size_t script_len, const u8 *script,
const u8 **wscript) const u8 **wscript)
{ {
struct wscript_by_wpkh *wpkh = get_wpkh(omap, script, script_len); struct wscript_by_wpkh *wpkh = get_wpkh(omap, script);
if (wpkh) { if (wpkh) {
*wscript = wpkh->wscript; *wscript = wpkh->wscript;

3
daemon/output_to_htlc.h

@ -15,7 +15,6 @@ struct htlc_output_map *get_htlc_output_map(const tal_t *ctx,
/* If this scriptPubkey pays to a HTLC, get the full wscript */ /* If this scriptPubkey pays to a HTLC, get the full wscript */
struct htlc *txout_get_htlc(struct htlc_output_map *omap, struct htlc *txout_get_htlc(struct htlc_output_map *omap,
const u8 *script, size_t script_len, const u8 *script, const u8 **wscript);
const u8 **wscript);
#endif /* LIGHTNING_DAEMON_OUTPUT_TO_HTLC_H */ #endif /* LIGHTNING_DAEMON_OUTPUT_TO_HTLC_H */

18
daemon/packets.c

@ -568,6 +568,10 @@ Pkt *accept_pkt_close_shutdown(struct peer *peer, const Pkt *pkt)
{ {
const CloseShutdown *c = pkt->close_shutdown; const CloseShutdown *c = pkt->close_shutdown;
peer->closing.their_script = tal_dup_arr(peer, u8,
c->scriptpubkey.data,
c->scriptpubkey.len, 0);
/* FIXME-OLD #2: /* FIXME-OLD #2:
* *
* 1. `OP_DUP` `OP_HASH160` `20` 20-bytes `OP_EQUALVERIFY` `OP_CHECKSIG` * 1. `OP_DUP` `OP_HASH160` `20` 20-bytes `OP_EQUALVERIFY` `OP_CHECKSIG`
@ -579,17 +583,15 @@ Pkt *accept_pkt_close_shutdown(struct peer *peer, const Pkt *pkt)
* A node receiving `close_shutdown` SHOULD fail the connection * A node receiving `close_shutdown` SHOULD fail the connection
* `script_pubkey` is not one of those forms. * `script_pubkey` is not one of those forms.
*/ */
if (!is_p2pkh(c->scriptpubkey.data, c->scriptpubkey.len) if (!is_p2pkh(peer->closing.their_script)
&& !is_p2sh(c->scriptpubkey.data, c->scriptpubkey.len) && !is_p2sh(peer->closing.their_script)
&& !is_p2wpkh(c->scriptpubkey.data, c->scriptpubkey.len) && !is_p2wpkh(peer->closing.their_script)
&& !is_p2wsh(c->scriptpubkey.data, c->scriptpubkey.len)) { && !is_p2wsh(peer->closing.their_script)) {
log_broken_blob(peer->log, "Bad script_pubkey %s", log_broken_blob(peer->log, "Bad script_pubkey %s",
c->scriptpubkey.data, c->scriptpubkey.len); peer->closing.their_script,
tal_count(peer->closing.their_script));
return pkt_err(peer, "Bad script_pubkey"); return pkt_err(peer, "Bad script_pubkey");
} }
peer->closing.their_script = tal_dup_arr(peer, u8,
c->scriptpubkey.data,
c->scriptpubkey.len, 0);
return NULL; return NULL;
} }

72
daemon/peer.c

@ -125,7 +125,6 @@ static const struct bitcoin_tx *bitcoin_spend_ours(struct peer *peer)
tx->output[0].script = scriptpubkey_p2sh(tx, tx->output[0].script = scriptpubkey_p2sh(tx,
bitcoin_redeem_single(tx, bitcoin_redeem_single(tx,
&peer->local.finalkey)); &peer->local.finalkey));
tx->output[0].script_length = tal_count(tx->output[0].script);
/* Witness length can vary, due to DER encoding of sigs, but we /* Witness length can vary, due to DER encoding of sigs, but we
* use 176 from an example run. */ * use 176 from an example run. */
@ -156,7 +155,7 @@ static void sign_commit_tx(struct peer *peer)
secp256k1_ecdsa_signature sig; secp256k1_ecdsa_signature sig;
/* Can't be signed already, and can't have scriptsig! */ /* Can't be signed already, and can't have scriptsig! */
assert(peer->local.commit->tx->input[0].script_length == 0); assert(!peer->local.commit->tx->input[0].script);
assert(!peer->local.commit->tx->input[0].witness); assert(!peer->local.commit->tx->input[0].witness);
peer_sign_ourcommit(peer, peer->local.commit->tx, &sig); peer_sign_ourcommit(peer, peer->local.commit->tx, &sig);
@ -173,7 +172,7 @@ static u64 commit_tx_fee(const struct bitcoin_tx *commit, u64 anchor_satoshis)
{ {
uint64_t i, total = 0; uint64_t i, total = 0;
for (i = 0; i < commit->output_count; i++) for (i = 0; i < tal_count(commit->output); i++)
total += commit->output[i].amount; total += commit->output[i].amount;
assert(anchor_satoshis >= total); assert(anchor_satoshis >= total);
@ -512,8 +511,6 @@ static bool bitcoin_create_anchor(struct peer *peer)
assert(peer->local.offer_anchor); assert(peer->local.offer_anchor);
tx->output[0].script = scriptpubkey_p2wsh(tx, peer->anchor.witnessscript); tx->output[0].script = scriptpubkey_p2wsh(tx, peer->anchor.witnessscript);
tx->output[0].script_length = tal_count(tx->output[0].script);
tx->output[0].amount = peer->anchor.input->out_amount; tx->output[0].amount = peer->anchor.input->out_amount;
tx->input[0].txid = peer->anchor.input->txid; tx->input[0].txid = peer->anchor.input->txid;
@ -533,7 +530,7 @@ static bool bitcoin_create_anchor(struct peer *peer)
peer->anchor.satoshis = tx->output[0].amount; peer->anchor.satoshis = tx->output[0].amount;
/* To avoid malleation, all inputs must be segwit! */ /* To avoid malleation, all inputs must be segwit! */
for (i = 0; i < tx->input_count; i++) for (i = 0; i < tal_count(tx->input); i++)
assert(tx->input[i].witness); assert(tx->input[i].witness);
return true; return true;
} }
@ -625,7 +622,7 @@ static bool open_ouranchor_pkt_in(struct peer *peer, const Pkt *pkt)
peer->local.commit->sig); peer->local.commit->sig);
if (!err && if (!err &&
!check_tx_sig(peer->local.commit->tx, 0, !check_tx_sig(peer->local.commit->tx, 0,
NULL, 0, NULL,
peer->anchor.witnessscript, peer->anchor.witnessscript,
&peer->remote.commitkey, &peer->remote.commitkey,
peer->local.commit->sig)) peer->local.commit->sig))
@ -1236,7 +1233,7 @@ static bool closing_pkt_in(struct peer *peer, const Pkt *pkt)
close_tx = peer_create_close_tx(c, peer, c->close_fee); close_tx = peer_create_close_tx(c, peer, c->close_fee);
if (!check_tx_sig(close_tx, 0, if (!check_tx_sig(close_tx, 0,
NULL, 0, NULL,
peer->anchor.witnessscript, peer->anchor.witnessscript,
&peer->remote.commitkey, &theirsig)) &peer->remote.commitkey, &theirsig))
return peer_comms_err(peer, return peer_comms_err(peer,
@ -1389,7 +1386,7 @@ static Pkt *handle_pkt_commit(struct peer *peer, const Pkt *pkt)
* check `sig` is valid for that transaction. * check `sig` is valid for that transaction.
*/ */
if (ci->sig && !check_tx_sig(ci->tx, 0, if (ci->sig && !check_tx_sig(ci->tx, 0,
NULL, 0, NULL,
peer->anchor.witnessscript, peer->anchor.witnessscript,
&peer->remote.commitkey, &peer->remote.commitkey,
ci->sig)) { ci->sig)) {
@ -1963,7 +1960,6 @@ static const struct bitcoin_tx *htlc_fulfill_tx(const struct peer *peer,
tx->output[0].script = scriptpubkey_p2sh(tx, tx->output[0].script = scriptpubkey_p2sh(tx,
bitcoin_redeem_single(tx, bitcoin_redeem_single(tx,
&peer->local.finalkey)); &peer->local.finalkey));
tx->output[0].script_length = tal_count(tx->output[0].script);
log_debug(peer->log, "Pre-witness txlen = %zu\n", log_debug(peer->log, "Pre-witness txlen = %zu\n",
measure_tx_cost(tx) / 4); measure_tx_cost(tx) / 4);
@ -3181,12 +3177,12 @@ static void json_connect(struct command *cmd,
bitcoin_txid(tx, &connect->input->txid); bitcoin_txid(tx, &connect->input->txid);
/* Find an output we know how to spend. */ /* Find an output we know how to spend. */
for (output = 0; output < tx->output_count; output++) { for (output = 0; output < tal_count(tx->output); output++) {
if (wallet_can_spend(cmd->dstate, &tx->output[output], if (wallet_can_spend(cmd->dstate, &tx->output[output],
&connect->input->walletkey)) &connect->input->walletkey))
break; break;
} }
if (output == tx->output_count) { if (output == tal_count(tx->output)) {
command_fail(cmd, "Tx doesn't send to wallet address"); command_fail(cmd, "Tx doesn't send to wallet address");
return; return;
} }
@ -3403,9 +3399,9 @@ void peers_new_block(struct lightningd_state *dstate, unsigned int height)
static bool outputscript_eq(const struct bitcoin_tx_output *out, static bool outputscript_eq(const struct bitcoin_tx_output *out,
size_t i, const u8 *script) size_t i, const u8 *script)
{ {
if (out[i].script_length != tal_count(script)) if (tal_count(out[i].script) != tal_count(script))
return false; return false;
return memcmp(out[i].script, script, out[i].script_length) == 0; return memcmp(out[i].script, script, tal_count(script)) == 0;
} }
/* This tx is their commitment; /* This tx is their commitment;
@ -3421,8 +3417,8 @@ static bool map_onchain_outputs(struct peer *peer,
size_t i; size_t i;
peer->onchain.to_us_idx = peer->onchain.to_them_idx = -1; peer->onchain.to_us_idx = peer->onchain.to_them_idx = -1;
peer->onchain.htlcs = tal_arr(tx, struct htlc *, tx->output_count); peer->onchain.htlcs = tal_arr(tx, struct htlc *, tal_count(tx->output));
peer->onchain.wscripts = tal_arr(tx, const u8 *, tx->output_count); peer->onchain.wscripts = tal_arr(tx, const u8 *, tal_count(tx->output));
to_us = commit_output_to_us(tx, peer, rhash, side, &to_us_wscript); to_us = commit_output_to_us(tx, peer, rhash, side, &to_us_wscript);
to_them = commit_output_to_them(tx, peer, rhash, side, to_them = commit_output_to_them(tx, peer, rhash, side,
@ -3431,7 +3427,7 @@ static bool map_onchain_outputs(struct peer *peer,
/* Now generate the wscript hashes for every possible HTLC. */ /* Now generate the wscript hashes for every possible HTLC. */
hmap = get_htlc_output_map(tx, peer, rhash, side, commit_num); hmap = get_htlc_output_map(tx, peer, rhash, side, commit_num);
for (i = 0; i < tx->output_count; i++) { for (i = 0; i < tal_count(tx->output); i++) {
log_debug(peer->log, "%s: output %zi", __func__, i); log_debug(peer->log, "%s: output %zi", __func__, i);
if (peer->onchain.to_us_idx == -1 if (peer->onchain.to_us_idx == -1
&& outputscript_eq(tx->output, i, to_us)) { && outputscript_eq(tx->output, i, to_us)) {
@ -3452,7 +3448,6 @@ static bool map_onchain_outputs(struct peer *peer,
/* Must be an HTLC output */ /* Must be an HTLC output */
peer->onchain.htlcs[i] = txout_get_htlc(hmap, peer->onchain.htlcs[i] = txout_get_htlc(hmap,
tx->output[i].script, tx->output[i].script,
tx->output[i].script_length,
peer->onchain.wscripts+i); peer->onchain.wscripts+i);
if (!peer->onchain.htlcs[i]) { if (!peer->onchain.htlcs[i]) {
log_add(peer->log, "no HTLC found"); log_add(peer->log, "no HTLC found");
@ -3481,20 +3476,16 @@ static bool is_mutual_close(const struct peer *peer,
if (!ours || !theirs) if (!ours || !theirs)
return false; return false;
if (tx->output_count != 2) if (tal_count(tx->output) != 2)
return false; return false;
/* Without knowing fee amounts, can't determine order. Check both. */ /* Without knowing fee amounts, can't determine order. Check both. */
if (scripteq(tx->output[0].script, tx->output[0].script_length, if (scripteq(tx->output[0].script, ours)
ours, tal_count(ours)) && scripteq(tx->output[1].script, theirs))
&& scripteq(tx->output[1].script, tx->output[1].script_length,
theirs, tal_count(theirs)))
return true; return true;
if (scripteq(tx->output[0].script, tx->output[0].script_length, if (scripteq(tx->output[0].script, theirs)
theirs, tal_count(theirs)) && scripteq(tx->output[1].script, ours))
&& scripteq(tx->output[1].script, tx->output[1].script_length,
ours, tal_count(ours)))
return true; return true;
return false; return false;
@ -3523,7 +3514,6 @@ static const struct bitcoin_tx *htlc_timeout_tx(const struct peer *peer,
tx->output[0].script = scriptpubkey_p2sh(tx, tx->output[0].script = scriptpubkey_p2sh(tx,
bitcoin_redeem_single(tx, bitcoin_redeem_single(tx,
&peer->local.finalkey)); &peer->local.finalkey));
tx->output[0].script_length = tal_count(tx->output[0].script);
log_unusual(peer->log, "Pre-witness txlen = %zu\n", log_unusual(peer->log, "Pre-witness txlen = %zu\n",
measure_tx_cost(tx) / 4); measure_tx_cost(tx) / 4);
@ -3859,7 +3849,7 @@ static void resolve_our_unilateral(struct peer *peer)
* resolved[] entries for these uncommitted HTLCs, too. */ * resolved[] entries for these uncommitted HTLCs, too. */
watch_tx(tx, peer, tx, our_unilateral_depth, NULL); watch_tx(tx, peer, tx, our_unilateral_depth, NULL);
for (i = 0; i < tx->output_count; i++) { for (i = 0; i < tal_count(tx->output); i++) {
/* FIXME-OLD #onchain: /* FIXME-OLD #onchain:
* *
* 1. _A's main output_: A node SHOULD spend this output to a * 1. _A's main output_: A node SHOULD spend this output to a
@ -3906,7 +3896,7 @@ static void resolve_their_unilateral(struct peer *peer)
unsigned int i; unsigned int i;
const struct bitcoin_tx *tx = peer->onchain.tx; const struct bitcoin_tx *tx = peer->onchain.tx;
for (i = 0; i < tx->output_count; i++) { for (i = 0; i < tal_count(tx->output); i++) {
/* FIXME-OLD #onchain: /* FIXME-OLD #onchain:
* *
* 1. _A's main output_: No action is required; this is a * 1. _A's main output_: No action is required; this is a
@ -3948,13 +3938,13 @@ static void resolve_mutual_close(struct peer *peer)
* the output, which is sent to its specified scriptpubkey (see FIXME-OLD * the output, which is sent to its specified scriptpubkey (see FIXME-OLD
* #2 "4.1: Closing initiation: close_shutdown"). * #2 "4.1: Closing initiation: close_shutdown").
*/ */
for (i = 0; i < peer->onchain.tx->output_count; i++) for (i = 0; i < tal_count(peer->onchain.tx->output); i++)
peer->onchain.resolved[i] = irrevocably_resolved(peer); peer->onchain.resolved[i] = irrevocably_resolved(peer);
/* No HTLCs. */ /* No HTLCs. */
peer->onchain.htlcs = tal_arrz(peer->onchain.tx, peer->onchain.htlcs = tal_arrz(peer->onchain.tx,
struct htlc *, struct htlc *,
peer->onchain.tx->output_count); tal_count(peer->onchain.tx->output));
} }
/* Called every time the tx spending the funding tx changes depth. */ /* Called every time the tx spending the funding tx changes depth. */
@ -4040,13 +4030,13 @@ static void resolve_their_steal(struct peer *peer,
/* Create steal_tx: don't need to steal to_us output */ /* Create steal_tx: don't need to steal to_us output */
if (peer->onchain.to_us_idx == -1) if (peer->onchain.to_us_idx == -1)
steal_tx = bitcoin_tx(tx, tx->output_count, 1); steal_tx = bitcoin_tx(tx, tal_count(tx->output), 1);
else else
steal_tx = bitcoin_tx(tx, tx->output_count - 1, 1); steal_tx = bitcoin_tx(tx, tal_count(tx->output) - 1, 1);
n = 0; n = 0;
log_debug(peer->log, "Analyzing tx to steal:"); log_debug(peer->log, "Analyzing tx to steal:");
for (i = 0; i < tx->output_count; i++) { for (i = 0; i < tal_count(tx->output); i++) {
/* FIXME-OLD #onchain: /* FIXME-OLD #onchain:
* 1. _A's main output_: No action is required; this is a * 1. _A's main output_: No action is required; this is a
* simple P2WPKH output. This output is considered * simple P2WPKH output. This output is considered
@ -4081,7 +4071,7 @@ static void resolve_their_steal(struct peer *peer,
input_total += tx->output[i].amount; input_total += tx->output[i].amount;
n++; n++;
} }
assert(n == steal_tx->input_count); assert(n == tal_count(steal_tx->input));
fee = get_feerate(peer->dstate) fee = get_feerate(peer->dstate)
* (measure_tx_cost(steal_tx) + wsize) / 1000; * (measure_tx_cost(steal_tx) + wsize) / 1000;
@ -4099,11 +4089,10 @@ static void resolve_their_steal(struct peer *peer,
steal_tx->output[0].script = scriptpubkey_p2sh(steal_tx, steal_tx->output[0].script = scriptpubkey_p2sh(steal_tx,
bitcoin_redeem_single(steal_tx, bitcoin_redeem_single(steal_tx,
&peer->local.finalkey)); &peer->local.finalkey));
steal_tx->output[0].script_length = tal_count(steal_tx->output[0].script);
/* Now, we can sign them all (they're all of same form). */ /* Now, we can sign them all (they're all of same form). */
n = 0; n = 0;
for (i = 0; i < tx->output_count; i++) { for (i = 0; i < tal_count(tx->output); i++) {
secp256k1_ecdsa_signature sig; secp256k1_ecdsa_signature sig;
/* Don't bother stealing the output already to us. */ /* Don't bother stealing the output already to us. */
@ -4122,7 +4111,7 @@ static void resolve_their_steal(struct peer *peer,
peer->onchain.wscripts[i]); peer->onchain.wscripts[i]);
n++; n++;
} }
assert(n == steal_tx->input_count); assert(n == tal_count(steal_tx->input));
broadcast_tx(peer, steal_tx, NULL); broadcast_tx(peer, steal_tx, NULL);
} }
@ -4165,7 +4154,7 @@ static enum watch_result anchor_spent(struct peer *peer,
struct htlc *h; struct htlc *h;
u64 commit_num; u64 commit_num;
assert(input_num < tx->input_count); assert(input_num < tal_count(tx->input));
/* We only ever sign single-input txs. */ /* We only ever sign single-input txs. */
if (input_num != 0) { if (input_num != 0) {
@ -4190,7 +4179,8 @@ static enum watch_result anchor_spent(struct peer *peer,
/* We need to resolve every output. */ /* We need to resolve every output. */
peer->onchain.resolved peer->onchain.resolved
= tal_arrz(tx, const struct bitcoin_tx *, tx->output_count); = tal_arrz(tx, const struct bitcoin_tx *,
tal_count(tx->output));
/* A mutual close tx. */ /* A mutual close tx. */
if (is_mutual_close(peer, tx)) { if (is_mutual_close(peer, tx)) {

14
daemon/secrets.c

@ -48,7 +48,7 @@ void peer_sign_theircommit(const struct peer *peer,
{ {
/* Commit tx only has one input: that of the anchor. */ /* Commit tx only has one input: that of the anchor. */
sign_tx_input(commit, 0, sign_tx_input(commit, 0,
NULL, 0, NULL,
peer->anchor.witnessscript, peer->anchor.witnessscript,
&peer->secrets->commit, &peer->secrets->commit,
&peer->local.commitkey, &peer->local.commitkey,
@ -61,7 +61,7 @@ void peer_sign_ourcommit(const struct peer *peer,
{ {
/* Commit tx only has one input: that of the anchor. */ /* Commit tx only has one input: that of the anchor. */
sign_tx_input(commit, 0, sign_tx_input(commit, 0,
NULL, 0, NULL,
peer->anchor.witnessscript, peer->anchor.witnessscript,
&peer->secrets->commit, &peer->secrets->commit,
&peer->local.commitkey, &peer->local.commitkey,
@ -75,7 +75,7 @@ void peer_sign_spend(const struct peer *peer,
{ {
/* Spend tx only has one input: that of the commit tx. */ /* Spend tx only has one input: that of the commit tx. */
sign_tx_input(spend, 0, sign_tx_input(spend, 0,
NULL, 0, NULL,
commit_witnessscript, commit_witnessscript,
&peer->secrets->final, &peer->secrets->final,
&peer->local.finalkey, &peer->local.finalkey,
@ -89,7 +89,7 @@ void peer_sign_htlc_refund(const struct peer *peer,
{ {
/* Spend tx only has one input: that of the commit tx. */ /* Spend tx only has one input: that of the commit tx. */
sign_tx_input(spend, 0, sign_tx_input(spend, 0,
NULL, 0, NULL,
htlc_witnessscript, htlc_witnessscript,
&peer->secrets->final, &peer->secrets->final,
&peer->local.finalkey, &peer->local.finalkey,
@ -103,7 +103,7 @@ void peer_sign_htlc_fulfill(const struct peer *peer,
{ {
/* Spend tx only has one input: that of the commit tx. */ /* Spend tx only has one input: that of the commit tx. */
sign_tx_input(spend, 0, sign_tx_input(spend, 0,
NULL, 0, NULL,
htlc_witnessscript, htlc_witnessscript,
&peer->secrets->final, &peer->secrets->final,
&peer->local.finalkey, &peer->local.finalkey,
@ -115,7 +115,7 @@ void peer_sign_mutual_close(const struct peer *peer,
secp256k1_ecdsa_signature *sig) secp256k1_ecdsa_signature *sig)
{ {
sign_tx_input(close, 0, sign_tx_input(close, 0,
NULL, 0, NULL,
peer->anchor.witnessscript, peer->anchor.witnessscript,
&peer->secrets->commit, &peer->secrets->commit,
&peer->local.commitkey, &peer->local.commitkey,
@ -130,7 +130,7 @@ void peer_sign_steal_input(const struct peer *peer,
{ {
/* Spend tx only has one input: that of the commit tx. */ /* Spend tx only has one input: that of the commit tx. */
sign_tx_input(spend, i, sign_tx_input(spend, i,
NULL, 0, NULL,
witnessscript, witnessscript,
&peer->secrets->final, &peer->secrets->final,
&peer->local.finalkey, &peer->local.finalkey,

6
daemon/wallet.c

@ -70,14 +70,14 @@ bool wallet_add_signed_input(struct lightningd_state *dstate,
secp256k1_ecdsa_signature sig; secp256k1_ecdsa_signature sig;
struct wallet *w = find_by_pubkey(dstate, walletkey); struct wallet *w = find_by_pubkey(dstate, walletkey);
assert(input_num < tx->input_count); assert(input_num < tal_count(tx->input));
if (!w) if (!w)
return false; return false;
redeemscript = bitcoin_redeem_p2wpkh(tx, &w->pubkey); redeemscript = bitcoin_redeem_p2wpkh(tx, &w->pubkey);
sign_tx_input(tx, input_num, sign_tx_input(tx, input_num,
redeemscript, tal_count(redeemscript), redeemscript,
p2wpkh_scriptcode(redeemscript, &w->pubkey), p2wpkh_scriptcode(redeemscript, &w->pubkey),
&w->privkey, &w->privkey,
&w->pubkey, &w->pubkey,
@ -98,7 +98,7 @@ bool wallet_can_spend(struct lightningd_state *dstate,
struct ripemd160 h; struct ripemd160 h;
struct wallet *w; struct wallet *w;
if (!is_p2sh(output->script, output->script_length)) if (!is_p2sh(output->script))
return NULL; return NULL;
memcpy(&h, output->script + 2, 20); memcpy(&h, output->script + 2, 20);

9
find_p2sh_out.c

@ -9,15 +9,12 @@ static u32 find_output(const struct bitcoin_tx *tx, const u8 *scriptpubkey)
{ {
u32 i; u32 i;
for (i = 0; i < tx->output_count; i++) { for (i = 0; i < tal_count(tx->output); i++) {
if (scripteq(tx->output[i].script, if (scripteq(tx->output[i].script, scriptpubkey))
tx->output[i].script_length,
scriptpubkey,
tal_count(scriptpubkey)))
break; break;
} }
/* FIXME: Return failure! */ /* FIXME: Return failure! */
if (i == tx->output_count) if (i == tal_count(tx->output))
errx(1, "No matching output in tx"); errx(1, "No matching output in tx");
return i; return i;
} }

14
permute_tx.c

@ -14,9 +14,9 @@ static bool input_better(const struct bitcoin_tx_input *a,
return a->index < b->index; return a->index < b->index;
/* These shouldn't happen, but let's get a canonical order anyway. */ /* These shouldn't happen, but let's get a canonical order anyway. */
if (a->script_length != b->script_length) if (tal_len(a->script) != tal_len(b->script))
return a->script_length < b->script_length; return tal_len(a->script) < tal_len(b->script);
cmp = memcmp(a->script, b->script, a->script_length); cmp = memcmp(a->script, b->script, tal_len(a->script));
if (cmp != 0) if (cmp != 0)
return cmp < 0; return cmp < 0;
return a->sequence_number < b->sequence_number; return a->sequence_number < b->sequence_number;
@ -74,16 +74,16 @@ static bool output_better(const struct bitcoin_tx_output *a,
return a->amount < b->amount; return a->amount < b->amount;
/* Lexographic sort. */ /* Lexographic sort. */
if (a->script_length < b->script_length) if (tal_len(a->script) < tal_len(b->script))
len = a->script_length; len = tal_len(a->script);
else else
len = b->script_length; len = tal_len(b->script);
ret = memcmp(a->script, b->script, len); ret = memcmp(a->script, b->script, len);
if (ret != 0) if (ret != 0)
return ret < 0; return ret < 0;
return a->script_length < b->script_length; return tal_len(a->script) < tal_len(b->script);
} }
static size_t find_best_out(struct bitcoin_tx_output *outputs, size_t num) static size_t find_best_out(struct bitcoin_tx_output *outputs, size_t num)

Loading…
Cancel
Save