question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

signDigest occasionally generates invalid signatures

See original GitHub issue

Perhaps 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:closed
  • Created 6 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
starlabs007commented, Jan 20, 2018

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!

1reaction
ricmoocommented, Dec 21, 2017

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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

SignedXml generates invalid signatures - Stack Overflow
My problem is that, I can't seem to generate valid signatures. Both the third party service, and an online signature verifier I found,...
Read more >
Digital Signature Service - European Commission
Beyond valid and invalid digital signature however, there are a lot of cases when one cannot determine the validity of a digital signature....
Read more >
Digital Signatures Workflow Guide - Adobe
Digital Signatures Workflow Guide for the Adobe® Acrobat Family of Products. ... Invalid signatures either have an invalid certificate or the document has ......
Read more >
Invalid code signature problem with Xcode12 - Apple Developer
I-AM-RICH because it has an invalid code signature, inadequate entitlements or its profile has not been explicitly trusted by the user. User Info:...
Read more >
Digital Signatures for PDF documents - iText
signature, involuntary, for example due to a transmission error, or deliberately, if somebody wants to create a forgery from the original document.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found