Introduction
This guide demonstrates how to use PHP to create cryptocurrency mnemonics (seed phrases), private keys, and addresses for Bitcoin (BTC), Litecoin (LTC), and Ethereum (ETH). The implementation leverages robust PHP libraries to ensure security and compatibility with blockchain standards.
Project Dependencies
Required Libraries
- A PHP implementation for Bitcoin-related operations (requires 64-bit PHP 7.0+)
- Used for generating mnemonics and deriving private keys.
- Ethereum utility functions for PHP, including address generation.
Installation
composer require bitwasp/bitcoin
composer require web3p/ethereum-utilGenerating BTC Mnemonics, Private Keys, and Addresses
Code Implementation
use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39Mnemonic;
use BitWasp\Bitcoin\Mnemonic\MnemonicFactory;
use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39SeedGenerator;
use BitWasp\Bitcoin\Key\Factory\HierarchicalKeyFactory;
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
// Generate entropy for mnemonic
$entropy = random_bytes(Bip39Mnemonic::MIN_ENTROPY_BYTE_LEN);
$bip39 = MnemonicFactory::bip39();
$mnemonic = $bip39->entropyToMnemonic($entropy);
echo "mnemonic: " . $mnemonic . PHP_EOL . PHP_EOL;
// Derive seed from mnemonic (optional passphrase: 'hello')
$seedGenerator = new Bip39SeedGenerator();
$seed = $seedGenerator->getSeed($mnemonic);
echo "seed: " . $seed->getHex() . PHP_EOL;
// Generate hierarchical keys
$hdFactory = new HierarchicalKeyFactory();
$master = $hdFactory->fromEntropy($seed);
$hardened = $master->derivePath("49'/0'/0'/0/0");
// Output WIF (Wallet Import Format) and address
echo 'WIF: ' . $hardened->getPrivateKey()->toWif() . PHP_EOL;
$address = new PayToPubKeyHashAddress($hardened->getPublicKey()->getPubKeyHash());
echo 'address: ' . $address->getAddress() . PHP_EOL;Key Outputs
- Mnemonic: 12-24 word seed phrase (BIP-39 standard)
- Seed: Hexadecimal representation of the derived seed
- WIF: Private key in Wallet Import Format (compatible with wallets like Electrum)
- Address: BTC public address (P2PKH format)
Generating ETH Mnemonics, Private Keys, and Addresses
Code Implementation
use Web3p\EthereumUtil\Util;
// Reuse mnemonic generation from BTC example
$util = new Util();
$hardened = $master->derivePath("44'/60'/0'/0/0");
echo " - m/44'/60'/0'/0/0 " . PHP_EOL;
echo " public key: " . $hardened->getPublicKey()->getHex() . PHP_EOL;
echo " private key: " . $hardened->getPrivateKey()->getHex() . PHP_EOL;
echo " address: " . $util->publicKeyToAddress($util->privateKeyToPublicKey($hardened->getPrivateKey()->getHex())) . PHP_EOL;Key Outputs
- Private Key: 64-character hexadecimal string (compatible with MetaMask/imToken)
- Address: ETH public address (checksummed format)
Generating LTC Mnemonics, Private Keys, and Addresses
Code Implementation
use BitWasp\Bitcoin\Network\NetworkFactory;
$network = NetworkFactory::litecoin();
$hardened = $master->derivePath("44'/2'/0'/0/0");
echo 'WIF: ' . $hardened->getPrivateKey()->toWif($network) . PHP_EOL;
$address = new PayToPubKeyHashAddress($hardened->getPublicKey()->getPubKeyHash());
echo 'address: ' . $address->getAddress($network) . PHP_EOL;Key Outputs
- WIF: Litecoin-specific private key format
- Address: LTC public address (P2PKH format)
FAQs
1. What is a mnemonic seed phrase?
A mnemonic is a human-readable representation of cryptographic entropy, typically 12-24 words (BIP-39 standard), used to derive private keys and addresses.
2. Can I use the same mnemonic for BTC and ETH?
Yes! The same mnemonic can generate keys for multiple blockchains by using different derivation paths (e.g., 44'/0'/0' for BTC vs. 44'/60'/0' for ETH).
3. Is it safe to run this code in production?
Ensure your PHP environment is secure (64-bit, 7.0+). For high-value wallets, consider hardware security modules (HSMs) or air-gapped systems.
4. How do I import a WIF private key into a wallet?
Most wallets (e.g., Electrum, Exodus) support WIF imports via their UI. For ETH, use the raw hexadecimal private key.
5. Why doesn’t this include Ethereum V3 keystore generation?
The PHP ecosystem lacks a widely adopted library for V3 keystores. Consider using Python or Node.js for this feature.
6. What’s the difference between seed and private key?
- Seed: Master secret derived from the mnemonic (used to generate multiple keys).
- Private Key: Cryptographically secures a single address.
Conclusion
This guide provides a comprehensive PHP implementation for generating cryptocurrency mnemonics, private keys, and addresses. By leveraging bitcoin-php and ethereum-util, developers can build secure wallet functionalities directly into PHP applications.
👉 Explore advanced blockchain APIs for enterprise-grade solutions.
👉 Learn about multi-signature wallets to enhance security.
For further reading, refer to BIP-32 (HD Wallets) and BIP-44 (multi-coin hierarchies).