Browse Source

create_commit_tx: don't use protobufs in the API.

Hand anchor details and pubkeys directly; this is what we want
for the actual daemon which doesn't keep raw packets around.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 9 years ago
parent
commit
d95d8a99c2
  1. 48
      commit_tx.c
  2. 14
      commit_tx.h
  3. 2
      test-cli/check-commit-sig.c
  4. 2
      test-cli/create-commit-tx.c
  5. 2
      test-cli/open-anchor.c
  6. 2
      test-cli/open-commit-sig.c
  7. 27
      test-cli/pkt.c
  8. 9
      test-cli/pkt.h
  9. 2
      test-cli/update-channel-accept.c
  10. 2
      test-cli/update-channel-complete.c
  11. 4
      test-cli/update-channel-signature.c

48
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, struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
OpenChannel *ours, const struct pubkey *our_final,
OpenChannel *theirs, const struct pubkey *their_final,
OpenAnchor *anchor, 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 sha256 *rhash,
const struct channel_state *cstate) const struct channel_state *cstate)
{ {
struct bitcoin_tx *tx; struct bitcoin_tx *tx;
const u8 *redeemscript; const u8 *redeemscript;
struct pubkey ourkey, theirkey;
struct rel_locktime locktime;
size_t i, num; size_t i, num;
uint64_t total; uint64_t total;
@ -61,32 +62,23 @@ struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
+ tal_count(cstate->b.htlcs)); + tal_count(cstate->b.htlcs));
/* Our input spends the anchor tx output. */ /* Our input spends the anchor tx output. */
proto_to_sha256(anchor->txid, &tx->input[0].txid.sha); tx->input[0].txid = *anchor_txid;
tx->input[0].index = anchor->output_index; tx->input[0].index = anchor_index;
tx->input[0].input_amount = anchor->amount; tx->input[0].input_amount = anchor_satoshis;
/* 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);
/* First output is a P2SH to a complex redeem script (usu. for me) */ /* First output is a P2SH to a complex redeem script (usu. for me) */
redeemscript = bitcoin_redeem_secret_or_delay(tx, &ourkey, redeemscript = bitcoin_redeem_secret_or_delay(tx, our_final,
&locktime, their_locktime,
&theirkey, their_final,
rhash); rhash);
tx->output[0].script = scriptpubkey_p2sh(tx, redeemscript); tx->output[0].script = scriptpubkey_p2sh(tx, redeemscript);
tx->output[0].script_length = tal_count(tx->output[0].script); tx->output[0].script_length = tal_count(tx->output[0].script);
tx->output[0].amount = cstate->a.pay_msat / 1000; tx->output[0].amount = cstate->a.pay_msat / 1000;
/* Second output is a P2SH payment to them. */ /* Second output is a P2SH payment to them. */
tx->output[1].script = scriptpubkey_p2sh(ctx, tx->output[1].script = scriptpubkey_p2sh(tx,
bitcoin_redeem_single(ctx, bitcoin_redeem_single(tx,
&theirkey)); their_final));
tx->output[1].script_length = tal_count(tx->output[1].script); tx->output[1].script_length = tal_count(tx->output[1].script);
tx->output[1].amount = cstate->b.pay_msat / 1000; 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. */ /* HTLCs we've sent. */
for (i = 0; i < tal_count(cstate->a.htlcs); i++) { for (i = 0; i < tal_count(cstate->a.htlcs); i++) {
if (!add_htlc(tx, num, cstate->a.htlcs[i], &ourkey, &theirkey, if (!add_htlc(tx, num, cstate->a.htlcs[i],
rhash, &locktime, scriptpubkey_htlc_send)) our_final, their_final,
rhash, their_locktime, scriptpubkey_htlc_send))
return tal_free(tx); return tal_free(tx);
total += tx->output[num++].amount; total += tx->output[num++].amount;
} }
/* HTLCs we've received. */ /* HTLCs we've received. */
for (i = 0; i < tal_count(cstate->b.htlcs); i++) { for (i = 0; i < tal_count(cstate->b.htlcs); i++) {
if (!add_htlc(tx, num, cstate->b.htlcs[i], &ourkey, &theirkey, if (!add_htlc(tx, num, cstate->b.htlcs[i],
rhash, &locktime, scriptpubkey_htlc_recv)) our_final, their_final,
rhash, their_locktime, scriptpubkey_htlc_recv))
return tal_free(tx); return tal_free(tx);
total += tx->output[num++].amount; total += tx->output[num++].amount;
} }

14
commit_tx.h

