How to Create a UserOperation Signature Using Java and Web3j in alchemy API

48 views Asked by At

I have created code to execute a UserOperation using Alchemy's API.

In order to sign UserOperation, I have created the following code to sign it, and the following execution results

Need help.

Code: -32507
Message: Invalid UserOp signature or paymaster signature

Implemented Code

  public static String signUserOperation(String userOperationHash) {
    ECKeyPair ecKeyPair = ECKeyPair.create(Numeric.hexStringToByteArray(privateKey));
    SignatureData org.web3j.crypto.signedMessage = signMessage(userOperationHash.getBytes(StandardCharsets.UTF_8),
        ecKeyPair);
    return signatureDataToHex(signedMessage);
  }

  public static String signatureDataToHex(SignatureData signature) {
     // Convert R, S, and V to hex strings, respectively
    String r = Numeric.toHexString(signature.getR());
    String s = Numeric.toHexString(signature.getS());
    String v = Numeric.toHexString(signature.getV());

    // strip 0x (if present)
    r = stripHexPrefix(r);
    s = stripHexPrefix(s);
    v = stripHexPrefix(v);

    // concatenate r, s, v and return
    return r + s + v;
  }

  private static String stripHexPrefix(String hexString) {
    if (hexString.startsWith("0x")) {
      return hexString.substring(2);
    }
    return hexString;
  }
0

There are 0 answers