Skip to content

Commit

Permalink
feat: add mnemonic strength (wevm#1618)
Browse files Browse the repository at this point in the history
* feat: add mnemonic strength

* chore: run lint

* Update tough-tigers-compare.md

* Update tough-tigers-compare.md

---------

Co-authored-by: jxom <[email protected]>
  • Loading branch information
0xArdy and jxom authored Dec 23, 2023
1 parent ee3e77c commit ad01462
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tough-tigers-compare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": minor
---

Added `strength` parameter to `generateMnemonic`.
4 changes: 4 additions & 0 deletions site/docs/accounts/mnemonic.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ import { english, generateMnemonic } from 'viem/accounts'
const mnemonic = generateMnemonic(english)
```

::: tip
You can customize the strength of the generated mnemonic by passing a value between 128 and 256 as the second argument to the `generateMnemonic` function. This value must be a multiple of 32.
:::

Available wordlists:

- `czech`
Expand Down
5 changes: 5 additions & 0 deletions src/accounts/generateMnemonic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ test('traditionalChinese', () => {
const phrase = generateMnemonic(traditionalChinese)
expect(phrase.split(' ').length).toBe(12)
})

test('english 256 bits strength', () => {
const phrase = generateMnemonic(english, 256)
expect(phrase.split(' ').length).toBe(24)
})
8 changes: 6 additions & 2 deletions src/accounts/generateMnemonic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ export type GenerateMnemonicErrorType = ErrorType
* @description Generates a random mnemonic phrase with a given wordlist.
*
* @param wordlist The wordlist to use for generating the mnemonic phrase.
* @param strength mnemonic strength 128-256 bits
*
* @returns A randomly generated mnemonic phrase.
*/
export function generateMnemonic(wordlist: string[]): string {
return generateMnemonic_(wordlist)
export function generateMnemonic(
wordlist: string[],
strength?: number,
): string {
return generateMnemonic_(wordlist, strength)
}

0 comments on commit ad01462

Please sign in to comment.