简体   繁体   English

尝试使用 laravel 8 上的 bitwasp/bitcoin package 从主公钥生成钱包地址但卡住了

[英]Trying to generate wallet addresses from Master Public Key using the bitwasp/bitcoin package on laravel 8 but stuck

I'm trying to generate wallet addresses from Master Public Key using the bitwasp/bitcoin package on laravel 8. I'm getting the issue below我正在尝试使用 laravel 8 上的 bitwasp/bitcoin package 从主公钥生成钱包地址。我遇到了以下问题

InvalidArgumentException
HD key magic bytes do not match network magic bytes
BitWasp\Bitcoin\Serializer\Key\HierarchicalKey\ExtendedKeySerializer::fromParser
C:\.........\vendor\bitwasp\bitcoin\src\Serializer\Key\HierarchicalKey\ExtendedKeySerializer.php:121

My code is as follows;我的代码如下;

class ApiController extends Controller
{
    private $network = NULL;

    public function __construct($network = 'bitcoin')
    {
        if (version_compare(PHP_VERSION, '7.3') >= 0) {
            $this->network = NetworkFactory::$network();
          } elseif (version_compare(PHP_VERSION, '7.2.3') >= 0) {
            $this->network = call_user_func("NetworkFactory::$network");
          } else {
            $this->network = call_user_func('NetworkFactory', $network);
          }
    }
 public function createBTC()
    {
        
        $xpub = 'MY_XPUB_KEY';
        $hdFactory = new HierarchicalKeyFactory();
        $key = $hdFactory->fromExtended($xpub, $this->network);
        $hardened = $key->derivePath("0/0");
        $privateKey =  $hardened->getPrivateKey()->toWif($this->network);
        $address = new PayToPubKeyHashAddress($hardened->getPublicKey()->getPubKeyHash($this->network));
        $address = $address->getAddress($this->network);



        $cred = new stdClass();
        $cred->balance = 0;
        $cred->address = $address;
        $cred->privateKey = $privateKey;

        return $json = json_encode($cred);
    }

}

Can't figure out what I'm doing wrong.无法弄清楚我做错了什么。 Any help would be much appreciated任何帮助将非常感激

By modifying the __construct method as below your problem should gonna be solved.通过如下修改__construct方法,您的问题应该会得到解决。

    public function __construct($network = 'bitcoin')
    {
        $this->network = NetworkFactory::bitcoin();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM