mirror of
https://github.com/pezkuwichain/pezkuwi-common.git
synced 2026-04-22 03:17:58 +00:00
35 lines
1.3 KiB
JavaScript
35 lines
1.3 KiB
JavaScript
import { hasBigInt } from '@pezkuwi/util';
|
|
import { bip39ToSeed, isReady } from '@pezkuwi/wasm-crypto';
|
|
import { mnemonicToSeedSync } from './bip39.js';
|
|
import { mnemonicValidate } from './validate.js';
|
|
/**
|
|
* @name mnemonicToLegacySeed
|
|
* @summary Creates a valid Ethereum/Bitcoin-compatible seed from a mnemonic input
|
|
* @example
|
|
* <BR>
|
|
*
|
|
* ```javascript
|
|
* import { mnemonicGenerate, mnemonicToLegacySeed, mnemonicValidate } from '@pezkuwi/util-crypto';
|
|
*
|
|
* const mnemonic = mnemonicGenerate(); // => string
|
|
* const isValidMnemonic = mnemonicValidate(mnemonic); // => boolean
|
|
*
|
|
* if (isValidMnemonic) {
|
|
* console.log(`Seed generated from mnemonic: ${mnemonicToLegacySeed(mnemonic)}`); => u8a
|
|
* }
|
|
* ```
|
|
*/
|
|
export function mnemonicToLegacySeed(mnemonic, password = '', onlyJs, byteLength = 32) {
|
|
if (!mnemonicValidate(mnemonic)) {
|
|
throw new Error('Invalid bip39 mnemonic specified');
|
|
}
|
|
else if (![32, 64].includes(byteLength)) {
|
|
throw new Error(`Invalid seed length ${byteLength}, expected 32 or 64`);
|
|
}
|
|
return byteLength === 32
|
|
? !hasBigInt || (!onlyJs && isReady())
|
|
? bip39ToSeed(mnemonic, password)
|
|
: mnemonicToSeedSync(mnemonic, password).subarray(0, 32)
|
|
: mnemonicToSeedSync(mnemonic, password);
|
|
}
|