web3j icon indicating copy to clipboard operation
web3j copied to clipboard

Sign.signMessage not working with Ethereum keys

Open doit-ceo opened this issue 4 years ago • 1 comments

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");
    	}

doit-ceo avatar Dec 20 '21 14:12 doit-ceo

Don't confuse the address with the actual public key: https://docs.metamask.io/guide/common-terms.html#address-public-key

Erel2 avatar Dec 20 '21 16:12 Erel2