furqansiddiqui/bip39-mnemonic-php
PHP implementation of BIP39 mnemonics for generating and validating seed phrases. Supports multiple wordlists/languages, entropy-to-mnemonic and mnemonic-to-seed conversion, checksum handling, and deterministic wallet seed derivation for crypto apps.
Installation
composer require furqansiddiqui/bip39-mnemonic-php
Add to composer.json if not using Composer globally. Requires PHP 8.2.
First Use Case: Generate a Mnemonic
use FurqanSiddiqui\Bip39\Bip39;
$bip39 = new Bip39();
$mnemonic = $bip39->generateMnemonic(); // e.g., "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
Validate a Mnemonic
$isValid = $bip39->validateMnemonic($mnemonic); // true/false
Convert Mnemonic to Seed (with \SensitiveParameter support)
$seed = $bip39->mnemonicToSeed($mnemonic, new \SensitiveParameter('your-passphrase-here'));
Check Wordlist The package includes the default BIP39 English wordlist. For custom wordlists:
$customWordlist = $bip39->getWordlist('custom'); // Requires custom wordlist file.
Mnemonic Generation
generateMnemonic() with optional entropy (default: 128-bit).$mnemonic = $bip39->generateMnemonic(256); // 24 words
Seed Derivation with \SensitiveParameter
\SensitiveParameter for sensitive data handling:
$seed = $bip39->mnemonicToSeed($mnemonic, new \SensitiveParameter('user-provided-passphrase'));
Validation
if (!$bip39->validateMnemonic($input)) {
throw new \InvalidArgumentException("Invalid mnemonic");
}
Wordlist Management
$bip39->setWordlist('path/to/custom_wordlist.txt');
$mnemonic = $bip39->generateMnemonic(); // Uses custom wordlist
Integration with Cryptography
paragonie/random_compat for secure entropy or web3p/hdwallet for key derivation:
use ParagonIE\ConstantTime\Binary\SecureRandom;
$entropy = SecureRandom::generateBytes(32); // 256-bit entropy
$mnemonic = $bip39->generateMnemonicFromEntropy($entropy);
Laravel Service Provider
Bind the Bip39 class to the container for easy dependency injection:
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->singleton(Bip39::class, function () {
return new Bip39();
});
}
Usage in controllers:
use FurqanSiddiqui\Bip39\Bip39;
public function generateMnemonic(Bip39 $bip39)
{
return $bip39->generateMnemonic();
}
Form Request Validation Validate mnemonics in Laravel requests:
use FurqanSiddiqui\Bip39\Bip39;
public function rules()
{
return [
'mnemonic' => ['required', function ($attribute, $value, $fail) {
$bip39 = new Bip39();
if (!$bip39->validateMnemonic($value)) {
$fail('The '.$attribute.' must be a valid BIP39 mnemonic.');
}
}],
];
}
Environment Configuration
Store passphrases or wordlist paths in .env:
BIP39_PASSPHRASE=default_passphrase
BIP39_WORDLIST_PATH=path/to/custom_wordlist.txt
Load them dynamically:
$bip39 = new Bip39();
$bip39->setPassphrase(config('bip39.passphrase'));
$bip39->setWordlist(config('bip39.wordlist_path'));
Testing with PHPUnit
Mock the Bip39 class in PHPUnit:
$mock = $this->createMock(Bip39::class);
$mock->method('generateMnemonic')->willReturn('test mnemonic');
$this->app->instance(Bip39::class, $mock);
Entropy vs. Word Count
generateMnemonic() defaults to 256-bit. It defaults to 128-bit (12 words). Always specify if higher security is needed.Passphrase Handling with \SensitiveParameter
.env or user-provided input.\SensitiveParameter to explicitly mark passphrases as sensitive:
$seed = $bip39->mnemonicToSeed($mnemonic, new \SensitiveParameter($passphrase));
Wordlist Case Sensitivity
Seed Storage
encrypt() or a secure vault:
$encryptedSeed = encrypt($seed);
Custom Wordlist Format
PHP 8.2 Compatibility
Validation Errors
validateMnemonic() returns false, check:
getWordlist() to inspect).Seed Mismatches
Performance
mnemonicToSeed()) is CPU-intensive. Avoid calling it in loops or high-frequency operations. Cache seeds if possible.Custom Entropy Sources
$customEntropy = bin2hex(random_bytes(32));
$mnemonic = $bip39->generateMnemonicFromEntropy($customEntropy);
Language Support
$bip39->setWordlist('path/to/spanish_wordlist.txt');
BIP39 Extensions
use BitWasp\Bitcoin\Key\Factory\Factory;
use BitWasp\Bitcoin\Crypto\Random\SecureRandom;
$seed = $bip39->mnemonicToSeed($mnemonic, '');
$masterKey = Factory::createMasterPrivateKey($seed, SecureRandom::getBytes(32));
Laravel Artisan Commands
// app/Console/Commands/GenerateMnemonic.php
public function handle(Bip39 $bip39)
{
$mnemonic = $bip39->generateMnemonic(256);
$this->info("Generated Mnemonic:\n".$mnemonic);
}
AppServiceProvider:
$this->commands([
Commands
How can I help you explore Laravel packages today?