diff --git a/common/test/run-json_filter.c b/common/test/run-json_filter.c index 23704c0ee..594eb00f7 100644 --- a/common/test/run-json_filter.c +++ b/common/test/run-json_filter.c @@ -149,10 +149,6 @@ void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED) /* Generated stub for towire_u8_array */ void towire_u8_array(u8 **pptr UNNEEDED, const u8 *arr UNNEEDED, size_t num UNNEEDED) { fprintf(stderr, "towire_u8_array called!\n"); abort(); } -/* Generated stub for type_to_string_ */ -const char *type_to_string_(const tal_t *ctx UNNEEDED, const char *typename UNNEEDED, - union printable_types u UNNEEDED) -{ fprintf(stderr, "type_to_string_ called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ bool deprecated_apis; diff --git a/lightningd/channel_control.c b/lightningd/channel_control.c index 94dc7e483..adee64850 100644 --- a/lightningd/channel_control.c +++ b/lightningd/channel_control.c @@ -711,7 +711,7 @@ bool peer_start_channeld(struct channel *channel, struct ext_key final_ext_key; if (bip32_key_from_parent( - ld->wallet->bip32_base, + ld->bip32_base, channel->final_key_idx, BIP32_FLAG_KEY_PUBLIC, &final_ext_key) != WALLY_OK) { diff --git a/lightningd/closing_control.c b/lightningd/closing_control.c index 73ccaf835..702182586 100644 --- a/lightningd/closing_control.c +++ b/lightningd/closing_control.c @@ -465,7 +465,7 @@ void peer_start_closingd(struct channel *channel, struct peer_fd *peer_fd) &index_val, &is_p2sh)) { if (bip32_key_from_parent( - ld->wallet->bip32_base, + ld->bip32_base, index_val, BIP32_FLAG_KEY_PUBLIC, &ext_key_val) != WALLY_OK) { @@ -838,7 +838,7 @@ static struct command_result *json_close(struct command *cmd, struct ext_key *final_ext_key = NULL; if (final_index) { if (bip32_key_from_parent( - channel->peer->ld->wallet->bip32_base, + channel->peer->ld->bip32_base, *final_index, BIP32_FLAG_KEY_PUBLIC, &ext_key_val) != WALLY_OK) { diff --git a/lightningd/lightningd.c b/lightningd/lightningd.c index 981cf1ea9..822ba0452 100644 --- a/lightningd/lightningd.c +++ b/lightningd/lightningd.c @@ -610,7 +610,9 @@ static void shutdown_global_subdaemons(struct lightningd *ld) * use BIP32 (a.k.a. "HD wallet") to generate keys from a single seed, so we * keep the maximum-ever-used key index in the db, and add them all to the * filter here. */ -static void init_txfilter(struct wallet *w, struct txfilter *filter) +static void init_txfilter(struct wallet *w, + const struct ext_key *bip32_base, + struct txfilter *filter) { /*~ This is defined in libwally, so we didn't have to reimplement */ struct ext_key ext; @@ -621,7 +623,7 @@ static void init_txfilter(struct wallet *w, struct txfilter *filter) bip32_max_index = db_get_intvar(w->db, "bip32_max_index", 0); /*~ One of the C99 things I unequivocally approve: for-loop scope. */ for (u64 i = 0; i <= bip32_max_index + w->keyscan_gap; i++) { - if (bip32_key_from_parent(w->bip32_base, i, BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) { + if (bip32_key_from_parent(bip32_base, i, BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) { abort(); } txfilter_add_derkey(filter, ext.pub_key); @@ -900,7 +902,6 @@ int main(int argc, char *argv[]) struct timers *timers; const char *stop_response; struct htlc_in_map *unconnected_htlcs_in; - struct ext_key *bip32_base; int sigchld_rfd; struct io_conn *sigchld_conn = NULL; int exit_code = 0; @@ -1040,12 +1041,12 @@ int main(int argc, char *argv[]) * standard of key storage; ours is in software for now, so the name * doesn't really make sense, but we can't call it the Badly-named * Daemon Software Module. */ - bip32_base = hsm_init(ld); + ld->bip32_base = hsm_init(ld); /*~ Our "wallet" code really wraps the db, which is more than a simple * bitcoin wallet (though it's that too). It also stores channel * states, invoices, payments, blocks and bitcoin transactions. */ - ld->wallet = wallet_new(ld, ld->timers, bip32_base); + ld->wallet = wallet_new(ld, ld->timers); /*~ We keep a filter of scriptpubkeys we're interested in. */ ld->owned_txfilter = txfilter_new(ld); @@ -1085,7 +1086,7 @@ int main(int argc, char *argv[]) errx(EXITCODE_WALLET_DB_MISMATCH, "Wallet sanity check failed."); /*~ Initialize the transaction filter with our pubkeys. */ - init_txfilter(ld->wallet, ld->owned_txfilter); + init_txfilter(ld->wallet, ld->bip32_base, ld->owned_txfilter); /*~ Get the blockheight we are currently at, UINT32_MAX is used to signal * an uninitialized wallet and that we should start off of bitcoind's diff --git a/lightningd/lightningd.h b/lightningd/lightningd.h index 2eeaa48ae..9739f80e0 100644 --- a/lightningd/lightningd.h +++ b/lightningd/lightningd.h @@ -210,6 +210,8 @@ struct lightningd { /* Sets of HTLCs we are holding onto for MPP. */ struct htlc_set_map *htlc_sets; + /* Derive all our keys from here (see bip32_pubkey) */ + struct ext_key *bip32_base; struct wallet *wallet; /* Outstanding waitsendpay commands. */ diff --git a/lightningd/onchain_control.c b/lightningd/onchain_control.c index 0a7e6f3ff..c2417f887 100644 --- a/lightningd/onchain_control.c +++ b/lightningd/onchain_control.c @@ -662,7 +662,7 @@ enum watch_result onchaind_funding_spent(struct channel *channel, return KEEP_WATCHING; } - if (!bip32_pubkey(ld->wallet->bip32_base, &final_key, + if (!bip32_pubkey(ld->bip32_base, &final_key, channel->final_key_idx)) { log_broken(channel->log, "Could not derive onchain key %"PRIu64, channel->final_key_idx); @@ -670,7 +670,7 @@ enum watch_result onchaind_funding_spent(struct channel *channel, } struct ext_key final_wallet_ext_key; if (bip32_key_from_parent( - ld->wallet->bip32_base, + ld->bip32_base, channel->final_key_idx, BIP32_FLAG_KEY_PUBLIC, &final_wallet_ext_key) != WALLY_OK) { diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index b9457e72f..bbee7dc6f 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -214,7 +214,7 @@ u8 *p2wpkh_for_keyidx(const tal_t *ctx, struct lightningd *ld, u64 keyidx) { struct pubkey shutdownkey; - if (!bip32_pubkey(ld->wallet->bip32_base, &shutdownkey, keyidx)) + if (!bip32_pubkey(ld->bip32_base, &shutdownkey, keyidx)) return NULL; return scriptpubkey_p2wpkh(ctx, &shutdownkey); diff --git a/lightningd/test/run-find_my_abspath.c b/lightningd/test/run-find_my_abspath.c index ad9c21095..c09bd0128 100644 --- a/lightningd/test/run-find_my_abspath.c +++ b/lightningd/test/run-find_my_abspath.c @@ -233,8 +233,7 @@ void waitblockheight_notify_new_block(struct lightningd *ld UNNEEDED, void wallet_blocks_heights(struct wallet *w UNNEEDED, u32 def UNNEEDED, u32 *min UNNEEDED, u32 *max UNNEEDED) { fprintf(stderr, "wallet_blocks_heights called!\n"); abort(); } /* Generated stub for wallet_new */ -struct wallet *wallet_new(struct lightningd *ld UNNEEDED, struct timers *timers UNNEEDED, - struct ext_key *bip32_base UNNEEDED) +struct wallet *wallet_new(struct lightningd *ld UNNEEDED, struct timers *timers UNNEEDED) { fprintf(stderr, "wallet_new called!\n"); abort(); } /* Generated stub for wallet_sanity_check */ bool wallet_sanity_check(struct wallet *w UNNEEDED) diff --git a/wallet/reservation.c b/wallet/reservation.c index fef179944..109cf9ccc 100644 --- a/wallet/reservation.c +++ b/wallet/reservation.c @@ -357,7 +357,7 @@ static struct command_result *finish_psbt(struct command *cmd, } psbt = psbt_using_utxos(cmd, cmd->ld->wallet, utxos, - cmd->ld->wallet->bip32_base, + cmd->ld->bip32_base, *locktime, BITCOIN_TX_RBF_SEQUENCE); /* Should we add a change output for the excess? */ @@ -381,7 +381,7 @@ static struct command_result *finish_psbt(struct command *cmd, "Failed to generate change address." " Keys exhausted."); - if (!bip32_pubkey(cmd->ld->wallet->bip32_base, &pubkey, keyidx)) + if (!bip32_pubkey(cmd->ld->bip32_base, &pubkey, keyidx)) return command_fail(cmd, LIGHTNINGD, "Failed to generate change address." " Keys generation failure"); diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index a42a8f722..89a7da987 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -980,10 +980,10 @@ static struct wallet *create_test_wallet(struct lightningd *ld, const tal_t *ctx w->ld = ld; ld->wallet = w; - w->bip32_base = tal(w, struct ext_key); + ld->bip32_base = tal(ld, struct ext_key); CHECK(bip32_key_from_seed(badseed, sizeof(badseed), BIP32_VER_TEST_PRIVATE, 0, - w->bip32_base) == WALLY_OK); + ld->bip32_base) == WALLY_OK); CHECK_MSG(w->db, "Failed opening the db"); w->db->data_version = 0; diff --git a/wallet/wallet.c b/wallet/wallet.c index 03029b4c1..e2a34b18d 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -89,16 +89,14 @@ static void outpointfilters_init(struct wallet *w) tal_free(stmt); } -struct wallet *wallet_new(struct lightningd *ld, struct timers *timers, - struct ext_key *bip32_base STEALS) +struct wallet *wallet_new(struct lightningd *ld, struct timers *timers) { struct wallet *wallet = tal(ld, struct wallet); wallet->ld = ld; wallet->log = new_log(wallet, ld->log_book, NULL, "wallet"); - wallet->bip32_base = tal_steal(wallet, bip32_base); wallet->keyscan_gap = 50; list_head_init(&wallet->unstored_payments); - wallet->db = db_setup(wallet, ld, wallet->bip32_base); + wallet->db = db_setup(wallet, ld, ld->bip32_base); db_begin_transaction(wallet->db); wallet->invoices = invoices_new(wallet, wallet->db, timers); @@ -666,7 +664,7 @@ bool wallet_can_spend(struct wallet *w, const u8 *script, for (i = 0; i <= bip32_max_index + w->keyscan_gap; i++) { u8 *s; - if (bip32_key_from_parent(w->bip32_base, i, + if (bip32_key_from_parent(w->ld->bip32_base, i, BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) { abort(); diff --git a/wallet/wallet.h b/wallet/wallet.h index 4661e3e39..759e99fdb 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -27,7 +27,6 @@ struct wallet { struct lightningd *ld; struct db *db; struct log *log; - struct ext_key *bip32_base; struct invoices *invoices; struct list_head unstored_payments; u64 max_channel_dbid; @@ -421,8 +420,7 @@ struct wallet_transaction { * This is guaranteed to either return a valid wallet, or abort with * `fatal` if it cannot be initialized. */ -struct wallet *wallet_new(struct lightningd *ld, struct timers *timers, - struct ext_key *bip32_base); +struct wallet *wallet_new(struct lightningd *ld, struct timers *timers); /** * wallet_confirm_tx - Confirm a tx which contains a UTXO. diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 250f560a6..f50f2579f 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -127,7 +127,7 @@ static struct command_result *json_newaddr(struct command *cmd, return command_fail(cmd, LIGHTNINGD, "Keys exhausted "); } - if (!bip32_pubkey(cmd->ld->wallet->bip32_base, &pubkey, keyidx)) + if (!bip32_pubkey(cmd->ld->bip32_base, &pubkey, keyidx)) return command_fail(cmd, LIGHTNINGD, "Keys generation failure"); b32script = scriptpubkey_p2wpkh(tmpctx, &pubkey); @@ -189,7 +189,7 @@ static struct command_result *json_listaddrs(struct command *cmd, break; } - if (!bip32_pubkey(cmd->ld->wallet->bip32_base, &pubkey, keyidx)) + if (!bip32_pubkey(cmd->ld->bip32_base, &pubkey, keyidx)) abort(); // p2sh @@ -251,7 +251,7 @@ static void json_add_utxo(struct json_stream *response, if (utxo->is_p2sh) { struct pubkey key; - bip32_pubkey(wallet->bip32_base, &key, utxo->keyindex); + bip32_pubkey(wallet->ld->bip32_base, &key, utxo->keyindex); json_add_hex_talarr(response, "redeemscript", bitcoin_redeem_p2sh_p2wpkh(tmpctx, &key)); @@ -650,7 +650,7 @@ static struct command_result *match_psbt_inputs_to_utxos(struct command *cmd, u8 *redeemscript; int wally_err; - bip32_pubkey(cmd->ld->wallet->bip32_base, &key, + bip32_pubkey(cmd->ld->bip32_base, &key, utxo->keyindex); redeemscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &key); scriptPubKey = scriptpubkey_p2sh(tmpctx, redeemscript); @@ -692,7 +692,7 @@ static void match_psbt_outputs_to_wallet(struct wally_psbt *psbt, continue; if (bip32_key_from_parent( - w->bip32_base, index, BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) { + w->ld->bip32_base, index, BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) { abort(); }