使用blockcypher/php-client创建交易时签名环节类型错误
解决blockcypher/php-client多签交易签名时的PrivateKey类型不匹配错误
我在使用blockcypher/php-client创建、签名并发送多签交易时遇到了签名错误,代码如下:
$request = clone $tx; /// For Sample Purposes Only. $params = array( 'includeToSignTx' => 1, 'script_type'=>'mutlisig-n-of-m', ); $txClient = new TXClient($apiContext); try { // dd($tx); $txSkeleton = $txClient->create($tx,$params); $privateKeys = array("private key"); $txSkeleton = $txClient->sign($txSkeleton, $privateKeys); $txSkeleton = $txClient->send($txSkeleton); } catch (Exception $ex) { dd("Created TX", "TXSkeleton", null, $request, $ex); exit(1); }
错误信息
FatalThrowableError in PrivateKeyList.php line 54: Type error: Argument 1 passed to BlockCypher\Crypto\PrivateKeyList::addPrivateKey() must be an instance of BitWasp\Bitcoin\Key\PrivateKeyInterface, instance of BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PrivateKey given, called in /home/pixel/Documents/tests/vendor/blockcypher/php-client/lib/BlockCypher/Crypto/PrivateKeyList.php on line 43
相关函数代码
public function addPrivateKey(PrivateKeyInterface $privateKey) { $pubKeyHex = $privateKey->getPublicKey()->getHex(); $this->privateKeys[$pubKeyHex] = $privateKey; }
解决方案
这个问题本质是依赖版本不兼容导致的类型匹配失败:blockcypher/php-client预期接收实现BitWasp\Bitcoin\Key\PrivateKeyInterface的对象,但你当前环境中的BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PrivateKey类并没有实现这个接口,通常是因为bitwasp/bitcoin库的版本与blockcypher/php-client要求的版本不匹配。
你可以按照以下步骤解决:
确认依赖版本要求
打开blockcypher/php-client的composer.json文件,查看它对bitwasp/bitcoin的版本约束。比如它可能要求^1.0或者某个特定版本范围。调整项目依赖
在你的项目composer.json中明确指定兼容的版本,比如:"require": { "blockcypher/php-client": "^1.0", "bitwasp/bitcoin": "^1.0" }然后执行命令更新依赖:
composer update这会确保安装的bitwasp/bitcoin版本与blockcypher/php-client兼容,PrivateKey类会正确实现预期的接口。
更新blockcypher/php-client(可选)
如果你的客户端版本过旧,尝试更新到最新版本,看是否已经修复了类型匹配的问题:composer require blockcypher/php-client:latest临时类型适配(不推荐)
如果无法调整版本,你可以尝试手动验证并转换类型,但这只是临时方案,可能引入其他兼容性问题:use BitWasp\Bitcoin\Key\PrivateKeyInterface; use BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Key\PrivateKey; // 假设你从私钥字符串创建PrivateKey实例 $privateKey = PrivateKey::fromWif("your-private-key"); if ($privateKey instanceof PrivateKeyInterface) { // 正常传入签名方法 $txSkeleton = $txClient->sign($txSkeleton, [$privateKey]); } else { // 这里需要根据实际版本调整,比如查看BitWasp库的文档找正确的接口实现 throw new \Exception("PrivateKey does not implement expected interface"); }
内容的提问来源于stack exchange,提问作者Gaurav Gupta
相关产品推荐
相关产品推荐

