core-lightning/test-cli/check-commit-sig.c
Rusty Russell 3260fb2ed1 protocol: add commitment fee logic.
Both sides elect a commitment fee, and the lowest is chosen.  That means
you can't game the other side (but if you offer too low, then can error
out of course).

Fees are split 50-50 if possible: originally the whole fee has to be
paid by the (single) funder.  Neither side can withdraw funds which
would make them unable to pay fees.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2015-07-29 16:16:24 +09:30

90 lines
2.6 KiB
C

#include <ccan/crypto/shachain/shachain.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
#include <ccan/opt/opt.h>
#include <ccan/str/hex/hex.h>
#include <ccan/err/err.h>
#include "lightning.pb-c.h"
#include "bitcoin/base58.h"
#include "pkt.h"
#include "bitcoin/script.h"
#include "permute_tx.h"
#include "bitcoin/signature.h"
#include "commit_tx.h"
#include "bitcoin/pubkey.h"
#include "bitcoin/privkey.h"
#include "protobuf_convert.h"
#include "funding.h"
#include <unistd.h>
int main(int argc, char *argv[])
{
const tal_t *ctx = tal_arr(NULL, char, 0);
OpenChannel *o1, *o2;
OpenAnchor *a;
struct bitcoin_tx *commit;
u8 *subscript;
struct pubkey pubkey1, pubkey2;
struct bitcoin_signature sig;
struct privkey privkey;
bool testnet;
struct sha256 rhash;
u64 our_amount, their_amount;
err_set_progname(argv[0]);
opt_register_noarg("--help|-h", opt_usage_and_exit,
"<open-channel-file1> <open-channel-file2> <open-anchor-file1> <commit-key1> [<commit-sig>]\n"
"Check the commit sig is valid (either in open-anchor or commit-sig packet)",
"Print this message.");
opt_parse(&argc, argv, opt_log_stderr_exit);
if (argc != 5 && argc != 6)
opt_usage_exit_fail("Expected 4 or 5 arguments");
o1 = pkt_from_file(argv[1], PKT__PKT_OPEN)->open;
o2 = pkt_from_file(argv[2], PKT__PKT_OPEN)->open;
a = pkt_from_file(argv[3], PKT__PKT_OPEN_ANCHOR)->open_anchor;
if (!key_from_base58(argv[4], strlen(argv[4]), &testnet, &privkey, &pubkey1))
errx(1, "Invalid private key '%s'", argv[4]);
if (!testnet)
errx(1, "Private key '%s' not on testnet!", argv[4]);
sig.stype = SIGHASH_ALL;
if (argc == 6) {
OpenCommitSig *cs = pkt_from_file(argv[5],
PKT__PKT_OPEN_COMMIT_SIG)
->open_commit_sig;
if (!proto_to_signature(cs->sig, &sig.sig))
errx(1, "Bad signature in %s", argv[5]);
} else {
if (!proto_to_signature(a->commit_sig, &sig.sig))
errx(1, "Bad signature in %s", argv[3]);
}
/* Pubkey well-formed? */
if (!proto_to_pubkey(o2->commit_key, &pubkey2))
errx(1, "Invalid o2 commit_key");
if (!initial_funding(o1, o2, a, commit_fee(o1, o2),
&our_amount, &their_amount))
errx(1, "Invalid open combination (need 1 anchor offer)");
/* Now create our commitment tx. */
proto_to_sha256(o1->revocation_hash, &rhash);
commit = create_commit_tx(ctx, o1, o2, a, &rhash,
our_amount, their_amount);
/* Check signature. */
subscript = bitcoin_redeem_2of2(ctx, &pubkey1, &pubkey2);
if (!check_tx_sig(commit, 0, subscript, tal_count(subscript),
&pubkey2, &sig))
errx(1, "Their signature invalid");
tal_free(ctx);
return 0;
}