For my use case I need to verify a ECDSA signature from a signed Ethereum message.
From the front end, the message is first converted into a Sha3/keccac256 hash using web3.js with below method: https://web3js.readthedocs.io/en/v1.2.11/web3-utils.html#sha3
My message now becomes: 0xd9eba16ed0ecae432b71fe008c98cc872bb4cc214d3220a36f365326cf807d68
Then a signature is generated with a private key from the method web3.eth.personal.sig: https://web3js.readthedocs.io/en/v1.2.11/web3-eth-personal.html#id15
My signature becomes: 0x7f21a0fbf7bd236f0c76df69803ec39d50da8e84756886844538c7b913f2cdb93fdea40bf14f25166206416d8a0e3c3e05c59e07df254cf2f36d6b1edc8830d91c
Using the ECDSA algorithm I should be able to distract the public key from the message hash and signature, my public key is:
0xe6712f6f5053277db9c37c5ada15559e9f3084db
My requirement it to distract this public key using Apex, I found there is a verify method on the Crypto class that can verify ECDSA-SHA256 signatures: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_crypto.htm#apex_System_Crypto_generateDigest
I tried to verify my signature with the message hash but I get an error: 'System.SecurityException: Invalid Crypto Key'
Also tried to convert the key and message / signature to blob with EncodingUtil.base64Decode.
The code I used:
Blob message = Blob.valueOf('0xd9eba16ed0ecae432b71fe008c98cc872bb4cc214d3220a36f365326cf807d68');
Blob signature = Blob.valueOf('0x7f21a0fbf7bd236f0c76df69803ec39d50da8e84756886844538c7b913f2cdb93fdea40bf14f25166206416d8a0e3c3e05c59e07df254cf2f36d6b1edc8830d91c');
Blob publicKey = Blob.valueOf('0xe6712f6f5053277db9c37c5ada15559e9f3084db');
System.debug(Crypto.verify('ECDSA-SHA256', message, signature, publicKey));
I think the Blob format of my signature / message might not be correct and should use some sort of Hash Blob type and maybe this is not supported with this method, anyone has any suggestions how I could verify this in Apex?
Here is an article explaining more about the ECDSA signature Ethereum uses: https://medium.com/mycrypto/the-magic-of-digital-signatures-on-ethereum-98fe184dc9c7
ECDSA-SHA2566
should beECDSA-SHA256
Crypto
class. You can try implementing this in a Function and calling it from Apex.