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

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