Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const ethUtil = require('ethereumjs-util')
- const createHash = require('create-hash')
- /**
- * Create a commitment hash with the amount of ENA coins to give to the user at the given dest address.
- */
- const generateHash = (amount, dest, nonce, userId, privateKey) => {
- const privkeyBuffer = new Buffer(privateKey, 'hex')
- // generate a hash of all the input parameters
- const data = createHash('sha256')
- .update(numberToUint256(amount))
- .update(ethUtil.toBuffer(dest))
- .update(numberToUint256(nonce))
- .update(numberToUint256(userId))
- .digest()
- // sign the hash with the server's private key
- const hash = ethUtil.ecsign(data, privkeyBuffer)
- const publicKey = ethUtil.ecrecover(data, hash.v, hash.r, hash.s)
- return {
- publicKey: bufferToHex(ethUtil.pubToAddress(publicKey)),
- data: bufferToHex(data),
- hash: {
- r: bufferToHex(hash.r),
- s: bufferToHex(hash.s),
- v: hash.v
- }
- }
- }
- const numberToUint256 = num => {
- const n = new Buffer(32)
- n.writeInt32LE(num)
- return n.reverse()
- }
- const bufferToHex = buffer => `0x${buffer.toString('hex')}`
Add Comment
Please, Sign In to add comment