简体   繁体   中英

Bit-Wasp/bitcoin-php Generate Wallet from Extended Public Key

I have been working around Bit-Wasp/bitcoin-php library for a while now and I encountered problems with it that I cannot resolve.

I have this as my code:

    public function bitcoinWalletFromPublicKey($key, $index) {

    $adapter = Bitcoin::getEcAdapter();

   if (config('market.btc_network') == "mainnet") {
       $btc = NetworkFactory::bitcoin();
       $bitcoinPrefixes = new BitcoinRegistry();
   } else {
        $btc = NetworkFactory::bitcoinTestnet();
        $bitcoinPrefixes = new BitcoinTestnetRegistry();
   }


    $slip132 = new Slip132(new KeyToScriptHelper($adapter));

    $pubkeytype=substr($key, 0, 4);

    if ($pubkeytype=='xpub' || $pubkeytype =='tpub') $pubPrefix = $slip132->p2pkh($bitcoinPrefixes);
    if ($pubkeytype=='ypub') $pubPrefix = $slip132->p2shP2wpkh($bitcoinPrefixes);
    if ($pubkeytype=='zpub' || $pubkeytype =='vpub') $pubPrefix = $slip132->p2wpkh($bitcoinPrefixes);

    $config = new GlobalPrefixConfig([
        new NetworkConfig($btc, [$pubPrefix])
    ]);



    $serializer = new Base58ExtendedKeySerializer(
        new ExtendedKeySerializer($adapter, $config)
    );

    $path = '0/' . $index;

    $fkey = $serializer->parse($btc, $key);

    $child_key = $fkey->derivePath($path);
    #$account0Key = $child_key->derivePath("84'/0'/0'");
    #$child_key = $fkey->derivePath("0/1");

    //dd($child_key->getAddress(new AddressCreator())->getAddress());
    return $child_key->getAddress(new AddressCreator())->getAddress();
}

I have two problems with this code:

Problem #1 On the first few lines of the code you will see that I used an If statement to check what network should it use. On my test im using testnet network and I'm sure as well that the code on my If / else { # code } works and it uses NetworkFactory::bitcoinTestnet() and new BitcoinTestnetRegistry() properly;

$key variable represents the Master Public Key of my user from Electrum wallet or whatever with a format of (xpub#########################/vpub#########################) or in my case since its on testnet it uses tpub######################### format. However, it returns an address with a format of bc#########, this means that its passing on mainnet network wherein it should be on testnet network.

Problem #2 On lower part of my code, I'm using $fkey = $serializer->parse($btc, $key); and $child_key = $fkey->derivePath($path) wherein $path = '0/' $index. $index here are just random numbers. It can be 0/1 or 0/99 or whatever 0/random.

Problem here is that somehow related to Problem #1, after it generates the wrong address, when I try to use this address for transaction my rpc returns an invalid address Error. As you can see as well I have a commented code $account0Key = $child_key->derivePath("84'/0'/0'"); wherein i got an error that it needs a private key instead of a public one. Now, my concern is that I do not want the users of the system i'm making to put their private keys whatsoever as it will might just compromise their wallets.

Basically, What I want to achieve using with this library from BitWasp is when a user put in their master public key from their wallet, my system would be able to then generate an address to be used for a btc transaction. Please help.

在 getAddress() 方法中传递网络有效

return $child_key->getAddress(new AddressCreator())->getAddress($btc);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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