Guest User

Untitled

a guest
Nov 18th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. const ethUtil = require('ethereumjs-util')
  2. const createHash = require('create-hash')
  3.  
  4.  
  5. /**
  6. * Create a commitment hash with the amount of ENA coins to give to the user at the given dest address.
  7. */
  8. const generateHash = (amount, dest, nonce, userId, privateKey) => {
  9. const privkeyBuffer = new Buffer(privateKey, 'hex')
  10.  
  11. // generate a hash of all the input parameters
  12. const data = createHash('sha256')
  13. .update(numberToUint256(amount))
  14. .update(ethUtil.toBuffer(dest))
  15. .update(numberToUint256(nonce))
  16. .update(numberToUint256(userId))
  17. .digest()
  18.  
  19. // sign the hash with the server's private key
  20. const hash = ethUtil.ecsign(data, privkeyBuffer)
  21. const publicKey = ethUtil.ecrecover(data, hash.v, hash.r, hash.s)
  22.  
  23. return {
  24. publicKey: bufferToHex(ethUtil.pubToAddress(publicKey)),
  25. data: bufferToHex(data),
  26. hash: {
  27. r: bufferToHex(hash.r),
  28. s: bufferToHex(hash.s),
  29. v: hash.v
  30. }
  31. }
  32. }
  33.  
  34.  
  35. const numberToUint256 = num => {
  36. const n = new Buffer(32)
  37. n.writeInt32LE(num)
  38. return n.reverse()
  39. }
  40.  
  41.  
  42. const bufferToHex = buffer => `0x${buffer.toString('hex')}`
Add Comment
Please, Sign In to add comment