Browse Source

close-channel / create-close-tx: take into account updates.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
ppa-0.6.1
Rusty Russell 10 years ago
parent
commit
9caf2c71e8
  1. 19
      close-channel.c
  2. 11
      close_tx.c
  3. 1
      close_tx.h
  4. 17
      create-close-tx.c

19
close-channel.c

@ -34,6 +34,8 @@ int main(int argc, char *argv[])
bool testnet, complete = false; bool testnet, complete = false;
struct pubkey pubkey1, pubkey2; struct pubkey pubkey1, pubkey2;
u8 *redeemscript; u8 *redeemscript;
int64_t delta;
size_t i;
err_set_progname(argv[0]); err_set_progname(argv[0]);
@ -41,14 +43,14 @@ int main(int argc, char *argv[])
opt_register_noarg("--complete", opt_set_bool, &complete, opt_register_noarg("--complete", opt_set_bool, &complete,
"Create a close_transaction_complete msg instead"); "Create a close_transaction_complete msg instead");
opt_register_noarg("--help|-h", opt_usage_and_exit, opt_register_noarg("--help|-h", opt_usage_and_exit,
"<anchor-tx> <open-channel-file1> <open-channel-file2> <commit-privkey>\n" "<anchor-tx> <open-channel-file1> <open-channel-file2> <commit-privkey> [update-protobuf]...\n"
"Create the signature needed for the close transaction", "Create the signature needed for the close transaction",
"Print this message."); "Print this message.");
opt_parse(&argc, argv, opt_log_stderr_exit); opt_parse(&argc, argv, opt_log_stderr_exit);
if (argc != 5) if (argc < 5)
opt_usage_exit_fail("Expected 4 arguments"); opt_usage_exit_fail("Expected 4+ arguments");
anchor = bitcoin_tx_from_file(ctx, argv[1]); anchor = bitcoin_tx_from_file(ctx, argv[1]);
o1 = pkt_from_file(argv[2], PKT__PKT_OPEN)->open; o1 = pkt_from_file(argv[2], PKT__PKT_OPEN)->open;
@ -62,6 +64,13 @@ int main(int argc, char *argv[])
bitcoin_txid(anchor, &anchor_txid); bitcoin_txid(anchor, &anchor_txid);
/* Get delta by accumulting all the updates. */
delta = 0;
for (i = 5; i < argc; i++) {
Update *u = pkt_from_file(argv[i], PKT__PKT_UPDATE)->update;
delta += u->delta;
}
/* Get pubkeys */ /* Get pubkeys */
if (!proto_to_pubkey(o1->anchor->pubkey, &pubkey2)) if (!proto_to_pubkey(o1->anchor->pubkey, &pubkey2))
errx(1, "Invalid o1 commit pubkey"); errx(1, "Invalid o1 commit pubkey");
@ -75,7 +84,9 @@ int main(int argc, char *argv[])
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
/* Now create the close tx to spend 2/2 output of anchor. */ /* Now create the close tx to spend 2/2 output of anchor. */
close_tx = create_close_tx(ctx, o1, o2, &anchor_txid, /* Assumes that updates are all from closer -> closee */
close_tx = create_close_tx(ctx, o1, o2, complete ? -delta : delta,
&anchor_txid,
find_p2sh_out(anchor, redeemscript)); find_p2sh_out(anchor, redeemscript));
/* Sign it for them. */ /* Sign it for them. */

11
close_tx.c

