Author: Chen Jinjian
personal blog: HTTPS: //jian1098.github.io
CSDN blog: https: //blog.csdn.net/c_jian
Jane book: https: //www.jianshu.com/u/8ba9ac5706b6
Contact: jian1098 @qq.com
Install extension
composer require bitwasp/bitcoin # BTC库,需要运行在64位的php7.0+上
composer require web3p/ethereum-util # ETH库
Note: Use the extensions that bitwasp
need to be installed PHP
in the library gmp
, otherwise an error will be reported Call to undefined function gmp_init()
. For the LNMP
environment to install the gmp extension, please refer to my other article "LNMP1.6 install gmp extension"
Generate mnemonic words
<?php
require_once './vendor/autoload.php';
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
use BitWasp\Bitcoin\Crypto\Random\Random;
use BitWasp\Bitcoin\Key\Factory\HierarchicalKeyFactory;
use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39Mnemonic;
use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39SeedGenerator;
use BitWasp\Bitcoin\Mnemonic\MnemonicFactory;
//生成助记词
function createMnemonicWord(){
// 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;
}
Create BTC
address
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();
}
Parameter description: mnemonicWord
(string with spaces) is the mnemonic phrase generated in the previous step; offset
(integer) is the number of addresses generated by the same mnemonic phrase, the same mnemonic phrase with the same number will get the same address
Create ETH
address
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后一样的地址
Create LTC
address
<?php
require_once './vendor/autoload.php';
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Crypto\Random\Random;
use BitWasp\Bitcoin\Key\Factory\HierarchicalKeyFactory;
use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39Mnemonic;
use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39SeedGenerator;
use BitWasp\Bitcoin\Mnemonic\MnemonicFactory;
use BitWasp\Bitcoin\Network\NetworkFactory;
// 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;
Reference article
- https://www.cnblogs.com/ygcool/p/11714335.html
- https://iancoleman.io/bip39/
- https://github.com/bitcoin/bips