@bopen-io/wallet-toolbox 1.7.18

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 (390) hide show
  1. package/.claude/settings.local.json +10 -0
  2. package/.env.template +22 -0
  3. package/.github/ISSUE_TEMPLATE/bug_report.md +40 -0
  4. package/.github/ISSUE_TEMPLATE/discussion.md +24 -0
  5. package/.github/pull_request_template.md +22 -0
  6. package/.github/workflows/push.yaml +145 -0
  7. package/.prettierrc +10 -0
  8. package/CHANGELOG.md +280 -0
  9. package/CONTRIBUTING.md +89 -0
  10. package/README.md +43 -0
  11. package/docs/README.md +85 -0
  12. package/docs/client.md +19627 -0
  13. package/docs/monitor.md +953 -0
  14. package/docs/open-rpc/index.html +46 -0
  15. package/docs/services.md +6377 -0
  16. package/docs/setup.md +1268 -0
  17. package/docs/storage.md +5367 -0
  18. package/docs/wallet.md +19626 -0
  19. package/jest.config.ts +25 -0
  20. package/license.md +28 -0
  21. package/out/tsconfig.all.tsbuildinfo +1 -0
  22. package/package.json +63 -0
  23. package/src/CWIStyleWalletManager.ts +1999 -0
  24. package/src/Setup.ts +579 -0
  25. package/src/SetupClient.ts +322 -0
  26. package/src/SetupWallet.ts +108 -0
  27. package/src/SimpleWalletManager.ts +526 -0
  28. package/src/Wallet.ts +1169 -0
  29. package/src/WalletAuthenticationManager.ts +153 -0
  30. package/src/WalletLogger.ts +213 -0
  31. package/src/WalletPermissionsManager.ts +3660 -0
  32. package/src/WalletSettingsManager.ts +114 -0
  33. package/src/__tests/CWIStyleWalletManager.test.d.ts.map +1 -0
  34. package/src/__tests/CWIStyleWalletManager.test.js.map +1 -0
  35. package/src/__tests/CWIStyleWalletManager.test.ts +675 -0
  36. package/src/__tests/WalletPermissionsManager.callbacks.test.ts +323 -0
  37. package/src/__tests/WalletPermissionsManager.checks.test.ts +844 -0
  38. package/src/__tests/WalletPermissionsManager.encryption.test.ts +412 -0
  39. package/src/__tests/WalletPermissionsManager.fixtures.ts +307 -0
  40. package/src/__tests/WalletPermissionsManager.flows.test.ts +462 -0
  41. package/src/__tests/WalletPermissionsManager.initialization.test.ts +300 -0
  42. package/src/__tests/WalletPermissionsManager.pmodules.test.ts +798 -0
  43. package/src/__tests/WalletPermissionsManager.proxying.test.ts +724 -0
  44. package/src/__tests/WalletPermissionsManager.tokens.test.ts +503 -0
  45. package/src/index.all.ts +27 -0
  46. package/src/index.client.ts +25 -0
  47. package/src/index.mobile.ts +21 -0
  48. package/src/index.ts +1 -0
  49. package/src/monitor/Monitor.ts +412 -0
  50. package/src/monitor/MonitorDaemon.ts +188 -0
  51. package/src/monitor/README.md +3 -0
  52. package/src/monitor/__test/MonitorDaemon.man.test.ts +45 -0
  53. package/src/monitor/tasks/TaskCheckForProofs.ts +243 -0
  54. package/src/monitor/tasks/TaskCheckNoSends.ts +73 -0
  55. package/src/monitor/tasks/TaskClock.ts +33 -0
  56. package/src/monitor/tasks/TaskFailAbandoned.ts +54 -0
  57. package/src/monitor/tasks/TaskMonitorCallHistory.ts +26 -0
  58. package/src/monitor/tasks/TaskNewHeader.ts +93 -0
  59. package/src/monitor/tasks/TaskPurge.ts +68 -0
  60. package/src/monitor/tasks/TaskReorg.ts +89 -0
  61. package/src/monitor/tasks/TaskReviewStatus.ts +48 -0
  62. package/src/monitor/tasks/TaskSendWaiting.ts +122 -0
  63. package/src/monitor/tasks/TaskSyncWhenIdle.ts +26 -0
  64. package/src/monitor/tasks/TaskUnFail.ts +151 -0
  65. package/src/monitor/tasks/WalletMonitorTask.ts +47 -0
  66. package/src/sdk/CertOpsWallet.ts +18 -0
  67. package/src/sdk/PrivilegedKeyManager.ts +372 -0
  68. package/src/sdk/README.md +13 -0
  69. package/src/sdk/WERR_errors.ts +234 -0
  70. package/src/sdk/WalletError.ts +170 -0
  71. package/src/sdk/WalletErrorFromJson.ts +80 -0
  72. package/src/sdk/WalletServices.interfaces.ts +700 -0
  73. package/src/sdk/WalletSigner.interfaces.ts +11 -0
  74. package/src/sdk/WalletStorage.interfaces.ts +606 -0
  75. package/src/sdk/__test/CertificateLifeCycle.test.ts +131 -0
  76. package/src/sdk/__test/PrivilegedKeyManager.test.ts +738 -0
  77. package/src/sdk/__test/WalletError.test.ts +318 -0
  78. package/src/sdk/__test/validationHelpers.test.ts +21 -0
  79. package/src/sdk/index.ts +10 -0
  80. package/src/sdk/types.ts +226 -0
  81. package/src/services/README.md +11 -0
  82. package/src/services/ServiceCollection.ts +248 -0
  83. package/src/services/Services.ts +603 -0
  84. package/src/services/__tests/ARC.man.test.ts +123 -0
  85. package/src/services/__tests/ARC.timeout.man.test.ts +79 -0
  86. package/src/services/__tests/ArcGorillaPool.man.test.ts +108 -0
  87. package/src/services/__tests/arcServices.test.ts +8 -0
  88. package/src/services/__tests/bitrails.test.ts +56 -0
  89. package/src/services/__tests/getMerklePath.test.ts +15 -0
  90. package/src/services/__tests/getRawTx.test.ts +13 -0
  91. package/src/services/__tests/postBeef.test.ts +104 -0
  92. package/src/services/__tests/verifyBeef.test.ts +50 -0
  93. package/src/services/chaintracker/BHServiceClient.ts +212 -0
  94. package/src/services/chaintracker/ChaintracksChainTracker.ts +71 -0
  95. package/src/services/chaintracker/__tests/ChaintracksChainTracker.test.ts +33 -0
  96. package/src/services/chaintracker/__tests/ChaintracksServiceClient.test.ts +29 -0
  97. package/src/services/chaintracker/chaintracks/Api/BlockHeaderApi.ts +72 -0
  98. package/src/services/chaintracker/chaintracks/Api/BulkIngestorApi.ts +83 -0
  99. package/src/services/chaintracker/chaintracks/Api/BulkStorageApi.ts +92 -0
  100. package/src/services/chaintracker/chaintracks/Api/ChaintracksApi.ts +64 -0
  101. package/src/services/chaintracker/chaintracks/Api/ChaintracksClientApi.ts +189 -0
  102. package/src/services/chaintracker/chaintracks/Api/ChaintracksFetchApi.ts +18 -0
  103. package/src/services/chaintracker/chaintracks/Api/ChaintracksFsApi.ts +58 -0
  104. package/src/services/chaintracker/chaintracks/Api/ChaintracksStorageApi.ts +386 -0
  105. package/src/services/chaintracker/chaintracks/Api/LiveIngestorApi.ts +25 -0
  106. package/src/services/chaintracker/chaintracks/Chaintracks.ts +609 -0
  107. package/src/services/chaintracker/chaintracks/ChaintracksService.ts +199 -0
  108. package/src/services/chaintracker/chaintracks/ChaintracksServiceClient.ts +154 -0
  109. package/src/services/chaintracker/chaintracks/Ingest/BulkIngestorBase.ts +176 -0
  110. package/src/services/chaintracker/chaintracks/Ingest/BulkIngestorCDN.ts +174 -0
  111. package/src/services/chaintracker/chaintracks/Ingest/BulkIngestorCDNBabbage.ts +18 -0
  112. package/src/services/chaintracker/chaintracks/Ingest/BulkIngestorWhatsOnChainCdn.ts +113 -0
  113. package/src/services/chaintracker/chaintracks/Ingest/BulkIngestorWhatsOnChainWs.ts +81 -0
  114. package/src/services/chaintracker/chaintracks/Ingest/LiveIngestorBase.ts +86 -0
  115. package/src/services/chaintracker/chaintracks/Ingest/LiveIngestorTeranodeP2P.ts +59 -0
  116. package/src/services/chaintracker/chaintracks/Ingest/LiveIngestorWhatsOnChainPoll.ts +104 -0
  117. package/src/services/chaintracker/chaintracks/Ingest/LiveIngestorWhatsOnChainWs.ts +66 -0
  118. package/src/services/chaintracker/chaintracks/Ingest/WhatsOnChainIngestorWs.ts +566 -0
  119. package/src/services/chaintracker/chaintracks/Ingest/WhatsOnChainServices.ts +219 -0
  120. package/src/services/chaintracker/chaintracks/Ingest/__tests/BulkIngestorCDNBabbage.test.ts +54 -0
  121. package/src/services/chaintracker/chaintracks/Ingest/__tests/LiveIngestorWhatsOnChainPoll.test.ts +33 -0
  122. package/src/services/chaintracker/chaintracks/Ingest/__tests/WhatsOnChainServices.test.ts +124 -0
  123. package/src/services/chaintracker/chaintracks/Storage/BulkStorageBase.ts +92 -0
  124. package/src/services/chaintracker/chaintracks/Storage/ChaintracksKnexMigrations.ts +104 -0
  125. package/src/services/chaintracker/chaintracks/Storage/ChaintracksStorageBase.ts +382 -0
  126. package/src/services/chaintracker/chaintracks/Storage/ChaintracksStorageIdb.ts +574 -0
  127. package/src/services/chaintracker/chaintracks/Storage/ChaintracksStorageKnex.ts +438 -0
  128. package/src/services/chaintracker/chaintracks/Storage/ChaintracksStorageMemory.ts +29 -0
  129. package/src/services/chaintracker/chaintracks/Storage/ChaintracksStorageNoDb.ts +304 -0
  130. package/src/services/chaintracker/chaintracks/Storage/__tests/ChaintracksStorageIdb.test.ts +102 -0
  131. package/src/services/chaintracker/chaintracks/Storage/__tests/ChaintracksStorageKnex.test.ts +45 -0
  132. package/src/services/chaintracker/chaintracks/__tests/Chaintracks.test.ts +77 -0
  133. package/src/services/chaintracker/chaintracks/__tests/ChaintracksClientApi.test.ts +192 -0
  134. package/src/services/chaintracker/chaintracks/__tests/LocalCdnServer.ts +75 -0
  135. package/src/services/chaintracker/chaintracks/__tests/createIdbChaintracks.test.ts +62 -0
  136. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest349/mainNetBlockHeaders.json +1 -0
  137. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest349/mainNet_0.headers +0 -0
  138. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest349/mainNet_1.headers +0 -0
  139. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest349/mainNet_2.headers +0 -0
  140. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest349/mainNet_3.headers +0 -0
  141. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest379/mainNetBlockHeaders.json +1 -0
  142. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest379/mainNet_0.headers +0 -0
  143. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest379/mainNet_1.headers +0 -0
  144. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest379/mainNet_2.headers +0 -0
  145. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest379/mainNet_3.headers +0 -0
  146. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest399/mainNetBlockHeaders.json +1 -0
  147. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest399/mainNet_0.headers +0 -0
  148. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest399/mainNet_1.headers +0 -0
  149. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest399/mainNet_2.headers +0 -0
  150. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest399/mainNet_3.headers +0 -0
  151. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest402/mainNetBlockHeaders.json +1 -0
  152. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest402/mainNet_0.headers +0 -0
  153. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest402/mainNet_1.headers +0 -0
  154. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest402/mainNet_2.headers +0 -0
  155. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest402/mainNet_3.headers +0 -0
  156. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest402/mainNet_4.headers +0 -0
  157. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest499/mainNetBlockHeaders.json +1 -0
  158. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest499/mainNet_0.headers +0 -0
  159. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest499/mainNet_1.headers +0 -0
  160. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest499/mainNet_2.headers +0 -0
  161. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest499/mainNet_3.headers +0 -0
  162. package/src/services/chaintracker/chaintracks/__tests/data/cdnTest499/mainNet_4.headers +0 -0
  163. package/src/services/chaintracker/chaintracks/createDefaultIdbChaintracksOptions.ts +92 -0
  164. package/src/services/chaintracker/chaintracks/createDefaultKnexChaintracksOptions.ts +111 -0
  165. package/src/services/chaintracker/chaintracks/createDefaultNoDbChaintracksOptions.ts +91 -0
  166. package/src/services/chaintracker/chaintracks/createIdbChaintracks.ts +60 -0
  167. package/src/services/chaintracker/chaintracks/createKnexChaintracks.ts +65 -0
  168. package/src/services/chaintracker/chaintracks/createNoDbChaintracks.ts +60 -0
  169. package/src/services/chaintracker/chaintracks/index.all.ts +12 -0
  170. package/src/services/chaintracker/chaintracks/index.client.ts +4 -0
  171. package/src/services/chaintracker/chaintracks/index.mobile.ts +37 -0
  172. package/src/services/chaintracker/chaintracks/util/BulkFileDataManager.ts +975 -0
  173. package/src/services/chaintracker/chaintracks/util/BulkFileDataReader.ts +60 -0
  174. package/src/services/chaintracker/chaintracks/util/BulkFilesReader.ts +336 -0
  175. package/src/services/chaintracker/chaintracks/util/BulkHeaderFile.ts +247 -0
  176. package/src/services/chaintracker/chaintracks/util/ChaintracksFetch.ts +69 -0
  177. package/src/services/chaintracker/chaintracks/util/ChaintracksFs.ts +141 -0
  178. package/src/services/chaintracker/chaintracks/util/HeightRange.ts +153 -0
  179. package/src/services/chaintracker/chaintracks/util/SingleWriterMultiReaderLock.ts +76 -0
  180. package/src/services/chaintracker/chaintracks/util/__tests/BulkFileDataManager.test.ts +304 -0
  181. package/src/services/chaintracker/chaintracks/util/__tests/ChaintracksFetch.test.ts +60 -0
  182. package/src/services/chaintracker/chaintracks/util/__tests/HeightRange.test.ts +67 -0
  183. package/src/services/chaintracker/chaintracks/util/__tests/SingleWriterMultiReaderLock.test.ts +49 -0
  184. package/src/services/chaintracker/chaintracks/util/blockHeaderUtilities.ts +573 -0
  185. package/src/services/chaintracker/chaintracks/util/dirtyHashes.ts +29 -0
  186. package/src/services/chaintracker/chaintracks/util/validBulkHeaderFilesByFileHash.ts +432 -0
  187. package/src/services/chaintracker/index.all.ts +4 -0
  188. package/src/services/chaintracker/index.client.ts +4 -0
  189. package/src/services/chaintracker/index.mobile.ts +4 -0
  190. package/src/services/createDefaultWalletServicesOptions.ts +77 -0
  191. package/src/services/index.ts +1 -0
  192. package/src/services/processingErrors/arcSuccessError.json +76 -0
  193. package/src/services/providers/ARC.ts +350 -0
  194. package/src/services/providers/Bitails.ts +256 -0
  195. package/src/services/providers/SdkWhatsOnChain.ts +83 -0
  196. package/src/services/providers/WhatsOnChain.ts +883 -0
  197. package/src/services/providers/__tests/WhatsOnChain.test.ts +242 -0
  198. package/src/services/providers/__tests/exchangeRates.test.ts +18 -0
  199. package/src/services/providers/exchangeRates.ts +265 -0
  200. package/src/services/providers/getBeefForTxid.ts +369 -0
  201. package/src/signer/README.md +5 -0
  202. package/src/signer/WalletSigner.ts +17 -0
  203. package/src/signer/methods/acquireDirectCertificate.ts +52 -0
  204. package/src/signer/methods/buildSignableTransaction.ts +183 -0
  205. package/src/signer/methods/completeSignedTransaction.ts +117 -0
  206. package/src/signer/methods/createAction.ts +172 -0
  207. package/src/signer/methods/internalizeAction.ts +106 -0
  208. package/src/signer/methods/proveCertificate.ts +43 -0
  209. package/src/signer/methods/signAction.ts +54 -0
  210. package/src/storage/README.md +14 -0
  211. package/src/storage/StorageIdb.ts +2304 -0
  212. package/src/storage/StorageKnex.ts +1425 -0
  213. package/src/storage/StorageProvider.ts +810 -0
  214. package/src/storage/StorageReader.ts +194 -0
  215. package/src/storage/StorageReaderWriter.ts +432 -0
  216. package/src/storage/StorageSyncReader.ts +34 -0
  217. package/src/storage/WalletStorageManager.ts +943 -0
  218. package/src/storage/__test/StorageIdb.test.ts +43 -0
  219. package/src/storage/__test/WalletStorageManager.test.ts +275 -0
  220. package/src/storage/__test/adminStats.man.test.ts +89 -0
  221. package/src/storage/__test/getBeefForTransaction.test.ts +385 -0
  222. package/src/storage/index.all.ts +11 -0
  223. package/src/storage/index.client.ts +7 -0
  224. package/src/storage/index.mobile.ts +6 -0
  225. package/src/storage/methods/ListActionsSpecOp.ts +70 -0
  226. package/src/storage/methods/ListOutputsSpecOp.ts +129 -0
  227. package/src/storage/methods/__test/GenerateChange/generateChangeSdk.test.ts +1057 -0
  228. package/src/storage/methods/__test/GenerateChange/randomValsUsed1.ts +20 -0
  229. package/src/storage/methods/__test/offsetKey.test.ts +274 -0
  230. package/src/storage/methods/attemptToPostReqsToNetwork.ts +389 -0
  231. package/src/storage/methods/createAction.ts +947 -0
  232. package/src/storage/methods/generateChange.ts +556 -0
  233. package/src/storage/methods/getBeefForTransaction.ts +139 -0
  234. package/src/storage/methods/getSyncChunk.ts +293 -0
  235. package/src/storage/methods/internalizeAction.ts +562 -0
  236. package/src/storage/methods/listActionsIdb.ts +183 -0
  237. package/src/storage/methods/listActionsKnex.ts +226 -0
  238. package/src/storage/methods/listCertificates.ts +73 -0
  239. package/src/storage/methods/listOutputsIdb.ts +203 -0
  240. package/src/storage/methods/listOutputsKnex.ts +263 -0
  241. package/src/storage/methods/offsetKey.ts +89 -0
  242. package/src/storage/methods/processAction.ts +420 -0
  243. package/src/storage/methods/purgeData.ts +251 -0
  244. package/src/storage/methods/purgeDataIdb.ts +10 -0
  245. package/src/storage/methods/reviewStatus.ts +101 -0
  246. package/src/storage/methods/reviewStatusIdb.ts +43 -0
  247. package/src/storage/methods/utils.Buffer.ts +33 -0
  248. package/src/storage/methods/utils.ts +56 -0
  249. package/src/storage/remoting/StorageClient.ts +567 -0
  250. package/src/storage/remoting/StorageMobile.ts +544 -0
  251. package/src/storage/remoting/StorageServer.ts +291 -0
  252. package/src/storage/remoting/__test/StorageClient.test.ts +113 -0
  253. package/src/storage/schema/KnexMigrations.ts +489 -0
  254. package/src/storage/schema/StorageIdbSchema.ts +150 -0
  255. package/src/storage/schema/entities/EntityBase.ts +210 -0
  256. package/src/storage/schema/entities/EntityCertificate.ts +188 -0
  257. package/src/storage/schema/entities/EntityCertificateField.ts +136 -0
  258. package/src/storage/schema/entities/EntityCommission.ts +148 -0
  259. package/src/storage/schema/entities/EntityOutput.ts +290 -0
  260. package/src/storage/schema/entities/EntityOutputBasket.ts +153 -0
  261. package/src/storage/schema/entities/EntityOutputTag.ts +121 -0
  262. package/src/storage/schema/entities/EntityOutputTagMap.ts +123 -0
  263. package/src/storage/schema/entities/EntityProvenTx.ts +319 -0
  264. package/src/storage/schema/entities/EntityProvenTxReq.ts +580 -0
  265. package/src/storage/schema/entities/EntitySyncState.ts +389 -0
  266. package/src/storage/schema/entities/EntityTransaction.ts +306 -0
  267. package/src/storage/schema/entities/EntityTxLabel.ts +121 -0
  268. package/src/storage/schema/entities/EntityTxLabelMap.ts +123 -0
  269. package/src/storage/schema/entities/EntityUser.ts +112 -0
  270. package/src/storage/schema/entities/MergeEntity.ts +73 -0
  271. package/src/storage/schema/entities/__tests/CertificateFieldTests.test.ts +353 -0
  272. package/src/storage/schema/entities/__tests/CertificateTests.test.ts +354 -0
  273. package/src/storage/schema/entities/__tests/CommissionTests.test.ts +371 -0
  274. package/src/storage/schema/entities/__tests/OutputBasketTests.test.ts +278 -0
  275. package/src/storage/schema/entities/__tests/OutputTagMapTests.test.ts +242 -0
  276. package/src/storage/schema/entities/__tests/OutputTagTests.test.ts +288 -0
  277. package/src/storage/schema/entities/__tests/OutputTests.test.ts +464 -0
  278. package/src/storage/schema/entities/__tests/ProvenTxReqTests.test.ts +340 -0
  279. package/src/storage/schema/entities/__tests/ProvenTxTests.test.ts +504 -0
  280. package/src/storage/schema/entities/__tests/SyncStateTests.test.ts +288 -0
  281. package/src/storage/schema/entities/__tests/TransactionTests.test.ts +604 -0
  282. package/src/storage/schema/entities/__tests/TxLabelMapTests.test.ts +361 -0
  283. package/src/storage/schema/entities/__tests/TxLabelTests.test.ts +198 -0
  284. package/src/storage/schema/entities/__tests/stampLogTests.test.ts +90 -0
  285. package/src/storage/schema/entities/__tests/usersTests.test.ts +340 -0
  286. package/src/storage/schema/entities/index.ts +16 -0
  287. package/src/storage/schema/tables/TableCertificate.ts +21 -0
  288. package/src/storage/schema/tables/TableCertificateField.ts +12 -0
  289. package/src/storage/schema/tables/TableCommission.ts +13 -0
  290. package/src/storage/schema/tables/TableMonitorEvent.ts +9 -0
  291. package/src/storage/schema/tables/TableOutput.ts +64 -0
  292. package/src/storage/schema/tables/TableOutputBasket.ts +12 -0
  293. package/src/storage/schema/tables/TableOutputTag.ts +10 -0
  294. package/src/storage/schema/tables/TableOutputTagMap.ts +9 -0
  295. package/src/storage/schema/tables/TableProvenTx.ts +14 -0
  296. package/src/storage/schema/tables/TableProvenTxReq.ts +65 -0
  297. package/src/storage/schema/tables/TableSettings.ts +17 -0
  298. package/src/storage/schema/tables/TableSyncState.ts +18 -0
  299. package/src/storage/schema/tables/TableTransaction.ts +54 -0
  300. package/src/storage/schema/tables/TableTxLabel.ts +10 -0
  301. package/src/storage/schema/tables/TableTxLabelMap.ts +9 -0
  302. package/src/storage/schema/tables/TableUser.ts +16 -0
  303. package/src/storage/schema/tables/index.ts +16 -0
  304. package/src/storage/sync/StorageMySQLDojoReader.ts +696 -0
  305. package/src/storage/sync/index.ts +1 -0
  306. package/src/utility/Format.ts +133 -0
  307. package/src/utility/README.md +3 -0
  308. package/src/utility/ReaderUint8Array.ts +187 -0
  309. package/src/utility/ScriptTemplateBRC29.ts +73 -0
  310. package/src/utility/__tests/utilityHelpers.noBuffer.test.ts +109 -0
  311. package/src/utility/aggregateResults.ts +68 -0
  312. package/src/utility/identityUtils.ts +159 -0
  313. package/src/utility/index.all.ts +7 -0
  314. package/src/utility/index.client.ts +7 -0
  315. package/src/utility/parseTxScriptOffsets.ts +29 -0
  316. package/src/utility/stampLog.ts +69 -0
  317. package/src/utility/tscProofToMerklePath.ts +48 -0
  318. package/src/utility/utilityHelpers.buffer.ts +34 -0
  319. package/src/utility/utilityHelpers.noBuffer.ts +60 -0
  320. package/src/utility/utilityHelpers.ts +275 -0
  321. package/src/wab-client/WABClient.ts +94 -0
  322. package/src/wab-client/__tests/WABClient.man.test.ts +59 -0
  323. package/src/wab-client/auth-method-interactors/AuthMethodInteractor.ts +47 -0
  324. package/src/wab-client/auth-method-interactors/DevConsoleInteractor.ts +73 -0
  325. package/src/wab-client/auth-method-interactors/PersonaIDInteractor.ts +35 -0
  326. package/src/wab-client/auth-method-interactors/TwilioPhoneInteractor.ts +72 -0
  327. package/syncVersions.js +71 -0
  328. package/test/Wallet/StorageClient/storageClient.man.test.ts +75 -0
  329. package/test/Wallet/action/abortAction.test.ts +47 -0
  330. package/test/Wallet/action/createAction.test.ts +299 -0
  331. package/test/Wallet/action/createAction2.test.ts +1273 -0
  332. package/test/Wallet/action/createActionToGenerateBeefs.man.test.ts +293 -0
  333. package/test/Wallet/action/internalizeAction.a.test.ts +286 -0
  334. package/test/Wallet/action/internalizeAction.test.ts +682 -0
  335. package/test/Wallet/action/relinquishOutput.test.ts +37 -0
  336. package/test/Wallet/certificate/acquireCertificate.test.ts +298 -0
  337. package/test/Wallet/certificate/listCertificates.test.ts +346 -0
  338. package/test/Wallet/construct/Wallet.constructor.test.ts +57 -0
  339. package/test/Wallet/get/getHeaderForHeight.test.ts +82 -0
  340. package/test/Wallet/get/getHeight.test.ts +52 -0
  341. package/test/Wallet/get/getKnownTxids.test.ts +86 -0
  342. package/test/Wallet/get/getNetwork.test.ts +27 -0
  343. package/test/Wallet/get/getVersion.test.ts +27 -0
  344. package/test/Wallet/list/listActions.test.ts +279 -0
  345. package/test/Wallet/list/listActions2.test.ts +1381 -0
  346. package/test/Wallet/list/listCertificates.test.ts +118 -0
  347. package/test/Wallet/list/listOutputs.test.ts +447 -0
  348. package/test/Wallet/live/walletLive.man.test.ts +521 -0
  349. package/test/Wallet/local/localWallet.man.test.ts +93 -0
  350. package/test/Wallet/local/localWallet2.man.test.ts +277 -0
  351. package/test/Wallet/signAction/mountaintop.man.test.ts +130 -0
  352. package/test/Wallet/specOps/specOps.man.test.ts +220 -0
  353. package/test/Wallet/support/janitor.man.test.ts +40 -0
  354. package/test/Wallet/support/operations.man.test.ts +407 -0
  355. package/test/Wallet/support/reqErrorReview.2025.05.06.man.test.ts +347 -0
  356. package/test/Wallet/sync/Wallet.sync.test.ts +215 -0
  357. package/test/Wallet/sync/Wallet.updateWalletLegacyTestData.man.test.ts +203 -0
  358. package/test/Wallet/sync/setActive.test.ts +170 -0
  359. package/test/WalletClient/LocalKVStore.man.test.ts +114 -0
  360. package/test/WalletClient/WERR.man.test.ts +35 -0
  361. package/test/bsv-ts-sdk/LocalKVStore.test.ts +102 -0
  362. package/test/checkDB.ts +57 -0
  363. package/test/checkdb +0 -0
  364. package/test/examples/backup.man.test.ts +59 -0
  365. package/test/examples/pushdrop.test.ts +282 -0
  366. package/test/monitor/Monitor.test.ts +620 -0
  367. package/test/services/Services.test.ts +263 -0
  368. package/test/storage/KnexMigrations.test.ts +86 -0
  369. package/test/storage/StorageMySQLDojoReader.man.test.ts +60 -0
  370. package/test/storage/count.test.ts +177 -0
  371. package/test/storage/find.test.ts +195 -0
  372. package/test/storage/findLegacy.test.ts +67 -0
  373. package/test/storage/idb/allocateChange.test.ts +251 -0
  374. package/test/storage/idb/count.test.ts +158 -0
  375. package/test/storage/idb/find.test.ts +177 -0
  376. package/test/storage/idb/idbSpeed.test.ts +36 -0
  377. package/test/storage/idb/insert.test.ts +268 -0
  378. package/test/storage/idb/transactionAbort.test.ts +108 -0
  379. package/test/storage/idb/update.test.ts +999 -0
  380. package/test/storage/insert.test.ts +278 -0
  381. package/test/storage/update.test.ts +1021 -0
  382. package/test/storage/update2.test.ts +897 -0
  383. package/test/utils/TestUtilsWalletStorage.ts +2526 -0
  384. package/test/utils/localWalletMethods.ts +363 -0
  385. package/test/utils/removeFailedFromDatabase.sql +17 -0
  386. package/ts2md.json +44 -0
  387. package/tsconfig.all.json +31 -0
  388. package/tsconfig.client.json +29 -0
  389. package/tsconfig.json +17 -0
  390. package/tsconfig.mobile.json +28 -0