@ -9,6 +9,7 @@
struct bitcoin_tx *create_close_tx(const tal_t *ctx, struct bitcoin_tx *create_close_tx(const tal_t *ctx,
OpenChannel *ours, OpenChannel *ours,
OpenChannel *theirs, OpenChannel *theirs,
int64_t delta,
const struct sha256_double *anchor_txid, const struct sha256_double *anchor_txid,
unsigned int anchor_output) unsigned int anchor_output)
{ {
@ -30,16 +31,22 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx,
if (!proto_to_pubkey(theirs->final, &theirkey)) if (!proto_to_pubkey(theirs->final, &theirkey))
return tal_free(tx); return tal_free(tx);
/* delta must make sense. */
if (delta < 0 && ours->anchor->total - ours->commitment_fee < -delta)
return tal_free(tx);
if (delta > 0 && theirs->anchor->total - theirs->commitment_fee < delta)
return tal_free(tx);
proto_to_sha256(ours->revocation_hash, &redeem); proto_to_sha256(ours->revocation_hash, &redeem);
/* One output is to us. */ /* One output is to us. */
tx->output[0].amount = ours->anchor->total - ours->commitment_fee; tx->output[0].amount = ours->anchor->total - ours->commitment_fee + delta;
redeemscript = bitcoin_redeem_single(tx, &ourkey); redeemscript = bitcoin_redeem_single(tx, &ourkey);
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);
/* Other output is to them. */ /* Other output is to them. */
tx->output[1].amount = theirs->anchor->total - theirs->commitment_fee; tx->output[1].amount = theirs->anchor->total - theirs->commitment_fee - delta;
redeemscript = bitcoin_redeem_single(tx, &theirkey); redeemscript = bitcoin_redeem_single(tx, &theirkey);
tx->output[1].script = scriptpubkey_p2sh(tx, redeemscript); tx->output[1].script = scriptpubkey_p2sh(tx, redeemscript);
tx->output[1].script_length = tal_count(tx->output[1].script); tx->output[1].script_length = tal_count(tx->output[1].script);

1
close_tx.h

@ -10,6 +10,7 @@ struct sha256_double;
struct bitcoin_tx *create_close_tx(const tal_t *ctx, struct bitcoin_tx *create_close_tx(const tal_t *ctx,
OpenChannel *ours, OpenChannel *ours,
OpenChannel *theirs, OpenChannel *theirs,
int64_t delta,
const struct sha256_double *anchor_txid, const struct sha256_double *anchor_txid,
unsigned int anchor_output); unsigned int anchor_output);
#endif #endif

17
create-close-tx.c

@ -33,19 +33,21 @@ int main(int argc, char *argv[])
char *tx_hex; char *tx_hex;
CloseChannel *close; CloseChannel *close;
CloseChannelComplete *closecomplete; CloseChannelComplete *closecomplete;
size_t i;
int64_t delta;
err_set_progname(argv[0]); err_set_progname(argv[0]);
/* FIXME: Take update.pbs to adjust channel */ /* FIXME: Take update.pbs to adjust channel */
opt_register_noarg("--help|-h", opt_usage_and_exit, opt_register_noarg("--help|-h", opt_usage_and_exit,
"<anchor-tx> <open-channel-file1> <open-channel-file2> <close-protobuf> <close-complete-protobuf>\n" "<anchor-tx> <open-channel-file1> <open-channel-file2> <close-protobuf> <close-complete-protobuf> [update-protobuf]...\n"
"Create the close transaction from the signatures", "Create the close transaction from the signatures",
"Print this message."); "Print this message.");
opt_parse(&argc, argv, opt_log_stderr_exit); opt_parse(&argc, argv, opt_log_stderr_exit);
if (argc != 6) if (argc < 6)
opt_usage_exit_fail("Expected 5 arguments"); opt_usage_exit_fail("Expected 5+ arguments");
anchor = bitcoin_tx_from_file(ctx, argv[1]); anchor = bitcoin_tx_from_file(ctx, argv[1]);
o1 = pkt_from_file(argv[2], PKT__PKT_OPEN)->open; o1 = pkt_from_file(argv[2], PKT__PKT_OPEN)->open;
@ -61,11 +63,18 @@ int main(int argc, char *argv[])
if (!proto_to_pubkey(o2->anchor->pubkey, &pubkey2)) if (!proto_to_pubkey(o2->anchor->pubkey, &pubkey2))
errx(1, "Invalid anchor-2 key"); errx(1, "Invalid anchor-2 key");
/* Get delta by accumulting all the updates. */
delta = 0;
for (i = 6; i < argc; i++) {
Update *u = pkt_from_file(argv[i], PKT__PKT_UPDATE)->update;
delta += u->delta;
}
/* This is what the anchor pays to; figure out which output. */ /* This is what the anchor pays to; figure out which output. */
redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
/* Now create the close tx to spend 2/2 output of anchor. */ /* Now create the close tx to spend 2/2 output of anchor. */
close_tx = create_close_tx(ctx, o1, o2, &anchor_txid, close_tx = create_close_tx(ctx, o1, o2, delta, &anchor_txid,
find_p2sh_out(anchor, redeemscript)); find_p2sh_out(anchor, redeemscript));
/* Signatures well-formed? */ /* Signatures well-formed? */

Loading…
Cancel
Save