Created
September 7, 2018 23:12
-
-
Save amazingandyyy/aa14fa861a08aa8e530cea50a065ffcc to your computer and use it in GitHub Desktop.
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
const ethUtil = require("ethereumjs-util"); | |
function personalSign(privateKey, msgParams) { | |
var message = ethUtil.toBuffer(msgParams.data) | |
var msgHash = ethUtil.hashPersonalMessage(message) | |
var sig = ethUtil.ecsign(msgHash, privateKey) | |
var serialized = ethUtil.bufferToHex(this.concatSig(sig.v, sig.r, sig.s)) | |
return serialized | |
} | |
function concatSig(v, r, s) { | |
const rSig = ethUtil.fromSigned(r) | |
const sSig = ethUtil.fromSigned(s) | |
const vSig = ethUtil.bufferToInt(v) | |
const rStr = padWithZeroes(ethUtil.toUnsigned(rSig).toString('hex'), 64) | |
const sStr = padWithZeroes(ethUtil.toUnsigned(sSig).toString('hex'), 64) | |
const vStr = ethUtil.stripHexPrefix(ethUtil.intToHex(vSig)); | |
return ethUtil.addHexPrefix(rStr.concat(sStr, vStr)).toString('hex') | |
} | |
function padWithZeroes (number, length) { | |
var myString = '' + number | |
while (myString.length < length) { | |
myString = '0' + myString | |
} | |
return myString | |
} | |
const privKeyHex = '4af1bceebf7f3634ec3cff8a2c38e51178d5d4ce585c52d6043e5e2cc3418bb0' | |
const privKey = new Buffer(privKeyHex, 'hex') | |
const sig = personalSign(privKey, { data: "terms" }) | |
console.log(`signature: ${sig}`); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/ethereumjs/ethereumjs-util/blob/master/index.js