0

I'm trying to use ethereumjs to verify a signature. The signature verifies successfully with https://etherscan.io/verifySig using the following parameters:

  • [Step 1] Address: 0x817A8d457bF4bFcDB833a0C72eb2f879C857e1E5
  • [Step 2] Message Signature Hash: 0xa3ea6f0c3302f82103a62bb31bdbd9ea5a25436d2a3414d0cb1f60e7fc8cf0fb337c1205ad5e6180015e1fbb9fecffe956e519b6f4af79a08df7a1275fb8abc71c
  • [Step 3] Enter the original message that was signed: lol:18006416246562465

Unfortunately, it's not verifying with ethereumjs. Here's my code:

import assert from 'assert'
import { ecrecover, pubToAddress, keccak } from 'ethereumjs-util'

var signature = "a3ea6f0c3302f82103a62bb31bdbd9ea5a25436d2a3414d0cb1f60e7fc8cf0fb337c1205ad5e6180015e1fbb9fecffe956e519b6f4af79a08df7a1275fb8abc71c";
var message = 'lol:18006416246562465';
message = keccak(new Buffer(message));

var messageBuffer = new Buffer(message);

var r = new Buffer(signature.substring(0, 64), 'hex')
var s = new Buffer(signature.substring(64, 128), 'hex')
var v = parseInt(signature.substring(128, 130), 16);

var pub = ecrecover(messageBuffer, v, r, s);
var recoveredAddress = '0x' + pubToAddress(pub).toString('hex')

console.log(recoveredAddress);

If I'm understanding it correctly the recovered address should be equal to the original address but it isn't.

Is my understanding simply incorrect or am I doing something wrong?

2 Answers 2

0

I was able to accomplish signing using the web3 library. That library has some helpers in it so you don't need to construct the rsv and other pieces on your own. Here is an example function:

function generateSignature(message) {
  const signature = web3.eth.accounts.sign(
    web3.utils.soliditySha3(message),
    private_key
  );
  return signature.signature;
}

If this can correctly generate your signature you should be able to work backwards from there.

Another thing to keep in mind is that the spec specific that the message begins with "\x19Ethereum Signed Message:\n32" that may be why your code is not working correctly.

2
  • I'm not trying to sign messages - I'm trying to verify them. And the sample message I've shared does successfully validate with etherscan.io - I just can't get it to verify with ethereumjs
    – neubert
    Feb 7, 2022 at 14:36
  • Well building off of your "\x19Ethereum Signed Message:\n32" insight I tried to replace message = keccak(new Buffer(message)); with message = keccak(new Buffer("\x19Ethereum Signed Message:\n32" + keccak(new Buffer(message)))); but it did not give me the address as I was expecting
    – neubert
    Feb 7, 2022 at 20:01
0

This is what I needed to do:

#
#-----[ FIND ]------------------------------------------
#
message = keccak(new Buffer(message));
#
#-----[ REPLACE WITH ]----------------------------------
#
message = "\x19Ethereum Signed Message:\n" + message.length + message;
message = keccak(new Buffer(message));

Your Answer

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

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