@arkade-os/sdk 0.4.27 → 0.4.29

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