Skip to content

Instantly share code, notes, and snippets.

@islishude
Last active October 9, 2019 00:29
Show Gist options
  • Save islishude/dced97b4c167595d927cb4abb4da3937 to your computer and use it in GitHub Desktop.
Save islishude/dced97b4c167595d927cb4abb4da3937 to your computer and use it in GitHub Desktop.
r,s,v for ethereum transaction
// ref: https://github.com/ethereumjs/ethereumjs-util/blob/master/src/signature.ts#L15
const secp256k1 = require('secp256k1')
interface ISignature {
signature: Buffer,
recovery: number,
}
export const ecsign = function(
msgHash: Buffer,
privateKey: Buffer,
chainId: number,
) {
const sig: ISignature = secp256k1.sign(msgHash, privateKey)
const ret = {
r: sig.signature.slice(0, 32), // buffer
s: sig.signature.slice(32, 64), // buffer
v: sig.recovery + (chainId * 2 + 35), // number
}
return ret
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment