@arkade-os/sdk 0.4.27 → 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 (531) hide show
  1. package/README.md +31 -100
  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 -649
  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 -20
  156. package/dist/cjs/identity/index.js +0 -40
  157. package/dist/cjs/identity/seedIdentity.js +0 -477
  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 -202
  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 -308
  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 -188
  236. package/dist/cjs/wallet/index.js +0 -82
  237. package/dist/cjs/wallet/inputSignerRouter.js +0 -98
  238. package/dist/cjs/wallet/onchain.js +0 -290
  239. package/dist/cjs/wallet/ramps.js +0 -216
  240. package/dist/cjs/wallet/serviceWorker/wallet-message-handler.js +0 -953
  241. package/dist/cjs/wallet/serviceWorker/wallet.js +0 -1174
  242. package/dist/cjs/wallet/signingErrors.js +0 -32
  243. package/dist/cjs/wallet/unroll.js +0 -293
  244. package/dist/cjs/wallet/utils.js +0 -111
  245. package/dist/cjs/wallet/validation.js +0 -154
  246. package/dist/cjs/wallet/vtxo-manager.js +0 -1142
  247. package/dist/cjs/wallet/wallet.js +0 -2195
  248. package/dist/cjs/wallet/walletReceiveRotator.js +0 -547
  249. package/dist/cjs/worker/browser/service-worker-manager.js +0 -183
  250. package/dist/cjs/worker/browser/utils.js +0 -67
  251. package/dist/cjs/worker/errors.js +0 -16
  252. package/dist/cjs/worker/expo/asyncStorageTaskQueue.js +0 -78
  253. package/dist/cjs/worker/expo/index.js +0 -13
  254. package/dist/cjs/worker/expo/processors/contractPollProcessor.js +0 -62
  255. package/dist/cjs/worker/expo/processors/index.js +0 -6
  256. package/dist/cjs/worker/expo/taskQueue.js +0 -41
  257. package/dist/cjs/worker/expo/taskRunner.js +0 -73
  258. package/dist/cjs/worker/messageBus.js +0 -474
  259. package/dist/esm/adapters/asyncStorage.js +0 -1
  260. package/dist/esm/adapters/expo.js +0 -3
  261. package/dist/esm/adapters/fileSystem.js +0 -1
  262. package/dist/esm/adapters/indexedDB.js +0 -1
  263. package/dist/esm/adapters/localStorage.js +0 -1
  264. package/dist/esm/arkfee/celenv.js +0 -40
  265. package/dist/esm/arkfee/estimator.js +0 -139
  266. package/dist/esm/arkfee/index.js +0 -1
  267. package/dist/esm/arkfee/types.js +0 -22
  268. package/dist/esm/arknote/index.js +0 -124
  269. package/dist/esm/bip322/index.js +0 -267
  270. package/dist/esm/contracts/arkcontract.js +0 -140
  271. package/dist/esm/contracts/contractManager.js +0 -645
  272. package/dist/esm/contracts/contractWatcher.js +0 -594
  273. package/dist/esm/contracts/handlers/default.js +0 -90
  274. package/dist/esm/contracts/handlers/delegate.js +0 -87
  275. package/dist/esm/contracts/handlers/helpers.js +0 -110
  276. package/dist/esm/contracts/handlers/index.js +0 -12
  277. package/dist/esm/contracts/handlers/registry.js +0 -86
  278. package/dist/esm/contracts/handlers/vhtlc.js +0 -191
  279. package/dist/esm/contracts/index.js +0 -13
  280. package/dist/esm/contracts/types.js +0 -1
  281. package/dist/esm/contracts/vtxoOwnership.js +0 -69
  282. package/dist/esm/extension/asset/assetGroup.js +0 -224
  283. package/dist/esm/extension/asset/assetId.js +0 -148
  284. package/dist/esm/extension/asset/assetInput.js +0 -217
  285. package/dist/esm/extension/asset/assetOutput.js +0 -169
  286. package/dist/esm/extension/asset/assetRef.js +0 -144
  287. package/dist/esm/extension/asset/index.js +0 -8
  288. package/dist/esm/extension/asset/metadata.js +0 -182
  289. package/dist/esm/extension/asset/packet.js +0 -110
  290. package/dist/esm/extension/asset/types.js +0 -19
  291. package/dist/esm/extension/asset/utils.js +0 -99
  292. package/dist/esm/extension/index.js +0 -248
  293. package/dist/esm/extension/packet.js +0 -16
  294. package/dist/esm/forfeit.js +0 -41
  295. package/dist/esm/identity/descriptor.js +0 -161
  296. package/dist/esm/identity/descriptorProvider.js +0 -1
  297. package/dist/esm/identity/hdCapableIdentity.js +0 -17
  298. package/dist/esm/identity/index.js +0 -13
  299. package/dist/esm/identity/seedIdentity.js +0 -469
  300. package/dist/esm/identity/serialize.js +0 -164
  301. package/dist/esm/identity/singleKey.js +0 -121
  302. package/dist/esm/identity/staticDescriptorProvider.js +0 -61
  303. package/dist/esm/index.js +0 -87
  304. package/dist/esm/intent/index.js +0 -255
  305. package/dist/esm/musig2/index.js +0 -3
  306. package/dist/esm/musig2/keys.js +0 -21
  307. package/dist/esm/musig2/nonces.js +0 -11
  308. package/dist/esm/musig2/sign.js +0 -63
  309. package/dist/esm/networks.js +0 -22
  310. package/dist/esm/package.json +0 -3
  311. package/dist/esm/providers/ark.js +0 -572
  312. package/dist/esm/providers/delegator.js +0 -81
  313. package/dist/esm/providers/electrum.js +0 -864
  314. package/dist/esm/providers/errors.js +0 -54
  315. package/dist/esm/providers/expoArk.js +0 -78
  316. package/dist/esm/providers/expoIndexer.js +0 -107
  317. package/dist/esm/providers/expoUtils.js +0 -87
  318. package/dist/esm/providers/indexer.js +0 -626
  319. package/dist/esm/providers/onchain.js +0 -258
  320. package/dist/esm/providers/utils.js +0 -117
  321. package/dist/esm/repositories/contractRepository.js +0 -1
  322. package/dist/esm/repositories/inMemory/contractRepository.js +0 -51
  323. package/dist/esm/repositories/inMemory/walletRepository.js +0 -111
  324. package/dist/esm/repositories/index.js +0 -10
  325. package/dist/esm/repositories/indexedDB/contractRepository.js +0 -183
  326. package/dist/esm/repositories/indexedDB/db.js +0 -4
  327. package/dist/esm/repositories/indexedDB/manager.js +0 -95
  328. package/dist/esm/repositories/indexedDB/schema.js +0 -199
  329. package/dist/esm/repositories/indexedDB/walletRepository.js +0 -470
  330. package/dist/esm/repositories/indexedDB/websqlAdapter.js +0 -138
  331. package/dist/esm/repositories/migrations/contractRepositoryImpl.js +0 -121
  332. package/dist/esm/repositories/migrations/fromStorageAdapter.js +0 -58
  333. package/dist/esm/repositories/migrations/walletRepositoryImpl.js +0 -180
  334. package/dist/esm/repositories/realm/contractRepository.js +0 -112
  335. package/dist/esm/repositories/realm/index.js +0 -3
  336. package/dist/esm/repositories/realm/schemas.js +0 -153
  337. package/dist/esm/repositories/realm/types.js +0 -6
  338. package/dist/esm/repositories/realm/walletRepository.js +0 -301
  339. package/dist/esm/repositories/scriptFromAddress.js +0 -13
  340. package/dist/esm/repositories/serialization.js +0 -67
  341. package/dist/esm/repositories/sqlite/contractRepository.js +0 -131
  342. package/dist/esm/repositories/sqlite/index.js +0 -2
  343. package/dist/esm/repositories/sqlite/types.js +0 -1
  344. package/dist/esm/repositories/sqlite/walletRepository.js +0 -437
  345. package/dist/esm/repositories/walletRepository.js +0 -1
  346. package/dist/esm/script/address.js +0 -104
  347. package/dist/esm/script/base.js +0 -179
  348. package/dist/esm/script/default.js +0 -54
  349. package/dist/esm/script/delegate.js +0 -50
  350. package/dist/esm/script/tapscript.js +0 -615
  351. package/dist/esm/script/vhtlc.js +0 -167
  352. package/dist/esm/storage/asyncStorage.js +0 -46
  353. package/dist/esm/storage/fileSystem.js +0 -104
  354. package/dist/esm/storage/inMemory.js +0 -20
  355. package/dist/esm/storage/index.js +0 -1
  356. package/dist/esm/storage/indexedDB.js +0 -97
  357. package/dist/esm/storage/localStorage.js +0 -47
  358. package/dist/esm/tree/signingSession.js +0 -191
  359. package/dist/esm/tree/txTree.js +0 -188
  360. package/dist/esm/tree/validation.js +0 -101
  361. package/dist/esm/utils/anchor.js +0 -31
  362. package/dist/esm/utils/arkTransaction.js +0 -264
  363. package/dist/esm/utils/bip21.js +0 -123
  364. package/dist/esm/utils/syncCursors.js +0 -119
  365. package/dist/esm/utils/timelock.js +0 -22
  366. package/dist/esm/utils/transaction.js +0 -24
  367. package/dist/esm/utils/transactionHistory.js +0 -180
  368. package/dist/esm/utils/txSizeEstimator.js +0 -128
  369. package/dist/esm/utils/unknownFields.js +0 -169
  370. package/dist/esm/wallet/asset-manager.js +0 -325
  371. package/dist/esm/wallet/asset.js +0 -113
  372. package/dist/esm/wallet/batch.js +0 -180
  373. package/dist/esm/wallet/delegator.js +0 -303
  374. package/dist/esm/wallet/expo/background.js +0 -111
  375. package/dist/esm/wallet/expo/index.js +0 -2
  376. package/dist/esm/wallet/expo/wallet.js +0 -193
  377. package/dist/esm/wallet/hdDescriptorProvider.js +0 -184
  378. package/dist/esm/wallet/index.js +0 -75
  379. package/dist/esm/wallet/inputSignerRouter.js +0 -94
  380. package/dist/esm/wallet/onchain.js +0 -285
  381. package/dist/esm/wallet/ramps.js +0 -212
  382. package/dist/esm/wallet/serviceWorker/wallet-message-handler.js +0 -946
  383. package/dist/esm/wallet/serviceWorker/wallet.js +0 -1169
  384. package/dist/esm/wallet/signingErrors.js +0 -27
  385. package/dist/esm/wallet/unroll.js +0 -289
  386. package/dist/esm/wallet/utils.js +0 -103
  387. package/dist/esm/wallet/validation.js +0 -142
  388. package/dist/esm/wallet/vtxo-manager.js +0 -1136
  389. package/dist/esm/wallet/wallet.js +0 -2186
  390. package/dist/esm/wallet/walletReceiveRotator.js +0 -540
  391. package/dist/esm/worker/browser/service-worker-manager.js +0 -177
  392. package/dist/esm/worker/browser/utils.js +0 -63
  393. package/dist/esm/worker/errors.js +0 -11
  394. package/dist/esm/worker/expo/asyncStorageTaskQueue.js +0 -74
  395. package/dist/esm/worker/expo/index.js +0 -4
  396. package/dist/esm/worker/expo/processors/contractPollProcessor.js +0 -59
  397. package/dist/esm/worker/expo/processors/index.js +0 -1
  398. package/dist/esm/worker/expo/taskQueue.js +0 -37
  399. package/dist/esm/worker/expo/taskRunner.js +0 -69
  400. package/dist/esm/worker/messageBus.js +0 -470
  401. package/dist/types/adapters/asyncStorage.d.ts +0 -2
  402. package/dist/types/adapters/expo.d.ts +0 -4
  403. package/dist/types/adapters/fileSystem.d.ts +0 -2
  404. package/dist/types/adapters/indexedDB.d.ts +0 -2
  405. package/dist/types/adapters/localStorage.d.ts +0 -2
  406. package/dist/types/arkfee/celenv.d.ts +0 -25
  407. package/dist/types/arkfee/estimator.d.ts +0 -49
  408. package/dist/types/arkfee/index.d.ts +0 -2
  409. package/dist/types/arkfee/types.d.ts +0 -38
  410. package/dist/types/arknote/index.d.ts +0 -84
  411. package/dist/types/bip322/index.d.ts +0 -55
  412. package/dist/types/contracts/arkcontract.d.ts +0 -99
  413. package/dist/types/contracts/contractManager.d.ts +0 -411
  414. package/dist/types/contracts/contractWatcher.d.ts +0 -217
  415. package/dist/types/contracts/handlers/default.d.ts +0 -19
  416. package/dist/types/contracts/handlers/delegate.d.ts +0 -21
  417. package/dist/types/contracts/handlers/helpers.d.ts +0 -19
  418. package/dist/types/contracts/handlers/index.d.ts +0 -7
  419. package/dist/types/contracts/handlers/registry.d.ts +0 -65
  420. package/dist/types/contracts/handlers/vhtlc.d.ts +0 -32
  421. package/dist/types/contracts/index.d.ts +0 -14
  422. package/dist/types/contracts/types.d.ts +0 -250
  423. package/dist/types/contracts/vtxoOwnership.d.ts +0 -33
  424. package/dist/types/extension/asset/assetGroup.d.ts +0 -119
  425. package/dist/types/extension/asset/assetId.d.ts +0 -83
  426. package/dist/types/extension/asset/assetInput.d.ts +0 -64
  427. package/dist/types/extension/asset/assetOutput.d.ts +0 -54
  428. package/dist/types/extension/asset/assetRef.d.ts +0 -91
  429. package/dist/types/extension/asset/index.d.ts +0 -8
  430. package/dist/types/extension/asset/metadata.d.ts +0 -52
  431. package/dist/types/extension/asset/packet.d.ts +0 -41
  432. package/dist/types/extension/asset/types.d.ts +0 -16
  433. package/dist/types/extension/asset/utils.d.ts +0 -21
  434. package/dist/types/extension/index.d.ts +0 -56
  435. package/dist/types/extension/packet.d.ts +0 -21
  436. package/dist/types/forfeit.d.ts +0 -18
  437. package/dist/types/identity/descriptor.d.ts +0 -61
  438. package/dist/types/identity/descriptorProvider.d.ts +0 -42
  439. package/dist/types/identity/hdCapableIdentity.d.ts +0 -71
  440. package/dist/types/identity/index.d.ts +0 -57
  441. package/dist/types/identity/seedIdentity.d.ts +0 -270
  442. package/dist/types/identity/serialize.d.ts +0 -96
  443. package/dist/types/identity/singleKey.d.ts +0 -62
  444. package/dist/types/identity/staticDescriptorProvider.d.ts +0 -18
  445. package/dist/types/index.d.ts +0 -59
  446. package/dist/types/intent/index.d.ts +0 -86
  447. package/dist/types/musig2/index.d.ts +0 -4
  448. package/dist/types/musig2/keys.d.ts +0 -9
  449. package/dist/types/musig2/nonces.d.ts +0 -14
  450. package/dist/types/musig2/sign.d.ts +0 -27
  451. package/dist/types/networks.d.ts +0 -16
  452. package/dist/types/providers/ark.d.ts +0 -369
  453. package/dist/types/providers/delegator.d.ts +0 -82
  454. package/dist/types/providers/electrum.d.ts +0 -312
  455. package/dist/types/providers/errors.d.ts +0 -13
  456. package/dist/types/providers/expoArk.d.ts +0 -22
  457. package/dist/types/providers/expoIndexer.d.ts +0 -18
  458. package/dist/types/providers/expoUtils.d.ts +0 -18
  459. package/dist/types/providers/indexer.d.ts +0 -301
  460. package/dist/types/providers/onchain.d.ts +0 -148
  461. package/dist/types/providers/utils.d.ts +0 -12
  462. package/dist/types/repositories/contractRepository.d.ts +0 -32
  463. package/dist/types/repositories/inMemory/contractRepository.d.ts +0 -17
  464. package/dist/types/repositories/inMemory/walletRepository.d.ts +0 -29
  465. package/dist/types/repositories/index.d.ts +0 -9
  466. package/dist/types/repositories/indexedDB/contractRepository.d.ts +0 -21
  467. package/dist/types/repositories/indexedDB/db.d.ts +0 -4
  468. package/dist/types/repositories/indexedDB/manager.d.ts +0 -25
  469. package/dist/types/repositories/indexedDB/schema.d.ts +0 -9
  470. package/dist/types/repositories/indexedDB/walletRepository.d.ts +0 -28
  471. package/dist/types/repositories/indexedDB/websqlAdapter.d.ts +0 -49
  472. package/dist/types/repositories/migrations/contractRepositoryImpl.d.ts +0 -24
  473. package/dist/types/repositories/migrations/fromStorageAdapter.d.ts +0 -19
  474. package/dist/types/repositories/migrations/walletRepositoryImpl.d.ts +0 -27
  475. package/dist/types/repositories/realm/contractRepository.d.ts +0 -24
  476. package/dist/types/repositories/realm/index.d.ts +0 -4
  477. package/dist/types/repositories/realm/types.d.ts +0 -16
  478. package/dist/types/repositories/realm/walletRepository.d.ts +0 -34
  479. package/dist/types/repositories/scriptFromAddress.d.ts +0 -9
  480. package/dist/types/repositories/serialization.d.ts +0 -65
  481. package/dist/types/repositories/sqlite/contractRepository.d.ts +0 -33
  482. package/dist/types/repositories/sqlite/index.d.ts +0 -3
  483. package/dist/types/repositories/sqlite/types.d.ts +0 -18
  484. package/dist/types/repositories/walletRepository.d.ts +0 -72
  485. package/dist/types/script/address.d.ts +0 -67
  486. package/dist/types/script/base.d.ts +0 -105
  487. package/dist/types/script/default.d.ts +0 -44
  488. package/dist/types/script/delegate.d.ts +0 -40
  489. package/dist/types/script/tapscript.d.ts +0 -169
  490. package/dist/types/script/vhtlc.d.ts +0 -66
  491. package/dist/types/tree/signingSession.d.ts +0 -37
  492. package/dist/types/tree/txTree.d.ts +0 -28
  493. package/dist/types/tree/validation.d.ts +0 -15
  494. package/dist/types/utils/anchor.d.ts +0 -19
  495. package/dist/types/utils/arkTransaction.d.ts +0 -49
  496. package/dist/types/utils/bip21.d.ts +0 -38
  497. package/dist/types/utils/syncCursors.d.ts +0 -60
  498. package/dist/types/utils/timelock.d.ts +0 -9
  499. package/dist/types/utils/transaction.d.ts +0 -13
  500. package/dist/types/utils/transactionHistory.d.ts +0 -15
  501. package/dist/types/utils/txSizeEstimator.d.ts +0 -40
  502. package/dist/types/utils/unknownFields.d.ts +0 -83
  503. package/dist/types/wallet/asset-manager.d.ts +0 -69
  504. package/dist/types/wallet/asset.d.ts +0 -21
  505. package/dist/types/wallet/batch.d.ts +0 -107
  506. package/dist/types/wallet/delegator.d.ts +0 -48
  507. package/dist/types/wallet/expo/background.d.ts +0 -66
  508. package/dist/types/wallet/expo/index.d.ts +0 -4
  509. package/dist/types/wallet/hdDescriptorProvider.d.ts +0 -114
  510. package/dist/types/wallet/index.d.ts +0 -789
  511. package/dist/types/wallet/inputSignerRouter.d.ts +0 -35
  512. package/dist/types/wallet/onchain.d.ts +0 -109
  513. package/dist/types/wallet/ramps.d.ts +0 -64
  514. package/dist/types/wallet/serviceWorker/wallet-message-handler.d.ts +0 -543
  515. package/dist/types/wallet/serviceWorker/wallet.d.ts +0 -248
  516. package/dist/types/wallet/signingErrors.d.ts +0 -19
  517. package/dist/types/wallet/unroll.d.ts +0 -114
  518. package/dist/types/wallet/utils.d.ts +0 -36
  519. package/dist/types/wallet/validation.d.ts +0 -24
  520. package/dist/types/wallet/vtxo-manager.d.ts +0 -476
  521. package/dist/types/wallet/wallet.d.ts +0 -409
  522. package/dist/types/wallet/walletReceiveRotator.d.ts +0 -306
  523. package/dist/types/worker/browser/service-worker-manager.d.ts +0 -32
  524. package/dist/types/worker/browser/utils.d.ts +0 -17
  525. package/dist/types/worker/errors.d.ts +0 -7
  526. package/dist/types/worker/expo/index.d.ts +0 -7
  527. package/dist/types/worker/expo/processors/contractPollProcessor.d.ts +0 -19
  528. package/dist/types/worker/expo/processors/index.d.ts +0 -1
  529. package/dist/types/worker/expo/taskQueue.d.ts +0 -50
  530. package/dist/types/worker/expo/taskRunner.d.ts +0 -66
  531. package/dist/types/worker/messageBus.d.ts +0 -189
