Skip to content

Commit

Permalink
Add utility function to generate mnemonic
Browse files Browse the repository at this point in the history
  • Loading branch information
polarker committed Oct 7, 2024
1 parent 2e2881c commit 90ac6c5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/web3-wallet/src/hd-wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
deriveSchnorrPrivateKeyForGroup,
deriveSecp256K1PrivateKey,
deriveSecp256K1PrivateKeyForGroup,
generateMnemonic,
HDWallet
} from './hd-wallet'

Expand All @@ -41,6 +42,12 @@ describe('HD wallet', () => {
const testMnemonic =
'vault alarm sad mass witness property virus style good flower rice alpha viable evidence run glare pretty scout evil judge enroll refuse another lava'

it('should gerate mnemonic', () => {
expect(generateMnemonic().split(' ').length).toBe(24)
expect(generateMnemonic(12).split(' ').length).toBe(12)
expect(generateMnemonic(24).split(' ').length).toBe(24)
})

it('should derive private key based on index for secp256k1', () => {
expect(deriveSecp256K1PrivateKey(testMnemonic, 0)).toEqual(
'a642942e67258589cd2b1822c631506632db5a12aabcf413604e785300d762a5'
Expand Down
4 changes: 4 additions & 0 deletions packages/web3-wallet/src/hd-wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ import * as bip39 from 'bip39'
import { bip32 } from './noble-wrapper'
import { PrivateKeyWallet } from './privatekey-wallet'

export function generateMnemonic(wordLength?: 12 | 24): string {
return bip39.generateMnemonic(wordLength === 12 ? 128 : 256)
}

export function deriveHDWalletPrivateKey(
mnemonic: string,
keyType: KeyType,
Expand Down

0 comments on commit 90ac6c5

Please sign in to comment.