Skip to content

Commit 65c2bac

Browse files
niftyneicdecker
authored andcommitted
hsmd/wallet: pass the bip32_key down into migrations
we're about to add a migration that requires access to the bip32_key in order to calculate missing scriptpubkeys. prior to this patch, we don't have access to the bip32 key in the db migration, as it's set on the wallet but after the db migrations are run. here we patch it through so that every migration can access it
1 parent 4517435 commit 65c2bac

File tree

8 files changed

+42
-22
lines changed

8 files changed

+42
-22
lines changed

lightningd/lightningd.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -841,8 +841,7 @@ int main(int argc, char *argv[])
841841
/*~ Our "wallet" code really wraps the db, which is more than a simple
842842
* bitcoin wallet (though it's that too). It also stores channel
843843
* states, invoices, payments, blocks and bitcoin transactions. */
844-
ld->wallet = wallet_new(ld, ld->timers);
845-
ld->wallet->bip32_base = tal_steal(ld->wallet, bip32_base);
844+
ld->wallet = wallet_new(ld, ld->timers, bip32_base);
846845

847846
/*~ We keep track of how many 'coin moves' we've ever made.
848847
* Initialize the starting value from the database here. */

lightningd/test/run-find_my_abspath.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ void wallet_clean_utxos(struct wallet *w UNNEEDED, struct bitcoind *bitcoind UNN
250250
bool wallet_network_check(struct wallet *w UNNEEDED)
251251
{ fprintf(stderr, "wallet_network_check called!\n"); abort(); }
252252
/* Generated stub for wallet_new */
253-
struct wallet *wallet_new(struct lightningd *ld UNNEEDED, struct timers *timers UNNEEDED)
253+
struct wallet *wallet_new(struct lightningd *ld UNNEEDED, struct timers *timers UNNEEDED,
254+
struct ext_key *bip32_base UNNEEDED)
254255
{ fprintf(stderr, "wallet_new called!\n"); abort(); }
255256
/* AUTOGENERATED MOCKS END */
256257

wallet/db.c

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,25 @@
1414
#include <lightningd/log.h>
1515
#include <lightningd/plugin_hook.h>
1616
#include <wallet/db_common.h>
17+
#include <wally_bip32.h>
1718

1819
#define NSEC_IN_SEC 1000000000
1920

2021
struct migration {
2122
const char *sql;
22-
void (*func)(struct lightningd *ld, struct db *db);
23+
void (*func)(struct lightningd *ld, struct db *db,
24+
const struct ext_key *bip32_base);
2325
};
2426

25-
static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db);
27+
static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db,
28+
const struct ext_key *bip32_base);
29+
30+
static void migrate_our_funding(struct lightningd *ld, struct db *db,
31+
const struct ext_key *bip32_base);
32+
33+
static void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db,
34+
const struct ext_key *bip32_base);
2635

27-
static void migrate_our_funding(struct lightningd *ld, struct db *db);
2836

2937
/* Do not reorder or remove elements from this array, it is used to
3038
* migrate existing databases from a previous state, based on the
@@ -970,7 +978,8 @@ static int db_get_version(struct db *db)
970978
/**
971979
* db_migrate - Apply all remaining migrations from the current version
972980
*/
973-
static void db_migrate(struct lightningd *ld, struct db *db)
981+
static void db_migrate(struct lightningd *ld, struct db *db,
982+
const struct ext_key *bip32_base)
974983
{
975984
/* Attempt to read the version from the database */
976985
int current, orig, available;
@@ -997,7 +1006,7 @@ static void db_migrate(struct lightningd *ld, struct db *db)
9971006
tal_free(stmt);
9981007
}
9991008
if (dbmigrations[current].func)
1000-
dbmigrations[current].func(ld, db);
1009+
dbmigrations[current].func(ld, db, bip32_base);
10011010
}
10021011

10031012
/* Finally update the version number in the version table */
@@ -1029,14 +1038,15 @@ u32 db_data_version_get(struct db *db)
10291038
return version;
10301039
}
10311040

1032-
struct db *db_setup(const tal_t *ctx, struct lightningd *ld)
1041+
struct db *db_setup(const tal_t *ctx, struct lightningd *ld,
1042+
const struct ext_key *bip32_base)
10331043
{
10341044
struct db *db = db_open(ctx, ld->wallet_dsn);
10351045
db->log = new_log(db, ld->log_book, NULL, "database");
10361046

10371047
db_begin_transaction(db);
10381048

1039-
db_migrate(ld, db);
1049+
db_migrate(ld, db, bip32_base);
10401050

10411051
db->data_version = db_data_version_get(db);
10421052
db_commit_transaction(db);
@@ -1082,7 +1092,8 @@ void db_set_intvar(struct db *db, char *varname, s64 val)
10821092
}
10831093

