play.post.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* Import modules. */
  2. import {
  3. randomBytes,
  4. ripemd160,
  5. } from '@nexajs/crypto'
  6. import { decodeAddress } from '@nexajs/address'
  7. import {
  8. sha256,
  9. sha512,
  10. } from '@nexajs/crypto'
  11. import { entropyToMnemonic } from '@nexajs/hdnode'
  12. import moment from 'moment'
  13. import PouchDB from 'pouchdb'
  14. import { subscribeAddress } from '@nexajs/rostrum'
  15. import { v4 as uuidv4 } from 'uuid'
  16. import { binToHex } from '@nexajs/utils'
  17. import { Wallet } from '@nexajs/wallet'
  18. import hiLoHandler from './play/hi_lo.ts'
  19. /* Initialize databases. */
  20. const playsDb = new PouchDB(`http://${process.env.COUCHDB_USER}:${process.env.COUCHDB_PASSWORD}@127.0.0.1:5984/plays`)
  21. const logsDb = new PouchDB(`http://${process.env.COUCHDB_USER}:${process.env.COUCHDB_PASSWORD}@127.0.0.1:5984/logs`)
  22. const sleep = ms => new Promise(r => setTimeout(r, ms))
  23. export default defineEventHandler(async (event) => {
  24. /* Set (request) body. */
  25. const body = await readBody(event)
  26. /* Set player seed. */
  27. // FIXME WE MUST PERFORM PROPER DATA SANITIZATION!!!
  28. const playerSeed = body?.seed
  29. console.log('PLAYER SEED', playerSeed)
  30. // TODO Perform data validation for seed
  31. const treasuryAddress = process.env.TREASURY_ADDRESS
  32. console.log('TREASURY ADDRESS', treasuryAddress)
  33. /* Set vault mnemonic (from env). */
  34. const vaultMnemonic = process.env.MNEMONIC
  35. console.log('MNEMONIC', vaultMnemonic)
  36. /* Initialize wallet. */
  37. const vaultWallet = new Wallet(vaultMnemonic)
  38. console.log('WALLET', vaultWallet)
  39. /* Wait for wallet to initialize. */
  40. await sleep(100)
  41. /* Request (receiving) address. */
  42. const vaultAddress = vaultWallet.address
  43. // -------------------------------------------------------------------------
  44. const entropy = binToHex(randomBytes(32))
  45. /* Calculate mnemonic. */
  46. const mnemonic = entropyToMnemonic(entropy)
  47. /* Initialize wallet. */
  48. const wallet = new Wallet(mnemonic)
  49. /* Request (receiving) address. */
  50. const address = wallet.address
  51. /* Decode address. */
  52. const decoded = decodeAddress(address)
  53. /* Parse public key hash. */
  54. const pubKeyHash = binToHex(decoded.hash).slice(8)
  55. /* Create play id. */
  56. const _id = pubKeyHash
  57. /* Start monitoring address. */
  58. const cleanup = await subscribeAddress(address, hiLoHandler.bind(playerSeed))
  59. // console.log('CLEANUP', cleanup)
  60. /* Set creation date. */
  61. const createdAt = moment().valueOf()
  62. /* Set expiration date. */
  63. const expiresAt = moment().add(15, 'minutes').valueOf()
  64. /* Calculate db play. */
  65. const dbPlay = {
  66. _id,
  67. address,
  68. entropy,
  69. ...body,
  70. createdAt,
  71. expiresAt,
  72. }
  73. // console.log('DATABASE PLAY', dbPlay)
  74. /* Update plays db. */
  75. const response = await playsDb
  76. .put(dbPlay)
  77. .catch(err => console.error(err))
  78. // console.log('PLAY (api):', response)
  79. /* Calculate (server) key hash (for client). */
  80. const keyHash = sha512(sha512(entropy))
  81. const auth = {
  82. id: _id,
  83. //
  84. //
  85. //
  86. playerSeed,
  87. keyHash,
  88. }
  89. // TODO Calculate "authorization" hash
  90. const authHash = binToHex(ripemd160(sha256(JSON.stringify(auth)), 'binary'))
  91. /* Build play package. */
  92. const playPkg = {
  93. playid: _id,
  94. address,
  95. keyHash,
  96. authHash,
  97. createdAt,
  98. expiresAt,
  99. }
  100. /* Return package. */
  101. return playPkg
  102. })