Guest User

Untitled

a guest
Nov 22nd, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. import org.web3j.crypto.Keys;
  2. import org.web3j.crypto.Sign;
  3. import org.web3j.utils.Numeric;
  4.  
  5. import java.security.SignatureException;
  6.  
  7. public class SignUtil {
  8.  
  9. private static final String GETH_SIGN_PREFIX = "\u0019Ethereum Signed Message:\n32";
  10.  
  11. /**
  12. * This method is expecting the signed message to be a hash of the original message. The length of the message is
  13. * then hardcoded to 32. Also, this might only work for messages signed by geth, not sure if other clients
  14. * add the prefix to the signed message.
  15. * @param signedHash
  16. * @param originalMessageHashInHex
  17. * @return
  18. * @throws SignatureException
  19. */
  20. public static String getAddressUsedToSignHashedMessage(String signedHash, String originalMessageHashInHex) throws SignatureException {
  21. byte[] messageHashBytes = Numeric.hexStringToByteArray(originalMessageHashInHex);
  22. String r = signedHash.substring(0, 66);
  23. String s = "0x"+signedHash.substring(66, 130);
  24. String v = "0x"+signedHash.substring(130, 132);
  25. System.out.println();
  26. byte[] msgBytes = new byte[GETH_SIGN_PREFIX.getBytes().length + messageHashBytes.length];
  27. byte[] prefixBytes = GETH_SIGN_PREFIX.getBytes();
  28. System.arraycopy(prefixBytes, 0, msgBytes, 0, prefixBytes.length);
  29. System.arraycopy(messageHashBytes, 0, msgBytes, prefixBytes.length, messageHashBytes.length);
  30. String pubkey = Sign.signedMessageToKey(msgBytes,
  31. new Sign.SignatureData(Numeric.hexStringToByteArray(v)[0],
  32. Numeric.hexStringToByteArray(r),
  33. Numeric.hexStringToByteArray(s)))
  34. .toString(16);
  35. System.out.println("");
  36. System.out.println("Pubkey: " + pubkey);
  37. String address = Keys.getAddress(pubkey);
  38. return address;
  39. }
  40. }
Add Comment
Please, Sign In to add comment