Skip to content

Commit f3fa2d7

Browse files
sangbidarustyrussell
authored andcommitted
hsmd_wire: remove hsmd_derive_bip86_key and add it to hsmd_init_reply_v4
Instead of having a separate field to derive the bip86 base key, we return it in the hsmd init reply once we know that the hsm_secret is of mnemonic type
1 parent ba487d2 commit f3fa2d7

File tree

6 files changed

+17
-92
lines changed

6 files changed

+17
-92
lines changed

common/hsm_version.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* v6 with hsm_passphrase changes: c646d557d7561dd885df3cad5b99c82895cda4b040699f3853980ec61b2873fa
3333
* v6 with hsm_secret struct cleanup: 06c56396fe42f4f47911d7f865dd0004d264fc1348f89547743755b6b33fec90
3434
* v6 with hsm_secret_type TLV: 7bb5deb2367482feb084d304ee14b2373d42910ad56484fbf47614dbb3d4cb74
35+
* v6 with bip86_base in TLV: 6bb6e6ee256f22a6fb41856c90feebde3065a9074e79a46731e453a932be83f0
3536
*/
3637
#define HSM_MIN_VERSION 5
3738
#define HSM_MAX_VERSION 6

hsmd/hsmd.c

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -680,34 +680,6 @@ void hsmd_status_failed(enum status_failreason reason, const char *fmt, ...)
680680
status_send_fatal(take(towire_status_fail(NULL, reason, str)));
681681
}
682682