10841094
/* Will apply the current config fee settings to all channels */
1085-
static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db)
1095+
static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db *db,
1096+
const struct ext_key *bip32_base)
10861097
{
10871098
struct db_stmt *stmt = db_prepare_v2(
10881099
db, SQL("UPDATE channels SET feerate_base = ?, feerate_ppm = ?;"));
@@ -1100,7 +1111,8 @@ static void migrate_pr2342_feerate_per_channel(struct lightningd *ld, struct db
11001111
* is the same as the funding_satoshi for every channel where we are
11011112
* the `funder`
11021113
*/
1103-
static void migrate_our_funding(struct lightningd *ld, struct db *db)
1114+
static void migrate_our_funding(struct lightningd *ld, struct db *db,
1115+
const struct ext_key *bip32_base)
11041116
{
11051117
struct db_stmt *stmt;
11061118

@@ -1121,7 +1133,8 @@ static void migrate_our_funding(struct lightningd *ld, struct db *db)
11211133
* This migration loads all of the last_tx's and 're-formats' them into psbts,
11221134
* adds the required input witness utxo information, and then saves it back to disk
11231135
* */
1124-
void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db)
1136+
void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db,
1137+
const struct ext_key *bip32_base)
11251138
{
11261139
struct db_stmt *stmt, *update_stmt;
11271140

wallet/db.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <secp256k1_ecdh.h>
1616
#include <stdbool.h>
1717

18+
struct ext_key;
1819
struct lightningd;
1920
struct log;
2021
struct node_id;
@@ -23,7 +24,6 @@ struct db_stmt;
2324
struct db;
2425
struct wally_psbt;
2526

26-
void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db);
2727
/**
2828
* Macro to annotate a named SQL query.
2929
*
@@ -56,8 +56,10 @@ void migrate_last_tx_to_psbt(struct lightningd *ld, struct db *db);
5656
* Params:
5757
* @ctx: the tal_t context to allocate from
5858
* @ld: the lightningd context to hand to upgrade functions.
59+
* @bip32_base: the base all of our pubkeys are constructed on
5960
*/
60-
struct db *db_setup(const tal_t *ctx, struct lightningd *ld);
61+
struct db *db_setup(const tal_t *ctx, struct lightningd *ld,
62+
const struct ext_key *bip32_base);
6163

6264
/**
6365
* db_begin_transaction - Begin a transaction

wallet/test/run-db.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ static struct db *create_test_db(void)
7272
static bool test_empty_db_migrate(struct lightningd *ld)
7373
{
7474
struct db *db = create_test_db();
75+
const struct ext_key *bip32_base = NULL;
7576
CHECK(db);
7677
db_begin_transaction(db);
7778
CHECK(db_get_version(db) == -1);
78-
db_migrate(ld, db);
79+
db_migrate(ld, db, bip32_base);
7980
db_commit_transaction(db);
8081

8182
db_begin_transaction(db);
@@ -124,10 +125,11 @@ static bool test_vars(struct lightningd *ld)
124125
{
125126
struct db *db = create_test_db();
126127
char *varname = "testvar";
128+
const struct ext_key *bip32_base = NULL;
127129
CHECK(db);
128130

129131
db_begin_transaction(db);
130-
db_migrate(ld, db);
132+
db_migrate(ld, db, bip32_base);
131133
/* Check default behavior */
132134
CHECK(db_get_intvar(db, varname, 42) == 42);
133135

wallet/test/run-wallet.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,7 @@ static struct wallet *create_test_wallet(struct lightningd *ld, const tal_t *ctx
866866
int fd = mkstemp(filename);
867867
struct wallet *w = tal(ctx, struct wallet);
868868
static unsigned char badseed[BIP32_ENTROPY_LEN_128];
869+
const struct ext_key *bip32_base = NULL;
869870
CHECK_MSG(fd != -1, "Unable to generate temp filename");
870871
close(fd);
871872

@@ -885,7 +886,7 @@ static struct wallet *create_test_wallet(struct lightningd *ld, const tal_t *ctx
885886

886887
CHECK_MSG(w->db, "Failed opening the db");
887888
db_begin_transaction(w->db);
888-
db_migrate(ld, w->db);
889+
db_migrate(ld, w->db, bip32_base);
889890
w->db->data_version = 0;
890891
db_commit_transaction(w->db);
891892
CHECK_MSG(!wallet_err, "DB migration failed");

wallet/wallet.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,17 @@ static void outpointfilters_init(struct wallet *w)
5858
tal_free(stmt);
5959
}
6060

61-
struct wallet *wallet_new(struct lightningd *ld, struct timers *timers)
61+
struct wallet *wallet_new(struct lightningd *ld, struct timers *timers,
62+
struct ext_key *bip32_base STEALS)
6263
{
6364
struct wallet *wallet = tal(ld, struct wallet);
6465
wallet->ld = ld;
65-
wallet->db = db_setup(wallet, ld);
6666
wallet->log = new_log(wallet, ld->log_book, NULL, "wallet");
67-
wallet->bip32_base = NULL;
67+
wallet->bip32_base = tal_steal(wallet, bip32_base);
6868
wallet->keyscan_gap = 50;
6969
list_head_init(&wallet->unstored_payments);
7070
list_head_init(&wallet->unreleased_txs);
71+
wallet->db = db_setup(wallet, ld, wallet->bip32_base);
7172

7273
db_begin_transaction(wallet->db);
7374
wallet->invoices = invoices_new(wallet, wallet->db, timers);

wallet/wallet.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,8 @@ struct wallet_transaction {
331331
* This is guaranteed to either return a valid wallet, or abort with
332332
* `fatal` if it cannot be initialized.
333333
*/
334-
struct wallet *wallet_new(struct lightningd *ld, struct timers *timers);
334+
struct wallet *wallet_new(struct lightningd *ld, struct timers *timers,
335+
struct ext_key *bip32_base);
335336

336337
/**
337338
* wallet_confirm_tx - Confirm a tx which contains a UTXO.

0 commit comments

Comments
 (0)