mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 07:04:22 +01:00
lightningd: move bip32_base pointer into struct lightningd.
It's needed as the db and wallet is being set up (db migrations), so it's simpler this way to always use ld->bip32_base for the next patch. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
@@ -149,10 +149,6 @@ void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED)
|
|||||||
/* Generated stub for towire_u8_array */
|
/* Generated stub for towire_u8_array */
|
||||||
void towire_u8_array(u8 **pptr UNNEEDED, const u8 *arr UNNEEDED, size_t num UNNEEDED)
|
void towire_u8_array(u8 **pptr UNNEEDED, const u8 *arr UNNEEDED, size_t num UNNEEDED)
|
||||||
{ fprintf(stderr, "towire_u8_array called!\n"); abort(); }
|
{ 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 */
|
/* AUTOGENERATED MOCKS END */
|
||||||
|
|
||||||
bool deprecated_apis;
|
bool deprecated_apis;
|
||||||
|
|||||||
@@ -711,7 +711,7 @@ bool peer_start_channeld(struct channel *channel,
|
|||||||
|
|
||||||
struct ext_key final_ext_key;
|
struct ext_key final_ext_key;
|
||||||
if (bip32_key_from_parent(
|
if (bip32_key_from_parent(
|
||||||
ld->wallet->bip32_base,
|
ld->bip32_base,
|
||||||
channel->final_key_idx,
|
channel->final_key_idx,
|
||||||
BIP32_FLAG_KEY_PUBLIC,
|
BIP32_FLAG_KEY_PUBLIC,
|
||||||
&final_ext_key) != WALLY_OK) {
|
&final_ext_key) != WALLY_OK) {
|
||||||
|
|||||||
@@ -465,7 +465,7 @@ void peer_start_closingd(struct channel *channel, struct peer_fd *peer_fd)
|
|||||||
&index_val,
|
&index_val,
|
||||||
&is_p2sh)) {
|
&is_p2sh)) {
|
||||||
if (bip32_key_from_parent(
|
if (bip32_key_from_parent(
|
||||||
ld->wallet->bip32_base,
|
ld->bip32_base,
|
||||||
index_val,
|
index_val,
|
||||||
BIP32_FLAG_KEY_PUBLIC,
|
BIP32_FLAG_KEY_PUBLIC,
|
||||||
&ext_key_val) != WALLY_OK) {
|
&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;
|
struct ext_key *final_ext_key = NULL;
|
||||||
if (final_index) {
|
if (final_index) {
|
||||||
if (bip32_key_from_parent(
|
if (bip32_key_from_parent(
|
||||||
channel->peer->ld->wallet->bip32_base,
|
channel->peer->ld->bip32_base,
|
||||||
*final_index,
|
*final_index,
|
||||||
BIP32_FLAG_KEY_PUBLIC,
|
BIP32_FLAG_KEY_PUBLIC,
|
||||||
&ext_key_val) != WALLY_OK) {
|
&ext_key_val) != WALLY_OK) {
|
||||||
|
|||||||
@@ -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
|
* 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
|
* keep the maximum-ever-used key index in the db, and add them all to the
|
||||||
* filter here. */
|
* 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 */
|
/*~ This is defined in libwally, so we didn't have to reimplement */
|
||||||
struct ext_key ext;
|
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);
|
bip32_max_index = db_get_intvar(w->db, "bip32_max_index", 0);
|
||||||
/*~ One of the C99 things I unequivocally approve: for-loop scope. */
|
/*~ One of the C99 things I unequivocally approve: for-loop scope. */
|
||||||
for (u64 i = 0; i <= bip32_max_index + w->keyscan_gap; i++) {
|
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();
|
abort();
|
||||||
}
|
}
|
||||||
txfilter_add_derkey(filter, ext.pub_key);
|
txfilter_add_derkey(filter, ext.pub_key);
|
||||||
@@ -900,7 +902,6 @@ int main(int argc, char *argv[])
|
|||||||
struct timers *timers;
|
struct timers *timers;
|
||||||
const char *stop_response;
|
const char *stop_response;
|
||||||
struct htlc_in_map *unconnected_htlcs_in;
|
struct htlc_in_map *unconnected_htlcs_in;
|
||||||
struct ext_key *bip32_base;
|
|
||||||
int sigchld_rfd;
|
int sigchld_rfd;
|
||||||
struct io_conn *sigchld_conn = NULL;
|
struct io_conn *sigchld_conn = NULL;
|
||||||
int exit_code = 0;
|
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
|
* 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
|
* doesn't really make sense, but we can't call it the Badly-named
|
||||||
* Daemon Software Module. */
|
* 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
|
/*~ Our "wallet" code really wraps the db, which is more than a simple
|
||||||
* bitcoin wallet (though it's that too). It also stores channel
|
* bitcoin wallet (though it's that too). It also stores channel
|
||||||
* states, invoices, payments, blocks and bitcoin transactions. */
|
* 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. */
|
/*~ We keep a filter of scriptpubkeys we're interested in. */
|
||||||
ld->owned_txfilter = txfilter_new(ld);
|
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.");
|
errx(EXITCODE_WALLET_DB_MISMATCH, "Wallet sanity check failed.");
|
||||||
|
|
||||||
/*~ Initialize the transaction filter with our pubkeys. */
|
/*~ 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
|
/*~ 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
|
* an uninitialized wallet and that we should start off of bitcoind's
|
||||||
|
|||||||
@@ -210,6 +210,8 @@ struct lightningd {
|
|||||||
/* Sets of HTLCs we are holding onto for MPP. */
|
/* Sets of HTLCs we are holding onto for MPP. */
|
||||||
struct htlc_set_map *htlc_sets;
|
struct htlc_set_map *htlc_sets;
|
||||||
|
|
||||||
|
/* Derive all our keys from here (see bip32_pubkey) */
|
||||||
|
struct ext_key *bip32_base;
|
||||||
struct wallet *wallet;
|
struct wallet *wallet;
|
||||||
|
|
||||||
/* Outstanding waitsendpay commands. */
|
/* Outstanding waitsendpay commands. */
|
||||||
|
|||||||
@@ -662,7 +662,7 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
|
|||||||
return KEEP_WATCHING;
|
return KEEP_WATCHING;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!bip32_pubkey(ld->wallet->bip32_base, &final_key,
|
if (!bip32_pubkey(ld->bip32_base, &final_key,
|
||||||
channel->final_key_idx)) {
|
channel->final_key_idx)) {
|
||||||
log_broken(channel->log, "Could not derive onchain key %"PRIu64,
|
log_broken(channel->log, "Could not derive onchain key %"PRIu64,
|
||||||
channel->final_key_idx);
|
channel->final_key_idx);
|
||||||
@@ -670,7 +670,7 @@ enum watch_result onchaind_funding_spent(struct channel *channel,
|
|||||||
}
|
}
|
||||||
struct ext_key final_wallet_ext_key;
|
struct ext_key final_wallet_ext_key;
|
||||||
if (bip32_key_from_parent(
|
if (bip32_key_from_parent(
|
||||||
ld->wallet->bip32_base,
|
ld->bip32_base,
|
||||||
channel->final_key_idx,
|
channel->final_key_idx,
|
||||||
BIP32_FLAG_KEY_PUBLIC,
|
BIP32_FLAG_KEY_PUBLIC,
|
||||||
&final_wallet_ext_key) != WALLY_OK) {
|
&final_wallet_ext_key) != WALLY_OK) {
|
||||||
|
|||||||
@@ -214,7 +214,7 @@ u8 *p2wpkh_for_keyidx(const tal_t *ctx, struct lightningd *ld, u64 keyidx)
|
|||||||
{
|
{
|
||||||
struct pubkey shutdownkey;
|
struct pubkey shutdownkey;
|
||||||
|
|
||||||
if (!bip32_pubkey(ld->wallet->bip32_base, &shutdownkey, keyidx))
|
if (!bip32_pubkey(ld->bip32_base, &shutdownkey, keyidx))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
return scriptpubkey_p2wpkh(ctx, &shutdownkey);
|
return scriptpubkey_p2wpkh(ctx, &shutdownkey);
|
||||||
|
|||||||
@@ -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)
|
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(); }
|
{ fprintf(stderr, "wallet_blocks_heights called!\n"); abort(); }
|
||||||
/* Generated stub for wallet_new */
|
/* Generated stub for wallet_new */
|
||||||
struct wallet *wallet_new(struct lightningd *ld UNNEEDED, struct timers *timers UNNEEDED,
|
struct wallet *wallet_new(struct lightningd *ld UNNEEDED, struct timers *timers UNNEEDED)
|
||||||
struct ext_key *bip32_base UNNEEDED)
|
|
||||||
{ fprintf(stderr, "wallet_new called!\n"); abort(); }
|
{ fprintf(stderr, "wallet_new called!\n"); abort(); }
|
||||||
/* Generated stub for wallet_sanity_check */
|
/* Generated stub for wallet_sanity_check */
|
||||||
bool wallet_sanity_check(struct wallet *w UNNEEDED)
|
bool wallet_sanity_check(struct wallet *w UNNEEDED)
|
||||||
|
|||||||
@@ -357,7 +357,7 @@ static struct command_result *finish_psbt(struct command *cmd,
|
|||||||
}
|
}
|
||||||
|
|
||||||
psbt = psbt_using_utxos(cmd, cmd->ld->wallet, utxos,
|
psbt = psbt_using_utxos(cmd, cmd->ld->wallet, utxos,
|
||||||
cmd->ld->wallet->bip32_base,
|
cmd->ld->bip32_base,
|
||||||
*locktime, BITCOIN_TX_RBF_SEQUENCE);
|
*locktime, BITCOIN_TX_RBF_SEQUENCE);
|
||||||
|
|
||||||
/* Should we add a change output for the excess? */
|
/* 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."
|
"Failed to generate change address."
|
||||||
" Keys exhausted.");
|
" 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,
|
return command_fail(cmd, LIGHTNINGD,
|
||||||
"Failed to generate change address."
|
"Failed to generate change address."
|
||||||
" Keys generation failure");
|
" Keys generation failure");
|
||||||
|
|||||||
@@ -980,10 +980,10 @@ static struct wallet *create_test_wallet(struct lightningd *ld, const tal_t *ctx
|
|||||||
w->ld = ld;
|
w->ld = ld;
|
||||||
ld->wallet = w;
|
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),
|
CHECK(bip32_key_from_seed(badseed, sizeof(badseed),
|
||||||
BIP32_VER_TEST_PRIVATE, 0,
|
BIP32_VER_TEST_PRIVATE, 0,
|
||||||
w->bip32_base) == WALLY_OK);
|
ld->bip32_base) == WALLY_OK);
|
||||||
|
|
||||||
CHECK_MSG(w->db, "Failed opening the db");
|
CHECK_MSG(w->db, "Failed opening the db");
|
||||||
w->db->data_version = 0;
|
w->db->data_version = 0;
|
||||||
|
|||||||
@@ -89,16 +89,14 @@ static void outpointfilters_init(struct wallet *w)
|
|||||||
tal_free(stmt);
|
tal_free(stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct wallet *wallet_new(struct lightningd *ld, struct timers *timers,
|
struct wallet *wallet_new(struct lightningd *ld, struct timers *timers)
|
||||||
struct ext_key *bip32_base STEALS)
|
|
||||||
{
|
{
|
||||||
struct wallet *wallet = tal(ld, struct wallet);
|
struct wallet *wallet = tal(ld, struct wallet);
|
||||||
wallet->ld = ld;
|
wallet->ld = ld;
|
||||||
wallet->log = new_log(wallet, ld->log_book, NULL, "wallet");
|
wallet->log = new_log(wallet, ld->log_book, NULL, "wallet");
|
||||||
wallet->bip32_base = tal_steal(wallet, bip32_base);
|
|
||||||
wallet->keyscan_gap = 50;
|
wallet->keyscan_gap = 50;
|
||||||
list_head_init(&wallet->unstored_payments);
|
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);
|
db_begin_transaction(wallet->db);
|
||||||
wallet->invoices = invoices_new(wallet, wallet->db, timers);
|
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++) {
|
for (i = 0; i <= bip32_max_index + w->keyscan_gap; i++) {
|
||||||
u8 *s;
|
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)
|
BIP32_FLAG_KEY_PUBLIC, &ext)
|
||||||
!= WALLY_OK) {
|
!= WALLY_OK) {
|
||||||
abort();
|
abort();
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ struct wallet {
|
|||||||
struct lightningd *ld;
|
struct lightningd *ld;
|
||||||
struct db *db;
|
struct db *db;
|
||||||
struct log *log;
|
struct log *log;
|
||||||
struct ext_key *bip32_base;
|
|
||||||
struct invoices *invoices;
|
struct invoices *invoices;
|
||||||
struct list_head unstored_payments;
|
struct list_head unstored_payments;
|
||||||
u64 max_channel_dbid;
|
u64 max_channel_dbid;
|
||||||
@@ -421,8 +420,7 @@ struct wallet_transaction {
|
|||||||
* This is guaranteed to either return a valid wallet, or abort with
|
* This is guaranteed to either return a valid wallet, or abort with
|
||||||
* `fatal` if it cannot be initialized.
|
* `fatal` if it cannot be initialized.
|
||||||
*/
|
*/
|
||||||
struct wallet *wallet_new(struct lightningd *ld, struct timers *timers,
|
struct wallet *wallet_new(struct lightningd *ld, struct timers *timers);
|
||||||
struct ext_key *bip32_base);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* wallet_confirm_tx - Confirm a tx which contains a UTXO.
|
* wallet_confirm_tx - Confirm a tx which contains a UTXO.
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ static struct command_result *json_newaddr(struct command *cmd,
|
|||||||
return command_fail(cmd, LIGHTNINGD, "Keys exhausted ");
|
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");
|
return command_fail(cmd, LIGHTNINGD, "Keys generation failure");
|
||||||
|
|
||||||
b32script = scriptpubkey_p2wpkh(tmpctx, &pubkey);
|
b32script = scriptpubkey_p2wpkh(tmpctx, &pubkey);
|
||||||
@@ -189,7 +189,7 @@ static struct command_result *json_listaddrs(struct command *cmd,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!bip32_pubkey(cmd->ld->wallet->bip32_base, &pubkey, keyidx))
|
if (!bip32_pubkey(cmd->ld->bip32_base, &pubkey, keyidx))
|
||||||
abort();
|
abort();
|
||||||
|
|
||||||
// p2sh
|
// p2sh
|
||||||
@@ -251,7 +251,7 @@ static void json_add_utxo(struct json_stream *response,
|
|||||||
|
|
||||||
if (utxo->is_p2sh) {
|
if (utxo->is_p2sh) {
|
||||||
struct pubkey key;
|
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",
|
json_add_hex_talarr(response, "redeemscript",
|
||||||
bitcoin_redeem_p2sh_p2wpkh(tmpctx, &key));
|
bitcoin_redeem_p2sh_p2wpkh(tmpctx, &key));
|
||||||
@@ -650,7 +650,7 @@ static struct command_result *match_psbt_inputs_to_utxos(struct command *cmd,
|
|||||||
u8 *redeemscript;
|
u8 *redeemscript;
|
||||||
int wally_err;
|
int wally_err;
|
||||||
|
|
||||||
bip32_pubkey(cmd->ld->wallet->bip32_base, &key,
|
bip32_pubkey(cmd->ld->bip32_base, &key,
|
||||||
utxo->keyindex);
|
utxo->keyindex);
|
||||||
redeemscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &key);
|
redeemscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &key);
|
||||||
scriptPubKey = scriptpubkey_p2sh(tmpctx, redeemscript);
|
scriptPubKey = scriptpubkey_p2sh(tmpctx, redeemscript);
|
||||||
@@ -692,7 +692,7 @@ static void match_psbt_outputs_to_wallet(struct wally_psbt *psbt,
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (bip32_key_from_parent(
|
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();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user