@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,544 @@
1
+ import {
2
+ AbortActionArgs,
3
+ AbortActionResult,
4
+ InternalizeActionArgs,
5
+ ListActionsResult,
6
+ ListCertificatesResult,
7
+ ListOutputsResult,
8
+ RelinquishCertificateArgs,
9
+ RelinquishOutputArgs,
10
+ WalletInterface,
11
+ AuthFetch,
12
+ Validation
13
+ } from '@bsv/sdk'
14
+ import {
15
+ AuthId,
16
+ FindCertificatesArgs,
17
+ FindOutputBasketsArgs,
18
+ FindOutputsArgs,
19
+ FindProvenTxReqsArgs,
20
+ ProcessSyncChunkResult,
21
+ RequestSyncChunkArgs,
22
+ StorageCreateActionResult,
23
+ StorageInternalizeActionResult,
24
+ StorageProcessActionArgs,
25
+ StorageProcessActionResults,
26
+ SyncChunk,
27
+ UpdateProvenTxReqWithNewProvenTxArgs,
28
+ UpdateProvenTxReqWithNewProvenTxResult,
29
+ WalletStorageProvider
30
+ } from '../../sdk/WalletStorage.interfaces'
31
+ import { WERR_INVALID_OPERATION } from '../../sdk/WERR_errors'
32
+ import { WalletServices } from '../../sdk/WalletServices.interfaces'
33
+ import { TableSettings } from '../schema/tables/TableSettings'
34
+ import { TableUser } from '../schema/tables/TableUser'
35
+ import { TableSyncState } from '../schema/tables/TableSyncState'
36
+ import { TableCertificateX } from '../schema/tables/TableCertificate'
37
+ import { TableOutputBasket } from '../schema/tables/TableOutputBasket'
38
+ import { TableOutput } from '../schema/tables/TableOutput'
39
+ import { TableProvenTxReq } from '../schema/tables/TableProvenTxReq'
40
+ import { EntityTimeStamp } from '../../sdk/types'
41
+
42
+ /**
43
+ * `StorageClient` implements the `WalletStorageProvider` interface which allows it to
44
+ * serve as a BRC-100 wallet's active storage.
45
+ *
46
+ * Internally, it uses JSON-RPC over HTTPS to make requests of a remote server.
47
+ * Typically this server uses the `StorageServer` class to implement the service.
48
+ *
49
+ * The `AuthFetch` component is used to secure and authenticate the requests to the remote server.
50
+ *
51
+ * `AuthFetch` is initialized with a BRC-100 wallet which establishes the identity of
52
+ * the party making requests of the remote service.
53
+ *
54
+ * For details of the API implemented, follow the "See also" link for the `WalletStorageProvider` interface.
55
+ */
56
+ export class StorageClient implements WalletStorageProvider {
57
+ readonly endpointUrl: string
58
+ private readonly authClient: AuthFetch
59
+ private nextId = 1
60
+
61
+ // Track ephemeral (in-memory) "settings" if you wish to align with isAvailable() checks
62
+ public settings?: TableSettings
63
+
64
+ constructor(wallet: WalletInterface, endpointUrl: string) {
65
+ this.authClient = new AuthFetch(wallet)
66
+ this.endpointUrl = endpointUrl
67
+ }
68
+
69
+ /**
70
+ * The `StorageClient` implements the `WalletStorageProvider` interface.
71
+ * It does not implement the lower level `StorageProvider` interface.
72
+ *
73
+ * @returns false
74
+ */
75
+ isStorageProvider(): boolean {
76
+ return false
77
+ }
78
+
79
+ //////////////////////////////////////////////////////////////////////////////
80
+ // JSON-RPC helper
81
+ //////////////////////////////////////////////////////////////////////////////
82
+
83
+ /**
84
+ * Make a JSON-RPC call to the remote server.
85
+ * @param method The WalletStorage method name to call.
86
+ * @param params The array of parameters to pass to the method in order.
87
+ */
88
+ private async rpcCall<T>(method: string, params: unknown[]): Promise<T> {
89
+ try {
90
+ const id = this.nextId++
91
+ const body = {
92
+ jsonrpc: '2.0',
93
+ method,
94
+ params,
95
+ id
96
+ }
97
+
98
+ let response: Response
99
+ try {
100
+ response = await this.authClient.fetch(this.endpointUrl, {
101
+ method: 'POST',
102
+ headers: { 'Content-Type': 'application/json' },
103
+ body: JSON.stringify(body)
104
+ })
105
+ } catch (eu: unknown) {
106
+ throw eu
107
+ }
108
+
109
+ if (!response.ok) {
110
+ throw new Error(`WalletStorageClient rpcCall: network error ${response.status} ${response.statusText}`)
111
+ }
112
+
113
+ const json = await response.json()
114
+ if (json.error) {
115
+ const { code, message, data } = json.error
116
+ const err = new Error(`RPC Error: ${message}`)
117
+ // You could attach more info here if you like:
118
+ ;(err as any).code = code
119
+ ;(err as any).data = data
120
+ throw err
121
+ }
122
+
123
+ return json.result
124
+ } catch (eu: unknown) {
125
+ throw eu
126
+ }
127
+ }
128
+
129
+ /**
130
+ * @returns true once storage `TableSettings` have been retreived from remote storage.
131
+ */
132
+ isAvailable(): boolean {
133
+ // We'll just say "yes" if we have settings
134
+ return !!this.settings
135
+ }
136
+
137
+ /**
138
+ * @returns remote storage `TableSettings` if they have been retreived by `makeAvailable`.
139
+ * @throws WERR_INVALID_OPERATION if `makeAvailable` has not yet been called.
140
+ */
141
+ getSettings(): TableSettings {
142
+ if (!this.settings) {
143
+ throw new WERR_INVALID_OPERATION('call makeAvailable at least once before getSettings')
144
+ }
145
+ return this.settings
146
+ }
147
+
148
+ /**
149
+ * Must be called prior to making use of storage.
150
+ * Retreives `TableSettings` from remote storage provider.
151
+ * @returns remote storage `TableSettings`
152
+ */
153
+ async makeAvailable(): Promise<TableSettings> {
154
+ if (!this.settings) {
155
+ this.settings = await this.rpcCall<TableSettings>('makeAvailable', [])
156
+ }
157
+ return this.settings
158
+ }
159
+
160
+ //////////////////////////////////////////////////////////////////////////////
161
+ //
162
+ // Implementation of all WalletStorage interface methods
163
+ // They are simple pass-thrus to rpcCall
164
+ //
165
+ // IMPORTANT: The parameter ordering must match exactly as in your interface.
166
+ //////////////////////////////////////////////////////////////////////////////
167
+
168
+ /**
169
+ * Called to cleanup resources when no further use of this object will occur.
170
+ */
171
+ async destroy(): Promise<void> {
172
+ return this.rpcCall<void>('destroy', [])
173
+ }
174
+
175
+ /**
176
+ * Requests schema migration to latest.
177
+ * Typically remote storage will ignore this request.
178
+ * @param storageName Unique human readable name for remote storage if it does not yet exist.
179
+ * @param storageIdentityKey Unique identity key for remote storage if it does not yet exist.
180
+ * @returns current schema migration identifier
181
+ */
182
+ async migrate(storageName: string, storageIdentityKey: string): Promise<string> {
183
+ return this.rpcCall<string>('migrate', [storageName])
184
+ }
185
+
186
+ /**
187
+ * Remote storage does not offer `Services` to remote clients.
188
+ * @throws WERR_INVALID_OPERATION
189
+ */
190
+ getServices(): WalletServices {
191
+ // Typically, the client would not store or retrieve "Services" from a remote server.
192
+ // The "services" in local in-memory usage is a no-op or your own approach:
193
+ throw new WERR_INVALID_OPERATION(
194
+ 'getServices() not implemented in remote client. This method typically is not used remotely.'
195
+ )
196
+ }
197
+
198
+ /**
199
+ * Ignored. Remote storage cannot share `Services` with remote clients.
200
+ */
201
+ setServices(v: WalletServices): void {
202
+ // Typically no-op for remote client
203
+ // Because "services" are usually local definitions to the Storage.
204
+ }
205
+
206
+ /**
207
+ * Storage level processing for wallet `internalizeAction`.
208
+ * Updates internalized outputs in remote storage.
209
+ * Triggers proof validation of containing transaction.
210
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
211
+ * This must match the `AuthFetch` identity securing the remote conneciton.
212
+ * @param args Original wallet `internalizeAction` arguments.
213
+ * @returns `internalizeAction` results
214
+ */
215
+ async internalizeAction(auth: AuthId, args: InternalizeActionArgs): Promise<StorageInternalizeActionResult> {
216
+ return this.rpcCall<StorageInternalizeActionResult>('internalizeAction', [auth, args])
217
+ }
218
+
219
+ /**
220
+ * Storage level processing for wallet `createAction`.
221
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
222
+ * This must match the `AuthFetch` identity securing the remote conneciton.
223
+ * @param args Validated extension of original wallet `createAction` arguments.
224
+ * @returns `StorageCreateActionResults` supporting additional wallet processing to yield `createAction` results.
225
+ */
226
+ async createAction(auth: AuthId, args: Validation.ValidCreateActionArgs): Promise<StorageCreateActionResult> {
227
+ return this.rpcCall<StorageCreateActionResult>('createAction', [auth, args])
228
+ }
229
+
230
+ /**
231
+ * Storage level processing for wallet `createAction` and `signAction`.
232
+ *
233
+ * Handles remaining storage tasks once a fully signed transaction has been completed. This is common to both `createAction` and `signAction`.
234
+ *
235
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
236
+ * This must match the `AuthFetch` identity securing the remote conneciton.
237
+ * @param args `StorageProcessActionArgs` convey completed signed transaction to storage.
238
+ * @returns `StorageProcessActionResults` supporting final wallet processing to yield `createAction` or `signAction` results.
239
+ */
240
+ async processAction(auth: AuthId, args: StorageProcessActionArgs): Promise<StorageProcessActionResults> {
241
+ return this.rpcCall<StorageProcessActionResults>('processAction', [auth, args])
242
+ }
243
+
244
+ /**
245
+ * Aborts an action by `reference` string.
246
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
247
+ * This must match the `AuthFetch` identity securing the remote conneciton.
248
+ * @param args original wallet `abortAction` args.
249
+ * @returns `abortAction` result.
250
+ */
251
+ async abortAction(auth: AuthId, args: AbortActionArgs): Promise<AbortActionResult> {
252
+ return this.rpcCall<AbortActionResult>('abortAction', [auth, args])
253
+ }
254
+
255
+ /**
256
+ * Used to both find and initialize a new user by identity key.
257
+ * It is up to the remote storage whether to allow creation of new users by this method.
258
+ * @param identityKey of the user.
259
+ * @returns `TableUser` for the user and whether a new user was created.
260
+ */
261
+ async findOrInsertUser(identityKey): Promise<{ user: TableUser; isNew: boolean }> {
262
+ return this.rpcCall<{ user: TableUser; isNew: boolean }>('findOrInsertUser', [identityKey])
263
+ }
264
+
265
+ /**
266
+ * Used to both find and insert a `TableSyncState` record for the user to track wallet data replication across storage providers.
267
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
268
+ * This must match the `AuthFetch` identity securing the remote conneciton.
269
+ * @param storageName the name of the remote storage being sync'd
270
+ * @param storageIdentityKey the identity key of the remote storage being sync'd
271
+ * @returns `TableSyncState` and whether a new record was created.
272
+ */
273
+ async findOrInsertSyncStateAuth(
274
+ auth: AuthId,
275
+ storageIdentityKey: string,
276
+ storageName: string
277
+ ): Promise<{ syncState: TableSyncState; isNew: boolean }> {
278
+ const r = await this.rpcCall<{ syncState: TableSyncState; isNew: boolean }>('findOrInsertSyncStateAuth', [
279
+ auth,
280
+ storageIdentityKey,
281
+ storageName
282
+ ])
283
+ r.syncState = this.validateEntity(r.syncState, ['when'])
284
+ return r
285
+ }
286
+
287
+ /**
288
+ * Inserts a new certificate with fields and keyring into remote storage.
289
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
290
+ * This must match the `AuthFetch` identity securing the remote conneciton.
291
+ * @param certificate the certificate to insert.
292
+ * @returns record Id of the inserted `TableCertificate` record.
293
+ */
294
+ async insertCertificateAuth(auth: AuthId, certificate: TableCertificateX): Promise<number> {
295
+ const r = await this.rpcCall<number>('insertCertificateAuth', [auth, certificate])
296
+ return r
297
+ }
298
+
299
+ /**
300
+ * Storage level processing for wallet `listActions`.
301
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
302
+ * This must match the `AuthFetch` identity securing the remote conneciton.
303
+ * @param args Validated extension of original wallet `listActions` arguments.
304
+ * @returns `listActions` results.
305
+ */
306
+ async listActions(auth: AuthId, vargs: Validation.ValidListActionsArgs): Promise<ListActionsResult> {
307
+ const r = await this.rpcCall<ListActionsResult>('listActions', [auth, vargs])
308
+ return r
309
+ }
310
+
311
+ /**
312
+ * Storage level processing for wallet `listOutputs`.
313
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
314
+ * This must match the `AuthFetch` identity securing the remote conneciton.
315
+ * @param args Validated extension of original wallet `listOutputs` arguments.
316
+ * @returns `listOutputs` results.
317
+ */
318
+ async listOutputs(auth: AuthId, vargs: Validation.ValidListOutputsArgs): Promise<ListOutputsResult> {
319
+ const r = await this.rpcCall<ListOutputsResult>('listOutputs', [auth, vargs])
320
+ return r
321
+ }
322
+
323
+ /**
324
+ * Storage level processing for wallet `listCertificates`.
325
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
326
+ * This must match the `AuthFetch` identity securing the remote conneciton.
327
+ * @param args Validated extension of original wallet `listCertificates` arguments.
328
+ * @returns `listCertificates` results.
329
+ */
330
+ async listCertificates(auth: AuthId, vargs: Validation.ValidListCertificatesArgs): Promise<ListCertificatesResult> {
331
+ const r = await this.rpcCall<ListCertificatesResult>('listCertificates', [auth, vargs])
332
+ return r
333
+ }
334
+
335
+ /**
336
+ * Find user certificates, optionally with fields.
337
+ *
338
+ * This certificate retrieval method supports internal wallet operations.
339
+ * Field values are stored and retrieved encrypted.
340
+ *
341
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
342
+ * This must match the `AuthFetch` identity securing the remote conneciton.
343
+ * @param args `FindCertificatesArgs` determines which certificates to retrieve and whether to include fields.
344
+ * @returns array of certificates matching args.
345
+ */
346
+ async findCertificatesAuth(auth: AuthId, args: FindCertificatesArgs): Promise<TableCertificateX[]> {
347
+ const r = await this.rpcCall<TableCertificateX[]>('findCertificatesAuth', [auth, args])
348
+ this.validateEntities(r)
349
+ if (args.includeFields) {
350
+ for (const c of r) {
351
+ if (c.fields) this.validateEntities(c.fields)
352
+ }
353
+ }
354
+ return r
355
+ }
356
+
357
+ /**
358
+ * Find output baskets.
359
+ *
360
+ * This retrieval method supports internal wallet operations.
361
+ *
362
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
363
+ * This must match the `AuthFetch` identity securing the remote conneciton.
364
+ * @param args `FindOutputBasketsArgs` determines which baskets to retrieve.
365
+ * @returns array of output baskets matching args.
366
+ */
367
+ async findOutputBasketsAuth(auth: AuthId, args: FindOutputBasketsArgs): Promise<TableOutputBasket[]> {
368
+ const r = await this.rpcCall<TableOutputBasket[]>('findOutputBaskets', [auth, args])
369
+ this.validateEntities(r)
370
+ return r
371
+ }
372
+
373
+ /**
374
+ * Find outputs.
375
+ *
376
+ * This retrieval method supports internal wallet operations.
377
+ *
378
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
379
+ * This must match the `AuthFetch` identity securing the remote conneciton.
380
+ * @param args `FindOutputsArgs` determines which outputs to retrieve.
381
+ * @returns array of outputs matching args.
382
+ */
383
+ async findOutputsAuth(auth: AuthId, args: FindOutputsArgs): Promise<TableOutput[]> {
384
+ const r = await this.rpcCall<TableOutput[]>('findOutputsAuth', [auth, args])
385
+ this.validateEntities(r)
386
+ return r
387
+ }
388
+
389
+ /**
390
+ * Find requests for transaction proofs.
391
+ *
392
+ * This retrieval method supports internal wallet operations.
393
+ *
394
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
395
+ * This must match the `AuthFetch` identity securing the remote conneciton.
396
+ * @param args `FindProvenTxReqsArgs` determines which proof requests to retrieve.
397
+ * @returns array of proof requests matching args.
398
+ */
399
+ async findProvenTxReqs(args: FindProvenTxReqsArgs): Promise<TableProvenTxReq[]> {
400
+ const r = await this.rpcCall<TableProvenTxReq[]>('findProvenTxReqs', [args])
401
+ this.validateEntities(r)
402
+ return r
403
+ }
404
+
405
+ /**
406
+ * Relinquish a certificate.
407
+ *
408
+ * For storage supporting replication records must be kept of deletions. Therefore certificates are marked as deleted
409
+ * when relinquished, and no longer returned by `listCertificates`, but are still retained by storage.
410
+ *
411
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
412
+ * This must match the `AuthFetch` identity securing the remote conneciton.
413
+ * @param args original wallet `relinquishCertificate` args.
414
+ */
415
+ async relinquishCertificate(auth: AuthId, args: RelinquishCertificateArgs): Promise<number> {
416
+ return this.rpcCall<number>('relinquishCertificate', [auth, args])
417
+ }
418
+
419
+ /**
420
+ * Relinquish an output.
421
+ *
422
+ * Relinquishing an output removes the output from whatever basket was tracking it.
423
+ *
424
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
425
+ * This must match the `AuthFetch` identity securing the remote conneciton.
426
+ * @param args original wallet `relinquishOutput` args.
427
+ */
428
+ async relinquishOutput(auth: AuthId, args: RelinquishOutputArgs): Promise<number> {
429
+ return this.rpcCall<number>('relinquishOutput', [auth, args])
430
+ }
431
+
432
+ /**
433
+ * Process a "chunk" of replication data for the user.
434
+ *
435
+ * The normal data flow is for the active storage to push backups as a sequence of data chunks to backup storage providers.
436
+ *
437
+ * @param args a copy of the replication request args that initiated the sequence of data chunks.
438
+ * @param chunk the current data chunk to process.
439
+ * @returns whether processing is done, counts of inserts and udpates, and related progress tracking properties.
440
+ */
441
+ async processSyncChunk(args: RequestSyncChunkArgs, chunk: SyncChunk): Promise<ProcessSyncChunkResult> {
442
+ const r = await this.rpcCall<ProcessSyncChunkResult>('processSyncChunk', [args, chunk])
443
+ return r
444
+ }
445
+
446
+ /**
447
+ * Request a "chunk" of replication data for a specific user and storage provider.
448
+ *
449
+ * The normal data flow is for the active storage to push backups as a sequence of data chunks to backup storage providers.
450
+ * Also supports recovery where non-active storage can attempt to merge available data prior to becoming active.
451
+ *
452
+ * @param args that identify the non-active storage which will receive replication data and constrains the replication process.
453
+ * @returns the next "chunk" of replication data
454
+ */
455
+ async getSyncChunk(args: RequestSyncChunkArgs): Promise<SyncChunk> {
456
+ const r = await this.rpcCall<SyncChunk>('getSyncChunk', [args])
457
+ if (r.certificateFields) r.certificateFields = this.validateEntities(r.certificateFields)
458
+ if (r.certificates) r.certificates = this.validateEntities(r.certificates)
459
+ if (r.commissions) r.commissions = this.validateEntities(r.commissions)
460
+ if (r.outputBaskets) r.outputBaskets = this.validateEntities(r.outputBaskets)
461
+ if (r.outputTagMaps) r.outputTagMaps = this.validateEntities(r.outputTagMaps)
462
+ if (r.outputTags) r.outputTags = this.validateEntities(r.outputTags)
463
+ if (r.outputs) r.outputs = this.validateEntities(r.outputs)
464
+ if (r.provenTxReqs) r.provenTxReqs = this.validateEntities(r.provenTxReqs)
465
+ if (r.provenTxs) r.provenTxs = this.validateEntities(r.provenTxs)
466
+ if (r.transactions) r.transactions = this.validateEntities(r.transactions)
467
+ if (r.txLabelMaps) r.txLabelMaps = this.validateEntities(r.txLabelMaps)
468
+ if (r.txLabels) r.txLabels = this.validateEntities(r.txLabels)
469
+ if (r.user) r.user = this.validateEntity(r.user)
470
+ return r
471
+ }
472
+
473
+ /**
474
+ * Handles the data received when a new transaction proof is found in response to an outstanding request for proof data:
475
+ *
476
+ * - Creates a new `TableProvenTx` record.
477
+ * - Notifies all user transaction records of the new status.
478
+ * - Updates the proof request record to 'completed' status which enables delayed deletion.
479
+ *
480
+ * @param args proof request and new transaction proof data
481
+ * @returns results of updates
482
+ */
483
+ async updateProvenTxReqWithNewProvenTx(
484
+ args: UpdateProvenTxReqWithNewProvenTxArgs
485
+ ): Promise<UpdateProvenTxReqWithNewProvenTxResult> {
486
+ const r = await this.rpcCall<UpdateProvenTxReqWithNewProvenTxResult>('updateProvenTxReqWithNewProvenTx', [args])
487
+ return r
488
+ }
489
+
490
+ /**
491
+ * Ensures up-to-date wallet data replication to all configured backup storage providers,
492
+ * then promotes one of the configured backups to active,
493
+ * demoting the current active to new backup.
494
+ *
495
+ * @param auth Identifies client by identity key and the storage identity key of their currently active storage.
496
+ * This must match the `AuthFetch` identity securing the remote conneciton.
497
+ * @param newActiveStorageIdentityKey which must be a currently configured backup storage provider.
498
+ */
499
+ async setActive(auth: AuthId, newActiveStorageIdentityKey: string): Promise<number> {
500
+ return this.rpcCall<number>('setActive', [auth, newActiveStorageIdentityKey])
501
+ }
502
+
503
+ validateDate(date: Date | string | number): Date {
504
+ let r: Date
505
+ if (date instanceof Date) r = date
506
+ else r = new Date(date)
507
+ return r
508
+ }
509
+
510
+ /**
511
+ * Helper to force uniform behavior across database engines.
512
+ * Use to process all individual records with time stamps retreived from database.
513
+ */
514
+ validateEntity<T extends EntityTimeStamp>(entity: T, dateFields?: string[]): T {
515
+ entity.created_at = this.validateDate(entity.created_at)
516
+ entity.updated_at = this.validateDate(entity.updated_at)
517
+ if (dateFields) {
518
+ for (const df of dateFields) {
519
+ if (entity[df]) entity[df] = this.validateDate(entity[df])
520
+ }
521
+ }
522
+ for (const key of Object.keys(entity)) {
523
+ const val = entity[key]
524
+ if (val === null) {
525
+ entity[key] = undefined
526
+ } else if (val instanceof Uint8Array) {
527
+ entity[key] = Array.from(val)
528
+ }
529
+ }
530
+ return entity
531
+ }
532
+
533
+ /**
534
+ * Helper to force uniform behavior across database engines.
535
+ * Use to process all arrays of records with time stamps retreived from database.
536
+ * @returns input `entities` array with contained values validated.
537
+ */
538
+ validateEntities<T extends EntityTimeStamp>(entities: T[], dateFields?: string[]): T[] {
539
+ for (let i = 0; i < entities.length; i++) {
540
+ entities[i] = this.validateEntity(entities[i], dateFields)
541
+ }
542
+ return entities
543
+ }
544
+ }