signFusion.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* Import modules. */
  2. import { encryptForPubkey } from '@nexajs/crypto'
  3. import { binToHex } from '@nexajs/utils'
  4. import signSharedTx from '../../handlers/signSharedTx.ts'
  5. const DUST_VAL = 546
  6. export default async function () {
  7. /* Initialize locals. */
  8. let blindComponents
  9. let clubWallet
  10. let inputs
  11. let keys
  12. let outputs
  13. let publicKey
  14. // let rawTx
  15. let response
  16. let session
  17. let sessionid
  18. let transaction
  19. // FIXME Where do we get the session ID from??
  20. sessionid = '4e9654f9-3de9-4f9a-8169-3834f40847f5'
  21. /* Request session details. */
  22. session = await $fetch(`http://localhost:39159/v1/fusion/${sessionid}`)
  23. .catch(err => console.error(err))
  24. console.log('SESSION', session)
  25. /* Set inputs. */
  26. inputs = session.inputs
  27. // console.log('INPUTS', inputs)
  28. /* Initialize keys. */
  29. keys = []
  30. /* Handle (input) keys. */
  31. Object.keys(inputs).forEach(_inputid => {
  32. keys.push(_inputid)
  33. })
  34. // console.log('KEYS', keys)
  35. /* Sort (input) keys. */
  36. keys.sort()
  37. // console.log('KEYS (sorted)', keys)
  38. /* Initialize sorted inputs. */
  39. const sortedInputs = []
  40. /* Handle (input) keys. */
  41. keys.forEach(_keyid => {
  42. /* Set input. */
  43. const input = inputs[_keyid]
  44. console.log('HANDLE INPUT', input)
  45. let addressIdx = 0
  46. /* Find address index for input. */
  47. Object.keys(this.fusionInputs).forEach(_outpoint => {
  48. /* Set fusion input. */
  49. const fusionInput = this.fusionInputs[_outpoint]
  50. console.log('FUSION INPUT', fusionInput)
  51. if (fusionInput.address === input.address) {
  52. input.address_idx = addressIdx
  53. console.log('ADDED ADDRESS INDEX', addressIdx)
  54. // break
  55. }
  56. addressIdx++
  57. })
  58. /* Add input. */
  59. sortedInputs.push(input)
  60. })
  61. // console.log('INPUTS (sorted)', sortedInputs)
  62. outputs = session.outputs
  63. // console.log('OUTPUTS', outputs)
  64. /* Initialize (output) keys. */
  65. keys = []
  66. /* Handle keys. */
  67. Object.keys(outputs).forEach(_outputid => {
  68. keys.push(_outputid)
  69. })
  70. // console.log('KEYS', keys)
  71. /* Sort (output) keys. */
  72. keys.sort()
  73. // console.log('KEYS (sorted)', keys)
  74. /* Initialize sorted outputs. */
  75. const sortedOutputs = []
  76. /* Handle (output) keys. */
  77. keys.forEach(_keyid => {
  78. if (outputs[_keyid].value >= DUST_VAL) {
  79. /* Add input. */
  80. sortedOutputs.push(outputs[_keyid])
  81. }
  82. })
  83. // console.log('OUTPUTS (sorted)', sortedOutputs)
  84. /* Sign shared transaction. */
  85. transaction = signSharedTx.bind(this)(
  86. sessionid, this.mnemonic, sortedInputs, sortedOutputs)
  87. console.log('SIGNED TRANSACTION', transaction)
  88. // txObj.ins[1].script = txObj2.ins[1].script
  89. /* Initailize unlocked. */
  90. let unlocked = {}
  91. transaction.ins.forEach(_input => {
  92. /* Validate (unlocked) script. */
  93. if (_input.script) {
  94. /* Add input. */
  95. unlocked[binToHex(_input.hash)] = {
  96. tx_hash: binToHex(_input.hash.reverse()),
  97. unlocking: binToHex(_input.script),
  98. }
  99. }
  100. })
  101. console.log('UNLOCKED', unlocked)
  102. /* Prepare unlocked (inputs) for encryption. */
  103. unlocked = JSON.stringify(unlocked)
  104. // console.log('FUSION (unlocked)', unlocked)
  105. // TODO Handle any filtering required BEFORE submitting for fusion.
  106. clubWallet = await $fetch('/api/wallet')
  107. .catch(err => console.error(err))
  108. // console.log('CLUB WALLET', clubWallet)
  109. // FIXME Retrieve public key from a "public" endpoint.
  110. publicKey = clubWallet.publicKey
  111. // console.log('CLUB PUBLIC KEY', publicKey)
  112. /* Generate blind components. */
  113. blindComponents = encryptForPubkey(publicKey, unlocked)
  114. // console.log('BLINDED COMPONENTS', blindComponents)
  115. const body = {
  116. authid: binToHex(this.wallet.publicKey),
  117. actionid: 'unlock-components',
  118. // tierid,
  119. unlocked: blindComponents,
  120. }
  121. console.log('BODY', body)
  122. response = await $fetch('/v1', {
  123. method: 'POST',
  124. body,
  125. })
  126. .catch(err => console.error(err))
  127. console.log('RESPONSE', response)
  128. }