BIP39 mnemonic and seed generation utilities
Provides functionality for generating secure mnemonic phrases, validating checksums, and deriving cryptographic seeds according to the BIP39 standard.
- Source:
Example
// Generate a random mnemonic and seed
const { mnemonic, seed } = BIP39.random('my-passphrase');
// Validate an existing mnemonic
const isValid = BIP39.checkSum(mnemonic);
// Convert mnemonic to seed
const seed = BIP39.mnemonic2seed(mnemonic, 'passphrase');
Methods
(static) checkSum(mnemonicopt) → {boolean}
Validates the checksum of a BIP39 mnemonic phrase
The validation process:
- Converts words back to 11-bit indices
- Concatenates all indices to reconstruct the binary data
- Splits into entropy (128 bits) and checksum (4 bits)
- Recalculates checksum from entropy using SHA256
- Compares calculated checksum with embedded checksum
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
mnemonic |
string |
<optional> |
'' | Space-separated mnemonic phrase to validate |
- Source:
Returns:
True if checksum is valid, false otherwise
- Type
- boolean
Example
const validMnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
const isValid = BIP39.checkSum(validMnemonic); // true
const invalidMnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon";
const isInvalid = BIP39.checkSum(invalidMnemonic); // false
(static) mnemonic() → {string}
Generates a random 12-word mnemonic phrase using cryptographically secure entropy
The function:
- Generates 16 bytes (128 bits) of secure random entropy
- Computes SHA256 hash and takes first 4 bits as checksum
- Concatenates entropy + checksum to create 132 bits
- Splits into 12 groups of 11 bits each
- Maps each 11-bit value to a word from the BIP39 wordlist
- Source:
Returns:
Space-separated 12-word mnemonic phrase
- Type
- string
Example
const mnemonic = BIP39.mnemonic();
// Returns: "abandon ability able about above absent absorb abstract absurd abuse access accident"
(static) mnemonic2seed(mnemonicopt, passphraseopt) → {string}
Converts a mnemonic phrase to a seed with checksum validation
This method validates the mnemonic's checksum before deriving the seed, ensuring that only valid mnemonics are processed.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
mnemonic |
string |
<optional> |
'' | Space-separated mnemonic phrase |
passphrase |
string |
<optional> |
'' | Optional passphrase for additional security |
- Source:
Throws:
-
Throws 'invalid checksum' if mnemonic validation fails
- Type
- string
Returns:
Hex-encoded 64-byte seed
- Type
- string
Example
const mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
const seed = BIP39.mnemonic2seed(mnemonic, "passphrase");
// With invalid mnemonic
try {
const seed = BIP39.mnemonic2seed("invalid mnemonic phrase");
} catch (error) {
console.log(error); // "invalid checksum"
}
(static) random(passphraseopt) → {MnemonicResult}
Generates a random mnemonic with validated checksum and derives its seed
This is a convenience method that combines mnemonic generation and seed derivation with built-in checksum validation for additional security.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
passphrase |
string |
<optional> |
'' | Optional passphrase for seed derivation |
- Source:
Throws:
-
Throws 'invalid checksum' if generated mnemonic fails validation
- Type
- string
Returns:
Object containing both mnemonic and seed
- Type
- MnemonicResult
Example
const { mnemonic, seed } = BIP39.random('my-secure-passphrase');
console.log(mnemonic); // "word1 word2 word3 ..."
console.log(seed); // "a1b2c3d4e5f6..."
(static) seed(mnemonicopt, passphraseopt) → {string}
Derives a cryptographic seed from a mnemonic phrase using PBKDF2
Uses PBKDF2-HMAC-SHA512 with 2048 iterations as specified in BIP39. The salt is constructed as "mnemonic" + passphrase.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
mnemonic |
string |
<optional> |
'' | Space-separated mnemonic phrase |
passphrase |
string |
<optional> |
'' | Optional passphrase for additional security |
- Source:
Returns:
Hex-encoded 64-byte (512-bit) seed
- Type
- string
Example
const mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
const seed = BIP39.seed(mnemonic, "my-passphrase");
// Returns 128-character hex string