仅使用 BitcoinJ 中的私钥获取压缩 public 密钥和比特币地址

Get compressed public key and the bitcoin address using only the private key in BitcoinJ

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

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() 方法尝试了一些。但是我没有成功。

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

String publicKey = key.getPublicKeyAsHex();