common: expose validate_mnemonic so the option can use it.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2026-01-13 13:19:18 +10:30
parent f975bb37d4
commit 4f5e5aad18
2 changed files with 17 additions and 9 deletions

View File

@@ -29,7 +29,7 @@
#define HSM_SECRET_PLAIN_SIZE 32
/* Helper function to validate a mnemonic string */
static bool validate_mnemonic(const char *mnemonic, enum hsm_secret_error *err)
enum hsm_secret_error validate_mnemonic(const char *mnemonic)
{
struct words *words;
bool ok;
@@ -44,12 +44,10 @@ static bool validate_mnemonic(const char *mnemonic, enum hsm_secret_error *err)
/* Wordlists can persist, so provide a common context! */
tal_wally_end(notleak_with_children(tal(NULL, char)));
if (!ok) {
*err = HSM_SECRET_ERR_INVALID_MNEMONIC;
return false;
}
if (!ok)
return HSM_SECRET_ERR_INVALID_MNEMONIC;
return true;
return HSM_SECRET_OK;
}
struct secret *get_encryption_key(const tal_t *ctx, const char *passphrase)
@@ -314,7 +312,8 @@ static struct hsm_secret *extract_mnemonic_secret(const tal_t *ctx,
}
/* Validate mnemonic */
if (!validate_mnemonic(hsms->mnemonic, err)) {
*err = validate_mnemonic(hsms->mnemonic);
if (*err != HSM_SECRET_OK) {
return tal_free(hsms);
}
@@ -464,8 +463,9 @@ const char *read_stdin_mnemonic(const tal_t *ctx, enum hsm_secret_error *err)
}
/* Validate mnemonic */
if (!validate_mnemonic(line, err)) {
return NULL;
*err = validate_mnemonic(line);
if (*err != HSM_SECRET_OK) {
return tal_free(line);
}
*err = HSM_SECRET_OK;

View File

@@ -129,6 +129,14 @@ const char *hsm_secret_error_str(enum hsm_secret_error err);
*/
enum hsm_secret_type detect_hsm_secret_type(const u8 *hsm_secret, size_t len);
/**
* Check a BIP39 mnemonic is valid.
* @mnemonic - 12 words, single-space separated, nul terminate.
*
* Returns HSM_SECRET_ERR_INVALID_MNEMONIC or HSM_SECRET_OK.
*/
enum hsm_secret_error validate_mnemonic(const char *mnemonic);
/**
* Reads a BIP39 mnemonic from stdin with validation.
* Returns a newly allocated string on success, NULL on error.