gpt4 book ai didi

java - 在BitcoinJ Java中从WIF生成P2SH比特币地址

转载 作者:行者123 更新时间:2023-12-04 07:29:07 69 4
gpt4 key购买 nike

我正在尝试从 WIF 私钥创建 P2SH-Segwit 比特币地址。我正在使用 Java 中的 BitcoinJ 库 .请参阅以下代码。

String base58PrivateKeyString = "KyyJ5vVWjZck5nsAgDWvoN1u7Q8qp5FzE8WiCq97MbnRgdLesqJZ";

NetworkParameters params = MainNetParams.get();
ECKey key;

if (base58PrivateKeyString.length() == 51 || base58PrivateKeyString.length() == 52) {
DumpedPrivateKey dumpedPrivateKey = DumpedPrivateKey.fromBase58(params, base58PrivateKeyString);
key = dumpedPrivateKey.getKey();
} else {
BigInteger privKey = Base58.decodeToBigInteger(base58PrivateKeyString);
key = ECKey.fromPrivate(privKey);
}

// Getting the public key
String pubKeyStr = key.getPublicKeyAsHex();
System.out.println("Public key is: " + pubKeyStr + "\n");

// Getting the P2SH address
List<ECKey> eckeyAList = new ArrayList<>();
eckeyAList.add(key);
Script redeemScript = ScriptBuilder.createRedeemScript(1, eckeyAList);
Script script = ScriptBuilder.createP2SHOutputScript(redeemScript);
byte[] scriptHash = ScriptPattern.extractHashFromP2SH(script);
LegacyAddress legacyAddress = LegacyAddress.fromScriptHash(params, scriptHash);
System.out.println("P2S address from the WIF pivate key is: " + legacyAddress.toString()); //3Az5wdibtPRGac41aGtyqzT1ejtobvb6qW

它的输出公钥是 03b5319c83adf4a2e274c37401623c7bf0ba453cee3e119f3bc2c523d27059b64f。
它的 输出 P2SH 地址为 3Az5wdibtPRGac41aGtyqzT1ejtobvb6qW 这与 相差甚远正确的 P2SH 地址 38SGXvkMvq8Tsop8rFx2K4JnaFKYZkjd5z .
如果你们中有人知道纠正这个问题,我很乐意接受你的意见。

最佳答案

作为兑换脚本,您必须使用 P2WPKH(支付给见证人公钥哈希)
然后Redeem Script的创建将如下所示:
脚本redeemScript = ScriptBuilder.createP2WPKHOutputScript(key);

关于java - 在BitcoinJ Java中从WIF生成P2SH比特币地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68046300/

69 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com