common: expose validate_mnemonic so the option can use it.
What changed, and why it matters
This commit is a small internal code cleanup: it makes an existing BIP39 mnemonic validation helper function visible outside its source file so other parts of the program can reuse it. The function's behavior is unchanged; it still checks whether a 12-word recovery phrase is valid. There is no indication this fixes a security bug or introduces a vulnerability.
No security action required; treat as routine refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change exposes validate_mnemonic() by removing the static keyword, changing its signature from returning bool with an out-parameter to returning enum hsm_secret_error directly, and adding a declaration in common/hsm_secret.h. Existing call sites are updated to use the new return style. One minor behavior change: read_stdin_mnemonic() now frees the input line on validation failure instead of returning NULL while potentially leaking it. This is a normal refactoring with no security-relevant logic changes.
Changed components
common/hsm_secret.ccommon/hsm_secret.hInspect captured patch +17 / −9
diff --git a/common/hsm_secret.c b/common/hsm_secret.c
index 62348627..25355397 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 740f2ebc..1f6314e6 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.
Why this scored 17/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.