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>
This commit is contained in:
Rusty Russell 2016-01-22 06:41:47 +10:30
parent 65cc6bbd50
commit d95d8a99c2
11 changed files with 74 additions and 40 deletions

View File

@ -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;
}

View File

@ -1,19 +1,23 @@
#ifndef LIGHTNING_COMMIT_TX_H
#define LIGHTNING_COMMIT_TX_H
#include "config.h"
#include "lightning.pb-c.h"
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
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

View File

@ -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);

View File

@ -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)

View File

@ -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);

View File

@ -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)

View File

@ -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 <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);
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);
}

View File

@ -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 */

View File

@ -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)

View File

@ -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");

View File

@ -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");