php 创建BTC、LTC、ETH助记词、私钥和地址 使用示例(转)

项目依赖:

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 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;

// 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 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 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私钥、地址:

<?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;

注:还没有想到PHP如何实现生成以太坊的V3版本keystore,若有大佬实现过,恳请希望能留下足迹感激不尽

转载说明

文章转自:php 创建BTC、LTC、ETH助记词、私钥和地址 使用示例

原创文章,作者:Zeyu,如若转载,请注明出处:https://jinzhijun.cn/develop/1209

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注