@arkade-os/sdk 0.4.26 → 0.4.28

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 (522) hide show
  1. package/README.md +36 -125
  2. package/dist/adapters/asyncStorage.cjs +48 -0
  3. package/dist/adapters/asyncStorage.cjs.map +1 -0
  4. package/dist/adapters/asyncStorage.d.cts +16 -0
  5. package/dist/{types/storage → adapters}/asyncStorage.d.ts +5 -2
  6. package/dist/adapters/asyncStorage.js +46 -0
  7. package/dist/adapters/asyncStorage.js.map +1 -0
  8. package/dist/adapters/expo.cjs +19 -0
  9. package/dist/adapters/expo.cjs.map +1 -0
  10. package/dist/adapters/expo.d.cts +48 -0
  11. package/dist/adapters/expo.d.ts +48 -0
  12. package/dist/adapters/expo.js +6 -0
  13. package/dist/adapters/expo.js.map +1 -0
  14. package/dist/adapters/fileSystem.cjs +116 -0
  15. package/dist/adapters/fileSystem.cjs.map +1 -0
  16. package/dist/adapters/fileSystem.d.cts +17 -0
  17. package/dist/{types/storage → adapters}/fileSystem.d.ts +5 -2
  18. package/dist/adapters/fileSystem.js +93 -0
  19. package/dist/adapters/fileSystem.js.map +1 -0
  20. package/dist/adapters/indexedDB.cjs +103 -0
  21. package/dist/adapters/indexedDB.cjs.map +1 -0
  22. package/dist/adapters/indexedDB.d.cts +18 -0
  23. package/dist/{types/storage → adapters}/indexedDB.d.ts +5 -2
  24. package/dist/adapters/indexedDB.js +101 -0
  25. package/dist/adapters/indexedDB.js.map +1 -0
  26. package/dist/adapters/localStorage.cjs +50 -0
  27. package/dist/adapters/localStorage.cjs.map +1 -0
  28. package/dist/{types/storage/inMemory.d.ts → adapters/localStorage.d.cts} +6 -3
  29. package/dist/{types/storage → adapters}/localStorage.d.ts +5 -2
  30. package/dist/adapters/localStorage.js +48 -0
  31. package/dist/adapters/localStorage.js.map +1 -0
  32. package/dist/ark-TZ1gXAXU.d.cts +3880 -0
  33. package/dist/ark-TZ1gXAXU.d.ts +3880 -0
  34. package/dist/{types/worker/expo/asyncStorageTaskQueue.d.ts → asyncStorageTaskQueue-Cb1F_Z9s.d.ts} +6 -3
  35. package/dist/asyncStorageTaskQueue-EFqSmYTg.d.cts +49 -0
  36. package/dist/chunk-5BLDMQED.cjs +18 -0
  37. package/dist/chunk-5BLDMQED.cjs.map +1 -0
  38. package/dist/chunk-5PG7DV7A.cjs +805 -0
  39. package/dist/chunk-5PG7DV7A.cjs.map +1 -0
  40. package/dist/chunk-A3EMF7RN.js +95 -0
  41. package/dist/chunk-A3EMF7RN.js.map +1 -0
  42. package/dist/chunk-ADV27S4N.cjs +2701 -0
  43. package/dist/chunk-ADV27S4N.cjs.map +1 -0
  44. package/dist/chunk-BQLHADL7.js +13805 -0
  45. package/dist/chunk-BQLHADL7.js.map +1 -0
  46. package/dist/chunk-CFZMTDWI.js +209 -0
  47. package/dist/chunk-CFZMTDWI.js.map +1 -0
  48. package/dist/chunk-FG5ACJJW.cjs +212 -0
  49. package/dist/chunk-FG5ACJJW.cjs.map +1 -0
  50. package/dist/chunk-HW3JJ323.js +768 -0
  51. package/dist/chunk-HW3JJ323.js.map +1 -0
  52. package/dist/chunk-I3DGUUCT.cjs +838 -0
  53. package/dist/chunk-I3DGUUCT.cjs.map +1 -0
  54. package/dist/chunk-IPX2R7FR.cjs +100 -0
  55. package/dist/chunk-IPX2R7FR.cjs.map +1 -0
  56. package/dist/chunk-NSBPE2FW.js +15 -0
  57. package/dist/chunk-NSBPE2FW.js.map +1 -0
  58. package/dist/chunk-T64LAI7L.js +829 -0
  59. package/dist/chunk-T64LAI7L.js.map +1 -0
  60. package/dist/chunk-ZBUDLTBO.js +2671 -0
  61. package/dist/chunk-ZBUDLTBO.js.map +1 -0
  62. package/dist/chunk-ZLO6NETT.cjs +13910 -0
  63. package/dist/chunk-ZLO6NETT.cjs.map +1 -0
  64. package/dist/contracts/handlers/index.cjs +26 -0
  65. package/dist/contracts/handlers/index.cjs.map +1 -0
  66. package/dist/contracts/handlers/index.d.cts +7 -0
  67. package/dist/contracts/handlers/index.d.ts +7 -0
  68. package/dist/contracts/handlers/index.js +5 -0
  69. package/dist/contracts/handlers/index.js.map +1 -0
  70. package/dist/delegate-BFZs69hp.d.cts +84 -0
  71. package/dist/delegate-aaVGfWsV.d.ts +84 -0
  72. package/dist/index-B22cA64m.d.cts +199 -0
  73. package/dist/{types/storage/index.d.ts → index-C0IanN1m.d.cts} +3 -1
  74. package/dist/index-C0IanN1m.d.ts +11 -0
  75. package/dist/index-NDla_UoJ.d.ts +199 -0
  76. package/dist/index.cjs +480 -0
  77. package/dist/index.cjs.map +1 -0
  78. package/dist/index.d.cts +3343 -0
  79. package/dist/index.d.ts +3343 -0
  80. package/dist/index.js +7 -0
  81. package/dist/index.js.map +1 -0
  82. package/dist/repositories/realm/index.cjs +513 -0
  83. package/dist/repositories/realm/index.cjs.map +1 -0
  84. package/dist/repositories/realm/index.d.cts +217 -0
  85. package/dist/{types/repositories/realm/schemas.d.ts → repositories/realm/index.d.ts} +80 -112
  86. package/dist/repositories/realm/index.js +507 -0
  87. package/dist/repositories/realm/index.js.map +1 -0
  88. package/dist/repositories/sqlite/index.cjs +588 -0
  89. package/dist/repositories/sqlite/index.cjs.map +1 -0
  90. package/dist/repositories/sqlite/index.d.cts +118 -0
  91. package/dist/{types/repositories/sqlite/walletRepository.d.ts → repositories/sqlite/index.d.ts} +58 -5
  92. package/dist/repositories/sqlite/index.js +585 -0
  93. package/dist/repositories/sqlite/index.js.map +1 -0
  94. package/dist/taskRunner-C6Ff4OaU.d.cts +114 -0
  95. package/dist/taskRunner-yvPN8Z0K.d.ts +114 -0
  96. package/dist/wallet/expo/background.cjs +93 -0
  97. package/dist/wallet/expo/background.cjs.map +1 -0
  98. package/dist/wallet/expo/background.d.cts +84 -0
  99. package/dist/wallet/expo/background.d.ts +84 -0
  100. package/dist/wallet/expo/background.js +68 -0
  101. package/dist/wallet/expo/background.js.map +1 -0
  102. package/dist/wallet/expo/index.cjs +171 -0
  103. package/dist/wallet/expo/index.cjs.map +1 -0
  104. package/dist/wallet/expo/index.d.cts +122 -0
  105. package/dist/{types/wallet/expo/wallet.d.ts → wallet/expo/index.d.ts} +45 -22
  106. package/dist/wallet/expo/index.js +169 -0
  107. package/dist/wallet/expo/index.js.map +1 -0
  108. package/dist/wallet-AF-p-OWj.d.cts +774 -0
  109. package/dist/wallet-D9NBRqvC.d.ts +774 -0
  110. package/dist/worker/expo/index.cjs +140 -0
  111. package/dist/worker/expo/index.cjs.map +1 -0
  112. package/dist/worker/expo/index.d.cts +29 -0
  113. package/dist/worker/expo/index.d.ts +29 -0
  114. package/dist/worker/expo/index.js +121 -0
  115. package/dist/worker/expo/index.js.map +1 -0
  116. package/package.json +110 -76
  117. package/dist/cjs/adapters/asyncStorage.js +0 -5
  118. package/dist/cjs/adapters/expo.js +0 -8
  119. package/dist/cjs/adapters/fileSystem.js +0 -5
  120. package/dist/cjs/adapters/indexedDB.js +0 -5
  121. package/dist/cjs/adapters/localStorage.js +0 -5
  122. package/dist/cjs/arkfee/celenv.js +0 -43
  123. package/dist/cjs/arkfee/estimator.js +0 -143
  124. package/dist/cjs/arkfee/index.js +0 -5
  125. package/dist/cjs/arkfee/types.js +0 -26
  126. package/dist/cjs/arknote/index.js +0 -128
  127. package/dist/cjs/bip322/index.js +0 -270
  128. package/dist/cjs/contracts/arkcontract.js +0 -147
  129. package/dist/cjs/contracts/contractManager.js +0 -629
  130. package/dist/cjs/contracts/contractWatcher.js +0 -598
  131. package/dist/cjs/contracts/handlers/default.js +0 -93
  132. package/dist/cjs/contracts/handlers/delegate.js +0 -90
  133. package/dist/cjs/contracts/handlers/helpers.js +0 -115
  134. package/dist/cjs/contracts/handlers/index.js +0 -19
  135. package/dist/cjs/contracts/handlers/registry.js +0 -89
  136. package/dist/cjs/contracts/handlers/vhtlc.js +0 -194
  137. package/dist/cjs/contracts/index.js +0 -41
  138. package/dist/cjs/contracts/types.js +0 -2
  139. package/dist/cjs/contracts/vtxoOwnership.js +0 -78
  140. package/dist/cjs/extension/asset/assetGroup.js +0 -228
  141. package/dist/cjs/extension/asset/assetId.js +0 -152
  142. package/dist/cjs/extension/asset/assetInput.js +0 -222
  143. package/dist/cjs/extension/asset/assetOutput.js +0 -174
  144. package/dist/cjs/extension/asset/assetRef.js +0 -148
  145. package/dist/cjs/extension/asset/index.js +0 -23
  146. package/dist/cjs/extension/asset/metadata.js +0 -187
  147. package/dist/cjs/extension/asset/packet.js +0 -114
  148. package/dist/cjs/extension/asset/types.js +0 -22
  149. package/dist/cjs/extension/asset/utils.js +0 -105
  150. package/dist/cjs/extension/index.js +0 -254
  151. package/dist/cjs/extension/packet.js +0 -20
  152. package/dist/cjs/forfeit.js +0 -45
  153. package/dist/cjs/identity/descriptor.js +0 -169
  154. package/dist/cjs/identity/descriptorProvider.js +0 -2
  155. package/dist/cjs/identity/hdCapableIdentity.js +0 -2
  156. package/dist/cjs/identity/index.js +0 -38
  157. package/dist/cjs/identity/seedIdentity.js +0 -461
  158. package/dist/cjs/identity/serialize.js +0 -171
  159. package/dist/cjs/identity/singleKey.js +0 -126
  160. package/dist/cjs/identity/staticDescriptorProvider.js +0 -65
  161. package/dist/cjs/index.js +0 -200
  162. package/dist/cjs/intent/index.js +0 -259
  163. package/dist/cjs/musig2/index.js +0 -11
  164. package/dist/cjs/musig2/keys.js +0 -57
  165. package/dist/cjs/musig2/nonces.js +0 -48
  166. package/dist/cjs/musig2/sign.js +0 -102
  167. package/dist/cjs/networks.js +0 -26
  168. package/dist/cjs/package.json +0 -3
  169. package/dist/cjs/providers/ark.js +0 -577
  170. package/dist/cjs/providers/delegator.js +0 -85
  171. package/dist/cjs/providers/electrum.js +0 -869
  172. package/dist/cjs/providers/errors.js +0 -59
  173. package/dist/cjs/providers/expoArk.js +0 -82
  174. package/dist/cjs/providers/expoIndexer.js +0 -111
  175. package/dist/cjs/providers/expoUtils.js +0 -124
  176. package/dist/cjs/providers/indexer.js +0 -630
  177. package/dist/cjs/providers/onchain.js +0 -262
  178. package/dist/cjs/providers/utils.js +0 -121
  179. package/dist/cjs/repositories/contractRepository.js +0 -2
  180. package/dist/cjs/repositories/inMemory/contractRepository.js +0 -55
  181. package/dist/cjs/repositories/inMemory/walletRepository.js +0 -115
  182. package/dist/cjs/repositories/index.js +0 -34
  183. package/dist/cjs/repositories/indexedDB/contractRepository.js +0 -187
  184. package/dist/cjs/repositories/indexedDB/db.js +0 -19
  185. package/dist/cjs/repositories/indexedDB/manager.js +0 -100
  186. package/dist/cjs/repositories/indexedDB/schema.js +0 -204
  187. package/dist/cjs/repositories/indexedDB/walletRepository.js +0 -474
  188. package/dist/cjs/repositories/indexedDB/websqlAdapter.js +0 -144
  189. package/dist/cjs/repositories/migrations/contractRepositoryImpl.js +0 -127
  190. package/dist/cjs/repositories/migrations/fromStorageAdapter.js +0 -66
  191. package/dist/cjs/repositories/migrations/walletRepositoryImpl.js +0 -184
  192. package/dist/cjs/repositories/realm/contractRepository.js +0 -116
  193. package/dist/cjs/repositories/realm/index.js +0 -11
  194. package/dist/cjs/repositories/realm/schemas.js +0 -157
  195. package/dist/cjs/repositories/realm/types.js +0 -7
  196. package/dist/cjs/repositories/realm/walletRepository.js +0 -305
  197. package/dist/cjs/repositories/scriptFromAddress.js +0 -16
  198. package/dist/cjs/repositories/serialization.js +0 -82
  199. package/dist/cjs/repositories/sqlite/contractRepository.js +0 -135
  200. package/dist/cjs/repositories/sqlite/index.js +0 -7
  201. package/dist/cjs/repositories/sqlite/types.js +0 -2
  202. package/dist/cjs/repositories/sqlite/walletRepository.js +0 -441
  203. package/dist/cjs/repositories/walletRepository.js +0 -2
  204. package/dist/cjs/script/address.js +0 -108
  205. package/dist/cjs/script/base.js +0 -185
  206. package/dist/cjs/script/default.js +0 -57
  207. package/dist/cjs/script/delegate.js +0 -53
  208. package/dist/cjs/script/tapscript.js +0 -619
  209. package/dist/cjs/script/vhtlc.js +0 -170
  210. package/dist/cjs/storage/asyncStorage.js +0 -50
  211. package/dist/cjs/storage/fileSystem.js +0 -141
  212. package/dist/cjs/storage/inMemory.js +0 -24
  213. package/dist/cjs/storage/index.js +0 -2
  214. package/dist/cjs/storage/indexedDB.js +0 -101
  215. package/dist/cjs/storage/localStorage.js +0 -51
  216. package/dist/cjs/tree/signingSession.js +0 -229
  217. package/dist/cjs/tree/txTree.js +0 -192
  218. package/dist/cjs/tree/validation.js +0 -107
  219. package/dist/cjs/utils/anchor.js +0 -35
  220. package/dist/cjs/utils/arkTransaction.js +0 -271
  221. package/dist/cjs/utils/bip21.js +0 -127
  222. package/dist/cjs/utils/syncCursors.js +0 -128
  223. package/dist/cjs/utils/timelock.js +0 -59
  224. package/dist/cjs/utils/transaction.js +0 -28
  225. package/dist/cjs/utils/transactionHistory.js +0 -183
  226. package/dist/cjs/utils/txSizeEstimator.js +0 -132
  227. package/dist/cjs/utils/unknownFields.js +0 -174
  228. package/dist/cjs/wallet/asset-manager.js +0 -330
  229. package/dist/cjs/wallet/asset.js +0 -119
  230. package/dist/cjs/wallet/batch.js +0 -183
  231. package/dist/cjs/wallet/delegator.js +0 -302
  232. package/dist/cjs/wallet/expo/background.js +0 -116
  233. package/dist/cjs/wallet/expo/index.js +0 -9
  234. package/dist/cjs/wallet/expo/wallet.js +0 -230
  235. package/dist/cjs/wallet/hdDescriptorProvider.js +0 -159
  236. package/dist/cjs/wallet/index.js +0 -82
  237. package/dist/cjs/wallet/onchain.js +0 -290
  238. package/dist/cjs/wallet/ramps.js +0 -216
  239. package/dist/cjs/wallet/serviceWorker/wallet-message-handler.js +0 -953
  240. package/dist/cjs/wallet/serviceWorker/wallet.js +0 -1173
  241. package/dist/cjs/wallet/unroll.js +0 -289
  242. package/dist/cjs/wallet/utils.js +0 -111
  243. package/dist/cjs/wallet/validation.js +0 -154
  244. package/dist/cjs/wallet/vtxo-manager.js +0 -1142
  245. package/dist/cjs/wallet/wallet.js +0 -2049
  246. package/dist/cjs/worker/browser/service-worker-manager.js +0 -183
  247. package/dist/cjs/worker/browser/utils.js +0 -67
  248. package/dist/cjs/worker/errors.js +0 -16
  249. package/dist/cjs/worker/expo/asyncStorageTaskQueue.js +0 -78
  250. package/dist/cjs/worker/expo/index.js +0 -13
  251. package/dist/cjs/worker/expo/processors/contractPollProcessor.js +0 -62
  252. package/dist/cjs/worker/expo/processors/index.js +0 -6
  253. package/dist/cjs/worker/expo/taskQueue.js +0 -41
  254. package/dist/cjs/worker/expo/taskRunner.js +0 -73
  255. package/dist/cjs/worker/messageBus.js +0 -473
  256. package/dist/esm/adapters/asyncStorage.js +0 -1
  257. package/dist/esm/adapters/expo.js +0 -3
  258. package/dist/esm/adapters/fileSystem.js +0 -1
  259. package/dist/esm/adapters/indexedDB.js +0 -1
  260. package/dist/esm/adapters/localStorage.js +0 -1
  261. package/dist/esm/arkfee/celenv.js +0 -40
  262. package/dist/esm/arkfee/estimator.js +0 -139
  263. package/dist/esm/arkfee/index.js +0 -1
  264. package/dist/esm/arkfee/types.js +0 -22
  265. package/dist/esm/arknote/index.js +0 -124
  266. package/dist/esm/bip322/index.js +0 -267
  267. package/dist/esm/contracts/arkcontract.js +0 -140
  268. package/dist/esm/contracts/contractManager.js +0 -625
  269. package/dist/esm/contracts/contractWatcher.js +0 -594
  270. package/dist/esm/contracts/handlers/default.js +0 -90
  271. package/dist/esm/contracts/handlers/delegate.js +0 -87
  272. package/dist/esm/contracts/handlers/helpers.js +0 -110
  273. package/dist/esm/contracts/handlers/index.js +0 -12
  274. package/dist/esm/contracts/handlers/registry.js +0 -86
  275. package/dist/esm/contracts/handlers/vhtlc.js +0 -191
  276. package/dist/esm/contracts/index.js +0 -13
  277. package/dist/esm/contracts/types.js +0 -1
  278. package/dist/esm/contracts/vtxoOwnership.js +0 -69
  279. package/dist/esm/extension/asset/assetGroup.js +0 -224
  280. package/dist/esm/extension/asset/assetId.js +0 -148
  281. package/dist/esm/extension/asset/assetInput.js +0 -217
  282. package/dist/esm/extension/asset/assetOutput.js +0 -169
  283. package/dist/esm/extension/asset/assetRef.js +0 -144
  284. package/dist/esm/extension/asset/index.js +0 -8
  285. package/dist/esm/extension/asset/metadata.js +0 -182
  286. package/dist/esm/extension/asset/packet.js +0 -110
  287. package/dist/esm/extension/asset/types.js +0 -19
  288. package/dist/esm/extension/asset/utils.js +0 -99
  289. package/dist/esm/extension/index.js +0 -248
  290. package/dist/esm/extension/packet.js +0 -16
  291. package/dist/esm/forfeit.js +0 -41
  292. package/dist/esm/identity/descriptor.js +0 -161
  293. package/dist/esm/identity/descriptorProvider.js +0 -1
  294. package/dist/esm/identity/hdCapableIdentity.js +0 -1
  295. package/dist/esm/identity/index.js +0 -12
  296. package/dist/esm/identity/seedIdentity.js +0 -453
  297. package/dist/esm/identity/serialize.js +0 -164
  298. package/dist/esm/identity/singleKey.js +0 -121
  299. package/dist/esm/identity/staticDescriptorProvider.js +0 -61
  300. package/dist/esm/index.js +0 -87
  301. package/dist/esm/intent/index.js +0 -255
  302. package/dist/esm/musig2/index.js +0 -3
  303. package/dist/esm/musig2/keys.js +0 -21
  304. package/dist/esm/musig2/nonces.js +0 -11
  305. package/dist/esm/musig2/sign.js +0 -63
  306. package/dist/esm/networks.js +0 -22
  307. package/dist/esm/package.json +0 -3
  308. package/dist/esm/providers/ark.js +0 -572
  309. package/dist/esm/providers/delegator.js +0 -81
  310. package/dist/esm/providers/electrum.js +0 -864
  311. package/dist/esm/providers/errors.js +0 -54
  312. package/dist/esm/providers/expoArk.js +0 -78
  313. package/dist/esm/providers/expoIndexer.js +0 -107
  314. package/dist/esm/providers/expoUtils.js +0 -87
  315. package/dist/esm/providers/indexer.js +0 -626
  316. package/dist/esm/providers/onchain.js +0 -258
  317. package/dist/esm/providers/utils.js +0 -117
  318. package/dist/esm/repositories/contractRepository.js +0 -1
  319. package/dist/esm/repositories/inMemory/contractRepository.js +0 -51
  320. package/dist/esm/repositories/inMemory/walletRepository.js +0 -111
  321. package/dist/esm/repositories/index.js +0 -10
  322. package/dist/esm/repositories/indexedDB/contractRepository.js +0 -183
  323. package/dist/esm/repositories/indexedDB/db.js +0 -4
  324. package/dist/esm/repositories/indexedDB/manager.js +0 -95
  325. package/dist/esm/repositories/indexedDB/schema.js +0 -199
  326. package/dist/esm/repositories/indexedDB/walletRepository.js +0 -470
  327. package/dist/esm/repositories/indexedDB/websqlAdapter.js +0 -138
  328. package/dist/esm/repositories/migrations/contractRepositoryImpl.js +0 -121
  329. package/dist/esm/repositories/migrations/fromStorageAdapter.js +0 -58
  330. package/dist/esm/repositories/migrations/walletRepositoryImpl.js +0 -180
  331. package/dist/esm/repositories/realm/contractRepository.js +0 -112
  332. package/dist/esm/repositories/realm/index.js +0 -3
  333. package/dist/esm/repositories/realm/schemas.js +0 -153
  334. package/dist/esm/repositories/realm/types.js +0 -6
  335. package/dist/esm/repositories/realm/walletRepository.js +0 -301
  336. package/dist/esm/repositories/scriptFromAddress.js +0 -13
  337. package/dist/esm/repositories/serialization.js +0 -67
  338. package/dist/esm/repositories/sqlite/contractRepository.js +0 -131
  339. package/dist/esm/repositories/sqlite/index.js +0 -2
  340. package/dist/esm/repositories/sqlite/types.js +0 -1
  341. package/dist/esm/repositories/sqlite/walletRepository.js +0 -437
  342. package/dist/esm/repositories/walletRepository.js +0 -1
  343. package/dist/esm/script/address.js +0 -104
  344. package/dist/esm/script/base.js +0 -179
  345. package/dist/esm/script/default.js +0 -54
  346. package/dist/esm/script/delegate.js +0 -50
  347. package/dist/esm/script/tapscript.js +0 -615
  348. package/dist/esm/script/vhtlc.js +0 -167
  349. package/dist/esm/storage/asyncStorage.js +0 -46
  350. package/dist/esm/storage/fileSystem.js +0 -104
  351. package/dist/esm/storage/inMemory.js +0 -20
  352. package/dist/esm/storage/index.js +0 -1
  353. package/dist/esm/storage/indexedDB.js +0 -97
  354. package/dist/esm/storage/localStorage.js +0 -47
  355. package/dist/esm/tree/signingSession.js +0 -191
  356. package/dist/esm/tree/txTree.js +0 -188
  357. package/dist/esm/tree/validation.js +0 -101
  358. package/dist/esm/utils/anchor.js +0 -31
  359. package/dist/esm/utils/arkTransaction.js +0 -264
  360. package/dist/esm/utils/bip21.js +0 -123
  361. package/dist/esm/utils/syncCursors.js +0 -119
  362. package/dist/esm/utils/timelock.js +0 -22
  363. package/dist/esm/utils/transaction.js +0 -24
  364. package/dist/esm/utils/transactionHistory.js +0 -180
  365. package/dist/esm/utils/txSizeEstimator.js +0 -128
  366. package/dist/esm/utils/unknownFields.js +0 -169
  367. package/dist/esm/wallet/asset-manager.js +0 -325
  368. package/dist/esm/wallet/asset.js +0 -113
  369. package/dist/esm/wallet/batch.js +0 -180
  370. package/dist/esm/wallet/delegator.js +0 -297
  371. package/dist/esm/wallet/expo/background.js +0 -111
  372. package/dist/esm/wallet/expo/index.js +0 -2
  373. package/dist/esm/wallet/expo/wallet.js +0 -193
  374. package/dist/esm/wallet/hdDescriptorProvider.js +0 -155
  375. package/dist/esm/wallet/index.js +0 -75
  376. package/dist/esm/wallet/onchain.js +0 -285
  377. package/dist/esm/wallet/ramps.js +0 -212
  378. package/dist/esm/wallet/serviceWorker/wallet-message-handler.js +0 -946
  379. package/dist/esm/wallet/serviceWorker/wallet.js +0 -1168
  380. package/dist/esm/wallet/unroll.js +0 -285
  381. package/dist/esm/wallet/utils.js +0 -103
  382. package/dist/esm/wallet/validation.js +0 -142
  383. package/dist/esm/wallet/vtxo-manager.js +0 -1136
  384. package/dist/esm/wallet/wallet.js +0 -2041
  385. package/dist/esm/worker/browser/service-worker-manager.js +0 -177
  386. package/dist/esm/worker/browser/utils.js +0 -63
  387. package/dist/esm/worker/errors.js +0 -11
  388. package/dist/esm/worker/expo/asyncStorageTaskQueue.js +0 -74
  389. package/dist/esm/worker/expo/index.js +0 -4
  390. package/dist/esm/worker/expo/processors/contractPollProcessor.js +0 -59
  391. package/dist/esm/worker/expo/processors/index.js +0 -1
  392. package/dist/esm/worker/expo/taskQueue.js +0 -37
  393. package/dist/esm/worker/expo/taskRunner.js +0 -69
  394. package/dist/esm/worker/messageBus.js +0 -469
  395. package/dist/types/adapters/asyncStorage.d.ts +0 -2
  396. package/dist/types/adapters/expo.d.ts +0 -4
  397. package/dist/types/adapters/fileSystem.d.ts +0 -2
  398. package/dist/types/adapters/indexedDB.d.ts +0 -2
  399. package/dist/types/adapters/localStorage.d.ts +0 -2
  400. package/dist/types/arkfee/celenv.d.ts +0 -25
  401. package/dist/types/arkfee/estimator.d.ts +0 -49
  402. package/dist/types/arkfee/index.d.ts +0 -2
  403. package/dist/types/arkfee/types.d.ts +0 -38
  404. package/dist/types/arknote/index.d.ts +0 -84
  405. package/dist/types/bip322/index.d.ts +0 -55
  406. package/dist/types/contracts/arkcontract.d.ts +0 -99
  407. package/dist/types/contracts/contractManager.d.ts +0 -381
  408. package/dist/types/contracts/contractWatcher.d.ts +0 -217
  409. package/dist/types/contracts/handlers/default.d.ts +0 -19
  410. package/dist/types/contracts/handlers/delegate.d.ts +0 -21
  411. package/dist/types/contracts/handlers/helpers.d.ts +0 -19
  412. package/dist/types/contracts/handlers/index.d.ts +0 -7
  413. package/dist/types/contracts/handlers/registry.d.ts +0 -65
  414. package/dist/types/contracts/handlers/vhtlc.d.ts +0 -32
  415. package/dist/types/contracts/index.d.ts +0 -14
  416. package/dist/types/contracts/types.d.ts +0 -233
  417. package/dist/types/contracts/vtxoOwnership.d.ts +0 -33
  418. package/dist/types/extension/asset/assetGroup.d.ts +0 -119
  419. package/dist/types/extension/asset/assetId.d.ts +0 -83
  420. package/dist/types/extension/asset/assetInput.d.ts +0 -64
  421. package/dist/types/extension/asset/assetOutput.d.ts +0 -54
  422. package/dist/types/extension/asset/assetRef.d.ts +0 -91
  423. package/dist/types/extension/asset/index.d.ts +0 -8
  424. package/dist/types/extension/asset/metadata.d.ts +0 -52
  425. package/dist/types/extension/asset/packet.d.ts +0 -41
  426. package/dist/types/extension/asset/types.d.ts +0 -16
  427. package/dist/types/extension/asset/utils.d.ts +0 -21
  428. package/dist/types/extension/index.d.ts +0 -56
  429. package/dist/types/extension/packet.d.ts +0 -21
  430. package/dist/types/forfeit.d.ts +0 -18
  431. package/dist/types/identity/descriptor.d.ts +0 -61
  432. package/dist/types/identity/descriptorProvider.d.ts +0 -35
  433. package/dist/types/identity/hdCapableIdentity.d.ts +0 -44
  434. package/dist/types/identity/index.d.ts +0 -56
  435. package/dist/types/identity/seedIdentity.d.ts +0 -254
  436. package/dist/types/identity/serialize.d.ts +0 -96
  437. package/dist/types/identity/singleKey.d.ts +0 -62
  438. package/dist/types/identity/staticDescriptorProvider.d.ts +0 -18
  439. package/dist/types/index.d.ts +0 -59
  440. package/dist/types/intent/index.d.ts +0 -86
  441. package/dist/types/musig2/index.d.ts +0 -4
  442. package/dist/types/musig2/keys.d.ts +0 -9
  443. package/dist/types/musig2/nonces.d.ts +0 -14
  444. package/dist/types/musig2/sign.d.ts +0 -27
  445. package/dist/types/networks.d.ts +0 -16
  446. package/dist/types/providers/ark.d.ts +0 -369
  447. package/dist/types/providers/delegator.d.ts +0 -82
  448. package/dist/types/providers/electrum.d.ts +0 -312
  449. package/dist/types/providers/errors.d.ts +0 -13
  450. package/dist/types/providers/expoArk.d.ts +0 -22
  451. package/dist/types/providers/expoIndexer.d.ts +0 -18
  452. package/dist/types/providers/expoUtils.d.ts +0 -18
  453. package/dist/types/providers/indexer.d.ts +0 -301
  454. package/dist/types/providers/onchain.d.ts +0 -148
  455. package/dist/types/providers/utils.d.ts +0 -12
  456. package/dist/types/repositories/contractRepository.d.ts +0 -32
  457. package/dist/types/repositories/inMemory/contractRepository.d.ts +0 -17
  458. package/dist/types/repositories/inMemory/walletRepository.d.ts +0 -29
  459. package/dist/types/repositories/index.d.ts +0 -9
  460. package/dist/types/repositories/indexedDB/contractRepository.d.ts +0 -21
  461. package/dist/types/repositories/indexedDB/db.d.ts +0 -4
  462. package/dist/types/repositories/indexedDB/manager.d.ts +0 -25
  463. package/dist/types/repositories/indexedDB/schema.d.ts +0 -9
  464. package/dist/types/repositories/indexedDB/walletRepository.d.ts +0 -28
  465. package/dist/types/repositories/indexedDB/websqlAdapter.d.ts +0 -49
  466. package/dist/types/repositories/migrations/contractRepositoryImpl.d.ts +0 -24
  467. package/dist/types/repositories/migrations/fromStorageAdapter.d.ts +0 -19
  468. package/dist/types/repositories/migrations/walletRepositoryImpl.d.ts +0 -27
  469. package/dist/types/repositories/realm/contractRepository.d.ts +0 -24
  470. package/dist/types/repositories/realm/index.d.ts +0 -4
  471. package/dist/types/repositories/realm/types.d.ts +0 -16
  472. package/dist/types/repositories/realm/walletRepository.d.ts +0 -34
  473. package/dist/types/repositories/scriptFromAddress.d.ts +0 -9
  474. package/dist/types/repositories/serialization.d.ts +0 -65
  475. package/dist/types/repositories/sqlite/contractRepository.d.ts +0 -33
  476. package/dist/types/repositories/sqlite/index.d.ts +0 -3
  477. package/dist/types/repositories/sqlite/types.d.ts +0 -18
  478. package/dist/types/repositories/walletRepository.d.ts +0 -72
  479. package/dist/types/script/address.d.ts +0 -67
  480. package/dist/types/script/base.d.ts +0 -105
  481. package/dist/types/script/default.d.ts +0 -44
  482. package/dist/types/script/delegate.d.ts +0 -40
  483. package/dist/types/script/tapscript.d.ts +0 -169
  484. package/dist/types/script/vhtlc.d.ts +0 -66
  485. package/dist/types/tree/signingSession.d.ts +0 -37
  486. package/dist/types/tree/txTree.d.ts +0 -28
  487. package/dist/types/tree/validation.d.ts +0 -15
  488. package/dist/types/utils/anchor.d.ts +0 -19
  489. package/dist/types/utils/arkTransaction.d.ts +0 -49
  490. package/dist/types/utils/bip21.d.ts +0 -38
  491. package/dist/types/utils/syncCursors.d.ts +0 -60
  492. package/dist/types/utils/timelock.d.ts +0 -9
  493. package/dist/types/utils/transaction.d.ts +0 -13
  494. package/dist/types/utils/transactionHistory.d.ts +0 -15
  495. package/dist/types/utils/txSizeEstimator.d.ts +0 -40
  496. package/dist/types/utils/unknownFields.d.ts +0 -83
  497. package/dist/types/wallet/asset-manager.d.ts +0 -69
  498. package/dist/types/wallet/asset.d.ts +0 -21
  499. package/dist/types/wallet/batch.d.ts +0 -107
  500. package/dist/types/wallet/delegator.d.ts +0 -48
  501. package/dist/types/wallet/expo/background.d.ts +0 -66
  502. package/dist/types/wallet/expo/index.d.ts +0 -4
  503. package/dist/types/wallet/hdDescriptorProvider.d.ts +0 -93
  504. package/dist/types/wallet/index.d.ts +0 -755
  505. package/dist/types/wallet/onchain.d.ts +0 -109
  506. package/dist/types/wallet/ramps.d.ts +0 -64
  507. package/dist/types/wallet/serviceWorker/wallet-message-handler.d.ts +0 -543
  508. package/dist/types/wallet/serviceWorker/wallet.d.ts +0 -238
  509. package/dist/types/wallet/unroll.d.ts +0 -114
  510. package/dist/types/wallet/utils.d.ts +0 -36
  511. package/dist/types/wallet/validation.d.ts +0 -24
  512. package/dist/types/wallet/vtxo-manager.d.ts +0 -476
  513. package/dist/types/wallet/wallet.d.ts +0 -360
  514. package/dist/types/worker/browser/service-worker-manager.d.ts +0 -32
  515. package/dist/types/worker/browser/utils.d.ts +0 -17
  516. package/dist/types/worker/errors.d.ts +0 -7
  517. package/dist/types/worker/expo/index.d.ts +0 -7
  518. package/dist/types/worker/expo/processors/contractPollProcessor.d.ts +0 -19
  519. package/dist/types/worker/expo/processors/index.d.ts +0 -1
  520. package/dist/types/worker/expo/taskQueue.d.ts +0 -50
  521. package/dist/types/worker/expo/taskRunner.d.ts +0 -66
  522. package/dist/types/worker/messageBus.d.ts +0 -188
