【问题标题】:Get compressed public key and the bitcoin address using only the private key in BitcoinJ仅使用 BitcoinJ 中的私钥获取压缩的公钥和比特币地址
【发布时间】:2021-08-31 11:09:25
【问题描述】:

我已经知道使用 BitcoinJ 中的 ECKey 对象从 base58 编码的私钥中获取公钥。请参阅示例代码。

String base58PrivateKeyString = "---------------------private key here---------------------";
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);
}

// I'm not sure that I'm correct. Is this the correct compressed public key?
String publicKey = Hex.toHexString(ECKey.publicKeyFromPrivate(Base58.decodeToBigInteger(base58PrivateKeyString), true));

String bitcoin address; // I don't know how to get

但我还是不明白要从“key”对象中获取压缩的私钥比特币地址。我尝试了一些 compressPoint() 方法。但我没有成功。

【问题讨论】:

    标签: java bitcoin bitcoinj


    【解决方案1】:

    为了获得合格 WIF 的压缩公钥,只需使用 bitcoinJ 库中的以下函数。

    String publicKey = key.getPublicKeyAsHex();
    

    【讨论】: