Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Recipe: Util function to get v, r and s values from a signed message #78

Closed
naddison36 opened this issue Nov 23, 2017 · 2 comments · May be fixed by EzikeChris/ethers.js#17
Closed
Labels
discussion Questions, feedback and general information.

Comments

@naddison36
Copy link

naddison36 commented Nov 23, 2017

It would be nice to have a cryptographic utility function that returns the r, s and v values of a signature. These values are calculated as follows

const wallet = new Wallet(privateKey);
const signature = wallet.signMessage(message);
const r = signature.slice(0, 66);
const s = '0x' + signature.slice(66, 130);
const v = '0x' + signature.slice(130, 132);

For more details see https://ethereum.stackexchange.com/questions/1777/workflow-on-signing-a-string-with-private-key-followed-by-signature-verificatio

@ricmoo
Copy link
Member

ricmoo commented Nov 23, 2017

You can use the underlying SigningKey object to do this.

> (new ethers.SigningKey(privateKey)).signDigest(digest)
{ recoveryParam: 1,
  r: '0x8ce26421f2cfbc14a9917dbff12853bb4d1a1c9badfdc082647aba315341469c',
  s: '0x6d78375b703c4b3564c4b357b8510e3dd621719e4adcca90c6d8f3e8287391c0' }

Please keep in mind that v, depending on the context depends on the chain ID and in the future may further be hijacked (e.g. EIP 186) to have additional meanings. The recoveryParam is either 0 or 1. The value you need to pass into ecrecover in solidity is (27 + recoveryParam), which is the same as the unspecified network (i.e. chain ID 0). For other chain ID, v is ((35 + 2 * chainID) + recoveryParam).

Make sense?

@ricmoo ricmoo added the discussion Questions, feedback and general information. label Nov 24, 2017
@ricmoo ricmoo changed the title Util function to get v, r and s values from a signed message Add Recipe: Util function to get v, r and s values from a signed message Nov 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion Questions, feedback and general information.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants