@did-btcr2/method 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (310) hide show
  1. package/LICENSE +373 -0
  2. package/README.md +7 -0
  3. package/dist/browser.js +2364 -0
  4. package/dist/browser.js.map +7 -0
  5. package/dist/browser.mjs +2364 -0
  6. package/dist/browser.mjs.map +7 -0
  7. package/dist/cjs/bitcoin/constants.js +20 -0
  8. package/dist/cjs/bitcoin/constants.js.map +1 -0
  9. package/dist/cjs/bitcoin/errors.js +11 -0
  10. package/dist/cjs/bitcoin/errors.js.map +1 -0
  11. package/dist/cjs/bitcoin/index.js +95 -0
  12. package/dist/cjs/bitcoin/index.js.map +1 -0
  13. package/dist/cjs/bitcoin/interface.js +2 -0
  14. package/dist/cjs/bitcoin/interface.js.map +1 -0
  15. package/dist/cjs/bitcoin/network.js +17 -0
  16. package/dist/cjs/bitcoin/network.js.map +1 -0
  17. package/dist/cjs/bitcoin/rest-client.js +289 -0
  18. package/dist/cjs/bitcoin/rest-client.js.map +1 -0
  19. package/dist/cjs/bitcoin/rpc-client.js +722 -0
  20. package/dist/cjs/bitcoin/rpc-client.js.map +1 -0
  21. package/dist/cjs/bitcoin/taproot.js +219 -0
  22. package/dist/cjs/bitcoin/taproot.js.map +1 -0
  23. package/dist/cjs/btcr2/beacon/aggregation/coordinator.js +120 -0
  24. package/dist/cjs/btcr2/beacon/aggregation/coordinator.js.map +1 -0
  25. package/dist/cjs/btcr2/beacon/aggregation/messages/advert.js +24 -0
  26. package/dist/cjs/btcr2/beacon/aggregation/messages/advert.js.map +1 -0
  27. package/dist/cjs/btcr2/beacon/aggregation/messages/base.js +37 -0
  28. package/dist/cjs/btcr2/beacon/aggregation/messages/base.js.map +1 -0
  29. package/dist/cjs/btcr2/beacon/aggregation/messages/cohort-set.js +25 -0
  30. package/dist/cjs/btcr2/beacon/aggregation/messages/cohort-set.js.map +1 -0
  31. package/dist/cjs/btcr2/beacon/aggregation/messages/keygen.js +8 -0
  32. package/dist/cjs/btcr2/beacon/aggregation/messages/keygen.js.map +1 -0
  33. package/dist/cjs/btcr2/beacon/aggregation/messages/opt-in.js +23 -0
  34. package/dist/cjs/btcr2/beacon/aggregation/messages/opt-in.js.map +1 -0
  35. package/dist/cjs/btcr2/beacon/aggregation/messages/sign.js +7 -0
  36. package/dist/cjs/btcr2/beacon/aggregation/messages/sign.js.map +1 -0
  37. package/dist/cjs/btcr2/beacon/aggregation/models/cohort/index.js +92 -0
  38. package/dist/cjs/btcr2/beacon/aggregation/models/cohort/index.js.map +1 -0
  39. package/dist/cjs/btcr2/beacon/aggregation/models/cohort/status.js +8 -0
  40. package/dist/cjs/btcr2/beacon/aggregation/models/cohort/status.js.map +1 -0
  41. package/dist/cjs/btcr2/beacon/aggregation/participant.js +2 -0
  42. package/dist/cjs/btcr2/beacon/aggregation/participant.js.map +1 -0
  43. package/dist/cjs/btcr2/beacon/aggregation/protocol/nostr.js +57 -0
  44. package/dist/cjs/btcr2/beacon/aggregation/protocol/nostr.js.map +1 -0
  45. package/dist/cjs/btcr2/beacon/aggregation/protocol/service.js +2 -0
  46. package/dist/cjs/btcr2/beacon/aggregation/protocol/service.js.map +1 -0
  47. package/dist/cjs/btcr2/beacon/cid-aggregate.js +116 -0
  48. package/dist/cjs/btcr2/beacon/cid-aggregate.js.map +1 -0
  49. package/dist/cjs/btcr2/beacon/factory.js +30 -0
  50. package/dist/cjs/btcr2/beacon/factory.js.map +1 -0
  51. package/dist/cjs/btcr2/beacon/singleton.js +220 -0
  52. package/dist/cjs/btcr2/beacon/singleton.js.map +1 -0
  53. package/dist/cjs/btcr2/beacon/smt-aggregate.js +126 -0
  54. package/dist/cjs/btcr2/beacon/smt-aggregate.js.map +1 -0
  55. package/dist/cjs/btcr2/crud/create.js +102 -0
  56. package/dist/cjs/btcr2/crud/create.js.map +1 -0
  57. package/dist/cjs/btcr2/crud/deactivate.js +14 -0
  58. package/dist/cjs/btcr2/crud/deactivate.js.map +1 -0
  59. package/dist/cjs/btcr2/crud/read.js +686 -0
  60. package/dist/cjs/btcr2/crud/read.js.map +1 -0
  61. package/dist/cjs/btcr2/crud/update.js +195 -0
  62. package/dist/cjs/btcr2/crud/update.js.map +1 -0
  63. package/dist/cjs/btcr2/key-manager/index.js +290 -0
  64. package/dist/cjs/btcr2/key-manager/index.js.map +1 -0
  65. package/dist/cjs/btcr2/key-manager/interface.js +2 -0
  66. package/dist/cjs/btcr2/key-manager/interface.js.map +1 -0
  67. package/dist/cjs/did-btcr2.js +222 -0
  68. package/dist/cjs/did-btcr2.js.map +1 -0
  69. package/dist/cjs/index.js +27 -0
  70. package/dist/cjs/index.js.map +1 -0
  71. package/dist/cjs/interfaces/beacon.js +41 -0
  72. package/dist/cjs/interfaces/beacon.js.map +1 -0
  73. package/dist/cjs/interfaces/crud.js +2 -0
  74. package/dist/cjs/interfaces/crud.js.map +1 -0
  75. package/dist/cjs/interfaces/ibeacon.js +2 -0
  76. package/dist/cjs/interfaces/ibeacon.js.map +1 -0
  77. package/dist/cjs/package.json +1 -0
  78. package/dist/cjs/types/bitcoin.js +62 -0
  79. package/dist/cjs/types/bitcoin.js.map +1 -0
  80. package/dist/cjs/types/crud.js +2 -0
  81. package/dist/cjs/types/crud.js.map +1 -0
  82. package/dist/cjs/utils/appendix.js +221 -0
  83. package/dist/cjs/utils/appendix.js.map +1 -0
  84. package/dist/cjs/utils/beacons.js +206 -0
  85. package/dist/cjs/utils/beacons.js.map +1 -0
  86. package/dist/cjs/utils/did-document-builder.js +61 -0
  87. package/dist/cjs/utils/did-document-builder.js.map +1 -0
  88. package/dist/cjs/utils/did-document.js +380 -0
  89. package/dist/cjs/utils/did-document.js.map +1 -0
  90. package/dist/cjs/utils/general.js +195 -0
  91. package/dist/cjs/utils/general.js.map +1 -0
  92. package/dist/cjs/utils/identifier.js +238 -0
  93. package/dist/cjs/utils/identifier.js.map +1 -0
  94. package/dist/esm/bitcoin/constants.js +20 -0
  95. package/dist/esm/bitcoin/constants.js.map +1 -0
  96. package/dist/esm/bitcoin/errors.js +11 -0
  97. package/dist/esm/bitcoin/errors.js.map +1 -0
  98. package/dist/esm/bitcoin/index.js +95 -0
  99. package/dist/esm/bitcoin/index.js.map +1 -0
  100. package/dist/esm/bitcoin/interface.js +2 -0
  101. package/dist/esm/bitcoin/interface.js.map +1 -0
  102. package/dist/esm/bitcoin/network.js +17 -0
  103. package/dist/esm/bitcoin/network.js.map +1 -0
  104. package/dist/esm/bitcoin/rest-client.js +289 -0
  105. package/dist/esm/bitcoin/rest-client.js.map +1 -0
  106. package/dist/esm/bitcoin/rpc-client.js +722 -0
  107. package/dist/esm/bitcoin/rpc-client.js.map +1 -0
  108. package/dist/esm/bitcoin/taproot.js +219 -0
  109. package/dist/esm/bitcoin/taproot.js.map +1 -0
  110. package/dist/esm/btcr2/beacon/aggregation/coordinator.js +120 -0
  111. package/dist/esm/btcr2/beacon/aggregation/coordinator.js.map +1 -0
  112. package/dist/esm/btcr2/beacon/aggregation/messages/advert.js +24 -0
  113. package/dist/esm/btcr2/beacon/aggregation/messages/advert.js.map +1 -0
  114. package/dist/esm/btcr2/beacon/aggregation/messages/base.js +37 -0
  115. package/dist/esm/btcr2/beacon/aggregation/messages/base.js.map +1 -0
  116. package/dist/esm/btcr2/beacon/aggregation/messages/cohort-set.js +25 -0
  117. package/dist/esm/btcr2/beacon/aggregation/messages/cohort-set.js.map +1 -0
  118. package/dist/esm/btcr2/beacon/aggregation/messages/keygen.js +8 -0
  119. package/dist/esm/btcr2/beacon/aggregation/messages/keygen.js.map +1 -0
  120. package/dist/esm/btcr2/beacon/aggregation/messages/opt-in.js +23 -0
  121. package/dist/esm/btcr2/beacon/aggregation/messages/opt-in.js.map +1 -0
  122. package/dist/esm/btcr2/beacon/aggregation/messages/sign.js +7 -0
  123. package/dist/esm/btcr2/beacon/aggregation/messages/sign.js.map +1 -0
  124. package/dist/esm/btcr2/beacon/aggregation/models/cohort/index.js +92 -0
  125. package/dist/esm/btcr2/beacon/aggregation/models/cohort/index.js.map +1 -0
  126. package/dist/esm/btcr2/beacon/aggregation/models/cohort/status.js +8 -0
  127. package/dist/esm/btcr2/beacon/aggregation/models/cohort/status.js.map +1 -0
  128. package/dist/esm/btcr2/beacon/aggregation/participant.js +2 -0
  129. package/dist/esm/btcr2/beacon/aggregation/participant.js.map +1 -0
  130. package/dist/esm/btcr2/beacon/aggregation/protocol/nostr.js +57 -0
  131. package/dist/esm/btcr2/beacon/aggregation/protocol/nostr.js.map +1 -0
  132. package/dist/esm/btcr2/beacon/aggregation/protocol/service.js +2 -0
  133. package/dist/esm/btcr2/beacon/aggregation/protocol/service.js.map +1 -0
  134. package/dist/esm/btcr2/beacon/cid-aggregate.js +116 -0
  135. package/dist/esm/btcr2/beacon/cid-aggregate.js.map +1 -0
  136. package/dist/esm/btcr2/beacon/factory.js +30 -0
  137. package/dist/esm/btcr2/beacon/factory.js.map +1 -0
  138. package/dist/esm/btcr2/beacon/singleton.js +220 -0
  139. package/dist/esm/btcr2/beacon/singleton.js.map +1 -0
  140. package/dist/esm/btcr2/beacon/smt-aggregate.js +126 -0
  141. package/dist/esm/btcr2/beacon/smt-aggregate.js.map +1 -0
  142. package/dist/esm/btcr2/crud/create.js +102 -0
  143. package/dist/esm/btcr2/crud/create.js.map +1 -0
  144. package/dist/esm/btcr2/crud/deactivate.js +14 -0
  145. package/dist/esm/btcr2/crud/deactivate.js.map +1 -0
  146. package/dist/esm/btcr2/crud/read.js +686 -0
  147. package/dist/esm/btcr2/crud/read.js.map +1 -0
  148. package/dist/esm/btcr2/crud/update.js +195 -0
  149. package/dist/esm/btcr2/crud/update.js.map +1 -0
  150. package/dist/esm/btcr2/key-manager/index.js +290 -0
  151. package/dist/esm/btcr2/key-manager/index.js.map +1 -0
  152. package/dist/esm/btcr2/key-manager/interface.js +2 -0
  153. package/dist/esm/btcr2/key-manager/interface.js.map +1 -0
  154. package/dist/esm/did-btcr2.js +222 -0
  155. package/dist/esm/did-btcr2.js.map +1 -0
  156. package/dist/esm/index.js +27 -0
  157. package/dist/esm/index.js.map +1 -0
  158. package/dist/esm/interfaces/beacon.js +41 -0
  159. package/dist/esm/interfaces/beacon.js.map +1 -0
  160. package/dist/esm/interfaces/crud.js +2 -0
  161. package/dist/esm/interfaces/crud.js.map +1 -0
  162. package/dist/esm/interfaces/ibeacon.js +2 -0
  163. package/dist/esm/interfaces/ibeacon.js.map +1 -0
  164. package/dist/esm/types/bitcoin.js +62 -0
  165. package/dist/esm/types/bitcoin.js.map +1 -0
  166. package/dist/esm/types/crud.js +2 -0
  167. package/dist/esm/types/crud.js.map +1 -0
  168. package/dist/esm/utils/appendix.js +221 -0
  169. package/dist/esm/utils/appendix.js.map +1 -0
  170. package/dist/esm/utils/beacons.js +206 -0
  171. package/dist/esm/utils/beacons.js.map +1 -0
  172. package/dist/esm/utils/did-document-builder.js +61 -0
  173. package/dist/esm/utils/did-document-builder.js.map +1 -0
  174. package/dist/esm/utils/did-document.js +380 -0
  175. package/dist/esm/utils/did-document.js.map +1 -0
  176. package/dist/esm/utils/general.js +195 -0
  177. package/dist/esm/utils/general.js.map +1 -0
  178. package/dist/esm/utils/identifier.js +238 -0
  179. package/dist/esm/utils/identifier.js.map +1 -0
  180. package/dist/types/bitcoin/constants.d.ts +19 -0
  181. package/dist/types/bitcoin/constants.d.ts.map +1 -0
  182. package/dist/types/bitcoin/errors.d.ts +5 -0
  183. package/dist/types/bitcoin/errors.d.ts.map +1 -0
  184. package/dist/types/bitcoin/index.d.ts +75 -0
  185. package/dist/types/bitcoin/index.d.ts.map +1 -0
  186. package/dist/types/bitcoin/interface.d.ts +86 -0
  187. package/dist/types/bitcoin/interface.d.ts.map +1 -0
  188. package/dist/types/bitcoin/network.d.ts +2 -0
  189. package/dist/types/bitcoin/network.d.ts.map +1 -0
  190. package/dist/types/bitcoin/rest-client.d.ts +268 -0
  191. package/dist/types/bitcoin/rest-client.d.ts.map +1 -0
  192. package/dist/types/bitcoin/rpc-client.d.ts +506 -0
  193. package/dist/types/bitcoin/rpc-client.d.ts.map +1 -0
  194. package/dist/types/bitcoin/taproot.d.ts +34 -0
  195. package/dist/types/bitcoin/taproot.d.ts.map +1 -0
  196. package/dist/types/btcr2/beacon/aggregation/coordinator.d.ts +74 -0
  197. package/dist/types/btcr2/beacon/aggregation/coordinator.d.ts.map +1 -0
  198. package/dist/types/btcr2/beacon/aggregation/messages/advert.d.ts +22 -0
  199. package/dist/types/btcr2/beacon/aggregation/messages/advert.d.ts.map +1 -0
  200. package/dist/types/btcr2/beacon/aggregation/messages/base.d.ts +36 -0
  201. package/dist/types/btcr2/beacon/aggregation/messages/base.d.ts.map +1 -0
  202. package/dist/types/btcr2/beacon/aggregation/messages/cohort-set.d.ts +23 -0
  203. package/dist/types/btcr2/beacon/aggregation/messages/cohort-set.d.ts.map +1 -0
  204. package/dist/types/btcr2/beacon/aggregation/messages/keygen.d.ts +6 -0
  205. package/dist/types/btcr2/beacon/aggregation/messages/keygen.d.ts.map +1 -0
  206. package/dist/types/btcr2/beacon/aggregation/messages/opt-in.d.ts +22 -0
  207. package/dist/types/btcr2/beacon/aggregation/messages/opt-in.d.ts.map +1 -0
  208. package/dist/types/btcr2/beacon/aggregation/messages/sign.d.ts +5 -0
  209. package/dist/types/btcr2/beacon/aggregation/messages/sign.d.ts.map +1 -0
  210. package/dist/types/btcr2/beacon/aggregation/models/cohort/index.d.ts +77 -0
  211. package/dist/types/btcr2/beacon/aggregation/models/cohort/index.d.ts.map +1 -0
  212. package/dist/types/btcr2/beacon/aggregation/models/cohort/status.d.ts +7 -0
  213. package/dist/types/btcr2/beacon/aggregation/models/cohort/status.d.ts.map +1 -0
  214. package/dist/types/btcr2/beacon/aggregation/participant.d.ts +1 -0
  215. package/dist/types/btcr2/beacon/aggregation/participant.d.ts.map +1 -0
  216. package/dist/types/btcr2/beacon/aggregation/protocol/nostr.d.ts +36 -0
  217. package/dist/types/btcr2/beacon/aggregation/protocol/nostr.d.ts.map +1 -0
  218. package/dist/types/btcr2/beacon/aggregation/protocol/service.d.ts +6 -0
  219. package/dist/types/btcr2/beacon/aggregation/protocol/service.d.ts.map +1 -0
  220. package/dist/types/btcr2/beacon/cid-aggregate.d.ts +103 -0
  221. package/dist/types/btcr2/beacon/cid-aggregate.d.ts.map +1 -0
  222. package/dist/types/btcr2/beacon/factory.d.ts +17 -0
  223. package/dist/types/btcr2/beacon/factory.d.ts.map +1 -0
  224. package/dist/types/btcr2/beacon/singleton.d.ts +93 -0
  225. package/dist/types/btcr2/beacon/singleton.d.ts.map +1 -0
  226. package/dist/types/btcr2/beacon/smt-aggregate.d.ts +112 -0
  227. package/dist/types/btcr2/beacon/smt-aggregate.d.ts.map +1 -0
  228. package/dist/types/btcr2/crud/create.d.ts +92 -0
  229. package/dist/types/btcr2/crud/create.d.ts.map +1 -0
  230. package/dist/types/btcr2/crud/deactivate.d.ts +13 -0
  231. package/dist/types/btcr2/crud/deactivate.d.ts.map +1 -0
  232. package/dist/types/btcr2/crud/read.d.ts +341 -0
  233. package/dist/types/btcr2/crud/read.d.ts.map +1 -0
  234. package/dist/types/btcr2/crud/update.d.ts +83 -0
  235. package/dist/types/btcr2/crud/update.d.ts.map +1 -0
  236. package/dist/types/btcr2/key-manager/index.d.ts +145 -0
  237. package/dist/types/btcr2/key-manager/index.d.ts.map +1 -0
  238. package/dist/types/btcr2/key-manager/interface.d.ts +113 -0
  239. package/dist/types/btcr2/key-manager/interface.d.ts.map +1 -0
  240. package/dist/types/did-btcr2.d.ts +117 -0
  241. package/dist/types/did-btcr2.d.ts.map +1 -0
  242. package/dist/types/index.d.ts +26 -0
  243. package/dist/types/index.d.ts.map +1 -0
  244. package/dist/types/interfaces/beacon.d.ts +57 -0
  245. package/dist/types/interfaces/beacon.d.ts.map +1 -0
  246. package/dist/types/interfaces/crud.d.ts +35 -0
  247. package/dist/types/interfaces/crud.d.ts.map +1 -0
  248. package/dist/types/interfaces/ibeacon.d.ts +66 -0
  249. package/dist/types/interfaces/ibeacon.d.ts.map +1 -0
  250. package/dist/types/types/bitcoin.d.ts +827 -0
  251. package/dist/types/types/bitcoin.d.ts.map +1 -0
  252. package/dist/types/types/crud.d.ts +38 -0
  253. package/dist/types/types/crud.d.ts.map +1 -0
  254. package/dist/types/utils/appendix.d.ts +118 -0
  255. package/dist/types/utils/appendix.d.ts.map +1 -0
  256. package/dist/types/utils/beacons.d.ts +156 -0
  257. package/dist/types/utils/beacons.d.ts.map +1 -0
  258. package/dist/types/utils/did-document-builder.d.ts +13 -0
  259. package/dist/types/utils/did-document-builder.d.ts.map +1 -0
  260. package/dist/types/utils/did-document.d.ts +211 -0
  261. package/dist/types/utils/did-document.d.ts.map +1 -0
  262. package/dist/types/utils/general.d.ts +85 -0
  263. package/dist/types/utils/general.d.ts.map +1 -0
  264. package/dist/types/utils/identifier.d.ts +59 -0
  265. package/dist/types/utils/identifier.d.ts.map +1 -0
  266. package/package.json +137 -0
  267. package/src/bitcoin/constants.ts +19 -0
  268. package/src/bitcoin/errors.ts +10 -0
  269. package/src/bitcoin/index.ts +154 -0
  270. package/src/bitcoin/interface.ts +160 -0
  271. package/src/bitcoin/network.ts +17 -0
  272. package/src/bitcoin/rest-client.ts +415 -0
  273. package/src/bitcoin/rpc-client.ts +888 -0
  274. package/src/bitcoin/taproot.ts +237 -0
  275. package/src/btcr2/beacon/aggregation/coordinator.ts +135 -0
  276. package/src/btcr2/beacon/aggregation/messages/advert.ts +36 -0
  277. package/src/btcr2/beacon/aggregation/messages/base.ts +59 -0
  278. package/src/btcr2/beacon/aggregation/messages/cohort-set.ts +37 -0
  279. package/src/btcr2/beacon/aggregation/messages/keygen.ts +8 -0
  280. package/src/btcr2/beacon/aggregation/messages/opt-in.ts +35 -0
  281. package/src/btcr2/beacon/aggregation/messages/sign.ts +7 -0
  282. package/src/btcr2/beacon/aggregation/models/cohort/index.ts +112 -0
  283. package/src/btcr2/beacon/aggregation/models/cohort/status.ts +7 -0
  284. package/src/btcr2/beacon/aggregation/participant.ts +0 -0
  285. package/src/btcr2/beacon/aggregation/protocol/nostr.ts +81 -0
  286. package/src/btcr2/beacon/aggregation/protocol/service.ts +6 -0
  287. package/src/btcr2/beacon/cid-aggregate.ts +154 -0
  288. package/src/btcr2/beacon/factory.ts +36 -0
  289. package/src/btcr2/beacon/singleton.ts +257 -0
  290. package/src/btcr2/beacon/smt-aggregate.ts +136 -0
  291. package/src/btcr2/crud/create.ts +160 -0
  292. package/src/btcr2/crud/deactivate.ts +13 -0
  293. package/src/btcr2/crud/read.ts +946 -0
  294. package/src/btcr2/crud/update.ts +277 -0
  295. package/src/btcr2/key-manager/index.ts +364 -0
  296. package/src/btcr2/key-manager/interface.ts +129 -0
  297. package/src/canonicalize.d.ts +6 -0
  298. package/src/did-btcr2.ts +288 -0
  299. package/src/index.ts +34 -0
  300. package/src/interfaces/beacon.ts +68 -0
  301. package/src/interfaces/crud.ts +36 -0
  302. package/src/interfaces/ibeacon.ts +76 -0
  303. package/src/types/bitcoin.ts +1028 -0
  304. package/src/types/crud.ts +41 -0
  305. package/src/utils/appendix.ts +257 -0
  306. package/src/utils/beacons.ts +276 -0
  307. package/src/utils/did-document-builder.ts +73 -0
  308. package/src/utils/did-document.ts +474 -0
  309. package/src/utils/general.ts +204 -0
  310. package/src/utils/identifier.ts +276 -0
@@ -0,0 +1,415 @@
1
+ import { Btcr2Error, Bytes, UnixTimestamp } from '@did-btcr2/common';
2
+ import {
3
+ BlockResponse,
4
+ BlockV3,
5
+ GetBlockParams,
6
+ TxInPrevout,
7
+ } from '../types/bitcoin.js';
8
+ import { BitcoinRpcError } from './errors.js';
9
+ import { DEFAULT_REST_CLIENT_CONFIG } from './constants.js';
10
+
11
+ export type TransactionStatus = {
12
+ confirmed: boolean;
13
+ block_height: number;
14
+ block_hash: string;
15
+ block_time: UnixTimestamp;
16
+ };
17
+
18
+ export interface Vin {
19
+ txid: string;
20
+ vout: number;
21
+ prevout?: TxInPrevout;
22
+ scriptsig: string;
23
+ scriptsig_asm: string;
24
+ witness: string[];
25
+ is_coinbase: boolean;
26
+ sequence: number;
27
+ };
28
+
29
+ export interface Vout {
30
+ scriptpubkey: string;
31
+ scriptpubkey_asm: string;
32
+ scriptpubkey_type: string;
33
+ scriptpubkey_address?: string;
34
+ value: number;
35
+ }
36
+ export interface ChainStats {
37
+ funded_txo_count: number;
38
+ funded_txo_sum: number;
39
+ spent_txo_count: number;
40
+ spent_txo_sum: number;
41
+ tx_count: number;
42
+ }
43
+
44
+ export interface MempoolStats {
45
+ funded_txo_count: number;
46
+ funded_txo_sum: number;
47
+ spent_txo_count: number;
48
+ spent_txo_sum: number;
49
+ tx_count: number;
50
+ }
51
+ export interface AddressInfo {
52
+ address: string;
53
+ chain_stats: ChainStats;
54
+ mempool_stats: MempoolStats;
55
+ }
56
+ export interface RawTransactionRest {
57
+ txid: string;
58
+ version: number;
59
+ locktime: number;
60
+ vin: Array<Vin>;
61
+ vout: Array<Vout>;
62
+ size: number;
63
+ weight: number;
64
+ fee: number;
65
+ status: TransactionStatus;
66
+ }
67
+
68
+ export interface AddressUtxo {
69
+ txid: string;
70
+ vout: number;
71
+ status: TransactionStatus;
72
+ value: number;
73
+ }
74
+ export interface RestClientConfigParams {
75
+ host: string;
76
+ headers?: { [key: string]: string };
77
+ }
78
+
79
+ export class RestClientConfig {
80
+ host: string;
81
+ headers?: { [key: string]: string };
82
+ constructor({ host, headers }: RestClientConfigParams) {
83
+ this.host = host;
84
+ this.headers = headers;
85
+ }
86
+ }
87
+
88
+ export interface RestApiCallParams {
89
+ path: string;
90
+ url?: string;
91
+ method?: string;
92
+ body?: any;
93
+ headers?: any;
94
+ };
95
+
96
+ export interface RestResponse extends Response {
97
+ [key: string]: any;
98
+ }
99
+
100
+ /**
101
+ * Implements a strongly-typed BitcoinRest to connect to remote bitcoin node via REST API.
102
+ * @class BitcoinRest
103
+ * @type {BitcoinRest}
104
+ */
105
+ export default class BitcoinRest {
106
+ /**
107
+ * The encapsulated {@link RestClientConfig} object.
108
+ * @private
109
+ * @type {RestClientConfig}
110
+ */
111
+ private _config: RestClientConfig;
112
+
113
+ /**
114
+ * The api calls related to bitcoin transactions.
115
+ * @type {BitcoinTransaction}
116
+ */
117
+ public transaction: BitcoinTransaction;
118
+
119
+ /**
120
+ * The api calls related to bitcoin blocks.
121
+ * @type {BitcoinBlock}
122
+ */
123
+ public block: BitcoinBlock;
124
+
125
+ /**
126
+ * The api calls related to bitcoin addresses.
127
+ * @type {BitcoinAddress}
128
+ */
129
+ public address: BitcoinAddress;
130
+
131
+ /**
132
+ * The API call method that can be used to make requests to the REST API.
133
+ * @returns {Promise<any>} A promise resolving to the response data.
134
+ */
135
+ public api: (params: RestApiCallParams) => Promise<any> = this.call;
136
+
137
+ constructor(config: RestClientConfig){
138
+ this._config = new RestClientConfig(config);
139
+ this.api = this.call.bind(this);
140
+ this.transaction = new BitcoinTransaction(this.api);
141
+ this.block = new BitcoinBlock(this.api);
142
+ this.address = new BitcoinAddress(this.api);
143
+ }
144
+
145
+ /**
146
+ * Get the configuration object.
147
+ * @private
148
+ */
149
+ get config(): RestClientConfig {
150
+ const config = this._config;
151
+ return config;
152
+ }
153
+
154
+ /**
155
+ * Static method connects to a bitcoin node running a esplora REST API.
156
+ *
157
+ * @param {RestClientConfig} config The configuration object for the client (optional).
158
+ * @returns A new {@link BitcoinRest} instance.
159
+ * @example
160
+ * ```
161
+ * const rest = BitcoinRest.connect();
162
+ * ```
163
+ */
164
+ public static connect(config?: RestClientConfig): BitcoinRest {
165
+ return new BitcoinRest(config ?? DEFAULT_REST_CLIENT_CONFIG);
166
+ }
167
+
168
+ /**
169
+ * Make a REST API call to the configured Bitcoin node.
170
+ * @private
171
+ * @param {RestApiCallParams} params The parameters for the API call. See {@link RestApiCallParams} for details.
172
+ * @param {string} [params.path] The path to the API endpoint (required).
173
+ * @param {string} [params.url] The full URL to the API endpoint (optional).
174
+ * @param {string} [params.method] The HTTP method to use (default is 'GET').
175
+ * @param {any} [params.body] The body of the request (optional).
176
+ * @param {any} [params.headers] Additional headers to include in the request (optional).
177
+ * @returns {Promise<any>} A promise resolving to the response data.
178
+ */
179
+ private async call({ path, url, method, body, headers }: RestApiCallParams): Promise<any> {
180
+ // Construct the URL if not provided
181
+ url ??= `${this.config.host.replaceEnd('/')}${path}`;
182
+
183
+ // Set the method to GET if not provided
184
+ method ??= 'GET';
185
+
186
+ // Construct the request options
187
+ const requestInit = {
188
+ method,
189
+ headers : headers ?? {
190
+ 'Content-Type' : 'application/json',
191
+ ...this.config.headers,
192
+ }
193
+ } as any;
194
+
195
+ // If the method is POST or PUT, add the body to the request
196
+ if(body) {
197
+ requestInit.body = JSON.is(body) ? JSON.stringify(body) : body;
198
+ requestInit.method = 'POST';
199
+ }
200
+
201
+ // Make the request
202
+ const response = await fetch(url, requestInit) as RestResponse;
203
+
204
+ // Check if the response is a text/plain response
205
+ const data = (response.headers.get('Content-Type') ?? 'json') === 'text/plain'
206
+ ? await response.text()
207
+ : await response.json();
208
+
209
+ // Check if the response is ok (status in the range 200-299)
210
+ if (!response.ok) {
211
+ throw new Btcr2Error(
212
+ `Request to ${url} failed: ${response.status} - ${response.statusText}`,
213
+ 'FAILED_HTTP_REQUEST',
214
+ { data, response }
215
+ );
216
+ }
217
+
218
+ return data;
219
+ }
220
+ }
221
+
222
+ export class BitcoinTransaction {
223
+ private api: (params: RestApiCallParams) => Promise<any>;
224
+
225
+ constructor(api: (params: RestApiCallParams) => Promise<any>) {
226
+ this.api = api;
227
+ }
228
+
229
+ /**
230
+ * Returns the transaction in JSON format.
231
+ * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxid | Esplora GET /tx/:txid } for details.
232
+ * @param {string} txid The transaction id (required).
233
+ * @returns {GetRawTransaction} A promise resolving to data about a transaction in the form specified by verbosity.
234
+ */
235
+ public async get(txid: string): Promise<RawTransactionRest> {
236
+ return await this.api({ path: `/tx/${txid}` });
237
+ }
238
+
239
+ /**
240
+ * Returns the transaction in JSON format.
241
+ * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxid | Esplora GET /tx/:txid } for details.
242
+ * @param {string} txid The transaction id (required).
243
+ * @returns {GetRawTransaction} A promise resolving to data about a transaction in the form specified by verbosity.
244
+ */
245
+ public async isConfirmed(txid: string): Promise<boolean> {
246
+ const tx = await this.get(txid);
247
+ return tx.status.confirmed;
248
+ }
249
+
250
+ /**
251
+ * Returns the raw transaction in hex or as binary data.
252
+ * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidhex | Esplora GET /tx/:txid/hex } and
253
+ * {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidraw | Esplora GET /tx/:txid/raw } for details.
254
+ * @param {string} txid The transaction id (required).
255
+ * @returns {Promise<RawTransactionRest | string>} A promise resolving to the raw transaction in the specified format.
256
+ */
257
+ public async getHex(txid: string): Promise<string> {
258
+ return await this.api({ path: `/tx/${txid}/hex` });
259
+ }
260
+
261
+ /**
262
+ * Returns the raw transaction in hex or as binary data.
263
+ * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidhex | Esplora GET /tx/:txid/hex } and
264
+ * {@link https://github.com/blockstream/esplora/blob/master/API.md#get-txtxidraw | Esplora GET /tx/:txid/raw } for details.
265
+ * @param {string} txid The transaction id (required).
266
+ * @returns {Promise<RawTransactionRest | string>} A promise resolving to the raw transaction in the specified format.
267
+ */
268
+ public async getRaw(txid: string): Promise<Bytes> {
269
+ return await this.api({ path: `/tx/${txid}/raw` });
270
+ }
271
+
272
+ /**
273
+ * Broadcast a raw transaction to the network. The transaction should be provided as hex in the request body. The txid
274
+ * will be returned on success.
275
+ * See {@link https://github.com/blockstream/esplora/blob/master/API.md#post-tx | Esplora POST /tx } for details.
276
+ * @param {string} tx The raw transaction in hex format (required).
277
+ * @returns {Promise<string>} The transaction id of the broadcasted transaction.
278
+ */
279
+ public async send(tx: string): Promise<string> {
280
+ return await this.api({ path: '/tx', method: 'POST', body: tx, headers: { 'Content-Type': 'text/plain' } });
281
+ }
282
+ }
283
+
284
+ /**
285
+ * Implements a strongly-typed BitcoinRest to connect to remote bitcoin node via REST API for block-related operations.
286
+ * @class BitcoinBlock
287
+ * @type {BitcoinBlock}
288
+ */
289
+ export class BitcoinBlock {
290
+ private api: (params: RestApiCallParams) => Promise<any>;
291
+
292
+ constructor(api: (params: RestApiCallParams) => Promise<any>) {
293
+ this.api = api;
294
+ }
295
+
296
+ /**
297
+ * Returns the blockheight of the most-work fully-validated chain. The genesis block has height 0.
298
+ * @returns {Blockheight} The number of the blockheight with the most-work of the fully-validated chain.
299
+ */
300
+ public async count(): Promise<number> {
301
+ return await this.api({ path: '/blocks/tip/height' });
302
+ }
303
+
304
+ /**
305
+ * Returns the block data associated with a `blockhash` of a valid block.
306
+ * @param {GetBlockParams} params See {@link GetBlockParams} for details.
307
+ * @param {?string} params.blockhash The blockhash of the block to query.
308
+ * @param {?number} params.height The block height of the block to query.
309
+ * @returns {BlockResponse} A promise resolving to a {@link BlockResponse} formatted depending on `verbosity` level.
310
+ * @throws {BitcoinRpcError} If neither `blockhash` nor `height` is provided.
311
+ */
312
+ public async get({ blockhash, height }: GetBlockParams): Promise<BlockResponse | undefined> {
313
+ // Check if blockhash or height is provided, if neither throw an error
314
+ if(!blockhash && height === undefined) {
315
+ throw new BitcoinRpcError('blockhash or height required', 'INVALID_PARAMS_GET_BLOCK', { blockhash, height });
316
+ }
317
+
318
+ // If height is provided, get the blockhash
319
+ blockhash ??= await this.getHash(height!);
320
+ if(!blockhash || typeof blockhash !== 'string') {
321
+ return undefined;
322
+ }
323
+
324
+ // Get the block data
325
+ return await this.api({ path: `/block/${blockhash}` }) as BlockV3;
326
+ }
327
+
328
+ /**
329
+ * Get the block hash for a given block height.
330
+ * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-block-heightheight | Esplora GET /block-height/:height } for details.
331
+ * @param {number} height The block height (required).
332
+ * @returns {Promise<string>} The hash of the block currently at height..
333
+ */
334
+ public async getHash(height: number): Promise<string> {
335
+ return await this.api({ path: `/block-height/${height}` });
336
+ }
337
+ }
338
+
339
+ export class BitcoinAddress {
340
+ private api: (params: RestApiCallParams) => Promise<any>;
341
+
342
+ constructor(api: (params: RestApiCallParams) => Promise<any>) {
343
+ this.api = api;
344
+ }
345
+
346
+ /**
347
+ * Get transaction history for the specified address/scripthash, sorted with newest first.
348
+ * Returns up to 50 mempool transactions plus the first 25 confirmed transactions.
349
+ * See {@link https://github.com/blockstream/esplora/blob/master/API.md#get-addressaddresstxs | Esplora GET /address/:address/txs } for details.
350
+ * @param {string} addressOrScripthash The address or scripthash to check.
351
+ * @returns {Promise<Array<RawTransactionRest>>} A promise resolving to an array of {@link RawTransactionRest} objects.
352
+ */
353
+ public async getTxs(addressOrScripthash: string): Promise<Array<RawTransactionRest>> {
354
+ return await this.api({ path: `/address/${addressOrScripthash}/txs` });
355
+ }
356
+
357
+ /**
358
+ * Calls getAddressTxs and checks if any funds come back.
359
+ * Toggle if those funds are confirmed.
360
+ * @param {string} addressOrScripthash The address or scripthash to check.
361
+ * @returns {Promise<boolean>} True if the address has any funds, false otherwise.
362
+ */
363
+ public async isFundedAddress(addressOrScripthash: string): Promise<boolean> {
364
+ const txs = await this.getConfirmedTxs(addressOrScripthash);
365
+ const confirmed = txs.filter((tx: RawTransactionRest) => tx.status.confirmed);
366
+ return !!(confirmed && confirmed.length);
367
+ }
368
+
369
+ /**
370
+ * Get unconfirmed transaction history for the specified address/scripthash.
371
+ * Returns up to 50 transactions (no paging).
372
+ * @param {string} addressOrScripthash The address or scripthash to check.
373
+ * @returns {Promise<Array<RawTransactionRest>>} A promise resolving to an array of {@link RawTransactionRest} objects.
374
+ */
375
+ public async getTxsMempool(addressOrScripthash: string): Promise<Array<RawTransactionRest>> {
376
+ return await this.api({ path: `/address/${addressOrScripthash}/txs/mempool` });
377
+ }
378
+
379
+ /**
380
+ * Get information about an address/scripthash.
381
+ * Available fields: address/scripthash, chain_stats and mempool_stats.
382
+ * {chain,mempool}_stats each contain an object with tx_count, funded_txo_count, funded_txo_sum, spent_txo_count and spent_txo_sum.
383
+ * Elements-based chains don't have the {funded,spent}_txo_sum fields.
384
+ * @param {string} addressOrScripthash The address or scripthash to check.
385
+ * @returns {Promise<AddressInfo>} A promise resolving to an {@link AddressInfo} object.
386
+ */
387
+ public async getInfo(addressOrScripthash: string): Promise<AddressInfo> {
388
+ return await this.api({ path: `/address/${addressOrScripthash}` });
389
+ }
390
+
391
+ /**
392
+ * Get confirmed transaction history for the specified address/scripthash, sorted with newest first.
393
+ * Returns 25 transactions per page. More can be requested by specifying the last txid seen by the previous query.
394
+ * @param {string} addressOrScripthash The address or scripthash to check.
395
+ * @param lastSeenTxId The last transaction id seen by the previous query (optional).
396
+ * @returns {Promise<Array<RawTransactionRest>>} A promise resolving to an array of {@link RawTransactionRest} objects.
397
+ */
398
+ public async getConfirmedTxs(addressOrScripthash: string, lastSeenTxId?: string): Promise<Array<RawTransactionRest>> {
399
+ return await this.api({
400
+ path : lastSeenTxId
401
+ ? `/address/${addressOrScripthash}/txs/chain/${lastSeenTxId}`
402
+ : `/address/${addressOrScripthash}/txs/chain`
403
+ });
404
+ }
405
+
406
+ /**
407
+ * Get the list of unspent transaction outputs associated with the address/scripthash.
408
+ * See {@link https://github.com/Blockstream/esplora/blob/master/API.md#get-addressaddressutxo | Esplora GET /address/:address/utxo } for details.
409
+ * @param {string} addressOrScripthash The address or scripthash to check.
410
+ * @returns {Promise<Array<RawTransactionRest>>} A promise resolving to an array of {@link RawTransactionRest} objects.
411
+ */
412
+ public async getUtxos(addressOrScripthash: string): Promise<Array<AddressUtxo>> {
413
+ return await this.api({ path: `/address/${addressOrScripthash}/utxo` });
414
+ }
415
+ }