1

I can sign a text message using public key in web3js

const signature = await this.web3.eth.personal.sign(
            loginMessage,
            publicAddress,
            ""
        )

What would be its web3j equivalent?

1 Answer 1

0

In Web3J, we have the following Sign class. It has multiple static methods that you can use to create signatures. For example:

import org.web3j.crypto.Credentials;
import org.web3j.crypto.Sign;

public class Main {
    public static void main(String[] args) {
        Credentials credentials = Credentials.create("0x1234");
        byte[] msg = "hello".getBytes();
        Sign.SignatureData signature = Sign.signMessage(msg, credentials.getEcKeyPair());
        // do what you want with the signature
    }
}

The return of Sign.signMessage(...) is a SignatureData instance which contains the r, s and v values.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.