@@ -1,1142 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.VtxoManager = exports.DEFAULT_SETTLEMENT_CONFIG = exports.DEFAULT_RENEWAL_CONFIG = exports.DEFAULT_THRESHOLD_MS = exports.DEFAULT_THRESHOLD_SECONDS = void 0;
4
- exports.isVtxoExpiringSoon = isVtxoExpiringSoon;
5
- exports.getExpiringAndRecoverableVtxos = getExpiringAndRecoverableVtxos;
6
- const _1 = require(".");
7
- const errors_1 = require("../providers/errors");
8
- const arkTransaction_1 = require("../utils/arkTransaction");
9
- const tapscript_1 = require("../script/tapscript");
10
- const base_1 = require("@scure/base");
11
- const base_2 = require("../script/base");
12
- const transaction_1 = require("../utils/transaction");
13
- const txSizeEstimator_1 = require("../utils/txSizeEstimator");
14
- const arkfee_1 = require("../arkfee");
15
- const address_1 = require("../script/address");
16
- /**
17
- * Return whether a wallet exposes the properties required for boarding input sweep operations.
18
- *
19
- * @param wallet - Wallet to inspect
20
- * @returns `true` when the wallet supports boarding input sweep operations.
21
- */
22
- function isSweepCapable(wallet) {
23
- return ("boardingTapscript" in wallet &&
24
- "onchainProvider" in wallet &&
25
- "arkProvider" in wallet &&
26
- "network" in wallet);
27
- }
28
- /**
29
- * Assert that the wallet supports boarding input sweep operations.
30
- *
31
- * @param wallet - Wallet to inspect
32
- * @throws Error if the wallet does not support boarding input sweep operations.
33
- */
34
- function assertSweepCapable(wallet) {
35
- if (!isSweepCapable(wallet)) {
36
- throw new Error("Boarding UTXO sweep requires a Wallet instance with boardingTapscript, onchainProvider, arkProvider, and network");
37
- }
38
- }
39
- /**
40
- * Web Locks name used to serialize boarding-poll work across same-origin
41
- * browser contexts (tabs, service worker). Static because the goal is to
42
- * deduplicate polls for the *same* wallet — two distinct wallets on the
43
- * same origin will take turns, which is acceptable.
44
- */
45
- const BOARDING_POLL_LOCK_NAME = "arkade-boarding-poll";
46
- /**
47
- * Run `fn` under an exclusive Web Lock when the runtime provides one
48
- * (browser main thread, service worker). In environments without
49
- * `navigator.locks` (Node, React Native) the callback runs immediately
50
- * with no coordination.
51
- *
52
- * Uses `ifAvailable: true`: if another context already holds the lock,
53
- * skip this cycle entirely rather than queueing — the other context will
54
- * do the work and the next poll will re-check.
55
- */
56
- async function runWithCrossInstanceLock(name, fn) {
57
- const locks = typeof globalThis !== "undefined" &&
58
- typeof globalThis.navigator !== "undefined"
59
- ? globalThis.navigator.locks
60
- : undefined;
61
- if (!locks) {
62
- await fn();
63
- return;
64
- }
65
- await locks.request(name, { ifAvailable: true, mode: "exclusive" }, async (lock) => {
66
- if (lock === null)
67
- return;
68
- await fn();
69
- });
70
- }
71
- /** Default renewal threshold in seconds (3 days). */
72
- exports.DEFAULT_THRESHOLD_SECONDS = 3 * 24 * 60 * 60;
73
- /**
74
- * Default renewal threshold in milliseconds (3 days).
75
- */
76
- exports.DEFAULT_THRESHOLD_MS = exports.DEFAULT_THRESHOLD_SECONDS * 1000;
77
- /**
78
- * Default renewal configuration values.
79
- *
80
- * @see RenewalConfig
81
- * @deprecated Leave `renewalConfig` undefined and use `settlementConfig` instead.
82
- * @see SettlementConfig
83
- */
84
- exports.DEFAULT_RENEWAL_CONFIG = {
85
- thresholdMs: exports.DEFAULT_THRESHOLD_MS, // 3 days
86
- };
87
- /**
88
- * Default settlement configuration values.
89
- *
90
- * @see SettlementConfig
91
- *
92
- * @example
93
- * ```typescript
94
- * const wallet = await Wallet.create({
95
- * identity,
96
- * arkServerUrl: 'https://arkade.computer',
97
- * settlementConfig: DEFAULT_SETTLEMENT_CONFIG,
98
- * })
99
- * ```
100
- */
101
- exports.DEFAULT_SETTLEMENT_CONFIG = {
102
- vtxoThreshold: exports.DEFAULT_THRESHOLD_SECONDS,
103
- boardingUtxoSweep: true,
104
- pollIntervalMs: 60000,
105
- };
106
- /** Extracts the dust amount from the wallet, defaulting to 330 sats. */
107
- function getDustAmount(wallet) {
108
- return "dustAmount" in wallet ? wallet.dustAmount : 330n;
109
- }
110
- /**
111
- * Filter virtual outputs that are recoverable (swept and still spendable, or preconfirmed subdust)
112
- *
113
- * Recovery strategy:
114
- * - Always recover swept virtual outputs (they've been taken by the server)
115
- * - Only recover subdust preconfirmed virtual outputs (to avoid locking liquidity on settled virtual outputs with long expiry)
116
- *
117
- * @param vtxos - Array of virtual outputs to check
118
- * @param dustAmount - Dust threshold to identify subdust
119
- * @returns Array of recoverable virtual outputs
120
- */
121
- function getRecoverableVtxos(vtxos, dustAmount) {
122
- return vtxos.filter((vtxo) => {
123
- // Always recover swept virtual outputs
124
- if ((0, _1.isRecoverable)(vtxo)) {
125
- return true;
126
- }
127
- // also include virtual outputs that are not swept but expired
128
- if ((0, _1.isSpendable)(vtxo) && (0, _1.isExpired)(vtxo)) {
129
- return true;
130
- }
131
- // Recover preconfirmed subdust to consolidate small amounts
132
- if (vtxo.virtualStatus.state === "preconfirmed" &&
133
- (0, _1.isSubdust)(vtxo, dustAmount)) {
134
- return true;
135
- }
136
- return false;
137
- });
138
- }
139
- /**
140
- * Get recoverable virtual outputs including subdust outputs if the total value exceeds dust threshold.
141
- *
142
- * Decision is based on the combined total of ALL recoverable virtual outputs (regular + subdust),
143
- * not just the subdust portion alone.
144
- *
145
- * @param vtxos - Array of virtual outputs to check
146
- * @param dustAmount - Dust threshold amount in satoshis
147
- * @returns Object containing recoverable virtual outputs and whether subdust should be included
148
- */
149
- function getRecoverableWithSubdust(vtxos, dustAmount) {
150
- const recoverableVtxos = getRecoverableVtxos(vtxos, dustAmount);
151
- // Separate subdust from regular recoverable
152
- const subdust = [];
153
- const regular = [];
154
- for (const vtxo of recoverableVtxos) {
155
- if ((0, _1.isSubdust)(vtxo, dustAmount)) {
156
- subdust.push(vtxo);
157
- }
158
- else {
159
- regular.push(vtxo);
160
- }
161
- }
162
- // Calculate totals
163
- const regularTotal = regular.reduce((sum, vtxo) => sum + BigInt(vtxo.value), 0n);
164
- const subdustTotal = subdust.reduce((sum, vtxo) => sum + BigInt(vtxo.value), 0n);
165
- const combinedTotal = regularTotal + subdustTotal;
166
- // Include subdust only if the combined total exceeds dust threshold
167
- const shouldIncludeSubdust = combinedTotal >= dustAmount;
168
- const vtxosToRecover = shouldIncludeSubdust ? recoverableVtxos : regular;
169
- const totalAmount = vtxosToRecover.reduce((sum, vtxo) => sum + BigInt(vtxo.value), 0n);
170
- return {
171
- vtxosToRecover,
172
- includesSubdust: shouldIncludeSubdust,
173
- totalAmount,
174
- };
175
- }
176
- /**
177
- * Check if a virtual output is expiring soon based on threshold
178
- *
179
- * @param vtxo - The virtual output to check
180
- * @param thresholdMs - Threshold in milliseconds from now
181
- * @returns true if virtual output expires within threshold, false otherwise
182
- */
183
- function isVtxoExpiringSoon(vtxo, thresholdMs // in milliseconds
184
- ) {
185
- const realThresholdMs = thresholdMs <= 100 ? exports.DEFAULT_THRESHOLD_MS : thresholdMs;
186
- const { batchExpiry } = vtxo.virtualStatus;
187
- if (!batchExpiry)
188
- return false; // it doesn't expire
189
- // we use this as a workaround to avoid issue on regtest where expiry date is
190
- // expressed in blockheight instead of timestamp. If expiry, as Date, is before 2025,
191
- // then we admit it's too small to be a timestamp
192
- // TODO: API should return the expiry unit
193
- const expireAt = new Date(batchExpiry);
194
- if (expireAt.getFullYear() < 2025)
195
- return false;
196
- const now = Date.now();
197
- if (batchExpiry <= now)
198
- return false; // already expired
199
- return batchExpiry - now <= realThresholdMs;
200
- }
201
- /**
202
- * Filter virtual outputs that are expiring soon or are recoverable/subdust
203
- *
204
- * @param vtxos - Array of virtual outputs to check
205
- * @param thresholdMs - Threshold in milliseconds from now
206
- * @param dustAmount - Dust threshold amount in satoshis
207
- * @returns Array of virtual outputs expiring within threshold
208
- */
209
- function getExpiringAndRecoverableVtxos(vtxos, thresholdMs, dustAmount) {
210
- return vtxos.filter((vtxo) => isVtxoExpiringSoon(vtxo, thresholdMs) ||
211
- (0, _1.isRecoverable)(vtxo) ||
212
- ((0, _1.isSpendable)(vtxo) && (0, _1.isExpired)(vtxo)) ||
213
- (0, _1.isSubdust)(vtxo, dustAmount));
214
- }
215
- class VtxoManager {
216
- constructor(wallet,
217
- /** @deprecated Use settlementConfig instead */
218
- renewalConfig, settlementConfig) {
219
- this.wallet = wallet;
220
- this.renewalConfig = renewalConfig;
221
- this.knownBoardingUtxos = new Set();
222
- this.sweptBoardingUtxos = new Set();
223
- this.pollInProgress = false;
224
- this.disposed = false;
225
- this.consecutivePollFailures = 0;
226
- // Guards against renewal feedback loop: when renewVtxos() settles, the
227
- // server emits new VTXOs → vtxo_received → renewVtxos() again → infinite loop.
228
- this.renewalInProgress = false;
229
- this.lastRenewalTimestamp = 0;
230
- // Guards against a retry treadmill on the periodic-settle path: a failing
231
- // settle would otherwise re-submit identical intents on every 60s poll,
232
- // producing per-minute DeleteIntent RPCs forever. Mirrors the renewal
233
- // cooldown but with exponential backoff on consecutive failures, so a
234
- // persistently broken input eventually drops to the backoff cap instead
235
- // of hammering the server. Shared across boarding + expiring-VTXO work
236
- // because they now ride on the same settle intent.
237
- this.lastPeriodicSettleTimestamp = 0;
238
- this.consecutivePeriodicSettleFailures = 0;
239
- // Throttle for the VTXO_ALREADY_SPENT -> refreshVtxos() reconciliation.
240
- // The server's authoritative view says our local cache is stale, so we
241
- // trigger a full refresh to advance the global sync cursor. Rate-limit
242
- // to guard against a buggy indexer cycling us into a refresh storm.
243
- this.lastVtxoSpentRefreshTimestamp = 0;
244
- // Normalize: prefer settlementConfig, fall back to renewalConfig, default to enabled
245
- if (settlementConfig !== undefined) {
246
- this.settlementConfig = settlementConfig;
247
- }
248
- else if (renewalConfig && renewalConfig.enabled) {
249
- this.settlementConfig = {
250
- vtxoThreshold: renewalConfig.thresholdMs
251
- ? renewalConfig.thresholdMs / 1000
252
- : undefined,
253
- };
254
- }
255
- else if (renewalConfig) {
256
- // renewalConfig provided but not enabled → disabled
257
- this.settlementConfig = false;
258
- }
259
- else {
260
- // No config at all → enabled by default
261
- this.settlementConfig = { ...exports.DEFAULT_SETTLEMENT_CONFIG };
262
- }
263
- this.contractEventsSubscriptionReady =
264
- this.initializeSubscription().then((subscription) => {
265
- this.contractEventsSubscription = subscription;
266
- return subscription;
267
- });
268
- }
269
- // ========== Recovery Methods ==========
270
- /**
271
- * Recover swept/expired virtual outputs by settling them back to the wallet's Arkade address.
272
- *
273
- * This method:
274
- * 1. Fetches all virtual outputs (including recoverable ones)
275
- * 2. Filters for swept but still spendable virtual outputs and preconfirmed subdust
276
- * 3. Includes subdust virtual outputs if the total value >= dust threshold
277
- * 4. Settles everything back to the wallet's Arkade address
278
- *
279
- * Note: Settled virtual outputs with long expiry are NOT recovered to avoid locking liquidity unnecessarily.
280
- * Only preconfirmed subdust is recovered to consolidate small amounts.
281
- *
282
- * @param eventCallback - Optional callback to receive settlement events
283
- * @returns Settlement transaction ID
284
- * @throws Error if no recoverable virtual outputs found
285
- *
286
- * @example
287
- * ```typescript
288
- * const manager = await wallet.getVtxoManager();
289
- *
290
- * // Simple recovery
291
- * const txid = await manager.recoverVtxos();
292
- *
293
- * // With event callback
294
- * const txid = await manager.recoverVtxos((event) => {
295
- * console.log('Settlement event:', event.type);
296
- * });
297
- * ```
298
- */
299
- async recoverVtxos(eventCallback) {
300
- // Get all virtual outputs including recoverable ones
301
- const allVtxos = await this.wallet.getVtxos({
302
- withRecoverable: true,
303
- withUnrolled: false,
304
- });
305
- // Get dust amount from wallet
306
- const dustAmount = getDustAmount(this.wallet);
307
- // Filter recoverable virtual outputs and handle subdust logic
308
- const { vtxosToRecover, totalAmount } = getRecoverableWithSubdust(allVtxos, dustAmount);
309
- if (vtxosToRecover.length === 0) {
310
- throw new Error("No recoverable VTXOs found");
311
- }
312
- const arkAddress = await this.wallet.getAddress();
313
- // Settle all recoverable virtual outputs back to the wallet
314
- return this.wallet.settle({
315
- inputs: vtxosToRecover,
316
- outputs: [
317
- {
318
- address: arkAddress,
319
- amount: totalAmount,
320
- },
321
- ],
322
- }, eventCallback);
323
- }
324
- /**
325
- * Get information about recoverable balance without executing recovery.
326
- *
327
- * Useful for displaying to users before they decide to recover funds.
328
- *
329
- * @returns Object containing recoverable amounts and subdust information
330
- *
331
- * @example
332
- * ```typescript
333
- * const manager = await wallet.getVtxoManager();
334
- * const balance = await manager.getRecoverableBalance();
335
- *
336
- * if (balance.recoverable > 0n) {
337
- * console.log(`You can recover ${balance.recoverable} sats`);
338
- * if (balance.includesSubdust) {
339
- * console.log(`This includes ${balance.subdust} sats from subdust virtual outputs`);
340
- * }
341
- * }
342
- * ```
343
- */
344
- async getRecoverableBalance() {
345
- const allVtxos = await this.wallet.getVtxos({
346
- withRecoverable: true,
347
- withUnrolled: false,
348
- });
349
- const dustAmount = getDustAmount(this.wallet);
350
- const { vtxosToRecover, includesSubdust, totalAmount } = getRecoverableWithSubdust(allVtxos, dustAmount);
351
- // Calculate subdust amount separately for reporting
352
- const subdustAmount = vtxosToRecover
353
- .filter((v) => BigInt(v.value) < dustAmount)
354
- .reduce((sum, v) => sum + BigInt(v.value), 0n);
355
- return {
356
- recoverable: totalAmount,
357
- subdust: subdustAmount,
358
- includesSubdust,
359
- vtxoCount: vtxosToRecover.length,
360
- };
361
- }
362
- // ========== Renewal Methods ==========
363
- /**
364
- * Get virtual outputs that are expiring soon based on renewal configuration
365
- *
366
- * @param thresholdMs - Optional override for threshold in milliseconds
367
- * @returns Array of expiring virtual outputs, empty array if renewal is disabled or no virtual outputs expiring
368
- *
369
- * @example
370
- * ```typescript
371
- * const wallet = await Wallet.create({
372
- * identity,
373
- * arkServerUrl: 'https://arkade.computer',
374
- * settlementConfig: {
375
- * vtxoThreshold: 86_400 // 24 hours
376
- * },
377
- * });
378
- * const manager = await wallet.getVtxoManager();
379
- * const expiringVtxos = await manager.getExpiringVtxos();
380
- * if (expiringVtxos.length > 0) {
381
- * console.log(`${expiringVtxos.length} virtual outputs expiring soon`);
382
- * }
383
- * ```
384
- */
385
- async getExpiringVtxos(thresholdMs) {
386
- // If settlementConfig is explicitly false and no override provided, renewal is disabled
387
- if (this.settlementConfig === false && thresholdMs === undefined) {
388
- return [];
389
- }
390
- const vtxos = await this.wallet.getVtxos({ withRecoverable: true });
391
- // Resolve threshold: method param > settlementConfig (seconds→ms) > renewalConfig > default
392
- let threshold;
393
- if (thresholdMs !== undefined) {
394
- threshold = thresholdMs;
395
- }
396
- else if (this.settlementConfig !== false &&
397
- this.settlementConfig &&
398
- this.settlementConfig.vtxoThreshold !== undefined) {
399
- threshold = this.settlementConfig.vtxoThreshold * 1000;
400
- }
401
- else {
402
- threshold =
403
- this.renewalConfig?.thresholdMs ??
404
- exports.DEFAULT_RENEWAL_CONFIG.thresholdMs;
405
- }
406
- return getExpiringAndRecoverableVtxos(vtxos, threshold, getDustAmount(this.wallet));
407
- }
408
- /**
409
- * Renew expiring virtual outputs by settling them back to the wallet's address
410
- *
411
- * This method collects all expiring spendable virtual outputs (including recoverable ones) and settles
412
- * them back to the wallet, effectively refreshing their expiration time. This is the
413
- * primary way to prevent virtual outputs from expiring.
414
- *
415
- * @param eventCallback - Optional callback for settlement events
416
- * @returns Settlement transaction ID
417
- * @throws Error if no virtual outputs available to renew
418
- * @throws Error if total amount is below dust threshold
419
- *
420
- * @example
421
- * ```typescript
422
- * const manager = await wallet.getVtxoManager();
423
- *
424
- * // Simple renewal
425
- * const txid = await manager.renewVtxos();
426
- *
427
- * // With event callback
428
- * const txid = await manager.renewVtxos((event) => {
429
- * console.log('Settlement event:', event.type);
430
- * });
431
- * ```
432
- */
433
- async renewVtxos(eventCallback) {
434
- if (this.renewalInProgress) {
435
- throw new Error("Renewal already in progress");
436
- }
437
- this.renewalInProgress = true;
438
- try {
439
- // Get all virtual outputs (including recoverable ones)
440
- // Use default threshold to bypass settlementConfig gate (manual API should always work)
441
- const threshold = this.settlementConfig !== false &&
442
- this.settlementConfig?.vtxoThreshold !== undefined
443
- ? this.settlementConfig.vtxoThreshold * 1000
444
- : exports.DEFAULT_RENEWAL_CONFIG.thresholdMs;
445
- let vtxos = await this.getExpiringVtxos(threshold);
446
- if (vtxos.length === 0) {
447
- throw new Error("No VTXOs available to renew");
448
- }
449
- // Pre-flight: validate the chosen inputs against the indexer's
450
- // authoritative state before submitting. The cursor-derived
451
- // delta sync filters by `created_at`, so a VTXO created
452
- // before the cursor and spent recently can sit in the local
453
- // cache forever; settling against it yields a guaranteed
454
- // VTXO_ALREADY_SPENT 400. Refreshing the candidates here
455
- // catches that BEFORE the network round-trip.
456
- vtxos = await this.revalidateBeforeSettle(vtxos, threshold);
457
- if (vtxos.length === 0) {
458
- throw new Error("No VTXOs available to renew");
459
- }
460
- const totalAmount = vtxos.reduce((sum, vtxo) => sum + vtxo.value, 0);
461
- // Get dust amount from wallet
462
- const dustAmount = getDustAmount(this.wallet);
463
- // Check if total amount is above dust threshold
464
- if (BigInt(totalAmount) < dustAmount) {
465
- throw new Error(`Total amount ${totalAmount} is below dust threshold ${dustAmount}`);
466
- }
467
- const arkAddress = await this.wallet.getAddress();
468
- const txid = await this.wallet.settle({
469
- inputs: vtxos,
470
- outputs: [
471
- {
472
- address: arkAddress,
473
- amount: BigInt(totalAmount),
474
- },
475
- ],
476
- }, eventCallback);
477
- return txid;
478
- }
479
- finally {
480
- // Update cooldown on EVERY attempt (success or failure) so transient
481
- // settle failures (stream close, connector mismatch, duplicated input)
482
- // don't allow the next vtxo_received event to re-enter renewal
483
- // immediately. Without this, a failed settle leaves lastRenewalTimestamp
484
- // at its previous value and the cooldown check becomes a no-op.
485
- this.lastRenewalTimestamp = Date.now();
486
- this.renewalInProgress = false;
487
- }
488
- }
489
- // ========== Boarding Input Sweep Methods ==========
490
- /**
491
- * Get boarding inputs whose timelock has expired.
492
- *
493
- * These inputs can no longer be onboarded cooperatively via `settle()` and
494
- * must be swept back to a fresh boarding address using the unilateral exit path.
495
- *
496
- * @returns Array of expired boarding inputs
497
- *
498
- * @example
499
- * ```typescript
500
- * const manager = await wallet.getVtxoManager();
501
- * const expired = await manager.getExpiredBoardingUtxos();
502
- * if (expired.length > 0) {
503
- * console.log(`${expired.length} expired boarding inputs to sweep`);
504
- * }
505
- * ```
506
- */
507
- async getExpiredBoardingUtxos(prefetchedUtxos) {
508
- const boardingUtxos = prefetchedUtxos ?? (await this.wallet.getBoardingUtxos());
509
- const boardingTimelock = this.getBoardingTimelock();
510
- // For block-based timelocks, fetch the chain tip height
511
- let chainTipHeight;
512
- if (boardingTimelock.type === "blocks") {
513
- const tip = await this.getOnchainProvider().getChainTip();
514
- chainTipHeight = tip.height;
515
- }
516
- return boardingUtxos.filter((utxo) => (0, arkTransaction_1.hasBoardingTxExpired)(utxo, boardingTimelock, chainTipHeight));
517
- }
518
- /**
519
- * Sweep expired boarding inputs back to a fresh boarding address via
520
- * the unilateral exit path (onchain self-spend).
521
- *
522
- * This builds a raw onchain transaction that:
523
- * - Uses all expired boarding inputs as inputs (spent via the CSV exit script path)
524
- * - Has a single output to the wallet's boarding address (restarts the timelock)
525
- * - Batches multiple expired boarding inputs into one transaction
526
- * - Skips the sweep if the output after fees would be below dust
527
- *
528
- * No Arkade server involvement is needed — this is a pure onchain transaction.
529
- *
530
- * @returns The broadcast transaction ID
531
- * @throws Error if no expired boarding inputs are found
532
- * @throws Error if output after fees is below dust (not economical to sweep)
533
- * @throws Error if boarding input sweep is not enabled in settlementConfig
534
- *
535
- * @example
536
- * ```typescript
537
- * const wallet = await Wallet.create({
538
- * identity,
539
- * arkServerUrl: 'https://arkade.computer',
540
- * settlementConfig: {
541
- * boardingUtxoSweep: true,
542
- * },
543
- * });
544
- * const manager = await wallet.getVtxoManager();
545
- *
546
- * try {
547
- * const txid = await manager.sweepExpiredBoardingUtxos();
548
- * console.log('Swept expired boarding inputs:', txid);
549
- * } catch (e) {
550
- * console.log('No sweep needed or not economical');
551
- * }
552
- * ```
553
- */
554
- async sweepExpiredBoardingUtxos(prefetchedUtxos) {
555
- const sweepEnabled = this.settlementConfig !== false &&
556
- (this.settlementConfig?.boardingUtxoSweep ??
557
- exports.DEFAULT_SETTLEMENT_CONFIG.boardingUtxoSweep);
558
- if (!sweepEnabled) {
559
- throw new Error("Boarding UTXO sweep is not enabled in settlementConfig");
560
- }
561
- const allExpired = await this.getExpiredBoardingUtxos(prefetchedUtxos);
562
- // Filter out inputs already swept (tx broadcast but not yet confirmed).
563
- const expiredUtxos = allExpired.filter((u) => !this.sweptBoardingUtxos.has(`${u.txid}:${u.vout}`));
564
- if (expiredUtxos.length === 0) {
565
- throw new Error("No expired boarding UTXOs to sweep");
566
- }
567
- const boardingAddress = await this.wallet.getBoardingAddress();
568
- // Get fee rate from onchain provider
569
- const feeRate = (await this.getOnchainProvider().getFeeRate()) ?? 1;
570
- // Get the exit tap leaf script for signing
571
- const exitTapLeafScript = this.getBoardingExitLeaf();
572
- // Estimate transaction size for fee calculation
573
- const sequence = (0, base_2.getSequence)(exitTapLeafScript);
574
- // TapLeafScript: [{version, internalKey, merklePath}, scriptWithVersion]
575
- const leafScript = exitTapLeafScript[1];
576
- const leafScriptSize = leafScript.length - 1; // minus version byte
577
- const controlBlockSize = exitTapLeafScript[0].merklePath.length * 32;
578
- // Exit path witness: 1 Schnorr signature (64 bytes)
579
- const leafWitnessSize = 64;
580
- const estimator = txSizeEstimator_1.TxWeightEstimator.create();
581
- for (const _ of expiredUtxos) {
582
- estimator.addTapscriptInput(leafWitnessSize, leafScriptSize, controlBlockSize);
583
- }
584
- estimator.addOutputAddress(boardingAddress, this.getNetwork());
585
- const fee = Math.ceil(Number(estimator.vsize().value) * feeRate);
586
- const totalValue = expiredUtxos.reduce((sum, utxo) => sum + BigInt(utxo.value), 0n);
587
- const outputAmount = totalValue - BigInt(fee);
588
- // Dust check: skip if output after fees is below dust
589
- const dustAmount = getDustAmount(this.wallet);
590
- if (outputAmount < dustAmount) {
591
- throw new Error(`Sweep not economical: output ${outputAmount} sats after ${fee} sats fee is below dust (${dustAmount} sats)`);
592
- }
593
- // Build the raw transaction
594
- const tx = new transaction_1.Transaction();
595
- for (const utxo of expiredUtxos) {
596
- tx.addInput({
597
- txid: utxo.txid,
598
- index: utxo.vout,
599
- witnessUtxo: {
600
- script: this.getBoardingOutputScript(),
601
- amount: BigInt(utxo.value),
602
- },
603
- tapLeafScript: [exitTapLeafScript],
604
- sequence,
605
- });
606
- }
607
- tx.addOutputAddress(boardingAddress, outputAmount, this.getNetwork());
608
- // Sign and finalize
609
- const signedTx = await this.getIdentity().sign(tx);
610
- signedTx.finalize();
611
- // Broadcast
612
- const txid = await this.getOnchainProvider().broadcastTransaction(signedTx.hex);
613
- // Mark boarding inputs as swept to prevent duplicate broadcasts on next poll
614
- for (const u of expiredUtxos) {
615
- this.sweptBoardingUtxos.add(`${u.txid}:${u.vout}`);
616
- }
617
- // Mark the sweep output as "known" so the next poll doesn't try to
618
- // auto-settle it back into Arkade (it lands at the same boarding address).
619
- this.knownBoardingUtxos.add(`${txid}:0`);
620
- return txid;
621
- }
622
- // ========== Private Helpers ==========
623
- /** Asserts sweep capability and returns the typed wallet. */
624
- getSweepWallet() {
625
- assertSweepCapable(this.wallet);
626
- return this.wallet;
627
- }
628
- /** Decodes the boarding tapscript exit path to extract the CSV timelock. */
629
- getBoardingTimelock() {
630
- const wallet = this.getSweepWallet();
631
- const exitScript = tapscript_1.CSVMultisigTapscript.decode(base_1.hex.decode(wallet.boardingTapscript.exitScript));
632
- return exitScript.params.timelock;
633
- }
634
- /** Returns the TapLeafScript for the boarding tapscript's exit (CSV) path. */
635
- getBoardingExitLeaf() {
636
- return this.getSweepWallet().boardingTapscript.exit();
637
- }
638
- /** Returns the pkScript (output script) of the boarding tapscript. */
639
- getBoardingOutputScript() {
640
- return this.getSweepWallet().boardingTapscript.pkScript;
641
- }
642
- /** Returns the onchain provider for fee estimation and broadcasting. */
643
- getOnchainProvider() {
644
- return this.getSweepWallet().onchainProvider;
645
- }
646
- /** Returns the Ark provider for intent fee and server info lookups. */
647
- getArkProvider() {
648
- return this.getSweepWallet().arkProvider;
649
- }
650
- /** Returns the Bitcoin network configuration from the wallet. */
651
- getNetwork() {
652
- return this.getSweepWallet().network;
653
- }
654
- /** Returns the wallet's identity for transaction signing. */
655
- getIdentity() {
656
- return this.wallet.identity;
657
- }
658
- async initializeSubscription() {
659
- if (this.settlementConfig === false) {
660
- return undefined;
661
- }
662
- // Start polling for boarding inputs independently of contract manager
663
- // SSE setup. Use a short delay to let the wallet finish construction.
664
- this.startupPollTimeoutId = setTimeout(() => {
665
- if (this.disposed)
666
- return;
667
- this.startBoardingUtxoPoll();
668
- }, 1000);
669
- try {
670
- const [delegatorManager, contractManager, destination] = await Promise.all([
671
- this.wallet.getDelegatorManager(),
672
- this.wallet.getContractManager(),
673
- this.wallet.getAddress(),
674
- ]);
675
- const stopWatching = contractManager.onContractEvent((event) => {
676
- if (event.type !== "vtxo_received") {
677
- return;
678
- }
679
- const msSinceLastRenewal = Date.now() - this.lastRenewalTimestamp;
680
- const shouldRenew = !this.renewalInProgress &&
681
- msSinceLastRenewal >= VtxoManager.RENEWAL_COOLDOWN_MS;
682
- if (shouldRenew) {
683
- this.renewVtxos().catch((e) => {
684
- if (e instanceof Error) {
685
- if (e.message.includes("No VTXOs available to renew")) {
686
- // Not an error, just no virtual outputs eligible for renewal.
687
- return;
688
- }
689
- if (e.message.includes("is below dust threshold")) {
690
- // Not an error, just below dust threshold.
691
- // As more virtual outputs are received, the threshold will be raised.
692
- return;
693
- }
694
- if (e.message.includes("VTXO_ALREADY_REGISTERED") ||
695
- e.message.includes("duplicated input")) {
696
- // Virtual output is already being used in a concurrent
697
- // user-initiated operation. Skip silently — the
698
- // wallet's tx lock serializes these, but the
699
- // renewal will retry on the next cycle.
700
- return;
701
- }
702
- if (e.message.includes("VTXO_ALREADY_SPENT")) {
703
- // Our local VTXO cache is stale vs. the
704
- // server's authoritative view. Trigger a
705
- // throttled, targeted refresh on the
706
- // offending outpoint (if the server told
707
- // us which one), then skip — the next
708
- // cycle will see fresh data.
709
- void this.maybeRefreshAfterVtxoSpent(this.extractSpentOutpoint(e));
710
- return;
711
- }
712
- }
713
- console.error("Error renewing VTXOs:", e);
714
- });
715
- }
716
- if (delegatorManager) {
717
- delegatorManager
718
- .delegate(event.vtxos, destination)
719
- .catch((e) => {
720
- console.error("Error delegating VTXOs:", e);
721
- });
722
- }
723
- });
724
- return stopWatching;
725
- }
726
- catch (e) {
727
- console.error("Error renewing VTXOs from VtxoManager", e);
728
- return undefined;
729
- }
730
- }
731
- /**
732
- * VTXO_ALREADY_SPENT means the server's authoritative view of VTXO state
733
- * is ahead of ours — cross-instance race, pre-lock snapshot drift, or an
734
- * SSE gap left stale data in the local cache. Silent-swallowing
735
- * guarantees the same error on the next cycle because nothing
736
- * reconciles the cache.
737
- *
738
- * The cursor-derived delta sync filters by `created_at`, so a VTXO that
739
- * was created before the cursor but spent recently can never be
740
- * reconciled by `refreshVtxos()`. Use `refreshOutpoints` for surgical
741
- * recovery: query the indexer for the specific stale outpoint and
742
- * upsert its authoritative state into the wallet repository.
743
- *
744
- * Throttled because the same VTXO can fire repeatedly before the
745
- * upsert observably propagates through the renewal selector.
746
- */
747
- maybeRefreshAfterVtxoSpent(spentOutpoint) {
748
- if (this.vtxoSpentRefreshPromise) {
749
- return this.vtxoSpentRefreshPromise;
750
- }
751
- const now = Date.now();
752
- if (now - this.lastVtxoSpentRefreshTimestamp <
753
- VtxoManager.VTXO_SPENT_REFRESH_COOLDOWN_MS) {
754
- return Promise.resolve();
755
- }
756
- this.lastVtxoSpentRefreshTimestamp = now;
757
- this.vtxoSpentRefreshPromise = (async () => {
758
- try {
759
- const contractManager = await this.wallet.getContractManager();
760
- if (spentOutpoint) {
761
- await contractManager.refreshOutpoints([spentOutpoint]);
762
- }
763
- else {
764
- // No outpoint metadata — fall back to the broader refresh.
765
- await contractManager.refreshVtxos();
766
- }
767
- }
768
- catch (e) {
769
- console.error("Error refreshing VTXOs after VTXO_ALREADY_SPENT:", e);
770
- }
771
- finally {
772
- this.vtxoSpentRefreshPromise = undefined;
773
- }
774
- })();
775
- return this.vtxoSpentRefreshPromise;
776
- }
777
- /**
778
- * Extract the offending VTXO outpoint from a `VTXO_ALREADY_SPENT` error,
779
- * if the server attached one in `metadata.vtxo_outpoint`. Returns
780
- * `undefined` when the error isn't a parsed ArkError, isn't this code,
781
- * or doesn't carry the metadata.
782
- */
783
- extractSpentOutpoint(error) {
784
- const ark = (0, errors_1.maybeArkError)(error);
785
- if (!ark || ark.name !== "VTXO_ALREADY_SPENT")
786
- return undefined;
787
- const raw = ark.metadata?.vtxo_outpoint;
788
- if (typeof raw !== "string")
789
- return undefined;
790
- const [txid, voutStr] = raw.split(":");
791
- if (!txid || !voutStr)
792
- return undefined;
793
- const vout = Number(voutStr);
794
- if (!Number.isInteger(vout) || vout < 0)
795
- return undefined;
796
- return { txid, vout };
797
- }
798
- /**
799
- * Reconcile the chosen VTXOs with the indexer's authoritative state
800
- * before submitting a settle intent. Pulls the canonical record for
801
- * each candidate outpoint via {@link IContractManager.refreshOutpoints}
802
- * (which upserts the result into the wallet repository), then
803
- * re-selects through the standard expiring-vtxo filter so anything
804
- * the refresh flagged as spent is dropped.
805
- *
806
- * Best-effort: a failed refresh just falls back to the original
807
- * candidates and lets the post-submit `VTXO_ALREADY_SPENT` recovery
808
- * handle whatever slipped through.
809
- */
810
- async revalidateBeforeSettle(candidates, thresholdMs) {
811
- if (candidates.length === 0)
812
- return candidates;
813
- try {
814
- const cm = await this.wallet.getContractManager();
815
- await cm.refreshOutpoints(candidates.map((v) => ({ txid: v.txid, vout: v.vout })));
816
- }
817
- catch (e) {
818
- console.error("Error pre-validating VTXOs before settle:", e);
819
- return candidates;
820
- }
821
- // Re-select from the now-fresh local cache. Anything previously
822
- // selected but spent gets filtered out by the standard
823
- // `isSpendable`/`isSpent` checks inside getVtxos / getExpiringVtxos.
824
- try {
825
- const refreshed = await this.getExpiringVtxos(thresholdMs);
826
- const candidateKeys = new Set(candidates.map((v) => `${v.txid}:${v.vout}`));
827
- // Restrict to vtxos that were also in the original candidate set
828
- // — `getExpiringVtxos` may surface NEW vtxos and we don't want
829
- // pre-flight to silently expand the input set.
830
- return refreshed.filter((v) => candidateKeys.has(`${v.txid}:${v.vout}`));
831
- }
832
- catch (e) {
833
- console.error("Error re-selecting VTXOs after pre-validate:", e);
834
- return candidates;
835
- }
836
- }
837
- /** Computes the next poll delay, applying exponential backoff on failures. */
838
- getNextPollDelay() {
839
- if (this.settlementConfig === false)
840
- return 0;
841
- const baseMs = this.settlementConfig.pollIntervalMs ??
842
- exports.DEFAULT_SETTLEMENT_CONFIG.pollIntervalMs;
843
- if (this.consecutivePollFailures === 0)
844
- return baseMs;
845
- const backoff = Math.min(baseMs * Math.pow(2, this.consecutivePollFailures), VtxoManager.MAX_BACKOFF_MS);
846
- return backoff;
847
- }
848
- /**
849
- * Starts a polling loop that:
850
- * 1. Auto-settles new boarding inputs into Arkade
851
- * 2. Sweeps expired boarding inputs (when boardingUtxoSweep is enabled)
852
- *
853
- * Uses setTimeout chaining (not setInterval) so a slow/blocked poll
854
- * cannot stack up and the next delay can incorporate backoff.
855
- */
856
- startBoardingUtxoPoll() {
857
- if (this.settlementConfig === false)
858
- return;
859
- // Run once immediately, then schedule next
860
- this.pollBoardingUtxos();
861
- }
862
- schedulePoll() {
863
- if (this.disposed || this.settlementConfig === false)
864
- return;
865
- const delay = this.getNextPollDelay();
866
- this.pollTimeoutId = setTimeout(() => this.pollBoardingUtxos(), delay);
867
- }
868
- async pollBoardingUtxos() {
869
- // Guard: wallet must support boarding input + sweep operations
870
- if (!isSweepCapable(this.wallet))
871
- return;
872
- // Skip if disposed or a previous poll is still running
873
- if (this.disposed)
874
- return;
875
- if (this.pollInProgress)
876
- return;
877
- this.pollInProgress = true;
878
- // Create a promise that dispose() can await
879
- let resolve;
880
- const promise = new Promise((r) => (resolve = r));
881
- this.pollDone = { promise, resolve: resolve };
882
- let hadError = false;
883
- try {
884
- // Cross-instance guard: in browser / service worker environments,
885
- // serialize the poll body across tabs and SW contexts so only one
886
- // of them registers intents per interval. Without this, every tab
887
- // submits a parallel RegisterIntent for the same boarding input
888
- // and N-1 of them collide on the server's duplicated-input check,
889
- // each producing a DeleteIntent RPC. No-op outside the browser.
890
- await runWithCrossInstanceLock(BOARDING_POLL_LOCK_NAME, async () => {
891
- // Fetch boarding inputs once for the entire poll cycle so that
892
- // settle and sweep don't each hit the network independently.
893
- const boardingUtxos = await this.wallet.getBoardingUtxos();
894
- // Settle new (unexpired) boarding inputs + any near-expiry
895
- // VTXOs in a single intent, then sweep expired boarding
896
- // inputs. Sequential to avoid racing for the same inputs.
897
- try {
898
- await this.runPeriodicSettle(boardingUtxos);
899
- }
900
- catch (e) {
901
- hadError = true;
902
- console.error("Error during periodic settle:", e);
903
- }
904
- const sweepEnabled = this.settlementConfig !== false &&
905
- (this.settlementConfig?.boardingUtxoSweep ??
906
- exports.DEFAULT_SETTLEMENT_CONFIG.boardingUtxoSweep);
907
- if (sweepEnabled) {
908
- try {
909
- await this.sweepExpiredBoardingUtxos(boardingUtxos);
910
- }
911
- catch (e) {
912
- if (!(e instanceof Error) ||
913
- !e.message.includes("No expired boarding UTXOs")) {
914
- hadError = true;
915
- console.error("Error auto-sweeping boarding UTXOs:", e);
916
- }
917
- }
918
- }
919
- });
920
- }
921
- catch (e) {
922
- hadError = true;
923
- console.error("Error fetching boarding UTXOs:", e);
924
- }
925
- finally {
926
- if (hadError) {
927
- this.consecutivePollFailures++;
928
- }
929
- else {
930
- this.consecutivePollFailures = 0;
931
- }
932
- this.pollInProgress = false;
933
- this.pollDone.resolve();
934
- this.pollDone = undefined;
935
- this.schedulePoll();
936
- }
937
- }
938
- /**
939
- * Auto-settle new (unexpired) boarding inputs AND near-expiry VTXOs into
940
- * Arkade in a single intent. Skips boarding UTXOs that are already expired
941
- * (those are handled by sweep) and those already in-flight (tracked in
942
- * knownBoardingUtxos). If the event-driven renewal path is currently
943
- * running, VTXOs are omitted from this cycle to avoid double-spending.
944
- *
945
- * Failure bookkeeping: after every settle *attempt*, lastPeriodicSettleTimestamp
946
- * is armed and consecutive failures are counted so the next attempt is
947
- * blocked by an exponentially growing cooldown (capped). This stops a
948
- * persistently failing input from producing identical RegisterIntent +
949
- * DeleteIntent retries on every 60s poll.
950
- */
951
- async runPeriodicSettle(boardingUtxos) {
952
- // Exclude expired boarding inputs — those should be swept, not settled.
953
- // If we can't determine expired status, bail out entirely to avoid
954
- // accidentally settling expired inputs (which would conflict with sweep).
955
- let expiredSet;
956
- try {
957
- const boardingTimelock = this.getBoardingTimelock();
958
- let chainTipHeight;
959
- if (boardingTimelock.type === "blocks") {
960
- const tip = await this.getOnchainProvider().getChainTip();
961
- chainTipHeight = tip.height;
962
- }
963
- const expired = boardingUtxos.filter((utxo) => (0, arkTransaction_1.hasBoardingTxExpired)(utxo, boardingTimelock, chainTipHeight));
964
- expiredSet = new Set(expired.map((u) => `${u.txid}:${u.vout}`));
965
- }
966
- catch (e) {
967
- throw e instanceof Error ? e : new Error(String(e));
968
- }
969
- const unsettledBoarding = boardingUtxos.filter((u) => u.status.confirmed &&
970
- !this.knownBoardingUtxos.has(`${u.txid}:${u.vout}`) &&
971
- !expiredSet.has(`${u.txid}:${u.vout}`));
972
- // Collect near-expiry VTXOs unless the event-driven path is mid-renewal.
973
- // Skipping when renewalInProgress avoids double-submitting the same VTXOs.
974
- let expiringVtxos = [];
975
- if (!this.renewalInProgress) {
976
- try {
977
- expiringVtxos = await this.getExpiringVtxos();
978
- // Pre-flight validation: see comment in `renewVtxos`. The
979
- // local cache may carry vtxos that the indexer already
980
- // marks spent because the cursor-derived delta sync only
981
- // catches `created_at`-recent updates, not status changes
982
- // for older VTXOs.
983
- expiringVtxos =
984
- await this.revalidateBeforeSettle(expiringVtxos);
985
- }
986
- catch (e) {
987
- // Non-fatal: fall back to boarding-only settle.
988
- console.error("Error fetching expiring VTXOs:", e);
989
- }
990
- }
991
- if (unsettledBoarding.length === 0 && expiringVtxos.length === 0) {
992
- return;
993
- }
994
- // Respect the cooldown armed by the previous attempt. Cooldown grows
995
- // exponentially with consecutive failures and is capped by
996
- // PERIODIC_SETTLE_MAX_BACKOFF_MS.
997
- const cooldownMs = Math.min(VtxoManager.PERIODIC_SETTLE_COOLDOWN_MS *
998
- Math.pow(2, this.consecutivePeriodicSettleFailures), VtxoManager.PERIODIC_SETTLE_MAX_BACKOFF_MS);
999
- if (Date.now() - this.lastPeriodicSettleTimestamp < cooldownMs) {
1000
- return;
1001
- }
1002
- const dustAmount = getDustAmount(this.wallet);
1003
- // Fetch server intent-fee config so each input/output can be priced.
1004
- // Without this, settle sends `outputAmount = sum(inputs)` and the
1005
- // server rejects with INTENT_INSUFFICIENT_FEE whenever the operator
1006
- // charges non-zero intent fees.
1007
- const { fees } = await this.getArkProvider().getInfo();
1008
- const estimator = new arkfee_1.Estimator(fees.intentFee);
1009
- let totalAmount = 0n;
1010
- const filteredBoarding = [];
1011
- for (const u of unsettledBoarding) {
1012
- const inputFee = estimator.evalOnchainInput({
1013
- amount: BigInt(u.value),
1014
- });
1015
- if (inputFee.value >= BigInt(u.value)) {
1016
- // Fee exceeds input value — including it would drain the output.
1017
- continue;
1018
- }
1019
- filteredBoarding.push(u);
1020
- totalAmount += BigInt(u.value) - BigInt(inputFee.satoshis);
1021
- }
1022
- const filteredVtxos = [];
1023
- for (const v of expiringVtxos) {
1024
- const inputFee = estimator.evalOffchainInput({
1025
- amount: BigInt(v.value),
1026
- type: v.virtualStatus.state === "swept" ? "recoverable" : "vtxo",
1027
- weight: 0,
1028
- birth: v.createdAt,
1029
- expiry: v.virtualStatus.batchExpiry
1030
- ? new Date(v.virtualStatus.batchExpiry)
1031
- : undefined,
1032
- });
1033
- if (inputFee.satoshis >= v.value) {
1034
- continue;
1035
- }
1036
- filteredVtxos.push(v);
1037
- totalAmount += BigInt(v.value) - BigInt(inputFee.satoshis);
1038
- }
1039
- if (filteredBoarding.length === 0 && filteredVtxos.length === 0) {
1040
- return;
1041
- }
1042
- const arkAddress = await this.wallet.getAddress();
1043
- const outputFee = estimator.evalOffchainOutput({
1044
- amount: totalAmount,
1045
- script: base_1.hex.encode(address_1.ArkAddress.decode(arkAddress).pkScript),
1046
- });
1047
- totalAmount -= BigInt(outputFee.satoshis);
1048
- if (totalAmount < dustAmount)
1049
- return;
1050
- const includesVtxos = filteredVtxos.length > 0;
1051
- // Block the event-driven renewal path while this settle is in flight
1052
- // when VTXOs are part of the intent. Mirrors renewVtxos()'s guard so
1053
- // the two paths can't race on the same VTXO inputs.
1054
- if (includesVtxos) {
1055
- this.renewalInProgress = true;
1056
- }
1057
- let success = false;
1058
- let staleCacheSkip = false;
1059
- try {
1060
- try {
1061
- await this.wallet.settle({
1062
- inputs: [...filteredBoarding, ...filteredVtxos],
1063
- outputs: [{ address: arkAddress, amount: totalAmount }],
1064
- });
1065
- // Mark boarding inputs as known only after successful settle.
1066
- for (const u of filteredBoarding) {
1067
- this.knownBoardingUtxos.add(`${u.txid}:${u.vout}`);
1068
- }
1069
- success = true;
1070
- }
1071
- catch (e) {
1072
- if (e instanceof Error &&
1073
- e.message.includes("VTXO_ALREADY_SPENT")) {
1074
- // Local VTXO cache is stale vs. the server's
1075
- // authoritative view — not a transient failure.
1076
- // Trigger a throttled, targeted refresh on the
1077
- // offending outpoint and skip this cycle without
1078
- // bumping the failure counter, so the next poll
1079
- // can retry once the cache reconciles.
1080
- staleCacheSkip = true;
1081
- void this.maybeRefreshAfterVtxoSpent(this.extractSpentOutpoint(e));
1082
- }
1083
- else {
1084
- throw e;
1085
- }
1086
- }
1087
- }
1088
- finally {
1089
- this.lastPeriodicSettleTimestamp = Date.now();
1090
- if (includesVtxos) {
1091
- // Match event-path semantics: bump the renewal cooldown
1092
- // whether we succeeded or failed so a failed periodic settle
1093
- // doesn't let the next vtxo_received event re-enter renewal
1094
- // immediately.
1095
- this.lastRenewalTimestamp = Date.now();
1096
- this.renewalInProgress = false;
1097
- }
1098
- if (success) {
1099
- this.consecutivePeriodicSettleFailures = 0;
1100
- }
1101
- else if (!staleCacheSkip) {
1102
- // Don't bump on stale-cache skip: it's not a transient
1103
- // failure, and the next cycle should try immediately
1104
- // after the refresh lands.
1105
- this.consecutivePeriodicSettleFailures++;
1106
- }
1107
- }
1108
- }
1109
- async dispose() {
1110
- this.disposePromise ?? (this.disposePromise = (async () => {
1111
- this.disposed = true;
1112
- if (this.startupPollTimeoutId) {
1113
- clearTimeout(this.startupPollTimeoutId);
1114
- this.startupPollTimeoutId = undefined;
1115
- }
1116
- if (this.pollTimeoutId) {
1117
- clearTimeout(this.pollTimeoutId);
1118
- this.pollTimeoutId = undefined;
1119
- }
1120
- // Wait for any in-flight poll to finish (with timeout to avoid hanging)
1121
- if (this.pollDone) {
1122
- let timer;
1123
- const timeout = new Promise((r) => (timer = setTimeout(r, 30000)));
1124
- await Promise.race([this.pollDone.promise, timeout]);
1125
- clearTimeout(timer);
1126
- }
1127
- const subscription = await this.contractEventsSubscriptionReady;
1128
- this.contractEventsSubscription = undefined;
1129
- subscription?.();
1130
- })());
1131
- return this.disposePromise;
1132
- }
1133
- async [Symbol.asyncDispose]() {
1134
- await this.dispose();
1135
- }
1136
- }
1137
- exports.VtxoManager = VtxoManager;
1138
- VtxoManager.MAX_BACKOFF_MS = 5 * 60 * 1000; // 5 minutes
1139
- VtxoManager.RENEWAL_COOLDOWN_MS = 30000; // 30 seconds
1140
- VtxoManager.PERIODIC_SETTLE_COOLDOWN_MS = 30000;
1141
- VtxoManager.PERIODIC_SETTLE_MAX_BACKOFF_MS = 5 * 60 * 1000;
1142
- VtxoManager.VTXO_SPENT_REFRESH_COOLDOWN_MS = 30000;