MCPcopy Create free account
hub / github.com/ElementsProject/lightning / dumponchaindescriptors

Function dumponchaindescriptors

tools/lightning-hsmtool.c:481–566  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

479}
480
481static void dumponchaindescriptors(const char *hsm_secret_path,
482 const u32 version, bool show_secrets)
483{
484 u8 bip32_seed[BIP32_ENTROPY_LEN_256];
485 u32 salt = 0;
486 struct ext_key master_extkey;
487 char *enc_xkey, *descriptor;
488 struct descriptor_checksum checksum;
489 struct hsm_secret *hsms = load_hsm_secret(tmpctx, hsm_secret_path);
490 const char *path;
491
492 /* For BIP 86, we derive from subkey: m/86'/0'/0' */
493 if (use_bip86_derivation(tal_bytelen(hsms->secret_data))) {
494 /* First create the master key from the seed */
495 struct ext_key master_key, bip86_base;
496 u32 base_path[3];
497 base_path[0] = 86 | 0x80000000; /* 86' */
498 base_path[1] = 0x80000000; /* 0' */
499 base_path[2] = 0x80000000; /* 0' */
500
501 if (bip32_key_from_seed(hsms->secret_data, tal_bytelen(hsms->secret_data),
502 version, 0, &master_key) != WALLY_OK) {
503 errx(ERROR_LIBWALLY,
504 "Failed to create master key from BIP32 seed");
505 }
506
507 /* Derive the BIP86 base key */
508 if (bip32_key_from_parent_path(&master_key, base_path, 3, BIP32_FLAG_KEY_PRIVATE, &bip86_base) != WALLY_OK) {
509 errx(ERROR_LIBWALLY,
510 "Failed to derive BIP86 base key");
511 }
512 master_extkey = bip86_base;
513 /* Remaining path is 0/ */
514 path = "0";
515 } else {
516 struct secret hsm_secret;
517
518 /* Extract first 32 bytes for legacy compatibility */
519 memcpy(hsm_secret.data, hsms->secret_data, 32);
520
521 /* The root seed is derived from hsm_secret using hkdf.. */
522 do {
523 hkdf_sha256(bip32_seed, sizeof(bip32_seed),
524 &salt, sizeof(salt),
525 &hsm_secret, sizeof(hsm_secret),
526 "bip32 seed", strlen("bip32 seed"));
527 salt++;
528 /* ..Which is used to derive m/ */
529 } while (bip32_key_from_seed(bip32_seed, sizeof(bip32_seed),
530 version, 0, &master_extkey) != WALLY_OK);
531 /* Remaining path is 0/0/ */
532 path = "0/0";
533 }
534
535 if (show_secrets) {
536 if (bip32_key_to_base58(&master_extkey, BIP32_FLAG_KEY_PRIVATE,
537 &enc_xkey) != WALLY_OK)
538 errx(ERROR_LIBWALLY, "Can't encode xpriv");

Callers 1

mainFunction · 0.85

Calls 7

load_hsm_secretFunction · 0.85
use_bip86_derivationFunction · 0.85
tal_bytelenFunction · 0.85
errxFunction · 0.85
hkdf_sha256Function · 0.85
tal_freeFunction · 0.85
descriptor_checksumClass · 0.70

Tested by

no test coverage detected