1

I am using https://godoc.org/github.com/ethereum/go-ethereum/crypto#Sign to sign an arbitrary message and trying to recover the signer address using sigUtil.recoverPersonalSignature(https://www.npmjs.com/package/eth-sig-util#recoverpersonalsignature-msgparams). This doesn't return the correct address.

Example - Signing a message:

msg := "testmsg"
msgHash := crypto.Keccak256Hash([]byte(msg))
sigBytes, err := crypto.Sign(msgHash.Bytes(), privKey)
if err != nil {
        // handle error
}
sig := hexutil.Encode(sigBytes)

Signature recovery using https://www.npmjs.com/package/eth-sig-util#recoverpersonalsignature-msgparams, where ctx.request.body.msg and ctx.request.body.sig are the message and signatures respectively obtained from a http request body:

address = sigUtil.recoverPersonalSignature({data: ctx.request.body.msg, sig: ctx.request.body.sig});

What am I missing here? What might be the correct way to solve this?

1

From https://yos.io/2018/11/16/ethereum-signatures/:

The eth_sign method calculates an Ethereum specific signature with:

eth_sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))).

The prefix to the message makes the calculated signature recognisable as an Ethereum specific signature.

The above problem can therefore be solved as follows:

msg := "testmsg"
prefixedMsg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(msg), msg)
msgHash := crypto.Keccak256Hash([]byte(prefixedMsg))
sigBytes, err := crypto.Sign(msgHash.Bytes(), privKey)
if err != nil {
    // handle error
}
// optional: add 27 to the 65th byte
sigBytes[64] = uint8(int(sigBytes[64])) + 27
sig := hexutil.Encode(sigBytes)

The message signer can be correctly recovered using this signature. Note how I added 27 to the 65th byte of the signature. This gives me the exact Ethereum signature (v = 27 or 28) for address recovery.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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