web3j
web3j copied to clipboard
Sign.signMessage not working with Ethereum keys
Public & Private Ethereum keys not valid !
I'm getting the following exception using Ethereum Public/Private keys to sign a message
Exception in thread "main" java.lang.RuntimeException: Could not construct a recoverable key. Are your credentials valid?
at org.web3j.crypto.Sign.signMessage(Sign.java:94)
at org.web3j.crypto.Sign.signMessage(Sign.java:71)
Code
//Random account generated by MetaMask
public static String pvkey="0x3cc5e7bf2f8edbc3e892eac17c7c21a5966bfd9b82d9a55d2c423961ba359360";
public static String pukey="0x1A43D9D9F10058E03feDa50C2aA657b18f1e1181";
//Removing 0x
public static BigInteger bInt(String h) {
BigInteger b = new BigInteger(h.substring(2,h.length()), 16);
return b;
}
public static void main(String[] args) throws ExecutionException, InterruptedException{
String msg="1";
String msgH= "0x31";
ECKeyPair keyPair=new ECKeyPair(bInt(pvkey), bInt(pukey));
System.out.println("eth privateKey " + keyPair.getPrivateKey().toString(16));
System.out.println("eth publicKey " + keyPair.getPublicKey().toString(16));
Sign.SignatureData sd= Sign.signMessage(msg.getBytes(), keyPair);
System.out.println("Done");
}
Don't confuse the address with the actual public key: https://docs.metamask.io/guide/common-terms.html#address-public-key