mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-03 18:57:06 +01:00
wallet_get_newindex: encapsulate routine to get a new keyindex.
We'll want this for shutdown. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
ee00c2c508
commit
a0800e352a
3 changed files with 26 additions and 9 deletions
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <bitcoin/script.h>
|
#include <bitcoin/script.h>
|
||||||
#include <ccan/str/hex/hex.h>
|
#include <ccan/str/hex/hex.h>
|
||||||
|
#include <lightningd/lightningd.h>
|
||||||
|
|
||||||
struct wallet *wallet_new(const tal_t *ctx, struct log *log)
|
struct wallet *wallet_new(const tal_t *ctx, struct log *log)
|
||||||
{
|
{
|
||||||
|
@ -221,3 +222,13 @@ bool wallet_can_spend(struct wallet *w, const u8 *script,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s64 wallet_get_newindex(struct lightningd *ld)
|
||||||
|
{
|
||||||
|
u64 newidx = db_get_intvar(ld->wallet->db, "bip32_max_index", 0) + 1;
|
||||||
|
|
||||||
|
if (newidx == BIP32_INITIAL_HARDENED_CHILD)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
db_set_intvar(ld->wallet->db, "bip32_max_index", newidx);
|
||||||
|
return newidx;
|
||||||
|
}
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
#include <lightningd/utxo.h>
|
#include <lightningd/utxo.h>
|
||||||
#include <wally_bip32.h>
|
#include <wally_bip32.h>
|
||||||
|
|
||||||
|
struct lightningd;
|
||||||
|
|
||||||
struct wallet {
|
struct wallet {
|
||||||
struct db *db;
|
struct db *db;
|
||||||
struct log *log;
|
struct log *log;
|
||||||
|
@ -104,4 +106,12 @@ void wallet_confirm_utxos(struct wallet *w, const struct utxo **utxos);
|
||||||
bool wallet_can_spend(struct wallet *w, const u8 *script,
|
bool wallet_can_spend(struct wallet *w, const u8 *script,
|
||||||
u32 *index, bool *output_is_p2sh);
|
u32 *index, bool *output_is_p2sh);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* wallet_get_newindex - get a new index from the wallet.
|
||||||
|
* @ld: (in) lightning daemon
|
||||||
|
*
|
||||||
|
* Returns -1 on error (key exhaustion).
|
||||||
|
*/
|
||||||
|
s64 wallet_get_newindex(struct lightningd *ld);
|
||||||
|
|
||||||
#endif /* WALLET_WALLET_H */
|
#endif /* WALLET_WALLET_H */
|
||||||
|
|
|
@ -161,10 +161,7 @@ static void json_withdraw(struct command *cmd,
|
||||||
if (withdraw->changesatoshi <= 546)
|
if (withdraw->changesatoshi <= 546)
|
||||||
withdraw->changesatoshi = 0;
|
withdraw->changesatoshi = 0;
|
||||||
|
|
||||||
withdraw->change_key_index =
|
withdraw->change_key_index = wallet_get_newindex(ld);
|
||||||
db_get_intvar(ld->wallet->db, "bip32_max_index", 0) + 1;
|
|
||||||
db_set_intvar(ld->wallet->db, "bip32_max_index",
|
|
||||||
withdraw->change_key_index);
|
|
||||||
|
|
||||||
utxos = from_utxoptr_arr(withdraw, withdraw->utxos);
|
utxos = from_utxoptr_arr(withdraw, withdraw->utxos);
|
||||||
u8 *msg = towire_hsmctl_sign_withdrawal(cmd,
|
u8 *msg = towire_hsmctl_sign_withdrawal(cmd,
|
||||||
|
@ -238,14 +235,15 @@ static void json_newaddr(struct command *cmd,
|
||||||
struct ripemd160 p2sh;
|
struct ripemd160 p2sh;
|
||||||
struct pubkey pubkey;
|
struct pubkey pubkey;
|
||||||
u8 *redeemscript;
|
u8 *redeemscript;
|
||||||
u64 bip32_max_index = db_get_intvar(ld->wallet->db, "bip32_max_index", 0);
|
s64 keyidx;
|
||||||
|
|
||||||
if (bip32_max_index == BIP32_INITIAL_HARDENED_CHILD) {
|
keyidx = wallet_get_newindex(ld);
|
||||||
|
if (keyidx < 0) {
|
||||||
command_fail(cmd, "Keys exhausted ");
|
command_fail(cmd, "Keys exhausted ");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bip32_key_from_parent(ld->bip32_base, bip32_max_index,
|
if (bip32_key_from_parent(ld->bip32_base, keyidx,
|
||||||
BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) {
|
BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) {
|
||||||
command_fail(cmd, "Keys generation failure");
|
command_fail(cmd, "Keys generation failure");
|
||||||
return;
|
return;
|
||||||
|
@ -261,8 +259,6 @@ static void json_newaddr(struct command *cmd,
|
||||||
sha256(&h, redeemscript, tal_count(redeemscript));
|
sha256(&h, redeemscript, tal_count(redeemscript));
|
||||||
ripemd160(&p2sh, h.u.u8, sizeof(h));
|
ripemd160(&p2sh, h.u.u8, sizeof(h));
|
||||||
|
|
||||||
db_set_intvar(ld->wallet->db, "bip32_max_index", bip32_max_index + 1);
|
|
||||||
|
|
||||||
json_object_start(response, NULL);
|
json_object_start(response, NULL);
|
||||||
json_add_string(response, "address",
|
json_add_string(response, "address",
|
||||||
p2sh_to_base58(cmd, cmd->dstate->testnet, &p2sh));
|
p2sh_to_base58(cmd, cmd->dstate->testnet, &p2sh));
|
||||||
|
|
Loading…
Add table
Reference in a new issue