From 87f0ee635176167d7168d817e29a108362b7e500 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 10 Sep 2019 11:53:27 +0930 Subject: [PATCH] channeld: set option_static_remotekey when negotiated. Signed-off-by: Rusty Russell --- channeld/channel_wire.csv | 1 + channeld/channeld.c | 5 ++- channeld/full_channel.c | 2 + channeld/full_channel.h | 2 + channeld/test/run-full_channel.c | 4 +- common/initial_channel.c | 2 + common/initial_channel.h | 4 ++ devtools/mkcommit.c | 59 ++++++++++++++++----------- lightningd/channel_control.c | 6 ++- lightningd/onchain_control.c | 3 +- lightningd/opening_control.c | 27 +++++++++++- onchaind/onchain_wire.csv | 1 + onchaind/onchaind.c | 6 ++- onchaind/test/run-grind_feerate-bug.c | 2 +- onchaind/test/run-grind_feerate.c | 2 +- openingd/opening_wire.csv | 1 + openingd/openingd.c | 4 ++ 17 files changed, 99 insertions(+), 32 deletions(-) diff --git a/channeld/channel_wire.csv b/channeld/channel_wire.csv index e6df3b411..79242db83 100644 --- a/channeld/channel_wire.csv +++ b/channeld/channel_wire.csv @@ -69,6 +69,7 @@ msgdata,channel_init,upfront_shutdown_script,u8,upfront_shutdown_script_len msgdata,channel_init,remote_ann_node_sig,?secp256k1_ecdsa_signature, msgdata,channel_init,remote_ann_bitcoin_sig,?secp256k1_ecdsa_signature, msgdata,channel_init,announce_delay,u32, +msgdata,channel_init,option_static_remotekey,bool, # master->channeld funding hit new depth(funding locked if >= lock depth) msgtype,channel_funding_depth,1002 diff --git a/channeld/channeld.c b/channeld/channeld.c index a9bab9e4b..d74e00211 100644 --- a/channeld/channeld.c +++ b/channeld/channeld.c @@ -2866,6 +2866,7 @@ static void init_channel(struct peer *peer) struct secret last_remote_per_commit_secret; secp256k1_ecdsa_signature *remote_ann_node_sig; secp256k1_ecdsa_signature *remote_ann_bitcoin_sig; + bool option_static_remotekey; assert(!(fcntl(MASTER_FD, F_GETFL) & O_NONBLOCK)); @@ -2924,7 +2925,8 @@ static void init_channel(struct peer *peer) &peer->remote_upfront_shutdown_script, &remote_ann_node_sig, &remote_ann_bitcoin_sig, - &peer->announce_delay)) { + &peer->announce_delay, + &option_static_remotekey)) { master_badmsg(WIRE_CHANNEL_INIT, msg); } /* stdin == requests, 3 == peer, 4 = gossip, 5 = gossip_store, 6 = HSM */ @@ -2980,6 +2982,7 @@ static void init_channel(struct peer *peer) &points[LOCAL], &points[REMOTE], &funding_pubkey[LOCAL], &funding_pubkey[REMOTE], + option_static_remotekey, funder); if (!channel_force_htlcs(peer->channel, htlcs, hstates, diff --git a/channeld/full_channel.c b/channeld/full_channel.c index c6116bc11..14238246f 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -46,6 +46,7 @@ struct channel *new_full_channel(const tal_t *ctx, const struct basepoints *remote_basepoints, const struct pubkey *local_funding_pubkey, const struct pubkey *remote_funding_pubkey, + bool option_static_remotekey, enum side funder) { struct channel *channel = new_initial_channel(ctx, @@ -61,6 +62,7 @@ struct channel *new_full_channel(const tal_t *ctx, remote_basepoints, local_funding_pubkey, remote_funding_pubkey, + option_static_remotekey, funder); if (channel) { diff --git a/channeld/full_channel.h b/channeld/full_channel.h index eca838049..01d4ce160 100644 --- a/channeld/full_channel.h +++ b/channeld/full_channel.h @@ -24,6 +24,7 @@ * @remote_basepoints: remote basepoints. * @local_fundingkey: local funding key * @remote_fundingkey: remote funding key + * @option_static_remotekey: use `option_static_remotekey`. * @funder: which side initiated it. * * Returns state, or NULL if malformed. @@ -42,6 +43,7 @@ struct channel *new_full_channel(const tal_t *ctx, const struct basepoints *remote_basepoints, const struct pubkey *local_funding_pubkey, const struct pubkey *remote_funding_pubkey, + bool option_static_remotekey, enum side funder); /** diff --git a/channeld/test/run-full_channel.c b/channeld/test/run-full_channel.c index 1241cda46..2fdd75ed0 100644 --- a/channeld/test/run-full_channel.c +++ b/channeld/test/run-full_channel.c @@ -473,7 +473,7 @@ int main(void) &localbase, &remotebase, &local_funding_pubkey, &remote_funding_pubkey, - LOCAL); + false, LOCAL); rchannel = new_full_channel(tmpctx, &chainparams->genesis_blockhash, &funding_txid, funding_output_index, 0, @@ -484,7 +484,7 @@ int main(void) &remotebase, &localbase, &remote_funding_pubkey, &local_funding_pubkey, - REMOTE); + false, REMOTE); /* BOLT #3: * diff --git a/common/initial_channel.c b/common/initial_channel.c index c4a5be5c8..010dc84f5 100644 --- a/common/initial_channel.c +++ b/common/initial_channel.c @@ -22,6 +22,7 @@ struct channel *new_initial_channel(const tal_t *ctx, const struct basepoints *remote_basepoints, const struct pubkey *local_funding_pubkey, const struct pubkey *remote_funding_pubkey, + bool option_static_remotekey, enum side funder) { struct channel *channel = tal(ctx, struct channel); @@ -65,6 +66,7 @@ struct channel *new_initial_channel(const tal_t *ctx, if (channel->chainparams == NULL) return tal_free(channel); + channel->option_static_remotekey = option_static_remotekey; return channel; } diff --git a/common/initial_channel.h b/common/initial_channel.h index b4a91d3f0..4db6d72a6 100644 --- a/common/initial_channel.h +++ b/common/initial_channel.h @@ -64,6 +64,9 @@ struct channel { /* Chain params to check against */ const struct chainparams *chainparams; + + /* Is this using option_static_remotekey? */ + bool option_static_remotekey; }; /** @@ -101,6 +104,7 @@ struct channel *new_initial_channel(const tal_t *ctx, const struct basepoints *remote_basepoints, const struct pubkey *local_funding_pubkey, const struct pubkey *remote_funding_pubkey, + bool option_static_remotekey, enum side funder); diff --git a/devtools/mkcommit.c b/devtools/mkcommit.c index d671725f8..acb765403 100644 --- a/devtools/mkcommit.c +++ b/devtools/mkcommit.c @@ -10,6 +10,7 @@ */ #include #include +#include #include #include #include @@ -20,6 +21,7 @@ #include #include #include +#include #include #include #include @@ -247,6 +249,7 @@ int main(int argc, char *argv[]) const struct htlc **htlcmap; struct privkey local_htlc_privkey, remote_htlc_privkey; struct pubkey local_htlc_pubkey, remote_htlc_pubkey; + bool option_static_remotekey = false; const struct chainparams *chainparams = chainparams_for_network("bitcoin"); setup_locale(); @@ -254,30 +257,36 @@ int main(int argc, char *argv[]) secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); - if (argv[1] && streq(argv[1], "-v")) { - verbose = true; - argv++; - argc--; - } + opt_register_noarg("--help|-h", opt_usage_and_exit, + " [...]\n" + "Where are:\n" + " \n" + " \n" + " \n" + "Where are:\n" + " \n" + " \n" + " \n" + " \n" + " \n" + " \n" + "Where s are:\n" + " \n" + " \n" + " \n" + " \n", + "Show this message"); + opt_register_noarg("-v|--verbose", opt_set_bool, &verbose, + "Increase verbosity"); + opt_register_noarg("--option-static-remotekey", opt_set_bool, + &option_static_remotekey, + "Use option_static_remotekey generation rules"); + opt_register_version(); + + opt_parse(&argc, argv, opt_log_stderr_exit); if (argc < 1 + 7 + 3*2 + 6*2) - errx(1, "Usage: mkcommit [-v] [...]\n" - "Where are:\n" - " \n" - " \n" - " \n" - "Where are:\n" - " \n" - " \n" - " \n" - " \n" - " \n" - " \n" - "Where s are:\n" - " \n" - " \n" - " \n" - " \n"); + opt_usage_exit_fail("Too few arguments"); argnum = 1; commitnum = atol(argv[argnum++]); @@ -312,8 +321,11 @@ int main(int argc, char *argv[]) errx(1, "Can't afford local_msat"); printf("## HTLCs\n"); - while (argnum < argc) + while (argnum < argc) { + if (argnum + 4 > argc) + opt_usage_exit_fail("Too few arguments for htlc"); argnum += parse_htlc(argv + argnum, &htlcs, &hstates, &preimages); + } printf("\n"); if (!pubkey_from_privkey(&local.funding_privkey, &funding_localkey) @@ -355,6 +367,7 @@ int main(int argc, char *argv[]) &localconfig, &remoteconfig, &localbase, &remotebase, &funding_localkey, &funding_remotekey, + option_static_remotekey, fee_payer); if (!channel_force_htlcs(channel, htlcs, hstates, NULL, NULL, NULL, NULL, diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index b05bdc955..d9aa93897 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -431,7 +432,10 @@ void peer_start_channeld(struct channel *channel, remote_ann_bitcoin_sig, /* Delay announce by 60 seconds after * seeing block (adjustable if dev) */ - ld->topology->poll_seconds * 2); + ld->topology->poll_seconds * 2, + /* Set at channel open, even if not + * negotiated now! */ + channel->option_static_remotekey); /* We don't expect a response: we are triggered by funding_depth_cb. */ subd_send_msg(channel->owner, take(initmsg)); diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index a7f1df2e2..8c38eb992 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -543,7 +543,8 @@ enum watch_result onchaind_funding_spent(struct channel *channel, tal_count(stubs), channel->min_possible_feerate, channel->max_possible_feerate, - channel->future_per_commitment_point); + channel->future_per_commitment_point, + channel->option_static_remotekey); subd_send_msg(channel->owner, take(msg)); /* FIXME: Don't queue all at once, use an empty cb... */ diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 34c3dd3e6..14b1dd9f8 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -168,6 +169,7 @@ wallet_commit_channel(struct lightningd *ld, struct channel *channel; struct amount_msat our_msat; s64 final_key_idx; + bool option_static_remotekey; /* Get a key to use for closing outputs from this tx */ final_key_idx = wallet_get_newindex(ld); @@ -196,6 +198,27 @@ wallet_commit_channel(struct lightningd *ld, /* old_remote_per_commit not valid yet, copy valid one. */ channel_info->old_remote_per_commit = channel_info->remote_per_commit; + /* BOLT-930a9b44076a8f25a8626b31b3d5a55c0888308c #2: + * 1. type: 35 (`funding_signed`) + * 2. data: + * * [`channel_id`:`channel_id`] + * * [`signature`:`signature`] + * + * #### Requirements + * + * Both peers: + * - if `option_static_remotekey` was negotiated: + * - `option_static_remotekey` applies to all commitment + * transactions + * - otherwise: + * - `option_static_remotekey` does not apply to any commitment + * transactions + */ + /* i.e. We set it now for the channel permanently. */ + option_static_remotekey + = local_feature_negotiated(uc->peer->localfeatures, + LOCAL_STATIC_REMOTEKEY); + channel = new_channel(uc->peer, uc->dbid, NULL, /* No shachain yet */ CHANNELD_AWAITING_LOCKIN, @@ -238,7 +261,7 @@ wallet_commit_channel(struct lightningd *ld, ld->config.fee_base, ld->config.fee_per_satoshi, remote_upfront_shutdown_script, - false); + option_static_remotekey); /* Now we finally put it in the database. */ wallet_channel_insert(ld->wallet, channel); @@ -1106,6 +1129,8 @@ void peer_start_openingd(struct peer *peer, feerate_min(peer->ld, NULL), feerate_max(peer->ld, NULL), peer->localfeatures, + local_feature_negotiated(peer->localfeatures, + LOCAL_STATIC_REMOTEKEY), send_msg); subd_send_msg(uc->openingd, take(msg)); } diff --git a/onchaind/onchain_wire.csv b/onchaind/onchain_wire.csv index 08f7d362f..0af89927b 100644 --- a/onchaind/onchain_wire.csv +++ b/onchaind/onchain_wire.csv @@ -35,6 +35,7 @@ msgdata,onchain_init,num_htlcs,u64, msgdata,onchain_init,min_possible_feerate,u32, msgdata,onchain_init,max_possible_feerate,u32, msgdata,onchain_init,possible_remote_per_commit_point,?pubkey, +msgdata,onchain_init,option_static_remotekey,bool, #include # This is all the HTLCs: one per message diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index 85a88a0ba..e6006556c 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -67,6 +67,9 @@ static u32 reasonable_depth; /* The messages to send at that depth. */ static u8 **missing_htlc_msgs; +/* Does option_static_remotekey apply to this commitment tx? */ +bool option_static_remotekey; + /* If we broadcast a tx, or need a delay to resolve the output. */ struct proposed_resolution { /* This can be NULL if our proposal is to simply ignore it after depth */ @@ -2567,7 +2570,8 @@ int main(int argc, char *argv[]) &num_htlcs, &min_possible_feerate, &max_possible_feerate, - &possible_remote_per_commitment_point)) { + &possible_remote_per_commitment_point, + &option_static_remotekey)) { master_badmsg(WIRE_ONCHAIN_INIT, msg); } diff --git a/onchaind/test/run-grind_feerate-bug.c b/onchaind/test/run-grind_feerate-bug.c index ae305148e..fe0590eda 100644 --- a/onchaind/test/run-grind_feerate-bug.c +++ b/onchaind/test/run-grind_feerate-bug.c @@ -42,7 +42,7 @@ bool fromwire_onchain_dev_memleak(const void *p UNNEEDED) bool fromwire_onchain_htlc(const void *p UNNEEDED, struct htlc_stub *htlc UNNEEDED, bool *tell_if_missing UNNEEDED, bool *tell_immediately UNNEEDED) { fprintf(stderr, "fromwire_onchain_htlc called!\n"); abort(); } /* Generated stub for fromwire_onchain_init */ -bool fromwire_onchain_init(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct shachain *shachain UNNEEDED, struct bitcoin_blkid *chain_hash UNNEEDED, struct amount_sat *funding_amount_satoshi UNNEEDED, struct pubkey *old_remote_per_commitment_point UNNEEDED, struct pubkey *remote_per_commitment_point UNNEEDED, u32 *local_to_self_delay UNNEEDED, u32 *remote_to_self_delay UNNEEDED, u32 *feerate_per_kw UNNEEDED, struct amount_sat *local_dust_limit_satoshi UNNEEDED, struct bitcoin_txid *our_broadcast_txid UNNEEDED, u8 **local_scriptpubkey UNNEEDED, u8 **remote_scriptpubkey UNNEEDED, struct pubkey *ourwallet_pubkey UNNEEDED, enum side *funder UNNEEDED, struct basepoints *local_basepoints UNNEEDED, struct basepoints *remote_basepoints UNNEEDED, struct bitcoin_tx **tx UNNEEDED, u32 *tx_blockheight UNNEEDED, u32 *reasonable_depth UNNEEDED, secp256k1_ecdsa_signature **htlc_signature UNNEEDED, u64 *num_htlcs UNNEEDED, u32 *min_possible_feerate UNNEEDED, u32 *max_possible_feerate UNNEEDED, struct pubkey **possible_remote_per_commit_point UNNEEDED) +bool fromwire_onchain_init(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct shachain *shachain UNNEEDED, struct bitcoin_blkid *chain_hash UNNEEDED, struct amount_sat *funding_amount_satoshi UNNEEDED, struct pubkey *old_remote_per_commitment_point UNNEEDED, struct pubkey *remote_per_commitment_point UNNEEDED, u32 *local_to_self_delay UNNEEDED, u32 *remote_to_self_delay UNNEEDED, u32 *feerate_per_kw UNNEEDED, struct amount_sat *local_dust_limit_satoshi UNNEEDED, struct bitcoin_txid *our_broadcast_txid UNNEEDED, u8 **local_scriptpubkey UNNEEDED, u8 **remote_scriptpubkey UNNEEDED, struct pubkey *ourwallet_pubkey UNNEEDED, enum side *funder UNNEEDED, struct basepoints *local_basepoints UNNEEDED, struct basepoints *remote_basepoints UNNEEDED, struct bitcoin_tx **tx UNNEEDED, u32 *tx_blockheight UNNEEDED, u32 *reasonable_depth UNNEEDED, secp256k1_ecdsa_signature **htlc_signature UNNEEDED, u64 *num_htlcs UNNEEDED, u32 *min_possible_feerate UNNEEDED, u32 *max_possible_feerate UNNEEDED, struct pubkey **possible_remote_per_commit_point UNNEEDED, bool *option_static_remotekey UNNEEDED) { fprintf(stderr, "fromwire_onchain_init called!\n"); abort(); } /* Generated stub for fromwire_onchain_known_preimage */ bool fromwire_onchain_known_preimage(const void *p UNNEEDED, struct preimage *preimage UNNEEDED) diff --git a/onchaind/test/run-grind_feerate.c b/onchaind/test/run-grind_feerate.c index f575f2150..13468cf16 100644 --- a/onchaind/test/run-grind_feerate.c +++ b/onchaind/test/run-grind_feerate.c @@ -46,7 +46,7 @@ bool fromwire_onchain_dev_memleak(const void *p UNNEEDED) bool fromwire_onchain_htlc(const void *p UNNEEDED, struct htlc_stub *htlc UNNEEDED, bool *tell_if_missing UNNEEDED, bool *tell_immediately UNNEEDED) { fprintf(stderr, "fromwire_onchain_htlc called!\n"); abort(); } /* Generated stub for fromwire_onchain_init */ -bool fromwire_onchain_init(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct shachain *shachain UNNEEDED, struct bitcoin_blkid *chain_hash UNNEEDED, struct amount_sat *funding_amount_satoshi UNNEEDED, struct pubkey *old_remote_per_commitment_point UNNEEDED, struct pubkey *remote_per_commitment_point UNNEEDED, u32 *local_to_self_delay UNNEEDED, u32 *remote_to_self_delay UNNEEDED, u32 *feerate_per_kw UNNEEDED, struct amount_sat *local_dust_limit_satoshi UNNEEDED, struct bitcoin_txid *our_broadcast_txid UNNEEDED, u8 **local_scriptpubkey UNNEEDED, u8 **remote_scriptpubkey UNNEEDED, struct pubkey *ourwallet_pubkey UNNEEDED, enum side *funder UNNEEDED, struct basepoints *local_basepoints UNNEEDED, struct basepoints *remote_basepoints UNNEEDED, struct bitcoin_tx **tx UNNEEDED, u32 *tx_blockheight UNNEEDED, u32 *reasonable_depth UNNEEDED, secp256k1_ecdsa_signature **htlc_signature UNNEEDED, u64 *num_htlcs UNNEEDED, u32 *min_possible_feerate UNNEEDED, u32 *max_possible_feerate UNNEEDED, struct pubkey **possible_remote_per_commit_point UNNEEDED) +bool fromwire_onchain_init(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct shachain *shachain UNNEEDED, struct bitcoin_blkid *chain_hash UNNEEDED, struct amount_sat *funding_amount_satoshi UNNEEDED, struct pubkey *old_remote_per_commitment_point UNNEEDED, struct pubkey *remote_per_commitment_point UNNEEDED, u32 *local_to_self_delay UNNEEDED, u32 *remote_to_self_delay UNNEEDED, u32 *feerate_per_kw UNNEEDED, struct amount_sat *local_dust_limit_satoshi UNNEEDED, struct bitcoin_txid *our_broadcast_txid UNNEEDED, u8 **local_scriptpubkey UNNEEDED, u8 **remote_scriptpubkey UNNEEDED, struct pubkey *ourwallet_pubkey UNNEEDED, enum side *funder UNNEEDED, struct basepoints *local_basepoints UNNEEDED, struct basepoints *remote_basepoints UNNEEDED, struct bitcoin_tx **tx UNNEEDED, u32 *tx_blockheight UNNEEDED, u32 *reasonable_depth UNNEEDED, secp256k1_ecdsa_signature **htlc_signature UNNEEDED, u64 *num_htlcs UNNEEDED, u32 *min_possible_feerate UNNEEDED, u32 *max_possible_feerate UNNEEDED, struct pubkey **possible_remote_per_commit_point UNNEEDED, bool *option_static_remotekey UNNEEDED) { fprintf(stderr, "fromwire_onchain_init called!\n"); abort(); } /* Generated stub for fromwire_onchain_known_preimage */ bool fromwire_onchain_known_preimage(const void *p UNNEEDED, struct preimage *preimage UNNEEDED) diff --git a/openingd/opening_wire.csv b/openingd/opening_wire.csv index 810435118..365dfdd41 100644 --- a/openingd/opening_wire.csv +++ b/openingd/opening_wire.csv @@ -20,6 +20,7 @@ msgdata,opening_init,min_feerate,u32, msgdata,opening_init,max_feerate,u32, msgdata,opening_init,lfeatures_len,u16, msgdata,opening_init,lfeatures,u8,lfeatures_len +msgdata,opening_init,option_static_remotekey,bool, # Optional msg to send. msgdata,opening_init,len,u16, msgdata,opening_init,msg,u8,len diff --git a/openingd/openingd.c b/openingd/openingd.c index b84eb1e41..c9790fe58 100644 --- a/openingd/openingd.c +++ b/openingd/openingd.c @@ -105,6 +105,7 @@ struct state { /* Which chain we're on, so we can check/set `chain_hash` fields */ const struct chainparams *chainparams; + bool option_static_remotekey; }; static const u8 *dev_upfront_shutdown_script(const tal_t *ctx) @@ -660,6 +661,7 @@ static bool funder_finalize_channel_setup(struct state *state, &state->their_points, &state->our_funding_pubkey, &state->their_funding_pubkey, + state->option_static_remotekey, /* Funder is local */ LOCAL); /* We were supposed to do enough checks above, but just in case, @@ -1378,6 +1380,7 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg) &state->our_points, &theirs, &state->our_funding_pubkey, &their_funding_pubkey, + state->option_static_remotekey, REMOTE); /* We don't expect this to fail, but it does do some additional * internal sanity checks. */ @@ -1703,6 +1706,7 @@ int main(int argc, char *argv[]) &state->minimum_depth, &state->min_feerate, &state->max_feerate, &state->localfeatures, + &state->option_static_remotekey, &inner)) master_badmsg(WIRE_OPENING_INIT, msg);