Check arguments before looking for the mnemonic (closes #79)
check / check (push) Canceled after 0s

The key index, --words, the --to recipients and the input file of age encrypt and age decrypt were checked only after the mnemonic had been obtained, so a run that could not succeed still prompted for it or ran the mnemonic command. options.Index now refuses an index above derive.MaxIndex, which covers every command group; the mnemonic command calls the new childmnemonic.CheckWords first; age encrypt reads its recipients with the new agekey.Recipients, and both age commands open their input, before the key is derived. Encrypt now takes the parsed recipients. The README says which checks come first.

Model: opus-5-5
This commit is contained in:
2026-10-14 18:26:16 +00:00
parent efca0ebb39
commit 1a9cc39dd2
9 changed files with 162 additions and 62 deletions
+15 -5
View File
@@ -29,6 +29,17 @@ const DefaultWords = twelve
// ErrWordCount is returned for a length this tool does not offer.
var ErrWordCount = errors.New("a child mnemonic has 12, 18 or 24 words")
// CheckWords refuses a length this tool does not offer, so that the
// command can turn it down before it looks for the mnemonic.
func CheckWords(words uint32) error {
switch words {
case twelve, eighteen, twentyFour:
return nil
default:
return fmt.Errorf("%w, not %d", ErrWordCount, words)
}
}
// Derive returns the English child mnemonic of that many words at that
// key index, taken from the master key with BIP-85's own mnemonic
// application, number 39. The words come straight from the BIP-85
@@ -38,13 +49,12 @@ func Derive(
master *hdkeychain.ExtendedKey,
words, index uint32,
) (string, error) {
switch words {
case twelve, eighteen, twentyFour:
default:
return "", fmt.Errorf("%w, not %d", ErrWordCount, words)
err := CheckWords(words)
if err != nil {
return "", err
}
err := derive.CheckIndex(index)
err = derive.CheckIndex(index)
if err != nil {
return "", err
}