1
0

v1.post.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Import modules. */
  2. import moment from 'moment'
  3. import {
  4. decryptForPubkey,
  5. sha256,
  6. } from '@nexajs/crypto'
  7. import {
  8. binToHex,
  9. binToUtf8,
  10. } from '@nexajs/utils'
  11. import { v5 as uuidv5 } from 'uuid'
  12. /* Set Hush (UUIDv5) Namespace */
  13. // NOTE: Replace the 1st 4-bytes of the standard prefix with the
  14. // Hush Protocol ID (0x48555348).
  15. // For reference:
  16. // - uuidv5.DNS 6ba7b810-9dad-11d1-80b4-00c04fd430c8
  17. // - uuidv5.URL 6ba7b811-9dad-11d1-80b4-00c04fd430c8
  18. const HUSH_NAMESPACE = '48555348-9dad-11d1-80b4-00c04fd430c8'
  19. export default defineEventHandler(async (event) => {
  20. /* Initialize locals. */
  21. let componentid
  22. let components
  23. let profile
  24. let response
  25. let unlocked
  26. /* Set database. */
  27. const Db = event.context.Db
  28. /* Set wallet. */
  29. const Wallet = event.context.Wallet
  30. /* Set (request) body. */
  31. const body = await readBody(event)
  32. // console.log('BODY', body)
  33. if (!body) {
  34. setResponseStatus(event, 401)
  35. return `Authorization FAILED!`
  36. }
  37. /* Set profile parameters. */
  38. const authid = body.authid
  39. // console.log('AUTH ID', authid)
  40. const actionid = body.actionid
  41. // console.log('ACTION ID', actionid)
  42. /* Validate components. */
  43. if (body.components) {
  44. components = body.components
  45. components = decryptForPubkey(binToHex(Wallet.privateKey), components)
  46. components = binToUtf8(components)
  47. components = JSON.parse(components)
  48. // console.log('COMPONENTS', components)
  49. }
  50. /* Validate unlocked. */
  51. if (body.unlocked) {
  52. unlocked = body.unlocked
  53. unlocked = decryptForPubkey(binToHex(Wallet.privateKey), unlocked)
  54. unlocked = binToUtf8(unlocked)
  55. unlocked = JSON.parse(unlocked)
  56. // console.log('UNLOCKED', unlocked)
  57. }
  58. /* Validate auth ID. */
  59. if (typeof authid === 'undefined' || authid === null) {
  60. setResponseStatus(event, 401)
  61. return 'Authorization failed!'
  62. }
  63. console.log('DEBUG::INSERTING A NEW FUSION')
  64. const fusion = Db.fusions['4fa84224-be56-49ba-830a-fa3b6774eb01']
  65. console.log('FUSION', fusion)
  66. /* Request session. */
  67. profile = Db.profiles[authid]
  68. console.log('PROFILE', profile)
  69. /* Validate profile id. */
  70. if (typeof profile === 'undefined' || profile === null) {
  71. /* Build profile. */
  72. profile = {
  73. _id: authid,
  74. createdAt: moment().unix(),
  75. updatedAt: moment().unix(),
  76. }
  77. /* Request session. */
  78. response = await Db.put('profiles', authid, profile)
  79. .catch(err => console.error(err))
  80. // console.log('SAVE NEW PROFILE', response)
  81. } else {
  82. /* Build profile package. */
  83. profile.updatedAt = moment().unix()
  84. /* Request session. */
  85. response = await Db.put('profiles', authid, profile)
  86. .catch(err => console.error(err))
  87. // console.log('UPDATE PROFILE', response)
  88. }
  89. if (profile) {
  90. fusion.guests[authid] = {
  91. createdAt: profile.createdAt,
  92. updatedAt: profile.updatedAt,
  93. }
  94. } else {
  95. setResponseStatus(event, 401)
  96. return 'Oops! Authorization failed!'
  97. }
  98. /* Handle component unlocking. */
  99. if (actionid === 'unlock-components') {
  100. /* Update progress. */
  101. // fusion.progress = 75.5
  102. /* Set (new) updated at (timestamp). */
  103. fusion.updatedAt = moment().unix()
  104. console.log('***UNLOCK COMPONENT(S)***', unlocked)
  105. const inputs = fusion.inputs
  106. console.log('INPUTS', inputs)
  107. /* Handle inputs. */
  108. for (let i = 0; i < Object.keys(inputs).length; i++) {
  109. const outpoint = Object.keys(inputs)[i]
  110. console.log('UNLOCKING OUTPOINT', outpoint)
  111. const input = inputs[outpoint]
  112. console.log('UNLOCKING INPUT', input)
  113. // FIXME WE MUST VALIDATE THE OUTPOINT AUTH BEFORE UPDATING...
  114. if (unlocked && unlocked[outpoint] && unlocked[outpoint].unlocking) {
  115. /* Add unlocking script to input. */
  116. input.unlocking = unlocked[outpoint].unlocking
  117. }
  118. }
  119. console.log('UPDATED FUSION', fusion)
  120. return fusion
  121. }
  122. fusion.numGuests = Object.keys(fusion.guests).length
  123. /* Update progress. */
  124. fusion.progress = 12.5
  125. components.forEach(_component => {
  126. /* Validate inputs. */
  127. if (_component.tx_hash) {
  128. componentid = sha256(_component.tx_hash + ':' + _component.tx_pos)
  129. fusion.inputs[componentid] = {
  130. ..._component,
  131. unlocking: null,
  132. }
  133. }
  134. /* Validate outputs. */
  135. if (!_component.tx_hash) {
  136. componentid = sha256(_component.address + ':' + _component.value)
  137. fusion.outputs[componentid] = _component
  138. }
  139. })
  140. fusion.numInputs = Object.keys(fusion.inputs).length
  141. fusion.numOutputs = Object.keys(fusion.outputs).length
  142. /* Calculate session contents. */
  143. const sessionContents = JSON.stringify({ ...fusion.inputs, ...fusion.outputs })
  144. // console.log('SESSION CONTENTS', sessionContents)
  145. /* Calculate (UUIDv5) session hash. */
  146. const sessionHash = sha256(sessionContents)
  147. // console.log('SESSION HASH', sessionHash)
  148. /* Calculate session ID. */
  149. const sessionid = uuidv5(sessionHash, HUSH_NAMESPACE)
  150. // console.log('SESSION (uuid v5)', sessionid)
  151. /* Set session ID. */
  152. fusion.sessionid = sessionid
  153. /* Update progress. */
  154. fusion.progress = 75.5
  155. /* Set (new) updated at (timestamp). */
  156. fusion.updatedAt = moment().unix()
  157. await Db.put('fusions', '4fa84224-be56-49ba-830a-fa3b6774eb01', fusion)
  158. /* Build (response) package. */
  159. const pkg = {
  160. id: profile._id,
  161. components,
  162. createdAt: profile.createdAt,
  163. updatedAt: profile.updatedAt,
  164. }
  165. // console.log('PKG', pkg)
  166. /* Return pkg. */
  167. return pkg
  168. })