JAVA版本 personal_sign 签名验证

验证:

public static String recoverAddressFromSignature(String signature, String src) throws SignatureException {
    byte[] signatureBytes = Numeric.hexStringToByteArray(signature);
    byte v = signatureBytes[64];
    if (v < 27) {
        v += 27;
    }

    Sign.SignatureData sd = new Sign.SignatureData(
            v,
            (byte[]) Arrays.copyOfRange(signatureBytes, 0, 32),
            (byte[]) Arrays.copyOfRange(signatureBytes, 32, 64));
    BigInteger pubkey = Sign.signedMessageToKey(getMessageHash(Numeric.toHexString(src.getBytes()).getBytes()), sd);
    String addressRecovered = "0x" + Keys.getAddress(pubkey);
    logger.info("--->" + addressRecovered);
    String hexAddress = AddressType.User.normalize(addressRecovered);
    logger.info("--> " + hexAddress);
    return hexAddress;
}

签名信息:

  public static byte[] getMessageHash(byte[] message) {
    byte[] prefix = ("\u0019Conflux Signed Message:\n".concat(String.valueOf(message.length))).getBytes();
    byte[] result = new byte[prefix.length + message.length];
    System.arraycopy(prefix, 0, result, 0, prefix.length);
    System.arraycopy(message, 0, result, prefix.length, message.length);
    return result;
}

导包:

io.github.conflux-chain
conflux.web3j
1.0.5


org.web3j
core
4.5.11


org.bitcoinj
bitcoinj-core
0.15.3
compile

1 Like

多多分享 手里的干货交出来…

哈哈