BIP39
The BIP39 module provides utility functions for working with mnemonic phrases, entropy, and seed generation. It includes methods for converting mnemonic phrases to seeds synchronously and asynchronously, converting mnemonic phrases to their corresponding entropy values and vice versa, generating mnemonic phrases with specified strengths, and validating the correctness of mnemonic phrases.
Functions
- bip39.mnemonicToSeed(mnemonic, password)
Asynchronously convert a mnemonic to a seed.
- Arguments:
mnemonic (string) – The mnemonic phrase.
password (string) – The optional password.
- Throws:
Error – If an error occurs during the conversion.
- Returns:
Promise.<Buffer> – The generated seed.
// Example
const mnemonic = 'hollow appear story text start mask salt social child space aspect hurdle';
const password = 'password';
const seed = await mnemonicToSeed(mnemonic, password);
console.log(seed)
>> Buffer
- bip39.mnemonicToSeedSync(mnemonic, password)
Synchronously convert a mnemonic to a seed.
- Arguments:
mnemonic (string) – The mnemonic phrase.
password (string) – The optional password.
- Returns:
Buffer – The generated seed.
// Example
const mnemonic = 'hollow appear story text start mask salt social child space aspect hurdle';
const password = 'password';
const seed = mnemonicToSeedSync(mnemonic, password);
console.log(seed)
>> Buffer
- bip39.mnemonicToEntropy(mnemonic, wordlist)
Convert a mnemonic to its corresponding entropy value.
- Arguments:
mnemonic (string) – The mnemonic phrase.
wordlist (Array.<string>) – The optional wordlist.
- Throws:
Error – If the mnemonic is invalid or a wordlist is required but not found.
- Returns:
string – The corresponding entropy.
// Example
const mnemonic = 'hollow appear story text start mask salt social child space aspect hurdle';
const entropy = mnemonicToEntropy(mnemonic);
console.log(entropy)
>> 6ce1535a6fdd4b10efae6f27fa0835b7
- bip39.entropyToMnemonic(entropy, wordlist)
Convert entropy to its corresponding mnemonic.
- Arguments:
entropy (Buffer|string) – The entropy value or buffer.
wordlist (Array.<string>) – The optional wordlist.
- Throws:
Error – If the entropy is invalid or a wordlist is required but not found.
- Returns:
string – The corresponding mnemonic phrase.
// Example
const entropy = 'c1f651a1fb62bebf8db1ecacf66a6a3d';
const mnemonic = entropyToMnemonic(entropy);
console.log(mnemonic)
>> sea raw half walnut cloud garlic cycle diesel provide rebuild once key
- bip39.generateMnemonic(strength, rng, wordlist)
Generate a mnemonic phrase with the specified strength (in bits).
- Arguments:
strength (number) – The strength of the mnemonic in bits.
rng (function) – The random number generator function.
wordlist (Array.<string>) – The optional wordlist.
- Throws:
TypeError – If the strength is not divisible by 32.
- Returns:
string – The generated mnemonic phrase.
// Example
const mnemonic = generateMnemonic();
console.log(mnemonic)
>> gaze hole neither spring effort fringe kit neck girl lamp smart afraid
- bip39.validateMnemonic(mnemonic, wordlist)
Validate a mnemonic phrase.
- Arguments:
mnemonic (string) – The mnemonic phrase to validate.
wordlist (Array.<string>) – The optional wordlist.
- Returns:
boolean – True if the mnemonic is valid, false otherwise.
// Example
const mnemonic = 'invalid mnemonic';
const isValid = validateMnemonic(mnemonic);
console.log(isValid)
>> false
- bip39.getDefaultWordlist()
Get the currently set default wordlist.
- Throws:
Error – If the default wordlist is not set.
- Returns:
string – The language code of the default wordlist.
// Example
const language = getDefaultWordlist();
console.log(language)
>> english