项目依赖:
bitcoin-php bitcoin的php实现库,用于创建助记词和生成私钥 (此库需要运行在64位的php7.0+上)
ethereum-util 以太坊常用函数php实现库
composer require bitwasp/bitcoin composer require web3p/ethereum-util
创建助记词并生成BTC私钥(WIF格式,可用于导入其他钱包)、地址:
<?php require_once './vendor/autoload.php'; use BitWaspBitcoinAddressPayToPubKeyHashAddress; use BitWaspBitcoinBitcoin; use BitWaspBitcoinCryptoRandomRandom; use BitWaspBitcoinKeyFactoryHierarchicalKeyFactory; use BitWaspBitcoinMnemonicBip39Bip39Mnemonic; use BitWaspBitcoinMnemonicBip39Bip39SeedGenerator; use BitWaspBitcoinMnemonicMnemonicFactory; // Bip39 $math = Bitcoin::getMath(); $network = Bitcoin::getNetwork(); $random = new Random(); // 生成随机数(initial entropy) $entropy = $random->bytes(Bip39Mnemonic::MIN_ENTROPY_BYTE_LEN); $bip39 = MnemonicFactory::bip39(); // 通过随机数生成助记词 $mnemonic = $bip39->entropyToMnemonic($entropy); echo "mnemonic: " . $mnemonic.PHP_EOL.PHP_EOL;// 助记词 $seedGenerator = new Bip39SeedGenerator(); // 通过助记词生成种子,传入可选加密串'hello' $seed = $seedGenerator->getSeed($mnemonic); echo "seed: " . $seed->getHex() . PHP_EOL; $hdFactory = new HierarchicalKeyFactory(); $master = $hdFactory->fromEntropy($seed); $hardened = $master->derivePath("49'/0'/0'/0/0"); echo 'WIF: ' . $hardened->getPrivateKey()->toWif(); echo PHP_EOL; $address = new PayToPubKeyHashAddress($hardened->getPublicKey()->getPubKeyHash()); echo 'address: ' . $address->getAddress(); echo PHP_EOL;
创建助记词并生成ETH私钥、地址:
<?php require_once './vendor/autoload.php'; use BitWaspBitcoinBitcoin; use BitWaspBitcoinCryptoRandomRandom; use BitWaspBitcoinKeyFactoryHierarchicalKeyFactory; use BitWaspBitcoinMnemonicBip39Bip39Mnemonic; use BitWaspBitcoinMnemonicBip39Bip39SeedGenerator; use BitWaspBitcoinMnemonicMnemonicFactory; use Web3pEthereumUtilUtil; // Bip39 $math = Bitcoin::getMath(); $network = Bitcoin::getNetwork(); $random = new Random(); // 生成随机数(initial entropy) $entropy = $random->bytes(Bip39Mnemonic::MIN_ENTROPY_BYTE_LEN); $bip39 = MnemonicFactory::bip39(); // 通过随机数生成助记词 $mnemonic = $bip39->entropyToMnemonic($entropy); echo "mnemonic: " . $mnemonic.PHP_EOL.PHP_EOL;// 助记词 $seedGenerator = new Bip39SeedGenerator(); // 通过助记词生成种子,传入可选加密串'hello' $seed = $seedGenerator->getSeed($mnemonic); echo "seed: " . $seed->getHex() . PHP_EOL; $hdFactory = new HierarchicalKeyFactory(); $master = $hdFactory->fromEntropy($seed); $util = new Util(); // 设置路径account $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;// 可以导入到imtoken使用的私钥 echo " address: " . $util->publicKeyToAddress($util->privateKeyToPublicKey($hardened->getPrivateKey()->getHex())) . PHP_EOL;// 私钥导入imtoken后一样的地址
创建助记词并生成LTC私钥、地址:
<?php require_once './vendor/autoload.php'; use BitWaspBitcoinAddressPayToPubKeyHashAddress; use BitWaspBitcoinBitcoin; use BitWaspBitcoinCryptoRandomRandom; use BitWaspBitcoinKeyFactoryHierarchicalKeyFactory; use BitWaspBitcoinMnemonicBip39Bip39Mnemonic; use BitWaspBitcoinMnemonicBip39Bip39SeedGenerator; use BitWaspBitcoinMnemonicMnemonicFactory; use BitWaspBitcoinNetworkNetworkFactory; // Bip39 $math = Bitcoin::getMath();
// 设置莱特币网络 $network = NetworkFactory::litecoin(); $random = new Random(); // 生成随机数(initial entropy) $entropy = $random->bytes(Bip39Mnemonic::MIN_ENTROPY_BYTE_LEN); $bip39 = MnemonicFactory::bip39(); // 通过随机数生成助记词 $mnemonic = $bip39->entropyToMnemonic($entropy); //$mnemonic = 'security hurdle lift acoustic skate recall hotel elegant amateur hidden escape slow'; echo "mnemonic: " . $mnemonic.PHP_EOL.PHP_EOL;// 助记词 $seedGenerator = new Bip39SeedGenerator(); // 通过助记词生成种子,传入可选加密串'hello' $seed = $seedGenerator->getSeed($mnemonic); echo "seed: " . $seed->getHex() . PHP_EOL; $hdFactory = new HierarchicalKeyFactory(); $master = $hdFactory->fromEntropy($seed); $hardened = $master->derivePath("44'/2'/0'/0/0"); echo 'WIF ' . $hardened->getPrivateKey()->toWif($network); echo PHP_EOL; $address = new PayToPubKeyHashAddress($hardened->getPublicKey()->getPubKeyHash()); echo 'address ' . $address->getAddress($network); echo PHP_EOL;
注:还没有想到PHP如何实现生成以太坊的V3版本keystore,若有大佬实现过,恳请希望能留下足迹感激不尽