electrum.post.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* Import modules. */
  2. import { sha256 } from '@nexajs/crypto'
  3. import {
  4. binToHex,
  5. hexToBin,
  6. } from '@nexajs/utils'
  7. import BCHJS from '@psf/bch-js'
  8. import { ElectrumCluster } from 'electrum-cash'
  9. /* Initialize BCHJS. */
  10. const bchjs = new BCHJS()
  11. /* Initialize globals. */
  12. let electrum
  13. ;(async () => {
  14. // Initialize an electrum cluster where 2 out of 3 needs to be consistent,
  15. // polled randomly with fail-over (default).
  16. electrum = new ElectrumCluster('Electrum cluster example', '1.4.1', 2, 3)
  17. // Add some servers to the cluster.
  18. electrum.addServer('bch.imaginary.cash')
  19. electrum.addServer('electroncash.de')
  20. electrum.addServer('electroncash.dk')
  21. electrum.addServer('electron.jochen-hoenicke.de', 51002)
  22. electrum.addServer('electrum.imaginary.cash')
  23. // Wait for enough connections to be available.
  24. await electrum.ready()
  25. })()
  26. export default defineEventHandler(async (event) => {
  27. /* Initialize locals. */
  28. let address
  29. let body
  30. let hash160
  31. let method
  32. let newParams
  33. let params
  34. let response
  35. let script
  36. let scriptHash
  37. /* Set (request) body. */
  38. body = await readBody(event)
  39. console.log('BODY', body)
  40. if (!body || !body.method || !body.params) {
  41. return `Request FAILED!`
  42. }
  43. /* Set method. */
  44. method = body.method
  45. /* Set params. */
  46. params = body.params
  47. let collection = []
  48. /* Handle method. */
  49. switch(method) {
  50. case 'blockchain.transaction.get':
  51. if (Array.isArray(params)) {
  52. for (let i = 0; i < params.length; i++) {
  53. response = await electrum.request(method, params[i])
  54. collection.push(response)
  55. }
  56. } else {
  57. // Request the full transaction hex for the transaction ID.
  58. response = await electrum.request(method, params)
  59. collection = [ response ]
  60. }
  61. return collection
  62. case 'blockchain.scripthash.listunspent':
  63. if (Array.isArray(params)) {
  64. for (let i = 0; i < params.length; i++) {
  65. /* Set address. */
  66. address = params[i]
  67. /* Convert to script hash. */
  68. hash160 = bchjs.Address.toHash160(params[i])
  69. console.log('HASH160', hash160)
  70. /* Set script. */
  71. // FIXME Use `OP` and TypedArray.
  72. script = `76a914${hash160}88ac`
  73. // console.log('SCRIPT', script)
  74. /* Conver to hex. */
  75. script = hexToBin(script)
  76. /* Generate script hash. */
  77. scriptHash = sha256(script)
  78. scriptHash = scriptHash.reverse()
  79. /* Set (new) params. */
  80. newParams = binToHex(scriptHash)
  81. response = await electrum.request(method, newParams)
  82. /* Add (a reference to) address. */
  83. response = {
  84. address,
  85. utxos: response,
  86. }
  87. // collection.push(response)
  88. collection = [ ...collection, response ]
  89. }
  90. } else {
  91. // Request the full transaction hex for the transaction ID.
  92. response = await electrum.request(method, params)
  93. // collection.push(response)
  94. collection = [ ...response ]
  95. }
  96. return collection
  97. case 'blockchain.scripthash.get_history':
  98. if (Array.isArray(params)) {
  99. for (let i = 0; i < params.length; i++) {
  100. /* Set address. */
  101. address = params[i]
  102. console.log('ADDRESS', address)
  103. /* Convert to script hash. */
  104. hash160 = bchjs.Address.toHash160(params[i])
  105. console.log('HASH160', hash160)
  106. /* Set script. */
  107. // FIXME Use `OP` and TypedArray.
  108. script = `76a914${hash160}88ac`
  109. // console.log('SCRIPT', script)
  110. /* Conver to hex. */
  111. script = hexToBin(script)
  112. /* Generate script hash. */
  113. scriptHash = sha256(script)
  114. scriptHash = scriptHash.reverse()
  115. /* Set (new) params. */
  116. newParams = binToHex(scriptHash)
  117. response = await electrum.request(method, newParams)
  118. /* Add (a reference to) address. */
  119. response = {
  120. address,
  121. txs: response,
  122. }
  123. // collection.push(response)
  124. collection = [ ...collection, response ]
  125. }
  126. } else {
  127. // Request the full transaction hex for the transaction ID.
  128. response = await electrum.request(method, params)
  129. // collection.push(response)
  130. collection = [ ...response ]
  131. }
  132. return collection
  133. default:
  134. setResponseStatus(event, 401)
  135. return 'Oops! Invalid request!'
  136. }
  137. })