@@ -1,594 +0,0 @@
1
- import { extendVirtualCoinForContract } from "../wallet/utils.js";
2
- import { isEventSourceError } from "../providers/utils.js";
3
- import { getVtxosForContract } from "./vtxoOwnership.js";
4
- /**
5
- * Watches multiple contracts for virtual output state changes with resilient connection handling.
6
- *
7
- * Features:
8
- * - Automatic reconnection with exponential backoff
9
- * - Failsafe polling to catch missed events
10
- * - Polls immediately after (re)connection to sync state
11
- * - Graceful handling of subscription failures
12
- *
13
- * @example
14
- * ```typescript
15
- * const watcher = new ContractWatcher({
16
- * indexerProvider: wallet.indexerProvider,
17
- * });
18
- *
19
- * // Add the wallet's default contract
20
- * await watcher.addContract(defaultContract);
21
- *
22
- * // Add additional contracts (swaps, etc.)
23
- * await watcher.addContract(swapContract);
24
- *
25
- * // Start watching for events
26
- * const stop = await watcher.startWatching((event) => {
27
- * console.log(`${event.type} on contract ${event.contractScript}`);
28
- * });
29
- *
30
- * // Later: stop watching
31
- * stop();
32
- * ```
33
- */
34
- export class ContractWatcher {
35
- /**
36
- * Create a contract watcher with the given providers and polling settings.
37
- *
38
- * @param config - Contract watcher configuration
39
- * @see ContractWatcherConfig
40
- */
41
- constructor(config) {
42
- this.contracts = new Map();
43
- this.isWatching = false;
44
- this.connectionState = "disconnected";
45
- this.reconnectAttempts = 0;
46
- this.config = {
47
- failsafePollIntervalMs: 60000, // 1 minute
48
- reconnectDelayMs: 1000, // 1 second
49
- maxReconnectDelayMs: 30000, // 30 seconds
50
- maxReconnectAttempts: 0, // unlimited
51
- ...config,
52
- };
53
- }
54
- /**
55
- * Add a contract to be watched.
56
- *
57
- * Active contracts are immediately subscribed.
58
- *
59
- * All contracts are polled to discover any existing virtual outputs
60
- * (which may cause them to be watched even if inactive).
61
- */
62
- async addContract(contract) {
63
- const state = {
64
- contract,
65
- lastKnownVtxos: new Map(),
66
- };
67
- this.contracts.set(contract.script, state);
68
- // Seed the baseline from the repository BEFORE any poll or event
69
- // emits. Without this, the first poll after (re)start treats every
70
- // persisted vtxo as "new" and emits `vtxo_received` for each —
71
- // which downstream triggers a redundant per-vtxo sync on every
72
- // app launch and can confuse consumers that react to the event.
73
- await this.seedLastKnownVtxos(state);
74
- // If we're already watching, poll to discover virtual outputs and update subscription
75
- if (this.isWatching) {
76
- // Poll first to discover virtual outputs (may affect whether we watch this contract).
77
- await this.pollContracts([contract.script]);
78
- // Update subscription based on active state and virtual outputs.
79
- await this.tryUpdateSubscription();
80
- }
81
- }
82
- /**
83
- * Pre-populate `lastKnownVtxos` from the wallet repository.
84
- *
85
- * Runs on add (and can be re-run after reconnect) so polling always
86
- * compares the indexer's view against what is already persisted,
87
- * emitting only genuine deltas.
88
- */
89
- async seedLastKnownVtxos(state) {
90
- try {
91
- // Apply the same script gate used by getContractVtxos so a legacy
92
- // wrong-script row in the address bucket can't seed the baseline
93
- // and then look "spent" on the first poll.
94
- const cached = await getVtxosForContract(this.config.walletRepository, state.contract);
95
- for (const vtxo of cached) {
96
- if (vtxo.isSpent)
97
- continue;
98
- const key = `${vtxo.txid}:${vtxo.vout}`;
99
- state.lastKnownVtxos.set(key, vtxo);
100
- }
101
- }
102
- catch (error) {
103
- // Don't throw — the watcher can still recover via poll and
104
- // subscription events. A failed seed just means the first poll
105
- // may emit some redundant `vtxo_received` events for already
106
- // known vtxos.
107
- console.error(`ContractWatcher: failed to seed lastKnownVtxos for ${state.contract.script}`, error);
108
- }
109
- }
110
- /**
111
- * Update an existing contract.
112
- */
113
- async updateContract(contract) {
114
- const existing = this.contracts.get(contract.script);
115
- if (!existing) {
116
- throw new Error(`Contract ${contract.script} not found`);
117
- }
118
- existing.contract = contract;
119
- if (this.isWatching) {
120
- await this.tryUpdateSubscription();
121
- }
122
- }
123
- /**
124
- * Remove a contract from watching.
125
- */
126
- async removeContract(contractScript) {
127
- const state = this.contracts.get(contractScript);
128
- if (state) {
129
- this.contracts.delete(contractScript);
130
- if (this.isWatching) {
131
- await this.tryUpdateSubscription();
132
- }
133
- }
134
- }
135
- /**
136
- * Get all in-memory contracts.
137
- */
138
- getAllContracts() {
139
- return Array.from(this.contracts.values()).map((s) => s.contract);
140
- }
141
- /**
142
- * Contracts the watcher is actually tracking:
143
- * - all active contracts, plus
144
- * - inactive contracts that still hold known virtual outputs
145
- * (the subscription keeps watching them so `vtxo_spent` events for
146
- * those unspent outputs are still observed).
147
- *
148
- * This is the single source of truth for "contracts whose VTXO state
149
- * we still care about" — callers and the subscription itself fan out
150
- * over the same set so nothing is reconciled that isn't also watched.
151
- */
152
- getWatchedContracts() {
153
- return Array.from(this.contracts.values())
154
- .filter((s) => s.contract.state === "active" || s.lastKnownVtxos.size > 0)
155
- .map((s) => s.contract);
156
- }
157
- /**
158
- * Get virtual outputs for contracts, grouped by contract script.
159
- * @see WalletRepository for `repo`
160
- */
161
- async getContractVtxos(options) {
162
- const { contractScripts, includeSpent } = options;
163
- const repo = this.config.walletRepository;
164
- const contractsToQuery = Array.from(this.contracts.values());
165
- const asyncResults = contractsToQuery
166
- .filter((_) => {
167
- if (contractScripts &&
168
- !contractScripts.includes(_.contract.script))
169
- return false;
170
- return true;
171
- })
172
- .map(async (state) => {
173
- // Use contract address as cache key. Legacy address buckets
174
- // can contain rows from other contracts; gate by script before
175
- // converting so a wrong-script row never reaches the watcher.
176
- const cached = await getVtxosForContract(repo, state.contract);
177
- if (cached.length > 0) {
178
- // Convert to ContractVtxo with contractScript
179
- const contractVtxos = cached.map((v) => ({
180
- ...v,
181
- contractScript: state.contract.script,
182
- }));
183
- const filtered = includeSpent
184
- ? contractVtxos
185
- : contractVtxos.filter((v) => !v.isSpent);
186
- return [[state.contract.script, filtered]];
187
- }
188
- return [];
189
- });
190
- const results = await Promise.all(asyncResults);
191
- return new Map(results.flat(1));
192
- }
193
- /**
194
- * Start watching for virtual output events across all active contracts.
195
- */
196
- async startWatching(callback) {
197
- if (this.isWatching) {
198
- throw new Error("Already watching");
199
- }
200
- this.eventCallback = callback;
201
- this.isWatching = true;
202
- this.abortController = new AbortController();
203
- this.reconnectAttempts = 0;
204
- // Start connection
205
- await this.connect();
206
- // Start failsafe polling
207
- this.startFailsafePolling();
208
- return () => this.stopWatching();
209
- }
210
- /**
211
- * Stop watching for events.
212
- */
213
- async stopWatching() {
214
- this.isWatching = false;
215
- this.connectionState = "disconnected";
216
- this.abortController?.abort();
217
- // Clear timers
218
- if (this.reconnectTimeoutId) {
219
- clearTimeout(this.reconnectTimeoutId);
220
- this.reconnectTimeoutId = undefined;
221
- }
222
- if (this.failsafePollIntervalId) {
223
- clearInterval(this.failsafePollIntervalId);
224
- this.failsafePollIntervalId = undefined;
225
- }
226
- // Unsubscribe
227
- if (this.subscriptionId) {
228
- try {
229
- await this.config.indexerProvider.unsubscribeForScripts(this.subscriptionId);
230
- }
231
- catch {
232
- // Ignore unsubscribe errors
233
- }
234
- this.subscriptionId = undefined;
235
- }
236
- this.eventCallback = undefined;
237
- }
238
- /**
239
- * Check if currently watching.
240
- */
241
- isCurrentlyWatching() {
242
- return this.isWatching;
243
- }
244
- /**
245
- * Get current connection state.
246
- */
247
- getConnectionState() {
248
- return this.connectionState;
249
- }
250
- /**
251
- * Force a poll of all active contracts.
252
- * Useful for manual refresh or after app resume.
253
- */
254
- async forcePoll() {
255
- if (!this.isWatching)
256
- return;
257
- await this.pollAllContracts();
258
- }
259
- /**
260
- * Connect to the subscription.
261
- *
262
- * @param skipUpdate - Skip the leading `updateSubscription` call when
263
- * the caller has already established `subscriptionId`.
264
- */
265
- async connect(skipUpdate = false) {
266
- if (!this.isWatching)
267
- return;
268
- this.connectionState = "connecting";
269
- try {
270
- if (!skipUpdate) {
271
- await this.updateSubscription();
272
- }
273
- // Poll immediately after connection to sync state
274
- await this.pollAllContracts();
275
- this.connectionState = "connected";
276
- this.reconnectAttempts = 0;
277
- // Start listening
278
- this.listenLoop().catch((e) => {
279
- // This is handled asynchronously otherwise `connect()` would hang
280
- // indefinitely and block the caller.
281
- // Error management must be implemented to ensure the connection
282
- // is restored and events are fired.
283
- if (isEventSourceError(e)) {
284
- console.debug("ContractWatcher subscription disconnected; reconnecting");
285
- }
286
- else {
287
- console.error(e);
288
- }
289
- this.connectionState = "disconnected";
290
- this.eventCallback?.({
291
- type: "connection_reset",
292
- timestamp: Date.now(),
293
- });
294
- this.scheduleReconnect();
295
- });
296
- }
297
- catch (error) {
298
- console.error("ContractWatcher connection failed:", error);
299
- this.connectionState = "disconnected";
300
- this.eventCallback?.({
301
- type: "connection_reset",
302
- timestamp: Date.now(),
303
- });
304
- this.scheduleReconnect();
305
- }
306
- }
307
- /**
308
- * Schedule a reconnection attempt.
309
- */
310
- scheduleReconnect() {
311
- if (!this.isWatching)
312
- return;
313
- // Check max attempts
314
- if (this.config.maxReconnectAttempts > 0 &&
315
- this.reconnectAttempts >= this.config.maxReconnectAttempts) {
316
- console.error(`ContractWatcher: Max reconnection attempts (${this.config.maxReconnectAttempts}) reached`);
317
- return;
318
- }
319
- this.connectionState = "reconnecting";
320
- this.reconnectAttempts++;
321
- // Calculate delay with exponential backoff
322
- const delay = Math.min(this.config.reconnectDelayMs *
323
- Math.pow(2, this.reconnectAttempts - 1), this.config.maxReconnectDelayMs);
324
- this.reconnectTimeoutId = setTimeout(() => {
325
- this.reconnectTimeoutId = undefined;
326
- this.connect();
327
- }, delay);
328
- }
329
- /**
330
- * Start the failsafe polling interval.
331
- */
332
- startFailsafePolling() {
333
- if (this.failsafePollIntervalId) {
334
- clearInterval(this.failsafePollIntervalId);
335
- }
336
- this.failsafePollIntervalId = setInterval(() => {
337
- if (this.isWatching) {
338
- this.pollAllContracts().catch((error) => {
339
- console.error("ContractWatcher failsafe poll failed:", error);
340
- });
341
- }
342
- }, this.config.failsafePollIntervalMs);
343
- }
344
- async pollAllContracts() {
345
- const scripts = this.getWatchedContracts().map((c) => c.script);
346
- if (scripts.length === 0)
347
- return;
348
- await this.pollContracts(scripts);
349
- }
350
- /**
351
- * Poll specific contracts and emit events for changes.
352
- */
353
- async pollContracts(contractScripts) {
354
- if (!this.eventCallback)
355
- return;
356
- const now = Date.now();
357
- try {
358
- // Load all the virtual outputs for these contracts, from DB
359
- const vtxosMap = await this.getContractVtxos({
360
- contractScripts,
361
- includeSpent: false, // only spendable ones!
362
- });
363
- for (const contractScript of contractScripts) {
364
- const state = this.contracts.get(contractScript);
365
- if (!state)
366
- continue;
367
- const currentVtxos = vtxosMap.get(contractScript) || [];
368
- const currentKeys = new Set(currentVtxos.map((v) => `${v.txid}:${v.vout}`));
369
- // Find new virtual outputs and add them to the contract's state
370
- const newVtxos = [];
371
- for (const vtxo of currentVtxos) {
372
- const key = `${vtxo.txid}:${vtxo.vout}`;
373
- if (!state.lastKnownVtxos.has(key)) {
374
- newVtxos.push(vtxo);
375
- state.lastKnownVtxos.set(key, vtxo);
376
- }
377
- }
378
- // Find spent virtual outputs and remove them from the contract's state
379
- const spentVtxos = [];
380
- for (const [key, vtxo] of state.lastKnownVtxos) {
381
- if (!currentKeys.has(key)) {
382
- spentVtxos.push(vtxo);
383
- state.lastKnownVtxos.delete(key);
384
- }
385
- }
386
- // Emit events
387
- if (newVtxos.length > 0) {
388
- this.emitVtxoEvent(contractScript, newVtxos, "vtxo_received", now);
389
- }
390
- if (spentVtxos.length > 0) {
391
- // Note: We can't distinguish spent vs swept from polling alone
392
- // The subscription provides more accurate event types
393
- this.emitVtxoEvent(contractScript, spentVtxos, "vtxo_spent", now);
394
- }
395
- }
396
- }
397
- catch (error) {
398
- console.error("ContractWatcher poll failed:", error);
399
- // Don't throw - polling failures shouldn't crash the watcher
400
- }
401
- }
402
- async tryUpdateSubscription() {
403
- const hadSubscription = this.subscriptionId !== undefined;
404
- try {
405
- await this.updateSubscription();
406
- }
407
- catch (error) {
408
- // nothing, the connection will be retried later
409
- return;
410
- }
411
- // Cold start: `startWatching` may have run with zero scripts,
412
- // leaving `listenLoop` parked behind the reconnect timer. Kick
413
- // `connect` now so streaming resumes without waiting on the
414
- // backoff. `skipUpdate` avoids re-issuing `subscribeForScripts`.
415
- const justGotSubscription = !hadSubscription && this.subscriptionId !== undefined;
416
- const listenerParked = this.connectionState === "disconnected" ||
417
- this.connectionState === "reconnecting";
418
- if (this.isWatching && justGotSubscription && listenerParked) {
419
- if (this.reconnectTimeoutId) {
420
- clearTimeout(this.reconnectTimeoutId);
421
- this.reconnectTimeoutId = undefined;
422
- }
423
- this.reconnectAttempts = 0;
424
- this.connect(true).catch((error) => {
425
- console.warn("ContractWatcher cold-start connect failed:", error);
426
- });
427
- }
428
- }
429
- /**
430
- * Update the subscription with scripts that should be watched.
431
- *
432
- * Watches both active contracts and contracts with virtual outputs.
433
- */
434
- async updateSubscription() {
435
- const scriptsToWatch = this.getWatchedContracts().map((c) => c.script);
436
- if (scriptsToWatch.length === 0) {
437
- if (this.subscriptionId) {
438
- try {
439
- await this.config.indexerProvider.unsubscribeForScripts(this.subscriptionId);
440
- }
441
- catch {
442
- // Ignore
443
- }
444
- this.subscriptionId = undefined;
445
- }
446
- return;
447
- }
448
- try {
449
- this.subscriptionId =
450
- await this.config.indexerProvider.subscribeForScripts(scriptsToWatch, this.subscriptionId);
451
- }
452
- catch (error) {
453
- // If we sent a stale subscription ID that the server no longer
454
- // recognises, clear it and retry to create a fresh subscription.
455
- // The server currently returns HTTP 500 with a JSON body whose
456
- // message field looks like "subscription <uuid> not found".
457
- // All other errors (network failures, parse errors, etc.) are rethrown.
458
- const isStale = error instanceof Error &&
459
- /subscription\s+\S+\s+not\s+found/i.test(error.message);
460
- if (this.subscriptionId && isStale) {
461
- this.subscriptionId = undefined;
462
- this.subscriptionId =
463
- await this.config.indexerProvider.subscribeForScripts(scriptsToWatch);
464
- }
465
- else {
466
- throw error;
467
- }
468
- }
469
- }
470
- /**
471
- * Main listening loop for subscription events.
472
- */
473
- async listenLoop() {
474
- if (!this.subscriptionId || !this.abortController || !this.isWatching) {
475
- if (this.isWatching) {
476
- this.connectionState = "disconnected";
477
- this.scheduleReconnect();
478
- }
479
- return;
480
- }
481
- const subscription = this.config.indexerProvider.getSubscription(this.subscriptionId, this.abortController.signal);
482
- for await (const update of subscription) {
483
- if (!this.isWatching)
484
- break;
485
- this.handleSubscriptionUpdate(update);
486
- }
487
- // Stream ended normally - reconnect if still watching
488
- if (this.isWatching) {
489
- this.connectionState = "disconnected";
490
- this.scheduleReconnect();
491
- }
492
- }
493
- /**
494
- * Handle a subscription update.
495
- */
496
- handleSubscriptionUpdate(update) {
497
- if (!this.eventCallback)
498
- return;
499
- const timestamp = Date.now();
500
- if (update.newVtxos?.length) {
501
- this.processSubscriptionVtxos(update.newVtxos, "vtxo_received", timestamp);
502
- }
503
- if (update.spentVtxos?.length) {
504
- this.processSubscriptionVtxos(update.spentVtxos, "vtxo_spent", timestamp);
505
- }
506
- }
507
- /**
508
- * Process virtual outputs from subscription and route each VTXO to the
509
- * single contract that actually locks it via `vtxo.script`. If the script
510
- * doesn't match any watched contract, skip the VTXO rather than fan it
511
- * out to every matching contract — fan-out produced phantom state in
512
- * non-owning contracts that then never reconciled.
513
- */
514
- processSubscriptionVtxos(vtxos, eventType, timestamp) {
515
- const byContract = new Map();
516
- let unknownScript = 0;
517
- for (const vtxo of vtxos) {
518
- if (!this.contracts.has(vtxo.script)) {
519
- unknownScript++;
520
- continue;
521
- }
522
- let bucket = byContract.get(vtxo.script);
523
- if (!bucket) {
524
- bucket = [];
525
- byContract.set(vtxo.script, bucket);
526
- }
527
- bucket.push(vtxo);
528
- }
529
- if (unknownScript > 0) {
530
- // The failsafe poll is the backstop for these; log at debug so we
531
- // can correlate "VTXO state drift" reports with subscription
532
- // drops rather than chase phantom bugs.
533
- console.debug(`ContractWatcher.processSubscriptionVtxos[${eventType}]: dropped ${unknownScript} unknown-script VTXOs (${vtxos.length} total)`);
534
- }
535
- for (const [contractScript, bucketVtxos] of byContract) {
536
- const state = this.contracts.get(contractScript);
537
- if (state) {
538
- for (const vtxo of bucketVtxos) {
539
- const key = `${vtxo.txid}:${vtxo.vout}`;
540
- if (eventType === "vtxo_received") {
541
- state.lastKnownVtxos.set(key, vtxo);
542
- }
543
- else if (eventType === "vtxo_spent") {
544
- state.lastKnownVtxos.delete(key);
545
- }
546
- }
547
- }
548
- this.emitVtxoEvent(contractScript, bucketVtxos, eventType, timestamp);
549
- }
550
- }
551
- /**
552
- * Emit a virtual output event for a contract.
553
- */
554
- emitVtxoEvent(contractScript, vtxos, eventType, timestamp) {
555
- if (!this.eventCallback)
556
- return;
557
- const state = this.contracts.get(contractScript);
558
- if (!state)
559
- return;
560
- const extended = [];
561
- for (const v of vtxos) {
562
- try {
563
- const extendedVtxo = extendVirtualCoinForContract(v, state.contract);
564
- extended.push({ ...extendedVtxo, contractScript });
565
- }
566
- catch {
567
- console.warn("failed to extend vtxo: ", v);
568
- extended.push({ ...v, contractScript });
569
- }
570
- }
571
- switch (eventType) {
572
- case "vtxo_received":
573
- this.eventCallback({
574
- type: "vtxo_received",
575
- vtxos: extended,
576
- contractScript,
577
- contract: state.contract,
578
- timestamp,
579
- });
580
- return;
581
- case "vtxo_spent":
582
- this.eventCallback({
583
- type: "vtxo_spent",
584
- vtxos: extended,
585
- contractScript,
586
- contract: state.contract,
587
- timestamp,
588
- });
589
- return;
590
- default:
591
- return;
592
- }
593
- }
594
- }
@@ -1,90 +0,0 @@
1
- import { hex } from "@scure/base";
2
- import { DefaultVtxo } from "../../script/default.js";
3
- import { isCsvSpendable } from "./helpers.js";
4
- import { sequenceToTimelock, timelockToSequence } from "../../utils/timelock.js";
5
- import { normalizeToDescriptor, extractPubKey, } from "../../identity/descriptor.js";
6
- /**
7
- * Extract pubkey bytes from a descriptor or hex string.
8
- */
9
- function extractPubKeyBytes(value) {
10
- return hex.decode(extractPubKey(normalizeToDescriptor(value)));
11
- }
12
- /**
13
- * Handler for default wallet VTXOs.
14
- *
15
- * Default contracts use the standard forfeit + exit tapscript:
16
- * - forfeit: (Alice + Server) multisig for collaborative spending
17
- * - exit: (Alice) + CSV timelock for unilateral exit
18
- */
19
- export const DefaultContractHandler = {
20
- type: "default",
21
- createScript(params) {
22
- const typed = this.deserializeParams(params);
23
- return new DefaultVtxo.Script(typed);
24
- },
25
- serializeParams(params) {
26
- return {
27
- pubKey: hex.encode(params.pubKey),
28
- serverPubKey: hex.encode(params.serverPubKey),
29
- csvTimelock: timelockToSequence(params.csvTimelock).toString(),
30
- };
31
- },
32
- deserializeParams(params) {
33
- const csvTimelock = params.csvTimelock
34
- ? sequenceToTimelock(Number(params.csvTimelock))
35
- : DefaultVtxo.Script.DEFAULT_TIMELOCK;
36
- return {
37
- pubKey: extractPubKeyBytes(params.pubKey),
38
- serverPubKey: extractPubKeyBytes(params.serverPubKey),
39
- csvTimelock,
40
- };
41
- },
42
- selectPath(script, contract, context) {
43
- if (context.collaborative) {
44
- // Use forfeit path for collaborative spending
45
- return { leaf: script.forfeit() };
46
- }
47
- // Use exit path for unilateral exit (only if CSV is satisfied)
48
- const sequence = contract.params.csvTimelock
49
- ? Number(contract.params.csvTimelock)
50
- : undefined;
51
- if (!isCsvSpendable(context, sequence)) {
52
- return null;
53
- }
54
- return {
55
- leaf: script.exit(),
56
- sequence,
57
- };
58
- },
59
- getAllSpendingPaths(script, contract, context) {
60
- const paths = [];
61
- // Forfeit path available with server cooperation
62
- if (context.collaborative) {
63
- paths.push({ leaf: script.forfeit() });
64
- }
65
- // Exit path always possible (CSV checked at tx time)
66
- const exitPath = { leaf: script.exit() };
67
- if (contract.params.csvTimelock) {
68
- exitPath.sequence = Number(contract.params.csvTimelock);
69
- }
70
- paths.push(exitPath);
71
- return paths;
72
- },
73
- getSpendablePaths(script, contract, context) {
74
- const paths = [];
75
- if (context.collaborative) {
76
- paths.push({ leaf: script.forfeit() });
77
- }
78
- const exitSequence = contract.params.csvTimelock
79
- ? Number(contract.params.csvTimelock)
80
- : undefined;
81
- if (isCsvSpendable(context, exitSequence)) {
82
- const exitPath = { leaf: script.exit() };
83
- if (exitSequence !== undefined) {
84
- exitPath.sequence = exitSequence;
85
- }
86
- paths.push(exitPath);
87
- }
88
- return paths;
89
- },
90
- };