从 BitcoinJ 中的 WIF 生成 P2SH 比特币地址 Java
Generate P2SH bitcoin address from WIF in BitcoinJ Java
我正在尝试从 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
它的输出public密钥是03b5319c83adf4a2e274c37401623c7bf0ba453cee3e119f3bc2c523d27059b64f。
它的 输出 P2SH 地址是 3Az5wdibtPRGac41aGtyqzT1ejtobvb6qW 与 正确的 P2SH 地址 38SGXvkMvq8Tsop8rFx2K4JnaFKYZkjd5z.
相差甚远
如果你们中有人知道要纠正这个问题,我很乐意听取你的意见。
作为兑换脚本,您必须使用 P2WPKH(支付见证 Public 密钥哈希)
然后创建兑换脚本将如下所示:
脚本 redeemScript = ScriptBuilder.createP2WPKHOutputScript(key);
根据 Kotlin 中 Mikhail 的建议,这是正确的代码:
val params: NetworkParameters = MainNetParams.get()
val key: ECKey = ECKey.fromPrivate(BigInteger("2"))
val redeemScript: Script = ScriptBuilder.createP2WPKHOutputScript(key)
val script = ScriptBuilder.createP2SHOutputScript(redeemScript)
val scriptHash = ScriptPattern.extractHashFromP2SH(script)
val legacyAddress = LegacyAddress.fromScriptHash(params, scriptHash)
println("P2S address from the WIF private key is: $legacyAddress")
花了很多时间后我找到了解决方案。
// Getting P2SH address
LegacyAddress p2shAddressObj = LegacyAddress.fromScriptHash(PARAMS,
Utils.sha256hash160(ScriptBuilder
.createP2WPKHOutputScript(key.getPubKeyHash())
.getProgram())); // P2WPKH-P2SH (3* segwit compatible)
String p2shAddress = p2shAddressObj.toString();
我正在尝试从 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
它的输出public密钥是03b5319c83adf4a2e274c37401623c7bf0ba453cee3e119f3bc2c523d27059b64f。 它的 输出 P2SH 地址是 3Az5wdibtPRGac41aGtyqzT1ejtobvb6qW 与 正确的 P2SH 地址 38SGXvkMvq8Tsop8rFx2K4JnaFKYZkjd5z.
相差甚远如果你们中有人知道要纠正这个问题,我很乐意听取你的意见。
作为兑换脚本,您必须使用 P2WPKH(支付见证 Public 密钥哈希) 然后创建兑换脚本将如下所示:
脚本 redeemScript = ScriptBuilder.createP2WPKHOutputScript(key);
根据 Kotlin 中 Mikhail 的建议,这是正确的代码:
val params: NetworkParameters = MainNetParams.get()
val key: ECKey = ECKey.fromPrivate(BigInteger("2"))
val redeemScript: Script = ScriptBuilder.createP2WPKHOutputScript(key)
val script = ScriptBuilder.createP2SHOutputScript(redeemScript)
val scriptHash = ScriptPattern.extractHashFromP2SH(script)
val legacyAddress = LegacyAddress.fromScriptHash(params, scriptHash)
println("P2S address from the WIF private key is: $legacyAddress")
花了很多时间后我找到了解决方案。
// Getting P2SH address
LegacyAddress p2shAddressObj = LegacyAddress.fromScriptHash(PARAMS,
Utils.sha256hash160(ScriptBuilder
.createP2WPKHOutputScript(key.getPubKeyHash())
.getProgram())); // P2WPKH-P2SH (3* segwit compatible)
String p2shAddress = p2shAddressObj.toString();