mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-01-18 05:12:45 +01:00
create-commit-tx: allow creation of initial opening tx.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
fb036399eb
commit
a700a5cdca
@ -23,12 +23,12 @@
|
|||||||
#include <openssl/ec.h>
|
#include <openssl/ec.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
/* FIXME: this code doesn't work if we're not the ones proposing the delta */
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
const tal_t *ctx = tal_arr(NULL, char, 0);
|
const tal_t *ctx = tal_arr(NULL, char, 0);
|
||||||
OpenChannel *o1, *o2;
|
OpenChannel *o1, *o2;
|
||||||
Update *update;
|
Pkt *pkt;
|
||||||
UpdateAccept *update_acc;
|
|
||||||
struct bitcoin_tx *anchor, *commit;
|
struct bitcoin_tx *anchor, *commit;
|
||||||
struct sha256_double anchor_txid;
|
struct sha256_double anchor_txid;
|
||||||
EC_KEY *privkey;
|
EC_KEY *privkey;
|
||||||
@ -44,28 +44,25 @@ int main(int argc, char *argv[])
|
|||||||
err_set_progname(argv[0]);
|
err_set_progname(argv[0]);
|
||||||
|
|
||||||
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> <final-update> <final-update-accept> <commit-privkey> [<previous-updates>]\n"
|
"<anchor-tx> <open-channel-file1> <open-channel-file2> <commit-privkey> [final-update-accept|open-commit-sig] [<updates>]\n"
|
||||||
"Create the signature needed for the commit transaction",
|
"Create the signature needed for the commit 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 < 7)
|
if (argc < 6)
|
||||||
opt_usage_exit_fail("Expected 6+ arguments");
|
opt_usage_exit_fail("Expected 5+ arguments");
|
||||||
|
|
||||||
anchor = bitcoin_tx_from_file(ctx, argv[1]);
|
anchor = bitcoin_tx_from_file(ctx, argv[1]);
|
||||||
bitcoin_txid(anchor, &anchor_txid);
|
bitcoin_txid(anchor, &anchor_txid);
|
||||||
o1 = pkt_from_file(argv[2], PKT__PKT_OPEN)->open;
|
o1 = pkt_from_file(argv[2], PKT__PKT_OPEN)->open;
|
||||||
o2 = pkt_from_file(argv[3], PKT__PKT_OPEN)->open;
|
o2 = pkt_from_file(argv[3], PKT__PKT_OPEN)->open;
|
||||||
|
|
||||||
update = pkt_from_file(argv[4], PKT__PKT_UPDATE)->update;
|
privkey = key_from_base58(argv[4], strlen(argv[4]), &testnet, &pubkey1);
|
||||||
update_acc = pkt_from_file(argv[5], PKT__PKT_UPDATE_ACCEPT)->update_accept;
|
|
||||||
|
|
||||||
privkey = key_from_base58(argv[6], strlen(argv[6]), &testnet, &pubkey1);
|
|
||||||
if (!privkey)
|
if (!privkey)
|
||||||
errx(1, "Invalid private key '%s'", argv[6]);
|
errx(1, "Invalid private key '%s'", argv[4]);
|
||||||
if (!testnet)
|
if (!testnet)
|
||||||
errx(1, "Private key '%s' not on testnet!", argv[6]);
|
errx(1, "Private key '%s' not on testnet!", argv[4]);
|
||||||
|
|
||||||
/* Get pubkeys */
|
/* Get pubkeys */
|
||||||
if (!proto_to_pubkey(o1->anchor->pubkey, &pubkey2))
|
if (!proto_to_pubkey(o1->anchor->pubkey, &pubkey2))
|
||||||
@ -76,17 +73,37 @@ 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 o2 anchor pubkey");
|
errx(1, "Invalid o2 anchor pubkey");
|
||||||
|
|
||||||
/* Figure out cumulative delta since anchor. */
|
/* Their signature comes from open-commit or from update-accept. */
|
||||||
delta = update->delta;
|
sig2.stype = SIGHASH_ALL;
|
||||||
for (i = 7; i < argc; i++) {
|
pkt = any_pkt_from_file(argv[5]);
|
||||||
Update *u = pkt_from_file(argv[i], PKT__PKT_UPDATE)->update;
|
|
||||||
delta += u->delta;
|
switch (pkt->pkt_case) {
|
||||||
|
case PKT__PKT_UPDATE_ACCEPT:
|
||||||
|
if (!proto_to_signature(pkt->update_accept->sig, &sig2.sig))
|
||||||
|
errx(1, "Invalid update-accept sig");
|
||||||
|
break;
|
||||||
|
case PKT__PKT_OPEN_COMMIT_SIG:
|
||||||
|
if (!proto_to_signature(pkt->open_commit_sig->sig, &sig2.sig))
|
||||||
|
errx(1, "Invalid open-commit-sig sig");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
errx(1, "Unexpected packet type %u in %s",
|
||||||
|
pkt->pkt_case, argv[5]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Initial revocation hash comes from open. */
|
||||||
|
proto_to_sha256(o1->revocation_hash, &rhash);
|
||||||
|
|
||||||
|
delta = 0;
|
||||||
|
/* Figure out cumulative delta since anchor, update revocation hash */
|
||||||
|
for (i = 6; i < argc; i++) {
|
||||||
|
Update *u = pkt_from_file(argv[i], PKT__PKT_UPDATE)->update;
|
||||||
|
delta += u->delta;
|
||||||
|
proto_to_sha256(u->revocation_hash, &rhash);
|
||||||
|
}
|
||||||
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. */
|
||||||
proto_to_sha256(update->revocation_hash, &rhash);
|
|
||||||
commit = create_commit_tx(ctx, o1, o2, &rhash, delta, &anchor_txid,
|
commit = create_commit_tx(ctx, o1, o2, &rhash, delta, &anchor_txid,
|
||||||
find_p2sh_out(anchor, redeemscript));
|
find_p2sh_out(anchor, redeemscript));
|
||||||
|
|
||||||
@ -99,11 +116,6 @@ int main(int argc, char *argv[])
|
|||||||
sign_tx_input(ctx, commit, 0, redeemscript, tal_count(redeemscript),
|
sign_tx_input(ctx, commit, 0, redeemscript, tal_count(redeemscript),
|
||||||
privkey, &pubkey1, &sig1.sig);
|
privkey, &pubkey1, &sig1.sig);
|
||||||
|
|
||||||
/* Their signatures comes from the update_accept packet. */
|
|
||||||
sig2.stype = SIGHASH_ALL;
|
|
||||||
if (!proto_to_signature(update_acc->sig, &sig2.sig))
|
|
||||||
errx(1, "Invalid update-accept sig");
|
|
||||||
|
|
||||||
if (!check_2of2_sig(commit, 0, redeemscript, tal_count(redeemscript),
|
if (!check_2of2_sig(commit, 0, redeemscript, tal_count(redeemscript),
|
||||||
&pubkey1, &pubkey2, &sig1, &sig2))
|
&pubkey1, &pubkey2, &sig1, &sig2))
|
||||||
errx(1, "Signature failed");
|
errx(1, "Signature failed");
|
||||||
|
Loading…
Reference in New Issue
Block a user