| 535 | } |
| 536 | |
| 537 | static int dumponchaindescriptors(const char *hsm_secret_path, const char *old_passwd UNUSED, |
| 538 | const bool is_testnet) |
| 539 | { |
| 540 | struct secret hsm_secret; |
| 541 | char *passwd, *err; |
| 542 | u8 bip32_seed[BIP32_ENTROPY_LEN_256]; |
| 543 | u32 salt = 0; |
| 544 | u32 version = is_testnet ? |
| 545 | BIP32_VER_TEST_PRIVATE : BIP32_VER_MAIN_PRIVATE; |
| 546 | struct ext_key master_extkey; |
| 547 | char *enc_xpub, *descriptor; |
| 548 | struct descriptor_checksum checksum; |
| 549 | int exit_code = 0; |
| 550 | |
| 551 | /* This checks the file existence, too. */ |
| 552 | if (hsm_secret_is_encrypted(hsm_secret_path)) { |
| 553 | printf("Enter hsm_secret password:\n"); |
| 554 | fflush(stdout); |
| 555 | passwd = read_stdin_pass_with_exit_code(&err, &exit_code); |
| 556 | if (!passwd) |
| 557 | errx(exit_code, "%s", err); |
| 558 | get_encrypted_hsm_secret(&hsm_secret, hsm_secret_path, passwd); |
| 559 | free(passwd); |
| 560 | } else |
| 561 | get_hsm_secret(&hsm_secret, hsm_secret_path); |
| 562 | |
| 563 | /* We use m/0/0/k as the derivation tree for onchain funds. */ |
| 564 | |
| 565 | /* The root seed is derived from hsm_secret using hkdf.. */ |
| 566 | do { |
| 567 | hkdf_sha256(bip32_seed, sizeof(bip32_seed), |
| 568 | &salt, sizeof(salt), |
| 569 | &hsm_secret, sizeof(hsm_secret), |
| 570 | "bip32 seed", strlen("bip32 seed")); |
| 571 | salt++; |
| 572 | /* ..Which is used to derive m/ */ |
| 573 | } while (bip32_key_from_seed(bip32_seed, sizeof(bip32_seed), |
| 574 | version, 0, &master_extkey) != WALLY_OK); |
| 575 | |
| 576 | if (bip32_key_to_base58(&master_extkey, BIP32_FLAG_KEY_PUBLIC, &enc_xpub) != WALLY_OK) |
| 577 | errx(ERROR_LIBWALLY, "Can't encode xpub"); |
| 578 | |
| 579 | /* Now we format the descriptor strings (we only ever create P2WPKH and |
| 580 | * P2SH-P2WPKH outputs). */ |
| 581 | |
| 582 | descriptor = tal_fmt(NULL, "wpkh(%s/0/0/*)", enc_xpub); |
| 583 | if (!descriptor_checksum(descriptor, strlen(descriptor), &checksum)) |
| 584 | errx(ERROR_LIBWALLY, "Can't derive descriptor checksum for wpkh"); |
| 585 | printf("%s#%s\n", descriptor, checksum.csum); |
| 586 | tal_free(descriptor); |
| 587 | |
| 588 | descriptor = tal_fmt(NULL, "sh(wpkh(%s/0/0/*))", enc_xpub); |
| 589 | if (!descriptor_checksum(descriptor, strlen(descriptor), &checksum)) |
| 590 | errx(ERROR_LIBWALLY, "Can't derive descriptor checksum for sh(wpkh)"); |
| 591 | printf("%s#%s\n", descriptor, checksum.csum); |
| 592 | tal_free(descriptor); |
| 593 | |
| 594 | wally_free_string(enc_xpub); |
no test coverage detected