@@ -0,0 +1,189 @@
1
+ import { ChainTracker } from '@bsv/sdk'
2
+ import { BaseBlockHeader, BlockHeader } from './BlockHeaderApi'
3
+ import { Chain } from '../../../../sdk/types'
4
+
5
+ /**
6
+ * @public
7
+ */
8
+ export type HeaderListener = (header: BlockHeader) => void
9
+
10
+ /**
11
+ * @public
12
+ */
13
+ export type ReorgListener = (
14
+ depth: number,
15
+ oldTip: BlockHeader,
16
+ newTip: BlockHeader,
17
+ deactivatedHeaders?: BlockHeader[]
18
+ ) => void
19
+
20
+ /**
21
+ * @public
22
+ */
23
+ export interface ChaintracksPackageInfoApi {
24
+ name: string
25
+ version: string
26
+ }
27
+
28
+ /**
29
+ * @public
30
+ */
31
+ export interface ChaintracksInfoApi {
32
+ chain: Chain
33
+ heightBulk: number
34
+ heightLive: number
35
+ storage: string
36
+ bulkIngestors: string[]
37
+ liveIngestors: string[]
38
+ packages: ChaintracksPackageInfoApi[]
39
+ }
40
+
41
+ /**
42
+ * Chaintracks client API excluding events and callbacks
43
+ * @public
44
+ */
45
+ export interface ChaintracksClientApi extends ChainTracker {
46
+ /**
47
+ * Confirms the chain
48
+ */
49
+ getChain(): Promise<Chain>
50
+
51
+ /**
52
+ * @returns Summary of configuration and state.
53
+ */
54
+ getInfo(): Promise<ChaintracksInfoApi>
55
+
56
+ /**
57
+ * Return the latest chain height from configured bulk ingestors.
58
+ */
59
+ getPresentHeight(): Promise<number>
60
+
61
+ /**
62
+ * Adds headers in 80 byte serialized format to an array.
63
+ * Only adds active headers.
64
+ * array length divided by 80 is the actual number returned.
65
+ *
66
+ * @param height of first header
67
+ * @param count of headers, maximum
68
+ * @returns array of headers as serialized hex string
69
+ */
70
+ getHeaders(height: number, count: number): Promise<string>
71
+
72
+ /**
73
+ * Returns the active chain tip header
74
+ */
75
+ findChainTipHeader(): Promise<BlockHeader>
76
+
77
+ /**
78
+ * Returns the block hash of the active chain tip.
79
+ */
80
+ findChainTipHash(): Promise<string>
81
+
82
+ /**
83
+ * Returns block header for a given block height on active chain.
84
+ */
85
+ findHeaderForHeight(height: number): Promise<BlockHeader | undefined>
86
+
87
+ /**
88
+ * Returns block header for a given recent block hash or undefined.
89
+ * @param hash
90
+ */
91
+ findHeaderForBlockHash(hash: string): Promise<BlockHeader | undefined>
92
+
93
+ /**
94
+ * Submit a possibly new header for adding
95
+ *
96
+ * If the header is invalid or a duplicate it will not be added.
97
+ *
98
+ * This header will be ignored if the previous header has not already been inserted when this header
99
+ * is considered for insertion.
100
+ *
101
+ * @param header
102
+ * @returns immediately
103
+ */
104
+ addHeader(header: BaseBlockHeader): Promise<void>
105
+
106
+ /**
107
+ * Start or resume listening for new headers.
108
+ *
109
+ * Calls `synchronize` to catch up on headers that were found while not listening.
110
+ *
111
+ * Begins listening to any number of configured new header notification services.
112
+ *
113
+ * Begins sending notifications to subscribed listeners only after processing any
114
+ * previously found headers.
115
+ *
116
+ * May be called if already listening or synchronizing to listen.
117
+ *
118
+ * The `listening` API function which returns a Promise can be awaited.
119
+ */
120
+ startListening(): Promise<void>
121
+
122
+ /**
123
+ * Returns a Promise that will resolve when the previous call to startListening
124
+ * enters the listening-for-new-headers state.
125
+ */
126
+ listening(): Promise<void>
127
+
128
+ /**
129
+ * Returns true if actively listening for new headers and client api is enabled.
130
+ */
131
+ isListening(): Promise<boolean>
132
+
133
+ /**
134
+ * Returns true if `synchronize` has completed at least once.
135
+ */
136
+ isSynchronized(): Promise<boolean>
137
+
138
+ /**
139
+ * Subscribe to "header" events.
140
+ * @param listener
141
+ * @returns identifier for this subscription
142
+ * @throws ERR_NOT_IMPLEMENTED if callback events are not supported
143
+ */
144
+ subscribeHeaders(listener: HeaderListener): Promise<string>
145
+
146
+ /**
147
+ * Subscribe to "reorganization" events.
148
+ * @param listener
149
+ * @returns identifier for this subscription
150
+ * @throws ERR_NOT_IMPLEMENTED if callback events are not supported
151
+ */
152
+ subscribeReorgs(listener: ReorgListener): Promise<string>
153
+
154
+ /**
155
+ * Cancels all subscriptions with the given `subscriptionId` which was previously returned
156
+ * by a `subscribe` method.
157
+ * @param subscriptionId value previously returned by subscribeToHeaders or subscribeToReorgs
158
+ * @returns true if a subscription was canceled
159
+ * @throws ERR_NOT_IMPLEMENTED if callback events are not supported
160
+ */
161
+ unsubscribe(subscriptionId: string): Promise<boolean>
162
+
163
+ isValidRootForHeight(root: string, height: number): Promise<boolean>
164
+ currentHeight: () => Promise<number>
165
+ }
166
+
167
+ /**
168
+ * Full Chaintracks API including startListening with callbacks
169
+ */
170
+ export interface ChaintracksApi extends ChaintracksClientApi {
171
+ /**
172
+ * Start or resume listening for new headers.
173
+ *
174
+ * Calls `synchronize` to catch up on headers that were found while not listening.
175
+ *
176
+ * Begins listening to any number of configured new header notification services.
177
+ *
178
+ * Begins sending notifications to subscribed listeners only after processing any
179
+ * previously found headers.
180
+ *
181
+ * May be called if already listening or synchronizing to listen.
182
+ *
183
+ * `listening` callback will be called after listening for new live headers has begun.
184
+ * Alternatively, the `listening` API function which returns a Promise can be awaited.
185
+ *
186
+ * @param listening callback indicates when listening for new headers has started.
187
+ */
188
+ startListening(listening?: () => void): Promise<void>
189
+ }
@@ -0,0 +1,18 @@
1
+ import { HttpClient } from '@bsv/sdk'
2
+
3
+ /**
4
+ * Provides a simplified interface based on the @bsv/sdk `HttpClient` class
5
+ * with just the methods necesary for most Chaintracks operations.
6
+ *
7
+ * The primary purpose is to isolate and centralize external package dependency.
8
+ *
9
+ * Specific ingestors are free to use other means for access.
10
+ *
11
+ * The `ChaintracksFetch` class implements this interface.
12
+ */
13
+ export interface ChaintracksFetchApi {
14
+ httpClient: HttpClient
15
+ download(url: string): Promise<Uint8Array>
16
+ fetchJson<R>(url: string): Promise<R>
17
+ pathJoin(baseUrl: string, subpath: string): string
18
+ }
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Supports access to named data storage (file like).
3
+ */
4
+ export interface ChaintracksReadableFileApi {
5
+ path: string
6
+ close(): Promise<void>
7
+ /**
8
+ * Returns the length of the data storage in bytes.
9
+ */
10
+ getLength(): Promise<number>
11
+ /**
12
+ *
13
+ * @param length requested length to be returned, may return less than requested.
14
+ * @param offset starting offset in the existing data storage to read from, defaults to 0.
15
+ */
16
+ read(length?: number, offset?: number): Promise<Uint8Array>
17
+ }
18
+
19
+ /**
20
+ * Supports access and appending data to new or existing named data storage.
21
+ * New data is always appended to the end of existing data.
22
+ */
23
+ export interface ChaintracksAppendableFileApi extends ChaintracksReadableFileApi {
24
+ /**
25
+ * @param data data to add to the end of existing data.
26
+ */
27
+ append(data: Uint8Array): Promise<void>
28
+ }
29
+
30
+ /**
31
+ * Supports creation or re-creation of named data storage from position 0.
32
+ * Any pre-existing data is initially removed.
33
+ * Does not support reading existing data.
34
+ */
35
+ export interface ChaintracksWritableFileApi {
36
+ path: string
37
+ close(): Promise<void>
38
+ /**
39
+ * @param data data to add to the end of existing data.
40
+ */
41
+ append(data: Uint8Array): Promise<void>
42
+ }
43
+
44
+ /**
45
+ * Supports file-like access to named data storage.
46
+ *
47
+ * Only minimal functionality required by Chaintracks is supported.
48
+ */
49
+ export interface ChaintracksFsApi {
50
+ delete(path: string): Promise<void>
51
+ writeFile(path: string, data: Uint8Array): Promise<void>
52
+ readFile(path: string): Promise<Uint8Array>
53
+ openReadableFile(path: string): Promise<ChaintracksReadableFileApi>
54
+ openWritableFile(path: string): Promise<ChaintracksWritableFileApi>
55
+ openAppendableFile(path: string): Promise<ChaintracksAppendableFileApi>
56
+
57
+ pathJoin(...parts: string[]): string
58
+ }
@@ -0,0 +1,386 @@
1
+ import { HeightRange } from '../util/HeightRange'
2
+ import { BaseBlockHeader, BlockHeader, LiveBlockHeader } from './BlockHeaderApi'
3
+ import { Chain } from '../../../../sdk/types'
4
+ import { BulkHeaderFileInfo } from '../util/BulkHeaderFile'
5
+ import { BulkFileDataManager } from '../util/BulkFileDataManager'
6
+
7
+ /// StorageEngine Interface
8
+
9
+ export interface ChaintracksStorageBaseOptions {
10
+ /**
11
+ * Which chain is being tracked: main, test, or stn.
12
+ */
13
+ chain: Chain
14
+
15
+ /**
16
+ * How much of recent history is required to be kept in "live" block header storage.
17
+ *
18
+ * Headers with height less than active chain tip height minus `liveHeightThreshold`
19
+ * are not required to be kept in "live" storage and may be migrated to "bulk" storage.
20
+ *
21
+ * As no forks, orphans, or reorgs can affect "bulk" block header storage, an
22
+ * aggressively high number is recommended: At least an order of magnitude more than
23
+ * the deepest actual reorg you can imagine.
24
+ */
25
+ liveHeightThreshold: number
26
+
27
+ /**
28
+ * How much of recent history must be processed with full validation and reorg support.
29
+ *
30
+ * Must be less than or equal to `liveHeightThreshold`.
31
+ *
32
+ * Headers with height older than active chain tip height minus `reorgHeightThreshold`
33
+ * may use batch processing when ingesting headers.
34
+ */
35
+ reorgHeightThreshold: number
36
+
37
+ /**
38
+ * How many excess "live" headers to accumulate before migrating them as a chunk to the
39
+ * bulk header storage.
40
+ */
41
+ bulkMigrationChunkSize: number
42
+
43
+ /**
44
+ * Maximum number of headers per call to batchInsert
45
+ */
46
+ batchInsertLimit: number
47
+
48
+ /**
49
+ * Controls in memory caching and retrieval of missing bulk header data.
50
+ */
51
+ bulkFileDataManager: BulkFileDataManager | undefined
52
+ }
53
+
54
+ export interface ChaintracksStorageQueryApi {
55
+ log: (...args: any[]) => void
56
+
57
+ /**
58
+ * Returns the active chain tip header
59
+ * Throws an error if there is no tip.
60
+ */
61
+ findChainTipHeader(): Promise<LiveBlockHeader>
62
+
63
+ /**
64
+ * Returns the block hash of the active chain tip.
65
+ */
66
+ findChainTipHash(): Promise<string>
67
+
68
+ /**
69
+ * Returns the active chain tip header or undefined if there is no tip.
70
+ */
71
+ findChainTipHeaderOrUndefined(): Promise<LiveBlockHeader | undefined>
72
+
73
+ /**
74
+ * Returns the chainWork value of the active chain tip
75
+ */
76
+ findChainTipWork(): Promise<string>
77
+
78
+ /**
79
+ * Returns block header for a given block height on active chain.
80
+ * @param hash block hash
81
+ */
82
+ findHeaderForHeight(height: number): Promise<LiveBlockHeader | BlockHeader>
83
+
84
+ /**
85
+ * Returns block header for a given block height on active chain.
86
+ * @param hash block hash
87
+ */
88
+ findHeaderForHeightOrUndefined(height: number): Promise<LiveBlockHeader | BlockHeader | undefined>
89
+
90
+ /**
91
+ * Given two chain tip headers in a chain reorg scenario,
92
+ * return their common ancestor header.
93
+ * @param header1 First header in live part of the chain.
94
+ * @param header2 Second header in live part of the chain.
95
+ */
96
+ findCommonAncestor(header1: LiveBlockHeader, header2: LiveBlockHeader): Promise<LiveBlockHeader>
97
+
98
+ /**
99
+ * This is an original API. Proposed deprecation in favor of `findCommonAncestor`
100
+ * Given two headers that are both chain tips in a reorg scenario, returns
101
+ * the depth of the reorg (the greater of the heights of the two provided
102
+ * headers, minus the height of their last common ancestor)
103
+ */
104
+ findReorgDepth(header1: LiveBlockHeader, header2: LiveBlockHeader): Promise<number>
105
+
106
+ /**
107
+ * Returns true if the given merkleRoot is found in a block header on the active chain.
108
+ * @param merkleRoot of block header
109
+ */
110
+ isMerkleRootActive(merkleRoot: string): Promise<boolean>
111
+
112
+ /**
113
+ * Returns serialized headers as a Uint8Array.
114
+ * Only adds bulk and active live headers.
115
+ *
116
+ * @param height is the minimum header height to return, must be >= zero.
117
+ * @param count height + count - 1 is the maximum header height to return.
118
+ * @returns serialized headers as a Uint8Array.
119
+ */
120
+ getHeadersUint8Array(height: number, count: number): Promise<Uint8Array>
121
+
122
+ /**
123
+ * Returns an array of deserialized headers.
124
+ * Only adds bulk and active live headers.
125
+ *
126
+ * @param height is the minimum header height to return, must be >= zero.
127
+ * @param count height + count - 1 is the maximum header height to return.
128
+ * @returns array of deserialized headers
129
+ */
130
+ getHeaders(height: number, count: number): Promise<BaseBlockHeader[]>
131
+
132
+ /**
133
+ * Returns active `LiveBlockHeaders` with height in the given range.
134
+ *
135
+ * @param range
136
+ * @returns array of active `LiveBlockHeaders`
137
+ */
138
+ getLiveHeaders(range: HeightRange): Promise<LiveBlockHeader[]>
139
+
140
+ /**
141
+ * Returns serialized bulk headers in the given range.
142
+ *
143
+ * @param range
144
+ * @returns serialized headers as a Uint8Array.
145
+ */
146
+ getBulkHeaders(range: HeightRange): Promise<Uint8Array>
147
+
148
+ /**
149
+ * Returns block header for a given block height on active chain.
150
+ * @param hash block hash
151
+ */
152
+ findLiveHeaderForHeight(height: number): Promise<LiveBlockHeader | null>
153
+
154
+ /**
155
+ * Returns block header for a given headerId.
156
+
157
+ * Only from the "live" portion of the chain.
158
+ * @param headerId
159
+ */
160
+ findLiveHeaderForHeaderId(headerId: number): Promise<LiveBlockHeader>
161
+
162
+ /**
163
+ * Returns block header for a given block hash.
164
+ * Only from the "live" portion of the chain.
165
+ * Returns null if not found.
166
+ * @param hash block hash
167
+ */
168
+ findLiveHeaderForBlockHash(hash: string): Promise<LiveBlockHeader | null>
169
+
170
+ /**
171
+ * Returns block header for a given merkleRoot.
172
+ * Only from the "live" portion of the chain.
173
+ * @param merkleRoot
174
+ */
175
+ findLiveHeaderForMerkleRoot(merkleRoot: string): Promise<LiveBlockHeader | null>
176
+
177
+ /**
178
+ * Returns the height range of both bulk and live storage.
179
+ * Verifies that the ranges meet these requirements:
180
+ * - Both may be empty.
181
+ * - If bulk is empty, live must be empty or start with height zero.
182
+ * - If bulk is not empty it must start with height zero.
183
+ * - If bulk is not empty and live is not empty, live must start with the height after bulk.
184
+ */
185
+ getAvailableHeightRanges(): Promise<{ bulk: HeightRange; live: HeightRange }>
186
+
187
+ /**
188
+ * @returns The current minimum and maximum height active LiveBlockHeaders in the "live" database.
189
+ */
190
+ findLiveHeightRange(): Promise<HeightRange>
191
+
192
+ /**
193
+ * @returns The maximum headerId value used by existing records or -1 if there are none.
194
+ */
195
+ findMaxHeaderId(): Promise<number>
196
+
197
+ //
198
+ // Available Properties
199
+ //
200
+
201
+ /**
202
+ * Which chain is being tracked: "main" or "test".
203
+ */
204
+ chain: Chain
205
+
206
+ /**
207
+ * How much of recent history is required to be kept in "live" block header storage.
208
+ *
209
+ * Headers with height older than active chain tip height minus `liveHeightThreshold`
210
+ * are not required to be kept in "live" storage and may be migrated to "bulk" storage.
211
+ */
212
+ liveHeightThreshold: number
213
+
214
+ /**
215
+ * How much of recent history must be processed with full validation and reorg support.
216
+ *
217
+ * May be less than `liveHeightThreshold`.
218
+ *
219
+ * Headers with height older than active chain tip height minus ``
220
+ * may use batch processing when ingesting headers.
221
+ */
222
+ reorgHeightThreshold: number
223
+
224
+ /**
225
+ * How many excess "live" headers to accumulate before migrating them as a chunk to the
226
+ * bulk header storage.
227
+ */
228
+ bulkMigrationChunkSize: number
229
+
230
+ /**
231
+ * Maximum number of headers per call to batchInsert
232
+ */
233
+ batchInsertLimit: number
234
+ }
235
+
236
+ export type InsertHeaderResult = {
237
+ /**
238
+ * true only if the new header was inserted
239
+ */
240
+ added: boolean
241
+ /**
242
+ * true only if the header was not inserted because a matching hash already exists in the database.
243
+ */
244
+ dupe: boolean
245
+ /**
246
+ * true only if the new header became the active chain tip.
247
+ */
248
+ isActiveTip: boolean
249
+ /**
250
+ * zero if the insertion of the new header did not cause a reorg.
251
+ * If isActiveTip is true, and priorTip is not the new headers previous header,
252
+ * then the minimum height difference from the common active ancestor to this header (new tip) and priorTip.
253
+ */
254
+ reorgDepth: number
255
+ /**
256
+ * If `added` is true, this header was the active chain tip before the insert. It may or may not still be the active chain tip after the insert.
257
+ */
258
+ priorTip: LiveBlockHeader | undefined
259
+ /**
260
+ * If a reorg has occurred, these headers where active and are now deactivated.
261
+ */
262
+ deactivatedHeaders: LiveBlockHeader[]
263
+ /**
264
+ * header's previousHash was not found in database
265
+ */
266
+ noPrev: boolean
267
+ /**
268
+ * header matching previousHash does not have height - 1
269
+ */
270
+ badPrev: boolean
271
+ /**
272
+ * an active ancestor was not found in live storage or prev header.
273
+ */
274
+ noActiveAncestor: boolean
275
+ /**
276
+ * a current chain tip was not found in live storage or prev header.
277
+ */
278
+ noTip: boolean
279
+ }
280
+
281
+ export interface ChaintracksStorageBulkFileApi {
282
+ insertBulkFile(file: BulkHeaderFileInfo): Promise<number>
283
+ updateBulkFile(fileId: number, file: BulkHeaderFileInfo): Promise<number>
284
+ deleteBulkFile(fileId: number): Promise<number>
285
+ getBulkFiles(): Promise<BulkHeaderFileInfo[]>
286
+ getBulkFileData(fileId: number, offset?: number, length?: number): Promise<Uint8Array | undefined>
287
+ }
288
+
289
+ export interface ChaintracksStorageIngestApi {
290
+ log: (...args: any[]) => void
291
+
292
+ /**
293
+ * Attempts to insert a block header into the chain.
294
+ *
295
+ * Returns 'added' false and 'dupe' true if header's hash already exists in the live database
296
+ * Returns 'added' false and 'dupe' false if header's previousHash wasn't found in the live database, or height doesn't increment previous' height.
297
+ *
298
+ * Computes the header's chainWork from its bits and the previous header's chainWork.
299
+ *
300
+ * Returns 'added' true if the header was added to the live database.
301
+ * Returns 'isActiveTip' true if header's chainWork is greater than current active chain tip's chainWork.
302
+ *
303
+ * If the addition of this header caused a reorg (did not directly extend old active chain tip):
304
+ * Returns 'reorgDepth' the minimum height difference of the common ancestor to the two chain tips.
305
+ * Returns 'priorTip' the old active chain tip.
306
+ * If not a reorg:
307
+ * Returns 'reorgDepth' of zero.
308
+ * Returns 'priorTip' the active chain tip before this insert. May be unchanged.
309
+ *
310
+ * Implementation must call `pruneLiveBlockHeaders` after adding new header.
311
+ *
312
+ * @param header to insert
313
+ * @param prev if not undefined, the last bulk storage header with total bulk chainWork
314
+ */
315
+ insertHeader(header: BlockHeader, prev?: LiveBlockHeader): Promise<InsertHeaderResult>
316
+
317
+ /**
318
+ * Must be called after the addition of new LiveBlockHeaders.
319
+ *
320
+ * Checks the `StorageEngine` configuration options to see
321
+ * if BulkStorage is configured and if there is at least one
322
+ * `bulkMigrationChunkSize` woth of headers in excess of
323
+ * `liveHeightThreshold` available.
324
+ *
325
+ * If yes, then calls `migrateLiveToBulk` one or more times.
326
+ * @param activeTipHeight height of active tip after adds
327
+ */
328
+ pruneLiveBlockHeaders(activeTipHeight: number): Promise<void>
329
+
330
+ /**
331
+ * Migrates the oldest `count` LiveBlockHeaders to BulkStorage.
332
+ * BulkStorage must be configured.
333
+ * `count` must not exceed `bulkMigrationChunkSize`.
334
+ * `count` must leave at least `liveHeightThreshold` LiveBlockHeaders.
335
+ *
336
+ * @param count
337
+ *
338
+ * Steps:
339
+ * - Copy count oldest active LiveBlockHeaders from live database to buffer.
340
+ * - Append the buffer of headers to BulkStorage
341
+ * - Add the buffer's BlockHash, Height pairs to corresponding index table.
342
+ * - Add the buffer's MerkleRoot, Height pairs to corresponding index table.
343
+ * - Delete the records from the live database.
344
+ */
345
+ migrateLiveToBulk(count: number): Promise<void>
346
+
347
+ /**
348
+ * Delete live headers with height less or equal to `maxHeight`
349
+ * after they have been migrated to bulk storage.
350
+ *
351
+ * @param maxHeight delete all records with less or equal `height`
352
+ */
353
+ deleteOlderLiveBlockHeaders(maxHeight: number): Promise<number>
354
+
355
+ /**
356
+ * Async initialization method.
357
+ *
358
+ * May be called prior to other async methods to control when initialization occurs.
359
+ */
360
+ makeAvailable(): Promise<void>
361
+
362
+ /**
363
+ * Migrate storage schema to latest schema changes.
364
+ *
365
+ * Typically invoked automatically by `makeAvailable`.
366
+ */
367
+ migrateLatest(): Promise<void>
368
+
369
+ dropAllData(): Promise<void>
370
+
371
+ /**
372
+ * Release all resources. Makes the instance unusable.
373
+ */
374
+ destroy(): Promise<void>
375
+ }
376
+
377
+ export interface ChaintracksStorageApi extends ChaintracksStorageQueryApi, ChaintracksStorageIngestApi {
378
+ log: (...args: any[]) => void
379
+
380
+ bulkManager: BulkFileDataManager
381
+
382
+ /**
383
+ * Close and release all resources.
384
+ */
385
+ destroy(): Promise<void>
386
+ }
@@ -0,0 +1,25 @@
1
+ import { BlockHeader } from './BlockHeaderApi'
2
+ import { ChaintracksStorageApi } from './ChaintracksStorageApi'
3
+
4
+ /* eslint-disable @typescript-eslint/no-empty-interface */
5
+ export interface LiveIngestorApi {
6
+ /**
7
+ * Close and release all resources.
8
+ */
9
+ shutdown(): Promise<void>
10
+
11
+ getHeaderByHash(hash: string): Promise<BlockHeader | undefined>
12
+
13
+ /**
14
+ * Called before first Synchronize with reference to storage.
15
+ * Components requiring asynchronous setup can override base class implementation.
16
+ * @param storage
17
+ */
18
+ setStorage(storage: ChaintracksStorageApi, log: (...args: any[]) => void): Promise<void>
19
+
20
+ storage(): ChaintracksStorageApi
21
+
22
+ startListening(liveHeaders: BlockHeader[]): Promise<void>
23
+
24
+ stopListening(): void
25
+ }