nimiq / xpub
一个简单的类,用于在不使用GMP的情况下推导BTC和ETH扩展公钥和地址。
1.4.1
2024-01-10 14:44 UTC
Requires
- php: >=7.1
- ext-bcmath: *
- bitwasp/bech32: ^0.0.1
- kornrunner/keccak: ^1.0
- simplito/elliptic-php: ^1.0
- stephenhill/base58: ^1.1
README
一个简单的类,用于在不使用GMP的情况下推导BTC和ETH扩展公钥和地址。仅需要BCMath扩展(但如有可用,仍使用GMP进行更快的计算)。
支持以下格式
安装
Nimiq XPub包可通过Packagist包注册表获取,并可以使用Composer安装。
composer require nimiq/xpub
要求
- PHP >= 7.1
- BCMath或GMP扩展
用法
# PSR-4 autoloading with composer use Nimiq\XPub; # Create an XPub class instance from an xpub/tpub/ypub/upub/zpub/vpub string. $xpub = XPub::fromString( 'xpub...' ); // => BIP44 Original $xpub = XPub::fromString( 'ypub...' ); // => BIP49 Nested SegWit $xpub = XPub::fromString( 'zpub...' ); // => BIP84 Native SegWit # You can also specify the address scheme to override auto-detection. $xpub = XPub::fromString( 'xpub...', XPub::BIP84 ); $xpub = XPub::fromString( 'xpub...', XPub::BIP49 ); $xpub = XPub::fromString( 'zpub...', XPub::BIP44 ); # Derive a child extended public key from it. $xpub_i = $xpub->derive( $i ); # You can also pass an array to derive a path. $xpub_i_k = $xpub->derive( [$i, $k]); # An XPub can be serialized back into a string. # Pass $asHex = true to serialize into a HEX string, base58 is the default. $xpub_string = $xpub_i->toString( $asHex = false ); # An XPub can be converted into an address. # Pass $coin = 'eth' to convert into an ETH address. # # By default, xpubs are converted into standard addresses, # ypubs are converted into nested segwit addresses, # and zpubs are converted into native segwit addresses. $address = $xpub_i->toAddress( $coin = 'btc' );
转换
您可以使用此库在xpub格式之间进行转换,例如
// Parse any extended public key string $xpub = XPub::fromString( 'xpub...' ); // Change the version on the instance $xpub->version = 'zpub'; // Use any of the supported formats from the table at the top of the README // Stringify to the target format $zpub = $xpub->toString(); // 'zpub...'
辅助工具
XPub
类还公开了两个常见的哈希方法
# Get a hash160 $hashed_hex = XPub::hash160( $input_hex ); # Get a double sha256 $hashed_hex = XPub::doubleSha256( $input_hex );
开发
测试
要运行测试套件,请执行
composer run-script test # or php test/test.php