From 4f5e5aad18426443aee81710fdf6b85bbf399e41 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 13 Jan 2026 13:19:18 +1030 Subject: [PATCH] common: expose validate_mnemonic so the option can use it. Signed-off-by: Rusty Russell --- common/hsm_secret.c | 18 +++++++++--------- common/hsm_secret.h | 8 ++++++++ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/common/hsm_secret.c b/common/hsm_secret.c index 62348627f..253553973 100644 --- a/common/hsm_secret.c +++ b/common/hsm_secret.c @@ -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; diff --git a/common/hsm_secret.h b/common/hsm_secret.h index 740f2ebc0..1f6314e63 100644 --- a/common/hsm_secret.h +++ b/common/hsm_secret.h @@ -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.