crypto.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import crypto from 'crypto'
  2. import { BN, toBN } from 'web3-utils'
  3. import { pedersen } from '@/services'
  4. const CUT_LENGTH = 31
  5. export function parseNote(note) {
  6. const [, currency, amount, netId, hexNote] = note.split('-')
  7. return {
  8. ...parseHexNote(hexNote),
  9. netId,
  10. amount,
  11. currency
  12. }
  13. }
  14. export function parseHexNote(hexNote) {
  15. const buffNote = Buffer.from(hexNote.slice(2), 'hex')
  16. const commitment = buffPedersenHash(buffNote)
  17. const nullifierBuff = buffNote.slice(0, CUT_LENGTH)
  18. const nullifierHash = BigInt(buffPedersenHash(nullifierBuff))
  19. const nullifier = BigInt(leInt2Buff(buffNote.slice(0, CUT_LENGTH)))
  20. const secret = BigInt(leInt2Buff(buffNote.slice(CUT_LENGTH, CUT_LENGTH * 2)))
  21. return {
  22. secret,
  23. nullifier,
  24. commitment,
  25. nullifierBuff,
  26. nullifierHash,
  27. commitmentHex: toFixedHex(commitment),
  28. nullifierHex: toFixedHex(nullifierHash)
  29. }
  30. }
  31. export function leInt2Buff(value) {
  32. return new BN(value, 16, 'le')
  33. }
  34. export function randomBN(nbytes = 31) {
  35. return toBN(leInt2Buff(crypto.randomBytes(nbytes)).toString())
  36. }
  37. export function buffPedersenHash(buffer) {
  38. const [hash] = pedersen.unpackPoint(buffer)
  39. return pedersen.toStringBuffer(hash)
  40. }
  41. export function toFixedHex(value, length = 32) {
  42. const isBuffer = value instanceof Buffer
  43. const str = isBuffer ? value.toString('hex') : BigInt(value).toString(16)
  44. return '0x' + str.padStart(length * 2, '0')
  45. }
  46. export const isEmptyArray = (arr) => !Array.isArray(arr) || !arr.length
  47. export function packEncryptedMessage(encryptedMessage) {
  48. const nonceBuf = Buffer.from(encryptedMessage.nonce, 'base64')
  49. const ephemPublicKeyBuf = Buffer.from(encryptedMessage.ephemPublicKey, 'base64')
  50. const ciphertextBuf = Buffer.from(encryptedMessage.ciphertext, 'base64')
  51. const messageBuff = Buffer.concat([
  52. Buffer.alloc(24 - nonceBuf.length),
  53. nonceBuf,
  54. Buffer.alloc(32 - ephemPublicKeyBuf.length),
  55. ephemPublicKeyBuf,
  56. ciphertextBuf
  57. ])
  58. return '0x' + messageBuff.toString('hex')
  59. }
  60. export function unpackEncryptedMessage(encryptedMessage) {
  61. if (encryptedMessage.slice(0, 2) === '0x') {
  62. encryptedMessage = encryptedMessage.slice(2)
  63. }
  64. const messageBuff = Buffer.from(encryptedMessage, 'hex')
  65. const nonceBuf = messageBuff.slice(0, 24)
  66. const ephemPublicKeyBuf = messageBuff.slice(24, 56)
  67. const ciphertextBuf = messageBuff.slice(56)
  68. return {
  69. version: 'x25519-xsalsa20-poly1305',
  70. nonce: nonceBuf.toString('base64'),
  71. ephemPublicKey: ephemPublicKeyBuf.toString('base64'),
  72. ciphertext: ciphertextBuf.toString('base64')
  73. }
  74. }
  75. export function checkCommitments(events = []) {
  76. events.forEach(({ leafIndex }, i) => {
  77. // TODO reload events, need for if infura provider missing events
  78. if (leafIndex !== i) {
  79. console.error(`Missing deposit event for deposit #${i}`)
  80. throw new Error(window.$nuxt.$t('failedToFetchAllDepositEvents'))
  81. }
  82. })
  83. }