kielabokkie / bitcoin-address-validator
验证旧版、segwit、原生segwit(bech32)和taproot比特币地址
v2.2.0
2023-09-28 08:27 UTC
Requires
- php: ^7.3|^7.4|^8.0|^8.1
- ext-bcmath: *
Requires (Dev)
- phpunit/phpunit: ^7.5|^8.0|^9.0
README
验证旧版、segwit、原生segwit(bech32)、和taproot比特币地址。
要求
- PHP >= 7.3
请注意,此包从v2.1版本开始支持taproot地址。
安装
通过composer安装此包
composer require kielabokkie/bitcoin-address-validator
用法
首先实例化验证器类
$addressValidator = new \Kielabokkie\Bitcoin\AddressValidator;
验证任何类型的地址(旧版、segwit、原生segwit和taproot)
$addressValidator->isValid('1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i');
旧版(P2PKH)地址
$addressValidator->isPayToPublicKeyHash('1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i');
segwit(P2SH)地址
$addressValidator->isPayToScriptHash('3ALJH9Y951VCGcVZYAdpA3KchoP9McEj1G');
原生segwit(bech32)地址
$addressValidator->isBech32('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4');
taproot(P2TR)地址
$addressValidator->isPayToTaproot('bc1pveaamy78cq5hvl74zmfw52fxyjun3lh7lgt44j03ygx02zyk8lesgk06f6');
测试网
默认情况下,验证器仅将主网地址视为有效。如果您想验证主网和测试网地址,可以使用方法链
// Both valid $addressValidator->includeTestnet()->isBech32('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'); $addressValidator->includeTestnet()->isBech32('tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx');
如果您只想验证测试网地址,可以按以下方式操作
// Invalid $addressValidator->onlyTestnet()->isBech32('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'); // Valid $addressValidator->onlyTestnet()->isBech32('tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx');
测试
此包针对官方bitcoin/bitcoin仓库的测试数据进行测试。如果您遇到一个验证不正确的地址,请提交问题。
使用以下命令运行测试
composer test
鸣谢
此包基于以下包,并使用了大量它们的代码
- bitwasp/bech32 by @afk11
- brooksyang/bech32m by BrooksYang
- linusu/bitcoin-address-validator by @LinusU
所有荣誉归原作者所有。
捐赠
此包让您赚了很多钱,节省了时间,或者只是给您带来了快乐?
捐赠将非常感谢: 32vtWJSomccxQ6y1tgSwSHXN5PChpdYy27