generate-hashes.mjs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env node
  2. import crypto from 'crypto'
  3. import fs from 'fs'
  4. import moment from 'moment'
  5. import numeral from 'numeral'
  6. import PouchDB from 'pouchdb'
  7. import { v4 as uuidv4 } from 'uuid'
  8. import { randomBytes } from '@nexajs/crypto'
  9. import { dirname } from 'path'
  10. import { fileURLToPath } from 'url'
  11. import path from 'path'
  12. const __dirname = dirname(fileURLToPath(import.meta.url))
  13. /* Initialize databases. */
  14. const hashesDb = new PouchDB(`http://${process.env.COUCHDB_USER}:${process.env.COUCHDB_PASSWORD}@127.0.0.1:5984/hashes`)
  15. const logsDb = new PouchDB(`http://${process.env.COUCHDB_USER}:${process.env.COUCHDB_PASSWORD}@127.0.0.1:5984/logs`)
  16. console.log('\n Generating random hashes...')
  17. const epoch = 1
  18. const numHashes = 100
  19. const timestamp = moment().utc().format('LLLL') + ' UTC'
  20. const header = `Nexa Games Hashes
  21. Generated: ${timestamp}
  22. Hash count: ${numeral(numHashes).format('0,0')}
  23. Epoch: ${epoch}
  24. `
  25. const privateName = `000${epoch}_private.txt`
  26. const privatePath = path.join(__dirname, '..', 'secrets', privateName)
  27. fs.writeFileSync(privatePath, header)
  28. console.log('\n Write (header) completed successfully!')
  29. const publicName = `000${epoch}_public.txt`
  30. const publicPath = path.join(__dirname, '..', 'web', 'public', 'hashes', publicName)
  31. fs.writeFileSync(publicPath, header)
  32. console.log('\n Write (header) completed successfully!')
  33. for (let i = 0; i < numHashes; i++) {
  34. const entropy = randomBytes(16)
  35. // console.log('ENTROPY', entropy)
  36. const v4options = {
  37. random: entropy,
  38. }
  39. // console.log('V4OPTIONS', v4options)
  40. /* Generate secret. */
  41. const secret = uuidv4()
  42. const idx = String(i).padStart(4, '0')
  43. /* Generate hash. */
  44. const hash = crypto
  45. .createHash('sha512')
  46. .update(secret)
  47. .digest('hex')
  48. /* Create secret line. */
  49. const privateLine = `${idx}:${secret}:${hash}`
  50. /* Append to public file. */
  51. fs.appendFileSync(privatePath, '\n' + privateLine)
  52. // console.log('\n Write (hashes) completed successfully!')
  53. /* Create public line. */
  54. const publicLine = `${idx}:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx:${hash}`
  55. /* Append to public file. */
  56. fs.appendFileSync(publicPath, '\n' + publicLine)
  57. // console.log('\n Write (hashes) completed successfully!')
  58. const response = hashesDb.put({
  59. _id: hash,
  60. epoch,
  61. secret,
  62. gameid: null,
  63. playid: null,
  64. createdAt: moment().unix(),
  65. })
  66. console.log('RESPONSE', response)
  67. }
  68. console.log('\n Write (hashes) completed successfully!')
  69. console.log()