Signing Data in Java and Web3j
This code example will demonstrate how to perform an ethereum personal sign operation in java using web3j. This will match signatures generated by the personal sign functions of web3js
and geth
.
byte[] messageToSign = "Testing Testing 123".getBytes();
//Create prefix
String prefix = "\u0019Ethereum Signed Message:\n" + messageToSign.length;
// Concat prefix and message
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
try {
outputStream.write(prefix.getBytes());
outputStream.write(messageToSign);
} catch (IOException e) {
throw new RuntimeException("Error when generating signature", e);
}
// Hash the prefixed message
byte[] hashedPrefixedMessage = Hash.sha3(outputStream.toByteArray());
// Create credentials (key pair) from mnemonic
Credentials credentials = WalletUtils.loadBip39Credentials("password", "mnemonic");
//Sign the hashed message with the credentials private key
Sign.SignatureData signedMessage = Sign.signMessage(
hashedPrefixedMessage, credentials.getEcKeyPair(), false);
//Convert sig values from bytes[] to int / hex strings
int sigV = new BigInteger(signedMessage.getV()).intValue();
String hexSigR = Numeric.toHexString(signedMessage.getR());
String hexSigS = Numeric.toHexString(signedMessage.getS());
ecrecover
can now be used to verify the message was in fact signed by the expected signee, either within a smart contract or off-chain. Awesome!
- Kauri original title: Signing Data in Java and Web3j
- Kauri original link: https://kauri.io/signing-data-in-java-and-web3j/405c1ebadc29451b9df85864fb36b4bf/a
- Kauri original author: Craig Williams (@craig)
- Kauri original Publication date: 2019-12-18
- Kauri original tags: ethereum, java, web3j, signing
- Kauri original hash: QmYmr2mRtT4AB1xPACcsfnqJB8RiyczqLHMZu1z42Tthmt
- Kauri original checkpoint: QmRS3wCLX2MRi62bg9NTM89qNkgm3XjpKXciLvCKAr1f1g