diff --git a/close-channel.c b/close-channel.c index 258e37655..fb82ef25d 100644 --- a/close-channel.c +++ b/close-channel.c @@ -34,6 +34,8 @@ int main(int argc, char *argv[]) bool testnet, complete = false; struct pubkey pubkey1, pubkey2; u8 *redeemscript; + int64_t delta; + size_t i; err_set_progname(argv[0]); @@ -41,14 +43,14 @@ int main(int argc, char *argv[]) opt_register_noarg("--complete", opt_set_bool, &complete, "Create a close_transaction_complete msg instead"); opt_register_noarg("--help|-h", opt_usage_and_exit, - " \n" + " [update-protobuf]...\n" "Create the signature needed for the close transaction", "Print this message."); opt_parse(&argc, argv, opt_log_stderr_exit); - if (argc != 5) - opt_usage_exit_fail("Expected 4 arguments"); + if (argc < 5) + opt_usage_exit_fail("Expected 4+ arguments"); anchor = bitcoin_tx_from_file(ctx, argv[1]); 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); + /* 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 */ if (!proto_to_pubkey(o1->anchor->pubkey, &pubkey2)) errx(1, "Invalid o1 commit pubkey"); @@ -75,7 +84,9 @@ int main(int argc, char *argv[]) redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); /* 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)); /* Sign it for them. */ diff --git a/close_tx.c b/close_tx.c index f1988709a..eb18fa691 100644 --- a/close_tx.c +++ b/close_tx.c @@ -9,6 +9,7 @@ struct bitcoin_tx *create_close_tx(const tal_t *ctx, OpenChannel *ours, OpenChannel *theirs, + int64_t delta, const struct sha256_double *anchor_txid, 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)) 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); /* 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); tx->output[0].script = scriptpubkey_p2sh(tx, redeemscript); tx->output[0].script_length = tal_count(tx->output[0].script); /* 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); tx->output[1].script = scriptpubkey_p2sh(tx, redeemscript); tx->output[1].script_length = tal_count(tx->output[1].script); diff --git a/close_tx.h b/close_tx.h index f104ed0ea..77f75269e 100644 --- a/close_tx.h +++ b/close_tx.h @@ -10,6 +10,7 @@ struct sha256_double; struct bitcoin_tx *create_close_tx(const tal_t *ctx, OpenChannel *ours, OpenChannel *theirs, + int64_t delta, const struct sha256_double *anchor_txid, unsigned int anchor_output); #endif diff --git a/create-close-tx.c b/create-close-tx.c index 1aa8c233c..bbf16c93b 100644 --- a/create-close-tx.c +++ b/create-close-tx.c @@ -33,19 +33,21 @@ int main(int argc, char *argv[]) char *tx_hex; CloseChannel *close; CloseChannelComplete *closecomplete; + size_t i; + int64_t delta; err_set_progname(argv[0]); /* FIXME: Take update.pbs to adjust channel */ opt_register_noarg("--help|-h", opt_usage_and_exit, - " \n" + " [update-protobuf]...\n" "Create the close transaction from the signatures", "Print this message."); opt_parse(&argc, argv, opt_log_stderr_exit); - if (argc != 6) - opt_usage_exit_fail("Expected 5 arguments"); + if (argc < 6) + opt_usage_exit_fail("Expected 5+ arguments"); anchor = bitcoin_tx_from_file(ctx, argv[1]); 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)) 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. */ redeemscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2); /* 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)); /* Signatures well-formed? */