Last active
October 9, 2019 00:29
-
-
Save islishude/dced97b4c167595d927cb4abb4da3937 to your computer and use it in GitHub Desktop.
r,s,v for ethereum transaction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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