Skip to content

key generate subcommand silently coerces any word count other than 12 to 24 words #362

Closed

Description

@emma31-dev

Describe the bug

key generate accepts any integer for --entropy but only distinguishes 12 from everything else. The match arm is:

let mnemonic_type = match self.word_count {
    12 => WordCount::Words12,
    _ => WordCount::Words24,
};

So --entropy 15 produces a 24-word mnemonic, --entropy 0 produces a 24-word mnemonic, and --entropy 999 produces a 24-word mnemonic. No error, no warning, no indication that the requested value was ignored. BIP39 defines five valid lengths: 12, 15, 18, 21, 24 and bdk_wallet::keys::bip39 WordCount already support all five, so the restriction is purely in the CLI's argument handling.

To Reproduce

  1. Request a 15-word mnemonic:
    bdk-cli key generate --entropy 15
  2. Count the words in the output: it is 24, not 15.
  3. Confirm nonsense values are also accepted without error:
    bdk-cli key generate --entropy 0
    This also produces 24 words.

Expected behavior

--entropy 15 should produce a 15-word mnemonic, since the library supports it. Invalid values such as 0, 13, or 999 should be rejected at parse time with a message listing the valid options, not silently coerced. PossibleValuesParser or a custom value_parser handles both:

#[arg(
    long = "entropy",
    short = 'w',
    default_value = "WordCount::Words12",
    value_parser = word_count_parser
)]
pub word_count: WordCount,

in the word_count_parser:

let word_count = match word_count {
    "12" => WordCount::Words12,
    "15" => WordCount::Words15,
    "18" => WordCount::Words18,
    "21" => WordCount::Words21,
    "24" => WordCount::Words24,
    _ => return(Error::Generic("...")), 
};

The change is confined to KeySubCommand::Generate; no library or API changes are needed.

Build environment

· BDK-CLI tag/commit: main
· OS+version: macOS 12.7.6 (Monterey)
· Rust/Cargo version: rustc --version output
· Rust/Cargo target: x86_64-apple-darwin

Additional context

The silent coercion means a user's request is never honoured unless it happens to be exactly 12, and the failure mode is invisible. The command just succeeds and returns a valid mnemonic of the wrong length. Since usize parsing already rejects negative values, the only way to reach the match arm with an unintended value is a typo or an unsupported length, and both currently produce 24 words with no diagnostic. Validating at the clap boundary fixes both the missing lengths and the silent acceptance in a single change.

Activity

  1. Olorunshogo commented on Oct 24, 2026

    @Olorunshogo
    Contributor

    I would like to work on this and I intend to do it in the following ways:

    • Parse into WordCount using PossibleValuesParser restricted to 12/15/18/21/24, so invalid values fail at parse time and --help lists the options
    • Keep the flag name, env var, and default of 12 unchanged
    • Add tests for all five lengths and for rejected values
    • Add a CHANGELOG note, since values other than 12 that were silently coerced to 24 will now error

    Happy to adjust if you would prefer a different approach.

  2. emma31-dev commented on Oct 24, 2026

    @emma31-dev
    ContributorAuthor

    @Olorunshogo I initially planned to work on this but sure you can proceed. I'll review your changes later.

  3. Olorunshogo commented on Oct 24, 2026

    @Olorunshogo
    Contributor

    Implemented this according to plan and this fix now accepts all five BIP39 lengths (12, 15, 18, 21, 24) and rejects anything else at parse time with the valid options listed. The flag name, env var and default are unchanged.

    Ready for review.

  4. moved this from Todo to In Progress in BDK-CLIon Oct 24, 2026
  5. vadim-anfv commented on Nov 3, 2026

    @vadim-anfv
    Collaborator

    Good catch!

    One thing to fix in the description: it uses --word-count, but the code currently has --entropy (WORD_COUNT is only the env var name), so the commands as written fail with error: unexpected argument '--word-count' found.

    Could you update it so the description matches the code? I do agree --word-count is the better name, and I'll open a separate issue for the rename once #366 is merged, I have a couple of other thoughts on this code to put in it.

    Reproduced locally on master, got 24 words for all commands:

    bdk-cli key generate --entropy 15
    bdk-cli key generate --entropy 0
    bdk-cli key generate --entropy 999
    
  6. emma31-dev commented on Nov 3, 2026

    @emma31-dev
    ContributorAuthor

    Good catch!

    One thing to fix in the description: it uses --word-count, but the code currently has --entropy (WORD_COUNT is only the env var name), so the commands as written fail with error: unexpected argument '--word-count' found.

    Could you update it so the description matches the code? I do agree --word-count is the better name, and I'll open a separate issue for the rename once #366 is merged, I have a couple of other thoughts on this code to put in it.

    Reproduced locally on master, got 24 words for all commands:

    bdk-cli key generate --entropy 15
    bdk-cli key generate --entropy 0
    bdk-cli key generate --entropy 999
    

    I'll update it. I forgot to do so earlier.

  7. added
    enhancementNew feature or request
    and removed
    bugSomething isn't working
    on Nov 4, 2026
  8. added this to the CLI 4.1.0 milestone on Nov 4, 2026
  9. moved this from In Progress to Done in BDK-CLIon Nov 6, 2026
  10. added a commit that references this issue on Nov 6, 2026
    4e4f51b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

  • Status
    Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions