[英]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.我已经围绕 Bit-Wasp/bitcoin-php 库工作了一段时间,但遇到了无法解决的问题。
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.问题#1在代码的前几行你会看到我使用了一个 If 语句来检查它应该使用什么网络。 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;
在我的测试中,我使用 testnet 网络,我也确定 If / else { # code } 上的代码可以正常工作,并且它正确使用了 NetworkFactory::bitcoinTestnet() 和 new BitcoinTestnetRegistry();
$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. $key 变量代表我的用户的主公钥来自 Electrum 钱包或任何格式为 (xpub########################/vpub# ########################) 或在我的情况下,因为它在测试网上它使用 tpub############## ########### 格式。 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.
但是,它返回一个格式为 bc######### 的地址,这意味着它在主网网络上传递,其中它应该在测试网网络上。
Problem #2 On lower part of my code, I'm using $fkey = $serializer->parse($btc, $key);问题#2在我代码的下半部分,我使用了 $fkey = $serializer->parse($btc, $key); and $child_key = $fkey->derivePath($path) wherein $path = '0/' $index.
和 $child_key = $fkey->derivePath($path) 其中 $path = '0/' $index. $index here are just random numbers.
$index 这里只是随机数。 It can be 0/1 or 0/99 or whatever 0/random.
它可以是 0/1 或 0/99 或任何 0/随机。
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.这里的问题是与问题 #1 有某种关系,在它生成错误的地址后,当我尝试使用此地址进行交易时,我的 rpc 返回无效地址错误。 As you can see as well I have a commented code $account0Key = $child_key->derivePath("84'/0'/0'");
正如你所看到的,我有一个注释代码 $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.基本上,我想使用BitWasp 的这个库来实现的是,当用户从他们的钱包中放入他们的主公钥时,我的系统将能够生成一个用于 btc 交易的地址。 Please help.
请帮忙。
在 getAddress() 方法中传递网络有效
return $child_key->getAddress(new AddressCreator())->getAddress($btc);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.