@ -1,19 +1,23 @@
#ifndef LIGHTNING_COMMIT_TX_H #ifndef LIGHTNING_COMMIT_TX_H
#define LIGHTNING_COMMIT_TX_H #define LIGHTNING_COMMIT_TX_H
#include "config.h" #include "config.h"
#include "lightning.pb-c.h" #include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
struct channel_state; struct channel_state;
struct sha256_double; struct sha256_double;
struct sha256; struct pubkey;
struct rel_locktime;
/* Create commitment tx to spend the anchor tx output; doesn't fill in /* Create commitment tx to spend the anchor tx output; doesn't fill in
* input scriptsig. */ * input scriptsig. */
struct bitcoin_tx *create_commit_tx(const tal_t *ctx, struct bitcoin_tx *create_commit_tx(const tal_t *ctx,
OpenChannel *ours, const struct pubkey *our_final,
OpenChannel *theirs, const struct pubkey *their_final,
OpenAnchor *anchor, 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 sha256 *rhash,
const struct channel_state *cstate); const struct channel_state *cstate);
#endif #endif

2
test-cli/check-commit-sig.c

@ -76,7 +76,7 @@ int main(int argc, char *argv[])
/* Now create our commitment tx. */ /* Now create our commitment tx. */
proto_to_sha256(o1->revocation_hash, &rhash); 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. */ /* Check signature. */
subscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); subscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);

2
test-cli/create-commit-tx.c

@ -73,7 +73,7 @@ int main(int argc, char *argv[])
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
/* Now create commitment tx to spend 2/2 output of anchor. */ /* 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 */ /* This only fails on malformed packets */
if (!commit) if (!commit)

2
test-cli/open-anchor.c

@ -80,7 +80,7 @@ int main(int argc, char *argv[])
/* Now, create signature for their commitment tx. */ /* Now, create signature for their commitment tx. */
proto_to_sha256(o2->revocation_hash, &rhash); proto_to_sha256(o2->revocation_hash, &rhash);
invert_cstate(cstate); 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), sign_tx_input(commit, 0, redeemscript, tal_count(redeemscript),
&privkey, &pubkey1, &sig); &privkey, &pubkey1, &sig);

2
test-cli/open-commit-sig.c

@ -63,7 +63,7 @@ int main(int argc, char *argv[])
proto_to_sha256(o2->revocation_hash, &rhash); proto_to_sha256(o2->revocation_hash, &rhash);
invert_cstate(cstate); 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 contributions don't exceed fees, this fails. */
if (!commit) if (!commit)

27
test-cli/pkt.c

@ -1,7 +1,9 @@
#include "bitcoin/address.h" #include "bitcoin/address.h"
#include "bitcoin/locktime.h"
#include "bitcoin/pubkey.h" #include "bitcoin/pubkey.h"
#include "bitcoin/signature.h" #include "bitcoin/signature.h"
#include "bitcoin/tx.h" #include "bitcoin/tx.h"
#include "commit_tx.h"
#include "pkt.h" #include "pkt.h"
#include "protobuf_convert.h" #include "protobuf_convert.h"
#include <ccan/crypto/sha256/sha256.h> #include <ccan/crypto/sha256/sha256.h>
@ -224,3 +226,28 @@ struct pkt *update_complete_pkt(const tal_t *ctx,
uc.revocation_preimage = sha256_to_proto(ctx, revocation_preimage); uc.revocation_preimage = sha256_to_proto(ctx, revocation_preimage);
return to_pkt(ctx, PKT__PKT_UPDATE_COMPLETE, &uc); 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);
}

9
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, struct pkt *update_complete_pkt(const tal_t *ctx,
const struct sha256 *revocation_preimage); 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 */ #endif /* LIGHTNING_PKT_H */

2
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. */ /* Now create THEIR new commitment tx to spend 2/2 output of anchor. */
invert_cstate(cstate); 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 contributions don't exceed fees, this fails. */
if (!commit) if (!commit)

2
test-cli/update-channel-complete.c

@ -74,7 +74,7 @@ int main(int argc, char *argv[])
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
/* Check their signature signs our new commit tx correctly. */ /* 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) if (!commit)
errx(1, "Delta too large"); errx(1, "Delta too large");

4
test-cli/update-channel-signature.c

@ -86,7 +86,7 @@ int main(int argc, char *argv[])
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
/* Check our new commit is signed correctly by them. */ /* 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) if (!commit)
errx(1, "Invalid packets"); 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. */ /* Now create THEIR new commitment tx to spend 2/2 output of anchor. */
invert_cstate(cstate); 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) if (!commit)
errx(1, "Invalid packets"); errx(1, "Invalid packets");

Loading…
Cancel
Save