diff --git a/commit_tx.c b/commit_tx.c index 229ff93af..989aff254 100644 --- a/commit_tx.c +++ b/commit_tx.c @@ -43,16 +43,17 @@ static bool add_htlc(struct bitcoin_tx *tx, size_t n, } struct bitcoin_tx *create_commit_tx(const tal_t *ctx, - OpenChannel *ours, - OpenChannel *theirs, - OpenAnchor *anchor, + const struct pubkey *our_final, + const struct pubkey *their_final, + const struct rel_locktime *their_locktime, + const struct sha256_double *anchor_txid, + unsigned int anchor_index, + u64 anchor_satoshis, const struct sha256 *rhash, const struct channel_state *cstate) { struct bitcoin_tx *tx; const u8 *redeemscript; - struct pubkey ourkey, theirkey; - struct rel_locktime locktime; size_t i, num; uint64_t total; @@ -61,32 +62,23 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx, + tal_count(cstate->b.htlcs)); /* Our input spends the anchor tx output. */ - proto_to_sha256(anchor->txid, &tx->input[0].txid.sha); - tx->input[0].index = anchor->output_index; - tx->input[0].input_amount = anchor->amount; - - /* Output goes to our final pubkeys */ - if (!proto_to_pubkey(ours->final_key, &ourkey)) - return tal_free(tx); - if (!proto_to_pubkey(theirs->final_key, &theirkey)) - return tal_free(tx); - - if (!proto_to_rel_locktime(theirs->delay, &locktime)) - return tal_free(tx); + tx->input[0].txid = *anchor_txid; + tx->input[0].index = anchor_index; + tx->input[0].input_amount = anchor_satoshis; /* First output is a P2SH to a complex redeem script (usu. for me) */ - redeemscript = bitcoin_redeem_secret_or_delay(tx, &ourkey, - &locktime, - &theirkey, + redeemscript = bitcoin_redeem_secret_or_delay(tx, our_final, + their_locktime, + their_final, rhash); tx->output[0].script = scriptpubkey_p2sh(tx, redeemscript); tx->output[0].script_length = tal_count(tx->output[0].script); tx->output[0].amount = cstate->a.pay_msat / 1000; /* Second output is a P2SH payment to them. */ - tx->output[1].script = scriptpubkey_p2sh(ctx, - bitcoin_redeem_single(ctx, - &theirkey)); + tx->output[1].script = scriptpubkey_p2sh(tx, + bitcoin_redeem_single(tx, + their_final)); tx->output[1].script_length = tal_count(tx->output[1].script); tx->output[1].amount = cstate->b.pay_msat / 1000; @@ -96,15 +88,17 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx, /* HTLCs we've sent. */ for (i = 0; i < tal_count(cstate->a.htlcs); i++) { - if (!add_htlc(tx, num, cstate->a.htlcs[i], &ourkey, &theirkey, - rhash, &locktime, scriptpubkey_htlc_send)) + if (!add_htlc(tx, num, cstate->a.htlcs[i], + our_final, their_final, + rhash, their_locktime, scriptpubkey_htlc_send)) return tal_free(tx); total += tx->output[num++].amount; } /* HTLCs we've received. */ for (i = 0; i < tal_count(cstate->b.htlcs); i++) { - if (!add_htlc(tx, num, cstate->b.htlcs[i], &ourkey, &theirkey, - rhash, &locktime, scriptpubkey_htlc_recv)) + if (!add_htlc(tx, num, cstate->b.htlcs[i], + our_final, their_final, + rhash, their_locktime, scriptpubkey_htlc_recv)) return tal_free(tx); total += tx->output[num++].amount; } diff --git a/commit_tx.h b/commit_tx.h index 8347c3142..b35b563a0 100644 --- a/commit_tx.h +++ b/commit_tx.h @@ -1,19 +1,23 @@ #ifndef LIGHTNING_COMMIT_TX_H #define LIGHTNING_COMMIT_TX_H #include "config.h" -#include "lightning.pb-c.h" +#include #include struct channel_state; struct sha256_double; -struct sha256; +struct pubkey; +struct rel_locktime; /* Create commitment tx to spend the anchor tx output; doesn't fill in * input scriptsig. */ struct bitcoin_tx *create_commit_tx(const tal_t *ctx, - OpenChannel *ours, - OpenChannel *theirs, - OpenAnchor *anchor, + const struct pubkey *our_final, + const struct pubkey *their_final, + const struct rel_locktime *their_locktime, + const struct sha256_double *anchor_txid, + unsigned int anchor_index, + u64 anchor_satoshis, const struct sha256 *rhash, const struct channel_state *cstate); #endif diff --git a/test-cli/check-commit-sig.c b/test-cli/check-commit-sig.c index 34a551900..bc314dcf3 100644 --- a/test-cli/check-commit-sig.c +++ b/test-cli/check-commit-sig.c @@ -76,7 +76,7 @@ int main(int argc, char *argv[]) /* Now create our commitment tx. */ proto_to_sha256(o1->revocation_hash, &rhash); - commit = create_commit_tx(ctx, o1, o2, a, &rhash, cstate); + commit = commit_tx_from_pkts(ctx, o1, o2, a, &rhash, cstate); /* Check signature. */ subscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); diff --git a/test-cli/create-commit-tx.c b/test-cli/create-commit-tx.c index 6f03794f3..c3d090a95 100644 --- a/test-cli/create-commit-tx.c +++ b/test-cli/create-commit-tx.c @@ -73,7 +73,7 @@ int main(int argc, char *argv[]) redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); /* Now create commitment tx to spend 2/2 output of anchor. */ - commit = create_commit_tx(ctx, o1, o2, a, &rhash, cstate); + commit = commit_tx_from_pkts(ctx, o1, o2, a, &rhash, cstate); /* This only fails on malformed packets */ if (!commit) diff --git a/test-cli/open-anchor.c b/test-cli/open-anchor.c index 8b9ba2399..00e885d00 100644 --- a/test-cli/open-anchor.c +++ b/test-cli/open-anchor.c @@ -80,7 +80,7 @@ int main(int argc, char *argv[]) /* Now, create signature for their commitment tx. */ proto_to_sha256(o2->revocation_hash, &rhash); invert_cstate(cstate); - commit = create_commit_tx(ctx, o2, o1, &oa, &rhash, cstate); + commit = commit_tx_from_pkts(ctx, o2, o1, &oa, &rhash, cstate); sign_tx_input(commit, 0, redeemscript, tal_count(redeemscript), &privkey, &pubkey1, &sig); diff --git a/test-cli/open-commit-sig.c b/test-cli/open-commit-sig.c index 6955db537..3fb910f32 100644 --- a/test-cli/open-commit-sig.c +++ b/test-cli/open-commit-sig.c @@ -63,7 +63,7 @@ int main(int argc, char *argv[]) proto_to_sha256(o2->revocation_hash, &rhash); invert_cstate(cstate); - commit = create_commit_tx(ctx, o2, o1, a, &rhash, cstate); + commit = commit_tx_from_pkts(ctx, o2, o1, a, &rhash, cstate); /* If contributions don't exceed fees, this fails. */ if (!commit) diff --git a/test-cli/pkt.c b/test-cli/pkt.c index 08300275f..8c5765431 100644 --- a/test-cli/pkt.c +++ b/test-cli/pkt.c @@ -1,7 +1,9 @@ #include "bitcoin/address.h" +#include "bitcoin/locktime.h" #include "bitcoin/pubkey.h" #include "bitcoin/signature.h" #include "bitcoin/tx.h" +#include "commit_tx.h" #include "pkt.h" #include "protobuf_convert.h" #include @@ -224,3 +226,28 @@ struct pkt *update_complete_pkt(const tal_t *ctx, uc.revocation_preimage = sha256_to_proto(ctx, revocation_preimage); return to_pkt(ctx, PKT__PKT_UPDATE_COMPLETE, &uc); } + +struct bitcoin_tx *commit_tx_from_pkts(const tal_t *ctx, + OpenChannel *ours, + OpenChannel *theirs, + OpenAnchor *anchor, + const struct sha256 *rhash, + const struct channel_state *cstate) +{ + struct pubkey ourkey, theirkey; + struct sha256_double txid; + struct rel_locktime locktime; + + proto_to_sha256(anchor->txid, &txid.sha); + /* Output goes to our final pubkeys */ + if (!proto_to_pubkey(ours->final_key, &ourkey)) + return NULL; + if (!proto_to_pubkey(theirs->final_key, &theirkey)) + return NULL; + if (!proto_to_rel_locktime(theirs->delay, &locktime)) + return NULL; + + return create_commit_tx(ctx, &ourkey, &theirkey, &locktime, + &txid, anchor->output_index, anchor->amount, + rhash, cstate); +} diff --git a/test-cli/pkt.h b/test-cli/pkt.h index 6d4f13556..535c7abef 100644 --- a/test-cli/pkt.h +++ b/test-cli/pkt.h @@ -162,4 +162,13 @@ struct pkt *update_signature_pkt(const tal_t *ctx, struct pkt *update_complete_pkt(const tal_t *ctx, const struct sha256 *revocation_preimage); +struct channel_state; + +struct bitcoin_tx *commit_tx_from_pkts(const tal_t *ctx, + OpenChannel *ours, + OpenChannel *theirs, + OpenAnchor *anchor, + const struct sha256 *rhash, + const struct channel_state *cstate); + #endif /* LIGHTNING_PKT_H */ diff --git a/test-cli/update-channel-accept.c b/test-cli/update-channel-accept.c index 55f913e95..a5320f33b 100644 --- a/test-cli/update-channel-accept.c +++ b/test-cli/update-channel-accept.c @@ -84,7 +84,7 @@ int main(int argc, char *argv[]) /* Now create THEIR new commitment tx to spend 2/2 output of anchor. */ invert_cstate(cstate); - commit = create_commit_tx(ctx, o2, o1, a, &their_rhash, cstate); + commit = commit_tx_from_pkts(ctx, o2, o1, a, &their_rhash, cstate); /* If contributions don't exceed fees, this fails. */ if (!commit) diff --git a/test-cli/update-channel-complete.c b/test-cli/update-channel-complete.c index df9e5ce2f..306198e91 100644 --- a/test-cli/update-channel-complete.c +++ b/test-cli/update-channel-complete.c @@ -74,7 +74,7 @@ int main(int argc, char *argv[]) redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); /* Check their signature signs our new commit tx correctly. */ - commit = create_commit_tx(ctx, o1, o2, a, &our_rhash, cstate); + commit = commit_tx_from_pkts(ctx, o1, o2, a, &our_rhash, cstate); if (!commit) errx(1, "Delta too large"); diff --git a/test-cli/update-channel-signature.c b/test-cli/update-channel-signature.c index 26f5c6cd9..5487e9dfb 100644 --- a/test-cli/update-channel-signature.c +++ b/test-cli/update-channel-signature.c @@ -86,7 +86,7 @@ int main(int argc, char *argv[]) redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); /* Check our new commit is signed correctly by them. */ - commit = create_commit_tx(ctx, o1, o2, a, &our_rhash, cstate); + commit = commit_tx_from_pkts(ctx, o1, o2, a, &our_rhash, cstate); if (!commit) errx(1, "Invalid packets"); @@ -97,7 +97,7 @@ int main(int argc, char *argv[]) /* Now create THEIR new commitment tx to spend 2/2 output of anchor. */ invert_cstate(cstate); - commit = create_commit_tx(ctx, o2, o1, a, &their_rhash, cstate); + commit = commit_tx_from_pkts(ctx, o2, o1, a, &their_rhash, cstate); if (!commit) errx(1, "Invalid packets");