hdfchain / hdfchain-php-api
用于 Hdfchain 加密货币的 PHP API
Requires
- php: >=5.6
- guzzlehttp/guzzle: ^6.3
- mdanter/ecc: ^0.4.0
- stephenhill/base58: ^1.1
Requires (Dev)
This package is auto-updated.
Last update: 2024-09-22 22:09:15 UTC
README
PHP API for the Hdfchain Cryptocurrency
安装
IF PHP version >7.0 composer It can be ignored version, Command:
composer install --ignore-platform-reqs
或者
composer update --ignore-platform-reqs
PHP COMMOND CLI VERSION = 7.0 Add composer package to your project
composer require hdfchain/hdfchain-php-api
确保已安装 GMP PHP 扩展。在 ubuntu 中
sudo apt install php7.0-gmp
从仓库中
您也可以使用以下命令克隆 git 包
git clone https://github.com/hdfchain/hdfchain-php-api.git
但是您仍然需要使用 composer 获取库依赖。
composer install --no-dev
不要忘记包含 composer 自动加载器。
include __DIR__.'/../vendor/autoload.php';
使用示例
由于库具有广泛的功能,您可以在 examples 库中找到使用示例,或者查看 PHPUnit 测试。
生成种子
首先,我们需要获取网络实例以开始使用库。
$testnet = \Hdfchain\TestNet::instance();
以及主网相应地
$mainnet = \Hdfchain\MainNet::instance();
现在让我们生成一个种子,它也将被用于测试网。默认账户和分支地址将被推导以验证种子。
$seed = \Hdfchain\Crypto\ExtendedKey::generateSeed($testnet);
HD 钱包
当我们有可用的种子时,我们可以创建 HD 主密钥。
$master = \Hdfchain\Crypto\ExtendedKey::newMaster($seed, $testnet);
newMaster 方法将返回 ExtendedKey 对象,该对象具有用于处理 HD 钱包的变体 API。
ExtendedKey::privateChildKey($index)
从父 HD 私钥推导 HD 私有子密钥,返回 ExtendedKey 对象。
ExtendedKey::hardenedChildKey($index)
从父 HD 私钥推导 HD 坚固子密钥,返回 ExtendedKey 对象。
不能从 HD 公钥推导。
ExtendedKey::publicChildKey($index)
从父 HD 私有或公钥推导 HD 子密钥,返回 ExtendedKey 对象。
ExtendedKey::neuter
验证扩展密钥是公钥,返回 ExtendedKey 对象。
默认账户
使用这些基本方法,我们可以推导出根据 BIP44 的默认账户 HD 私有和公钥。
HD 路径 (m/44'/42'/0')
$defaultAccountPrivateKey = $master ->hardenedChildKey(44) ->hardenedChildKey(42) ->hardenedChildKey(0); $defaultAccountPublicKey = $master->neuter();
ExtendedKey 实现了 __toString() 方法,因此您可以轻松获取 HD 密钥的 Base58 表示形式。
echo sprintf("Default account HD private key: %s\n", $defaultAccountPrivateKey); echo sprintf("Default account HD public key: %s\n", $defaultAccountPublicKey);
从默认账户中,我们可以推导出 0 分支和 0 索引,然后获取默认地址。
$defaultAddress = $defaultAccountPublicKey ->publicChildKey(0) ->publicChildKey(0) ->getAddress(); echo sprintf("Default address: %s\n", $defaultAddress);