PHP生成助记词和BTC、ETH、LTC地址

本文作者:陈进坚
个人博客:https://jian1098.github.io
CSDN博客:https://blog.csdn.net/c_jian
简书:https://www.jianshu.com/u/8ba9ac5706b6
联系方式:[email protected]

安装扩展

composer require bitwasp/bitcoin		# BTC库,需要运行在64位的php7.0+上
composer require web3p/ethereum-util	 # ETH库

注意:使用bitwasp库需要安装PHPgmp扩展,不然会报错Call to undefined function gmp_init()LNMP环境安装gmp扩展可以看我的另一篇文章《LNMP1.6安装gmp扩展》

生成助记词

bytes(Bip39Mnemonic::MIN_ENTROPY_BYTE_LEN);
     $bip39 = MnemonicFactory::bip39();
     // 通过随机数生成助记词
     $mnemonic = $bip39->entropyToMnemonic($entropy);
     // 输出助记词
     echo $mnemonic;
 }

创建BTC地址

function createBtcAddress($mnemonicWord,$offset){
        $seedGenerator = new Bip39SeedGenerator();
        // 通过助记词生成种子,传入可选加密串'hello'
        $seed = $seedGenerator->getSeed($mnemonicWord);
        echo "seed: " . $seed->getHex() . PHP_EOL;				//种子
        $hdFactory = new HierarchicalKeyFactory();
        $master = $hdFactory->fromEntropy($seed);
        $hardened = $master->derivePath("44/0'/0'/0/".$offset);    //44的含义:https://github.com/bitcoin/bips
        echo 'WIF: ' . $hardened->getPrivateKey()->toWif();		  //私钥
        $address = new PayToPubKeyHashAddress($hardened->getPublicKey()->getPubKeyHash());
        return $address->getAddress();
    }

参数说明:mnemonicWord(带空格的字符串)是上一步生成的助记词;offset(整数)是同一个助记词生成的第几个地址,相同助记词传入相同数字会得到同一个地址

创建ETH地址

use Web3p\EthereumUtil\Util;	//头部要额外引入这个类

// 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地址

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;

参考文章

  • https://www.cnblogs.com/ygcool/p/11714335.html
  • https://iancoleman.io/bip39/
  • https://github.com/bitcoin/bips

你可能感兴趣的:(PHP)