UNPKG

815 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util-crypto authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { assert, stringToU8a } from '@polkadot/util';
4import { bip39ToMiniSecret, isReady } from '@polkadot/wasm-crypto';
5import { pbkdf2Encode } from "../pbkdf2/index.js";
6import { mnemonicToEntropy } from "./toEntropy.js";
7import { mnemonicValidate } from "./validate.js";
8export function mnemonicToMiniSecret(mnemonic, password = '', onlyJs) {
9 assert(mnemonicValidate(mnemonic), 'Invalid bip39 mnemonic specified');
10
11 if (!onlyJs && isReady()) {
12 return bip39ToMiniSecret(mnemonic, password);
13 }
14
15 const entropy = mnemonicToEntropy(mnemonic);
16 const salt = stringToU8a(`mnemonic${password}`); // return the first 32 bytes as the seed
17
18 return pbkdf2Encode(entropy, salt).password.slice(0, 32);
19}
\No newline at end of file