signDigest occasionally generates invalid signatures
See original GitHub issuePerhaps I’m doing this wrong but I get different results when using web3.eth.sign (the version that Truffle 3.x uses - have not updated to 4.x yet) versus ethers.js signDigest:
Here’s my web3 code:
function generateSignedData(msg, account) {
let seedData = web3.sha3(msg);
let signedData = web3.eth.sign(account, seedData);
let r = signedData.substr(0, 66);
let s = '0x' + signedData.substr(66, 64);
let v = parseInt(signedData.substr(130)) + 27;
return { hash: seedData, v: v, r: r, s: s };
}
This is the version with ethers.js (slightly complicated due to having to prefix the ethereum signed message string to the hash):
function generateEthersSignedData(msg, signingKey) {
var prefix = "\x19Ethereum Signed Message:\n32";
var msgBytes = ethers.utils.toUtf8Bytes(msg);
var msgDigest = ethers.utils.keccak256(msgBytes);
var final = ethers.utils.solidityKeccak256(['string', 'bytes32'], [prefix, msgDigest]);
var signature = signingKey.signDigest(final);
return { hash: msgDigest, v: signature.recoveryParam + 27, r: signature.r, s: signature.s };
}
As an example:
msg = '0x7f23b5eed5bc7e89f267f339561b2697faab234a2'
test private key used: 0x09a11afa58d6014843fd2c5fd4e21e7fadf96ca2d8ce9934af6b8e204314f25c
web3 result:
signed data: {
"hash": "0x69aff0e8e6bad68d84a1edb4175a4396d5a78d4d8b9d17e08034e2785bc8d2d7",
"v": 28,
"r": "0x7222038446034a0425b6e3f0cc3594f0d979c656206408f937c37a8180bb1bea",
"s": "0x47d061e4ded4aeac77fa86eb02d42ba7250964ac3eb9da1337090258ce79849"
}
ethers.js result:
ethers.js signed data: {
"hash": "0x69aff0e8e6bad68d84a1edb4175a4396d5a78d4d8b9d17e08034e2785bc8d2d7",
"v": 28,
"r": "0x7222038446034a0425b6e3f0cc3594f0d979c656206408f937c37a8180bb1bea",
"s": "0x047d061e4ded4aeac77fa86eb02d42ba7250964ac3eb9da1337090258ce79849"
}
Note the extra 0
prefixed for s
with the ethers.js result. Testing these results via ecrecover
in a contract that I’ve wrote I was able to successfully extract the owning address from the web3 version but not ethers.js (due to the extra 0
).
I’ve encountered extraneous 0
being added to r
, s
with certain values of msg
. I can provide more sample data if you’d like.
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (6 by maintainers)
Top GitHub Comments
I’ve finally had some extended time to investigate and test this further. So it turns out the issue was on my side as the contract had a modifier clause that caused the ethers.js signing version of my test to fail - something I totally overlooked.
SIgning works 100% now. Thanks for a great ethereum library and keep up the good work!
For another similar bug in another issue we resolved it down to being an issue with how the
v
was being adjusted.Can you include the JavaScript you are actually using to pass the signature into the contract? Are you suing the
ethers.Contract
object?