electrum.post.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. /* Set script. */
  70. // FIXME Use `OP` and TypedArray.
  71. script = `76a914${hash160}88ac`
  72. /* Conver to hex. */
  73. script = hexToBin(script)
  74. /* Generate script hash. */
  75. scriptHash = sha256(script)
  76. scriptHash = scriptHash.reverse()
  77. /* Set (new) params. */
  78. newParams = binToHex(scriptHash)
  79. response = await electrum.request(method, newParams)
  80. /* Add (a reference to) address. */
  81. response = {
  82. address,
  83. utxos: response,
  84. }
  85. // collection.push(response)
  86. collection = [ ...collection, response ]
  87. }
  88. } else {
  89. // Request the full transaction hex for the transaction ID.
  90. response = await electrum.request(method, params)
  91. // collection.push(response)
  92. collection = [ ...response ]
  93. }
  94. return collection
  95. case 'blockchain.scripthash.get_history':
  96. if (Array.isArray(params)) {
  97. for (let i = 0; i < params.length; i++) {
  98. /* Set address. */
  99. address = params[i]
  100. /* Convert to script hash. */
  101. hash160 = bchjs.Address.toHash160(params[i])
  102. /* Set script. */
  103. // FIXME Use `OP` and TypedArray.
  104. script = `76a914${hash160}88ac`
  105. /* Conver to hex. */
  106. script = hexToBin(script)
  107. /* Generate script hash. */
  108. scriptHash = sha256(script)
  109. scriptHash = scriptHash.reverse()
  110. /* Set (new) params. */
  111. newParams = binToHex(scriptHash)
  112. response = await electrum.request(method, newParams)
  113. /* Add (a reference to) address. */
  114. response = {
  115. address,
  116. txs: response,
  117. }
  118. // collection.push(response)
  119. collection = [ ...collection, response ]
  120. }
  121. } else {
  122. // Request the full transaction hex for the transaction ID.
  123. response = await electrum.request(method, params)
  124. // collection.push(response)
  125. collection = [ ...response ]
  126. }
  127. return collection
  128. default:
  129. setResponseStatus(event, 401)
  130. return 'Oops! Invalid request!'
  131. }
  132. })