diff --git a/lightningd/channel.c b/lightningd/channel.c index 6d2f9257c..625c864e6 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -169,7 +169,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid, u32 first_blocknum, u32 min_possible_feerate, u32 max_possible_feerate, - bool connected) + bool connected, + const struct basepoints *local_basepoints, + const struct pubkey *local_funding_pubkey) { struct channel *channel = tal(peer->ld, struct channel); @@ -227,6 +229,8 @@ struct channel *new_channel(struct peer *peer, u64 dbid, channel->min_possible_feerate = min_possible_feerate; channel->max_possible_feerate = max_possible_feerate; channel->connected = connected; + channel->local_basepoints = *local_basepoints; + channel->local_funding_pubkey = *local_funding_pubkey; derive_channel_seed(peer->ld, &channel->seed, &peer->id, channel->dbid); list_add_tail(&peer->channels, &channel->list); diff --git a/lightningd/channel.h b/lightningd/channel.h index aec4b939d..464b7e2ad 100644 --- a/lightningd/channel.h +++ b/lightningd/channel.h @@ -81,6 +81,12 @@ struct channel { /* Secret seed (FIXME: Move to hsm!) */ struct secret seed; + /* Our local basepoints */ + struct basepoints local_basepoints; + + /* Our funding tx pubkey. */ + struct pubkey local_funding_pubkey; + /* Their scriptpubkey if they sent shutdown. */ u8 *remote_shutdown_scriptpubkey; /* Address for any final outputs */ @@ -140,7 +146,9 @@ struct channel *new_channel(struct peer *peer, u64 dbid, u32 first_blocknum, u32 min_possible_feerate, u32 max_possible_feerate, - bool connected); + bool connected, + const struct basepoints *local_basepoints, + const struct pubkey *local_funding_pubkey); void delete_channel(struct channel *channel); diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index 8f7c8a2c3..364d381ca 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -49,6 +49,12 @@ struct uncommitted_channel { /* Secret seed (FIXME: Move to hsm!) */ struct secret seed; + /* Our basepoints for the channel. */ + struct basepoints local_basepoints; + + /* Public key for funding tx. */ + struct pubkey local_funding_pubkey; + /* Blockheight at creation, scans for funding confirmations * will start here */ u32 first_blocknum; @@ -238,7 +244,9 @@ wallet_commit_channel(struct lightningd *ld, uc->first_blocknum, feerate, feerate, /* We are connected */ - true); + true, + &uc->local_basepoints, + &uc->local_funding_pubkey); /* Now we finally put it in the database. */ wallet_channel_insert(ld->wallet, channel); @@ -273,7 +281,6 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp, struct bitcoin_tx *fundingtx; struct bitcoin_txid funding_txid, expected_txid; struct pubkey changekey; - struct pubkey local_fundingkey; struct crypto_state cs; secp256k1_ecdsa_signature remote_commit_sig; struct bitcoin_tx *remote_commit; @@ -321,11 +328,9 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp, &changekey, fc->wtx.change_key_index)) fatal("Error deriving change key %u", fc->wtx.change_key_index); - derive_basepoints(&fc->uc->seed, &local_fundingkey, NULL, NULL, NULL); - fundingtx = funding_tx(tmpctx, &funding_outnum, fc->wtx.utxos, fc->wtx.amount, - &local_fundingkey, + &fc->uc->local_funding_pubkey, &channel_info.remote_fundingkey, fc->wtx.change, &changekey, ld->wallet->bip32_base); @@ -353,7 +358,7 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp, fc->wtx.amount, fc->wtx.change, fc->wtx.change_key_index, type_to_string(fc, struct pubkey, - &local_fundingkey), + &fc->uc->local_funding_pubkey), type_to_string(fc, struct pubkey, &channel_info.remote_fundingkey)); command_fail(fc->cmd, JSONRPC2_INVALID_PARAMS, @@ -364,7 +369,7 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp, fc->wtx.amount, fc->wtx.change, fc->wtx.change_key_index, type_to_string(fc, struct pubkey, - &local_fundingkey), + &fc->uc->local_funding_pubkey), type_to_string(fc, struct pubkey, &channel_info.remote_fundingkey)); goto failed; @@ -392,7 +397,7 @@ static void opening_funder_finished(struct subd *openingd, const u8 *resp, msg = towire_hsm_sign_funding(tmpctx, channel->funding_satoshi, fc->wtx.change, fc->wtx.change_key_index, - &local_fundingkey, + &fc->uc->local_funding_pubkey, &channel_info.remote_fundingkey, fc->wtx.utxos); @@ -617,7 +622,12 @@ new_uncommitted_channel(struct lightningd *ld, uc->first_blocknum = get_block_height(ld->topology); uc->our_config.id = 0; + /* FIXME: Keep these in HSM! */ derive_channel_seed(ld, &uc->seed, &uc->peer->id, uc->dbid); + derive_basepoints(&uc->seed, + &uc->local_funding_pubkey, &uc->local_basepoints, + NULL, NULL); + uc->peer->uncommitted_channel = uc; tal_add_destructor(uc, destroy_uncommitted_channel); diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 2f5aab666..4460d8119 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -198,24 +198,22 @@ u32 feerate_max(struct lightningd *ld) static void sign_last_tx(struct channel *channel) { u8 *funding_wscript; - struct pubkey local_funding_pubkey; struct secrets secrets; secp256k1_ecdsa_signature sig; assert(!channel->last_tx->input[0].witness); - derive_basepoints(&channel->seed, &local_funding_pubkey, NULL, &secrets, - NULL); + derive_basepoints(&channel->seed, NULL, NULL, &secrets, NULL); funding_wscript = bitcoin_redeem_2of2(tmpctx, - &local_funding_pubkey, + &channel->local_funding_pubkey, &channel->channel_info.remote_fundingkey); /* Need input amount for signing */ channel->last_tx->input[0].amount = tal_dup(channel->last_tx->input, u64, &channel->funding_satoshi); sign_tx_input(channel->last_tx, 0, NULL, funding_wscript, &secrets.funding_privkey, - &local_funding_pubkey, + &channel->local_funding_pubkey, &sig); channel->last_tx->input[0].witness @@ -223,7 +221,7 @@ static void sign_last_tx(struct channel *channel) &channel->last_sig, &sig, &channel->channel_info.remote_fundingkey, - &local_funding_pubkey); + &channel->local_funding_pubkey); } static void remove_sig(struct bitcoin_tx *signed_tx) diff --git a/wallet/test/Makefile b/wallet/test/Makefile index 8dc3afb08..019fd944d 100644 --- a/wallet/test/Makefile +++ b/wallet/test/Makefile @@ -4,6 +4,7 @@ WALLET_TEST_PROGRAMS := $(WALLET_TEST_OBJS:.o=) WALLET_TEST_COMMON_OBJS := \ common/base32.o \ + common/derive_basepoints.o \ common/htlc_state.o \ common/type_to_string.o \ common/memleak.o \ diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 338ea031c..b500ab78a 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -59,13 +59,6 @@ void command_still_pending(struct command *cmd UNNEEDED) /* Generated stub for command_success */ void command_success(struct command *cmd UNNEEDED, struct json_result *response UNNEEDED) { fprintf(stderr, "command_success called!\n"); abort(); } -/* Generated stub for derive_basepoints */ -bool derive_basepoints(const struct secret *seed UNNEEDED, - struct pubkey *funding_pubkey UNNEEDED, - struct basepoints *basepoints UNNEEDED, - struct secrets *secrets UNNEEDED, - struct sha256 *shaseed UNNEEDED) -{ fprintf(stderr, "derive_basepoints called!\n"); abort(); } /* Generated stub for extract_channel_id */ bool extract_channel_id(const u8 *in_pkt UNNEEDED, struct channel_id *channel_id UNNEEDED) { fprintf(stderr, "extract_channel_id called!\n"); abort(); } @@ -988,6 +981,7 @@ int main(void) struct lightningd *ld; setup_tmpctx(); + secp256k1_ctx = wally_get_secp_context(); ld = tal(tmpctx, struct lightningd); /* Only elements in ld we should access */ diff --git a/wallet/wallet.c b/wallet/wallet.c index b1b71f361..80f77dac4 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -564,6 +564,9 @@ static struct channel *wallet_stmt2channel(const tal_t *ctx, struct wallet *w, s u8 *remote_shutdown_scriptpubkey; struct changed_htlc *last_sent_commit; s64 final_key_idx; + struct basepoints local_basepoints; + struct pubkey local_funding_pubkey; + struct secret seed; peer_dbid = sqlite3_column_int64(stmt, 1); peer = find_peer_by_dbid(w->ld, peer_dbid); @@ -623,6 +626,12 @@ static struct channel *wallet_stmt2channel(const tal_t *ctx, struct wallet *w, s log_broken(w->log, "%s: Final key < 0", __func__); return NULL; } + + /* FIXME: this belongs in HSM */ + derive_channel_seed(w->ld, &seed, &peer->id, + sqlite3_column_int64(stmt, 0)); + derive_basepoints(&seed, &local_funding_pubkey, &local_basepoints, + NULL, NULL); chan = new_channel(peer, sqlite3_column_int64(stmt, 0), &wshachain, sqlite3_column_int(stmt, 5), @@ -657,7 +666,8 @@ static struct channel *wallet_stmt2channel(const tal_t *ctx, struct wallet *w, s sqlite3_column_int(stmt, 36), sqlite3_column_int(stmt, 37), /* Not connected */ - false); + false, + &local_basepoints, &local_funding_pubkey); return chan; }