'ethereumjs-util check signed message
I have the following signature verification function:
const verifySignature = (nonce: number, signature: string): string => {
const msg = `Nonce: ${nonce}`
const msgHex = bufferToHex(Buffer.from(msg));
const msgBuffer = toBuffer(msgHex);
const msgHash = hashPersonalMessage(msgBuffer);
const signatureParams = fromRpcSig(signature);
const publicKey = ecrecover(
msgHash,
signatureParams.v,
signatureParams.r,
signatureParams.s
);
const addressBuffer = publicToAddress(publicKey);
return bufferToHex(addressBuffer);
}
I then have these tests:
describe('verify ethereum signature', () => {
const echash = Buffer.from(
'82ff40c0a986c6a5cfad4ddf4c3aa6996f1a7837f9c398e17e5de5cbd5a12b28',
'hex'
)
const ecprivkey = Buffer.from(
'3c9229289a6125f7fdf1885a77bb12c37a8d3b4962d936f7e3084dece32a3ca1',
'hex'
)
const nonce = 1234567
const expectedAddress = '0x58b4cafb614393925042e886f4b2413d799198f1'
it('should return the users 0xaddress', () => {
const signedMessage = ecsign(hashPersonalMessage(toBuffer(new Buffer(`Nonce: ${nonce}`, 'hex'))), ecprivkey)
const signedString = toRpcSig(signedMessage.v, signedMessage.r, signedMessage.s)
const address = verifySignature(nonce, signedString)
assert.equal(address, expectedAddress)
})
it('should return the wrong address if the message is wrong', () => {
const signedMessage = ecsign(hashPersonalMessage(toBuffer(new Buffer(`Wrong Message`, 'hex'))), ecprivkey)
const signedString = toRpcSig(signedMessage.v, signedMessage.r, signedMessage.s)
const address = verifySignature(nonce, signedString)
assert.notEqual(address, expectedAddress)
})
})
However, the signature always verifies, irrespectively of value. My question is, how can I ensure that the signed message is what is in fact expected?
Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
