UNPKG

804 BJavaScriptView Raw
1import { stringToU8a } from '@polkadot/util';
2import { bip39ToMiniSecret, isReady } from '@polkadot/wasm-crypto';
3import { pbkdf2Encode } from '../pbkdf2/index.js';
4import { mnemonicToEntropy } from './toEntropy.js';
5import { mnemonicValidate } from './validate.js';
6export function mnemonicToMiniSecret(mnemonic, password = '', wordlist, onlyJs) {
7 if (!mnemonicValidate(mnemonic, wordlist, onlyJs)) {
8 throw new Error('Invalid bip39 mnemonic specified');
9 }
10 else if (!wordlist && !onlyJs && isReady()) {
11 return bip39ToMiniSecret(mnemonic, password);
12 }
13 const entropy = mnemonicToEntropy(mnemonic, wordlist);
14 const salt = stringToU8a(`mnemonic${password}`);
15 // return the first 32 bytes as the seed
16 return pbkdf2Encode(entropy, salt).password.slice(0, 32);
17}