683-
/* Handle BIP86 key derivation request */
684-
static struct io_plan *handle_derive_bip86_key(struct io_conn *conn,
685-
struct client *c,
686-
const u8 *msg_in)
687-
{
688-
u8 *reply;
689-
u32 index;
690-
bool is_change;
691-
692-
/* Extract parameters from the wire message */
693-
if (!fromwire_hsmd_derive_bip86_key(msg_in, &index, &is_change))
694-
return bad_req(conn, c, msg_in);
695-
696-
/* Check if we have a mnemonic-based HSM secret */
697-
if (!use_bip86_derivation(tal_bytelen(hsm_secret->secret_data))) {
698-
return bad_req_fmt(conn, c, msg_in,
699-
"BIP86 derivation requires mnemonic-based HSM secret");
700-
}
701-
702-
/* Derive only the BIP86 base key (m/86'/0'/0') */
703-
struct ext_key bip86_base;
704-
derive_bip86_base_key(&bip86_base);
705-
706-
/* Return the full BIP86 base extended key */
707-
reply = towire_hsmd_derive_bip86_key_reply(NULL, &bip86_base);
708-
return req_reply(conn, c, take(reply));
709-
}
710-
711683
/* Handle BIP86 pubkey check request */
712684
static struct io_plan *handle_check_bip86_pubkey(struct io_conn *conn,
713685
struct client *c,
@@ -768,8 +740,6 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
768740
case WIRE_HSMD_CLIENT_HSMFD:
769741
return pass_client_hsmfd(conn, c, c->msg_in);
770742

771-
case WIRE_HSMD_DERIVE_BIP86_KEY:
772-
return handle_derive_bip86_key(conn, c, c->msg_in);
773743
case WIRE_HSMD_CHECK_BIP86_PUBKEY:
774744
return handle_check_bip86_pubkey(conn, c, c->msg_in);
775745

@@ -864,7 +834,6 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
864834
case WIRE_HSMD_PREAPPROVE_INVOICE_CHECK_REPLY:
865835
case WIRE_HSMD_PREAPPROVE_KEYSEND_CHECK_REPLY:
866836
case WIRE_HSMD_CHECK_PUBKEY_REPLY:
867-
case WIRE_HSMD_DERIVE_BIP86_KEY_REPLY:
868837
case WIRE_HSMD_CHECK_BIP86_PUBKEY_REPLY:
869838
case WIRE_HSMD_SIGN_ANCHORSPEND_REPLY:
870839
case WIRE_HSMD_SIGN_HTLC_TX_MINGLE_REPLY:

hsmd/hsmd_wire.csv

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ tlvtype,hsmd_init_tlvs,hsm_passphrase,1
3434
tlvdata,hsmd_init_tlvs,hsm_passphrase,passphrase,wirestring,
3535

3636
#include <common/bip32.h>
37+
#include <wally_bip32.h>
3738
# Sorry: I should have put version in v2 :(
3839
msgtype,hsmd_init_reply_v4,114
3940
# This gets upgraded when the wire protocol changes in incompatible
@@ -50,6 +51,8 @@ msgdata,hsmd_init_reply_v4,tlvs,hsmd_init_reply_v4_tlvs,
5051
# TLV to indicate HSM secret type
5152
tlvtype,hsmd_init_reply_v4_tlvs,hsm_secret_type,1
5253
tlvdata,hsmd_init_reply_v4_tlvs,hsm_secret_type,hsm_type,u8,
54+
tlvtype,hsmd_init_reply_v4_tlvs,bip86_base,2
55+
tlvdata,hsmd_init_reply_v4_tlvs,bip86_base,base,ext_key,
5356

5457
# HSM initialization failure response
5558
msgtype,hsmd_init_reply_failure,115
@@ -200,14 +203,6 @@ msgdata,hsmd_preapprove_keysend_check,check_only,bool,
200203
msgtype,hsmd_preapprove_keysend_check_reply,152
201204
msgdata,hsmd_preapprove_keysend_check_reply,approved,bool,
202205

203-
# Derive BIP86 key using path m/86'/0'/0'/change/address_index
204-
msgtype,hsmd_derive_bip86_key,54
205-
msgdata,hsmd_derive_bip86_key,index,u32,
206-
msgdata,hsmd_derive_bip86_key,is_change,bool,
207-
208-
msgtype,hsmd_derive_bip86_key_reply,154
209-
msgdata,hsmd_derive_bip86_key_reply,bip86_base,ext_key,
210-
211206
# Give me ECDH(node-id-secret,point)
212207
msgtype,hsmd_ecdh_req,1
213208
msgdata,hsmd_ecdh_req,point,pubkey,

hsmd/libhsmd.c

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
138138
case WIRE_HSMD_LOCK_OUTPOINT:
139139
return (client->capabilities & HSM_PERM_LOCK_OUTPOINT) != 0;
140140

141-
case WIRE_HSMD_DERIVE_BIP86_KEY:
142141
case WIRE_HSMD_CHECK_BIP86_PUBKEY:
143142
case WIRE_HSMD_INIT:
144143
case WIRE_HSMD_DEV_PREINIT:
@@ -209,7 +208,6 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
209208
case WIRE_HSMD_PREAPPROVE_KEYSEND_CHECK_REPLY:
210209
case WIRE_HSMD_DERIVE_SECRET_REPLY:
211210
case WIRE_HSMD_CHECK_PUBKEY_REPLY:
212-
case WIRE_HSMD_DERIVE_BIP86_KEY_REPLY:
213211
case WIRE_HSMD_CHECK_BIP86_PUBKEY_REPLY:
214212
case WIRE_HSMD_SIGN_ANCHORSPEND_REPLY:
215213
case WIRE_HSMD_SIGN_HTLC_TX_MINGLE_REPLY:
@@ -542,26 +540,9 @@ static void hsm_key_for_utxo(struct privkey *privkey, struct pubkey *pubkey,
542540
hsmd_status_debug("Derived public key %s from unilateral close",
543541
fmt_pubkey(tmpctx, pubkey));
544542
} else {
545-
/* Check if this is a BIP86 UTXO by examining the scriptPubkey */
546-
const size_t script_len = tal_bytelen(utxo->scriptPubkey);
547-
bool is_bip86 = false;
548-
549-
/* For P2TR scripts, we need to determine if it's BIP86 or regular P2TR
550-
* But BIP86 derivation requires mnemonic-based secrets */
551-
if (is_p2tr(utxo->scriptPubkey, script_len, NULL) &&
552-
use_bip86_derivation(tal_bytelen(secretstuff.bip32_seed))) {
553-
/* Try BIP86 derivation first and see if it matches */
554-
struct pubkey test_pubkey;
555-
bip86_key(NULL, &test_pubkey, utxo->keyindex);
556-
557-
/* Create P2TR scriptpubkey from BIP86 key and compare */
558-
const u8 *bip86_script = scriptpubkey_p2tr(tmpctx, &test_pubkey);
559-
if (memeq(utxo->scriptPubkey, script_len, bip86_script, tal_bytelen(bip86_script))) {
560-
is_bip86 = true;
561-
}
562-
}
563-
564-
if (is_bip86) {
543+
/* Modern HSMs use bip86 for p2tr. */
544+
if (is_p2tr(utxo->scriptPubkey, tal_bytelen(utxo->scriptPubkey), NULL)
545+
&& use_bip86_derivation(tal_bytelen(secretstuff.bip32_seed))) {
565546
/* Use BIP86 derivation */
566547
bip86_key(privkey, pubkey, utxo->keyindex);
567548
} else {
@@ -2316,7 +2297,6 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
23162297
return handle_derive_secret(client, msg);
23172298
case WIRE_HSMD_CHECK_PUBKEY:
23182299
return handle_check_pubkey(client, msg);
2319-
case WIRE_HSMD_DERIVE_BIP86_KEY:
23202300
case WIRE_HSMD_CHECK_BIP86_PUBKEY:
23212301
/* This should be handled by hsmd.c, not libhsmd */
23222302
return hsmd_status_bad_request_fmt(
@@ -2373,7 +2353,6 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
23732353
case WIRE_HSMD_PREAPPROVE_KEYSEND_REPLY:
23742354
case WIRE_HSMD_PREAPPROVE_INVOICE_CHECK_REPLY:
23752355
case WIRE_HSMD_PREAPPROVE_KEYSEND_CHECK_REPLY:
2376-
case WIRE_HSMD_DERIVE_BIP86_KEY_REPLY:
23772356
case WIRE_HSMD_CHECK_PUBKEY_REPLY:
23782357
case WIRE_HSMD_CHECK_BIP86_PUBKEY_REPLY:
23792358
case WIRE_HSMD_SIGN_ANCHORSPEND_REPLY:
@@ -2610,8 +2589,13 @@ u8 *hsmd_init(const u8 *secret_data, size_t secret_len, const u64 hsmd_version,
26102589
*/
26112590
/* Create TLV with HSM secret type */
26122591
struct tlv_hsmd_init_reply_v4_tlvs *tlvs = tlv_hsmd_init_reply_v4_tlvs_new(tmpctx);
2613-
tlvs->hsm_secret_type = tal(tlvs, u8);
2614-
*tlvs->hsm_secret_type = hsm_secret_type;
2592+
tlvs->hsm_secret_type = tal_dup(tlvs, u8, &hsm_secret_type);
2593+
2594+
/* If we have a mnemonic-based HSM, include the BIP86 base key */
2595+
if (use_bip86_derivation(tal_bytelen(secretstuff.bip32_seed))) {
2596+
tlvs->bip86_base = tal(tlvs, struct ext_key);
2597+
derive_bip86_base_key(tlvs->bip86_base);
2598+
}
26152599

26162600
return take(towire_hsmd_init_reply_v4(
26172601
NULL, hsmd_version, caps,

lightningd/hsm_control.c

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -190,25 +190,9 @@ struct ext_key *hsm_init(struct lightningd *ld)
190190
}
191191

192192
/* Check if we have a mnemonic-based HSM secret from TLV */
193-
bool is_mnemonic_secret = false;
194-
if (tlvs && tlvs->hsm_secret_type) {
195-
u8 secret_type = *tlvs->hsm_secret_type;
196-
is_mnemonic_secret = (secret_type == 2 || secret_type == 3); /* HSM_SECRET_MNEMONIC_NO_PASS or HSM_SECRET_MNEMONIC_WITH_PASS */
197-
}
198-
199-
if (is_mnemonic_secret) {
200-
/* Try to get BIP86 base key from HSM (works only for mnemonic secrets) */
201-
ld->bip86_base = tal(ld, struct ext_key);
202-
msg = towire_hsmd_derive_bip86_key(NULL, 0, false);
203-
const u8 *reply = hsm_sync_req(tmpctx, ld, take(msg));
204-
if (fromwire_hsmd_derive_bip86_key_reply(reply, ld->bip86_base)) {
205-
/* BIP86 derivation succeeded */
206-
log_info(ld->log, "Using BIP86 for new addresses, BIP32 for channels (mnemonic HSM secret)");
207-
/* Keep bip32_base for channel operations, database, etc. */
208-
} else {
209-
/* BIP86 derivation failed unexpectedly */
210-
ld->bip86_base = tal_free(ld->bip86_base);
211-
}
193+
if (tlvs->bip86_base) {
194+
ld->bip86_base = tal_steal(ld, tlvs->bip86_base);
195+
log_info(ld->log, "Using BIP86 for new addresses, BIP32 for channels (mnemonic HSM secret)");
212196
} else {
213197
/* Legacy HSM secret - don't attempt BIP86 derivation */
214198
log_info(ld->log, "Using BIP32 derivation for all operations (legacy HSM secret)");

wallet/test/run-wallet.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -790,14 +790,6 @@ u8 *towire_hsmd_get_channel_basepoints(const tal_t *ctx UNNEEDED, const struct n
790790
{
791791
return NULL;
792792
}
793-
u8 *towire_hsmd_derive_bip86_key(const tal_t *ctx UNNEEDED, u32 index UNNEEDED, bool is_change UNNEEDED)
794-
{
795-
return NULL;
796-
}
797-
bool fromwire_hsmd_derive_bip86_key_reply(const void *p UNNEEDED, struct ext_key *bip86_base UNNEEDED)
798-
{
799-
return true;
800-
}
801793
bool wire_sync_write(int fd UNNEEDED, const void *msg TAKES UNNEEDED)
802794
{
803795
return true;

0 commit comments

Comments
 (0)