@bsv/wallet-toolbox 1.3.2 → 1.3.4

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 (329) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/docs/client.md +97 -28
  3. package/docs/services.md +2 -1
  4. package/docs/setup.md +404 -14
  5. package/docs/storage.md +720 -42
  6. package/docs/wallet.md +97 -28
  7. package/out/src/CWIStyleWalletManager.d.ts.map +1 -1
  8. package/out/src/CWIStyleWalletManager.js +12 -5
  9. package/out/src/CWIStyleWalletManager.js.map +1 -1
  10. package/out/src/Setup.d.ts +2 -69
  11. package/out/src/Setup.d.ts.map +1 -1
  12. package/out/src/Setup.js.map +1 -1
  13. package/out/src/SetupClient.d.ts +126 -0
  14. package/out/src/SetupClient.d.ts.map +1 -0
  15. package/out/src/SetupClient.js +220 -0
  16. package/out/src/SetupClient.js.map +1 -0
  17. package/out/src/SetupWallet.d.ts +100 -0
  18. package/out/src/SetupWallet.d.ts.map +1 -0
  19. package/out/src/{storage/schema/tables/Certificate.js → SetupWallet.js} +1 -1
  20. package/out/src/SetupWallet.js.map +1 -0
  21. package/out/src/index.all.d.ts +2 -0
  22. package/out/src/index.all.d.ts.map +1 -1
  23. package/out/src/index.all.js +2 -0
  24. package/out/src/index.all.js.map +1 -1
  25. package/out/src/index.client.d.ts +1 -0
  26. package/out/src/index.client.d.ts.map +1 -1
  27. package/out/src/index.client.js +1 -0
  28. package/out/src/index.client.js.map +1 -1
  29. package/out/src/monitor/Monitor.d.ts.map +1 -1
  30. package/out/src/monitor/Monitor.js +4 -0
  31. package/out/src/monitor/Monitor.js.map +1 -1
  32. package/out/src/monitor/MonitorDaemon.d.ts.map +1 -1
  33. package/out/src/monitor/MonitorDaemon.js +0 -3
  34. package/out/src/monitor/MonitorDaemon.js.map +1 -1
  35. package/out/src/monitor/tasks/TaskUnFail.d.ts.map +1 -1
  36. package/out/src/monitor/tasks/TaskUnFail.js +1 -2
  37. package/out/src/monitor/tasks/TaskUnFail.js.map +1 -1
  38. package/out/src/sdk/WalletServices.interfaces.d.ts +9 -0
  39. package/out/src/sdk/WalletServices.interfaces.d.ts.map +1 -1
  40. package/out/src/services/Services.d.ts +2 -1
  41. package/out/src/services/Services.d.ts.map +1 -1
  42. package/out/src/services/Services.js +8 -0
  43. package/out/src/services/Services.js.map +1 -1
  44. package/out/src/services/__tests/postBeef.test.js +1 -1
  45. package/out/src/services/__tests/postBeef.test.js.map +1 -1
  46. package/out/src/storage/StorageIdb.d.ts +203 -0
  47. package/out/src/storage/StorageIdb.d.ts.map +1 -0
  48. package/out/src/storage/StorageIdb.js +2289 -0
  49. package/out/src/storage/StorageIdb.js.map +1 -0
  50. package/out/src/storage/StorageKnex.d.ts +6 -6
  51. package/out/src/storage/StorageKnex.d.ts.map +1 -1
  52. package/out/src/storage/StorageKnex.js +9 -29
  53. package/out/src/storage/StorageKnex.js.map +1 -1
  54. package/out/src/storage/StorageProvider.d.ts +3 -1
  55. package/out/src/storage/StorageProvider.d.ts.map +1 -1
  56. package/out/src/storage/StorageProvider.js +22 -0
  57. package/out/src/storage/StorageProvider.js.map +1 -1
  58. package/out/src/storage/StorageReader.d.ts +1 -1
  59. package/out/src/storage/StorageReader.d.ts.map +1 -1
  60. package/out/src/storage/StorageReader.js +5 -0
  61. package/out/src/storage/StorageReader.js.map +1 -1
  62. package/out/src/storage/__test/StorageIdb.test.d.ts +2 -0
  63. package/out/src/storage/__test/StorageIdb.test.d.ts.map +1 -0
  64. package/out/src/storage/__test/StorageIdb.test.js +16 -0
  65. package/out/src/storage/__test/StorageIdb.test.js.map +1 -0
  66. package/out/src/storage/methods/ListActionsSpecOp.d.ts +16 -0
  67. package/out/src/storage/methods/ListActionsSpecOp.d.ts.map +1 -0
  68. package/out/src/storage/methods/ListActionsSpecOp.js +40 -0
  69. package/out/src/storage/methods/ListActionsSpecOp.js.map +1 -0
  70. package/out/src/storage/methods/ListOutputsSpecOp.d.ts +26 -0
  71. package/out/src/storage/methods/ListOutputsSpecOp.d.ts.map +1 -0
  72. package/out/src/storage/methods/ListOutputsSpecOp.js +71 -0
  73. package/out/src/storage/methods/ListOutputsSpecOp.js.map +1 -0
  74. package/out/src/storage/methods/listActionsIdb.d.ts +5 -0
  75. package/out/src/storage/methods/listActionsIdb.d.ts.map +1 -0
  76. package/out/src/storage/methods/listActionsIdb.js +155 -0
  77. package/out/src/storage/methods/listActionsIdb.js.map +1 -0
  78. package/out/src/storage/methods/{listActions.d.ts → listActionsKnex.d.ts} +1 -1
  79. package/out/src/storage/methods/listActionsKnex.d.ts.map +1 -0
  80. package/out/src/storage/methods/{listActions.js → listActionsKnex.js} +3 -34
  81. package/out/src/storage/methods/listActionsKnex.js.map +1 -0
  82. package/out/src/storage/methods/listOutputsIdb.d.ts +5 -0
  83. package/out/src/storage/methods/listOutputsIdb.d.ts.map +1 -0
  84. package/out/src/storage/methods/listOutputsIdb.js +181 -0
  85. package/out/src/storage/methods/listOutputsIdb.js.map +1 -0
  86. package/out/src/storage/methods/{listOutputs.d.ts → listOutputsKnex.d.ts} +1 -1
  87. package/out/src/storage/methods/listOutputsKnex.d.ts.map +1 -0
  88. package/out/src/storage/methods/{listOutputs.js → listOutputsKnex.js} +7 -76
  89. package/out/src/storage/methods/listOutputsKnex.js.map +1 -0
  90. package/out/src/storage/methods/purgeDataIdb.d.ts +4 -0
  91. package/out/src/storage/methods/purgeDataIdb.d.ts.map +1 -0
  92. package/out/src/storage/methods/purgeDataIdb.js +9 -0
  93. package/out/src/storage/methods/purgeDataIdb.js.map +1 -0
  94. package/out/src/storage/methods/reviewStatus.d.ts +11 -0
  95. package/out/src/storage/methods/reviewStatus.d.ts.map +1 -1
  96. package/out/src/storage/methods/reviewStatus.js +11 -0
  97. package/out/src/storage/methods/reviewStatus.js.map +1 -1
  98. package/out/src/storage/methods/reviewStatusIdb.d.ts +20 -0
  99. package/out/src/storage/methods/reviewStatusIdb.d.ts.map +1 -0
  100. package/out/src/storage/methods/reviewStatusIdb.js +35 -0
  101. package/out/src/storage/methods/reviewStatusIdb.js.map +1 -0
  102. package/out/src/storage/schema/StorageIdbSchema.d.ts +133 -0
  103. package/out/src/storage/schema/StorageIdbSchema.d.ts.map +1 -0
  104. package/out/src/storage/schema/{tables/CertificateField.js → StorageIdbSchema.js} +1 -1
  105. package/out/src/storage/schema/StorageIdbSchema.js.map +1 -0
  106. package/out/src/storage/schema/entities/__tests/ProvenTxTests.test.js +1 -0
  107. package/out/src/storage/schema/entities/__tests/ProvenTxTests.test.js.map +1 -1
  108. package/out/src/storage/schema/tables/{Certificate.d.ts → TableCertificate.d.ts} +1 -1
  109. package/out/src/storage/schema/tables/TableCertificate.d.ts.map +1 -0
  110. package/out/src/storage/schema/tables/TableCertificate.js +3 -0
  111. package/out/src/storage/schema/tables/TableCertificate.js.map +1 -0
  112. package/out/src/storage/schema/tables/{CertificateField.d.ts → TableCertificateField.d.ts} +1 -1
  113. package/out/src/storage/schema/tables/TableCertificateField.d.ts.map +1 -0
  114. package/out/src/storage/schema/tables/TableCertificateField.js +3 -0
  115. package/out/src/storage/schema/tables/TableCertificateField.js.map +1 -0
  116. package/out/src/storage/schema/tables/{Commission.d.ts → TableCommission.d.ts} +1 -1
  117. package/out/src/storage/schema/tables/TableCommission.d.ts.map +1 -0
  118. package/out/src/storage/schema/tables/TableCommission.js +3 -0
  119. package/out/src/storage/schema/tables/TableCommission.js.map +1 -0
  120. package/out/src/storage/schema/tables/{MonitorEvent.d.ts → TableMonitorEvent.d.ts} +1 -1
  121. package/out/src/storage/schema/tables/TableMonitorEvent.d.ts.map +1 -0
  122. package/out/src/storage/schema/tables/TableMonitorEvent.js +3 -0
  123. package/out/src/storage/schema/tables/TableMonitorEvent.js.map +1 -0
  124. package/out/src/storage/schema/tables/{Output.d.ts → TableOutput.d.ts} +1 -1
  125. package/out/src/storage/schema/tables/TableOutput.d.ts.map +1 -0
  126. package/out/src/storage/schema/tables/{Output.js → TableOutput.js} +1 -1
  127. package/out/src/storage/schema/tables/TableOutput.js.map +1 -0
  128. package/out/src/storage/schema/tables/{OutputBasket.d.ts → TableOutputBasket.d.ts} +1 -1
  129. package/out/src/storage/schema/tables/TableOutputBasket.d.ts.map +1 -0
  130. package/out/src/storage/schema/tables/TableOutputBasket.js +3 -0
  131. package/out/src/storage/schema/tables/TableOutputBasket.js.map +1 -0
  132. package/out/src/storage/schema/tables/{OutputTag.d.ts → TableOutputTag.d.ts} +1 -1
  133. package/out/src/storage/schema/tables/TableOutputTag.d.ts.map +1 -0
  134. package/out/src/storage/schema/tables/{MonitorEvent.js → TableOutputTag.js} +1 -1
  135. package/out/src/storage/schema/tables/TableOutputTag.js.map +1 -0
  136. package/out/src/storage/schema/tables/{OutputTagMap.d.ts → TableOutputTagMap.d.ts} +1 -1
  137. package/out/src/storage/schema/tables/TableOutputTagMap.d.ts.map +1 -0
  138. package/out/src/storage/schema/tables/TableOutputTagMap.js +3 -0
  139. package/out/src/storage/schema/tables/TableOutputTagMap.js.map +1 -0
  140. package/out/src/storage/schema/tables/{ProvenTx.d.ts → TableProvenTx.d.ts} +1 -1
  141. package/out/src/storage/schema/tables/TableProvenTx.d.ts.map +1 -0
  142. package/out/src/storage/schema/tables/{Commission.js → TableProvenTx.js} +1 -1
  143. package/out/src/storage/schema/tables/TableProvenTx.js.map +1 -0
  144. package/out/src/storage/schema/tables/{ProvenTxReq.d.ts → TableProvenTxReq.d.ts} +1 -1
  145. package/out/src/storage/schema/tables/TableProvenTxReq.d.ts.map +1 -0
  146. package/out/src/storage/schema/tables/TableProvenTxReq.js +3 -0
  147. package/out/src/storage/schema/tables/TableProvenTxReq.js.map +1 -0
  148. package/out/src/storage/schema/tables/TableSettings.d.ts +1 -1
  149. package/out/src/storage/schema/tables/TableSettings.d.ts.map +1 -1
  150. package/out/src/storage/schema/tables/{SyncState.d.ts → TableSyncState.d.ts} +1 -1
  151. package/out/src/storage/schema/tables/TableSyncState.d.ts.map +1 -0
  152. package/out/src/storage/schema/tables/TableSyncState.js +3 -0
  153. package/out/src/storage/schema/tables/TableSyncState.js.map +1 -0
  154. package/out/src/storage/schema/tables/{Transaction.d.ts → TableTransaction.d.ts} +1 -1
  155. package/out/src/storage/schema/tables/TableTransaction.d.ts.map +1 -0
  156. package/out/src/storage/schema/tables/{Transaction.js → TableTransaction.js} +1 -1
  157. package/out/src/storage/schema/tables/TableTransaction.js.map +1 -0
  158. package/out/src/storage/schema/tables/{TxLabel.d.ts → TableTxLabel.d.ts} +1 -1
  159. package/out/src/storage/schema/tables/TableTxLabel.d.ts.map +1 -0
  160. package/out/src/storage/schema/tables/{OutputBasket.js → TableTxLabel.js} +1 -1
  161. package/out/src/storage/schema/tables/TableTxLabel.js.map +1 -0
  162. package/out/src/storage/schema/tables/{TxLabelMap.d.ts → TableTxLabelMap.d.ts} +1 -1
  163. package/out/src/storage/schema/tables/TableTxLabelMap.d.ts.map +1 -0
  164. package/out/src/storage/schema/tables/TableTxLabelMap.js +3 -0
  165. package/out/src/storage/schema/tables/TableTxLabelMap.js.map +1 -0
  166. package/out/src/storage/schema/tables/{User.d.ts → TableUser.d.ts} +1 -1
  167. package/out/src/storage/schema/tables/TableUser.d.ts.map +1 -0
  168. package/out/src/storage/schema/tables/{SyncState.js → TableUser.js} +1 -1
  169. package/out/src/storage/schema/tables/TableUser.js.map +1 -0
  170. package/out/src/storage/schema/tables/index.d.ts +15 -15
  171. package/out/src/storage/schema/tables/index.d.ts.map +1 -1
  172. package/out/src/storage/schema/tables/index.js +15 -15
  173. package/out/src/storage/schema/tables/index.js.map +1 -1
  174. package/out/test/Wallet/local/localWallet2.man.test.js +4 -0
  175. package/out/test/Wallet/local/localWallet2.man.test.js.map +1 -1
  176. package/out/test/Wallet/support/operations.man.test.d.ts +2 -0
  177. package/out/test/Wallet/support/operations.man.test.d.ts.map +1 -0
  178. package/out/test/Wallet/support/{opers1.man.test.js → operations.man.test.js} +41 -6
  179. package/out/test/Wallet/support/operations.man.test.js.map +1 -0
  180. package/out/test/storage/find.test.js +1 -1
  181. package/out/test/storage/find.test.js.map +1 -1
  182. package/out/test/storage/idb/allocateChange.test.d.ts +2 -0
  183. package/out/test/storage/idb/allocateChange.test.d.ts.map +1 -0
  184. package/out/test/storage/idb/allocateChange.test.js +110 -0
  185. package/out/test/storage/idb/allocateChange.test.js.map +1 -0
  186. package/out/test/storage/idb/count.test.d.ts +2 -0
  187. package/out/test/storage/idb/count.test.d.ts.map +1 -0
  188. package/out/test/storage/idb/count.test.js +129 -0
  189. package/out/test/storage/idb/count.test.js.map +1 -0
  190. package/out/test/storage/idb/find.test.d.ts +2 -0
  191. package/out/test/storage/idb/find.test.d.ts.map +1 -0
  192. package/out/test/storage/idb/find.test.js +131 -0
  193. package/out/test/storage/idb/find.test.js.map +1 -0
  194. package/out/test/storage/idb/idbSpeed.test.d.ts +2 -0
  195. package/out/test/storage/idb/idbSpeed.test.d.ts.map +1 -0
  196. package/out/test/storage/idb/idbSpeed.test.js +29 -0
  197. package/out/test/storage/idb/idbSpeed.test.js.map +1 -0
  198. package/out/test/storage/idb/insert.test.d.ts +2 -0
  199. package/out/test/storage/idb/insert.test.d.ts.map +1 -0
  200. package/out/test/storage/idb/insert.test.js +242 -0
  201. package/out/test/storage/idb/insert.test.js.map +1 -0
  202. package/out/test/storage/idb/transactionAbort.test.d.ts +2 -0
  203. package/out/test/storage/idb/transactionAbort.test.d.ts.map +1 -0
  204. package/out/test/storage/idb/transactionAbort.test.js +97 -0
  205. package/out/test/storage/idb/transactionAbort.test.js.map +1 -0
  206. package/out/test/storage/idb/update.test.d.ts +2 -0
  207. package/out/test/storage/idb/update.test.d.ts.map +1 -0
  208. package/out/test/storage/idb/update.test.js +902 -0
  209. package/out/test/storage/idb/update.test.js.map +1 -0
  210. package/out/test/storage/update.test.js +2 -2
  211. package/out/test/storage/update.test.js.map +1 -1
  212. package/out/test/utils/TestUtilsWalletStorage.d.ts +24 -1
  213. package/out/test/utils/TestUtilsWalletStorage.d.ts.map +1 -1
  214. package/out/test/utils/TestUtilsWalletStorage.js +147 -3
  215. package/out/test/utils/TestUtilsWalletStorage.js.map +1 -1
  216. package/out/test/wallet/list/listActions.test.d.ts +1 -1
  217. package/out/test/wallet/list/listActions.test.d.ts.map +1 -1
  218. package/out/test/wallet/list/listActions.test.js +9 -3
  219. package/out/test/wallet/list/listActions.test.js.map +1 -1
  220. package/out/test/wallet/list/listOutputs.test.d.ts +1 -1
  221. package/out/test/wallet/list/listOutputs.test.d.ts.map +1 -1
  222. package/out/test/wallet/list/listOutputs.test.js +34 -219
  223. package/out/test/wallet/list/listOutputs.test.js.map +1 -1
  224. package/out/tsconfig.all.tsbuildinfo +1 -1
  225. package/package.json +3 -1
  226. package/src/CWIStyleWalletManager.ts +12 -5
  227. package/src/Setup.ts +1 -71
  228. package/src/SetupClient.ts +312 -0
  229. package/src/SetupWallet.ts +105 -0
  230. package/src/index.all.ts +2 -0
  231. package/src/index.client.ts +1 -0
  232. package/src/monitor/Monitor.ts +4 -0
  233. package/src/monitor/MonitorDaemon.ts +0 -1
  234. package/src/monitor/tasks/TaskUnFail.ts +3 -4
  235. package/src/sdk/WalletServices.interfaces.ts +10 -0
  236. package/src/services/Services.ts +13 -1
  237. package/src/services/__tests/postBeef.test.ts +1 -1
  238. package/src/storage/StorageIdb.ts +2298 -0
  239. package/src/storage/StorageKnex.ts +7 -32
  240. package/src/storage/StorageProvider.ts +28 -0
  241. package/src/storage/StorageReader.ts +5 -1
  242. package/src/storage/__test/StorageIdb.test.ts +15 -0
  243. package/src/storage/methods/ListActionsSpecOp.ts +68 -0
  244. package/src/storage/methods/ListOutputsSpecOp.ts +125 -0
  245. package/src/storage/methods/listActionsIdb.ts +181 -0
  246. package/src/storage/methods/{listActions.ts → listActionsKnex.ts} +2 -64
  247. package/src/storage/methods/listOutputsIdb.ts +199 -0
  248. package/src/storage/methods/{listOutputs.ts → listOutputsKnex.ts} +9 -133
  249. package/src/storage/methods/purgeDataIdb.ts +15 -0
  250. package/src/storage/methods/reviewStatus.ts +11 -0
  251. package/src/storage/methods/reviewStatusIdb.ts +43 -0
  252. package/src/storage/schema/StorageIdbSchema.ts +150 -0
  253. package/src/storage/schema/entities/__tests/ProvenTxTests.test.ts +2 -0
  254. package/src/storage/schema/tables/TableSettings.ts +1 -1
  255. package/src/storage/schema/tables/index.ts +15 -15
  256. package/test/Wallet/local/localWallet2.man.test.ts +5 -0
  257. package/test/Wallet/support/{opers1.man.test.ts → operations.man.test.ts} +43 -14
  258. package/test/storage/find.test.ts +1 -1
  259. package/test/storage/idb/allocateChange.test.ts +251 -0
  260. package/test/storage/idb/count.test.ts +158 -0
  261. package/test/storage/idb/find.test.ts +177 -0
  262. package/test/storage/idb/idbSpeed.test.ts +34 -0
  263. package/test/storage/idb/insert.test.ts +268 -0
  264. package/test/storage/idb/transactionAbort.test.ts +108 -0
  265. package/test/storage/idb/update.test.ts +1000 -0
  266. package/test/storage/update.test.ts +2 -2
  267. package/test/utils/TestUtilsWalletStorage.ts +188 -4
  268. package/test/wallet/list/listActions.test.ts +15 -5
  269. package/test/wallet/list/listOutputs.test.ts +29 -214
  270. package/out/src/storage/methods/listActions.d.ts.map +0 -1
  271. package/out/src/storage/methods/listActions.js.map +0 -1
  272. package/out/src/storage/methods/listOutputs.d.ts.map +0 -1
  273. package/out/src/storage/methods/listOutputs.js.map +0 -1
  274. package/out/src/storage/schema/tables/Certificate.d.ts.map +0 -1
  275. package/out/src/storage/schema/tables/Certificate.js.map +0 -1
  276. package/out/src/storage/schema/tables/CertificateField.d.ts.map +0 -1
  277. package/out/src/storage/schema/tables/CertificateField.js.map +0 -1
  278. package/out/src/storage/schema/tables/Commission.d.ts.map +0 -1
  279. package/out/src/storage/schema/tables/Commission.js.map +0 -1
  280. package/out/src/storage/schema/tables/MonitorEvent.d.ts.map +0 -1
  281. package/out/src/storage/schema/tables/MonitorEvent.js.map +0 -1
  282. package/out/src/storage/schema/tables/Output.d.ts.map +0 -1
  283. package/out/src/storage/schema/tables/Output.js.map +0 -1
  284. package/out/src/storage/schema/tables/OutputBasket.d.ts.map +0 -1
  285. package/out/src/storage/schema/tables/OutputBasket.js.map +0 -1
  286. package/out/src/storage/schema/tables/OutputTag.d.ts.map +0 -1
  287. package/out/src/storage/schema/tables/OutputTag.js +0 -3
  288. package/out/src/storage/schema/tables/OutputTag.js.map +0 -1
  289. package/out/src/storage/schema/tables/OutputTagMap.d.ts.map +0 -1
  290. package/out/src/storage/schema/tables/OutputTagMap.js +0 -3
  291. package/out/src/storage/schema/tables/OutputTagMap.js.map +0 -1
  292. package/out/src/storage/schema/tables/ProvenTx.d.ts.map +0 -1
  293. package/out/src/storage/schema/tables/ProvenTx.js +0 -3
  294. package/out/src/storage/schema/tables/ProvenTx.js.map +0 -1
  295. package/out/src/storage/schema/tables/ProvenTxReq.d.ts.map +0 -1
  296. package/out/src/storage/schema/tables/ProvenTxReq.js +0 -3
  297. package/out/src/storage/schema/tables/ProvenTxReq.js.map +0 -1
  298. package/out/src/storage/schema/tables/SyncState.d.ts.map +0 -1
  299. package/out/src/storage/schema/tables/SyncState.js.map +0 -1
  300. package/out/src/storage/schema/tables/Transaction.d.ts.map +0 -1
  301. package/out/src/storage/schema/tables/Transaction.js.map +0 -1
  302. package/out/src/storage/schema/tables/TxLabel.d.ts.map +0 -1
  303. package/out/src/storage/schema/tables/TxLabel.js +0 -3
  304. package/out/src/storage/schema/tables/TxLabel.js.map +0 -1
  305. package/out/src/storage/schema/tables/TxLabelMap.d.ts.map +0 -1
  306. package/out/src/storage/schema/tables/TxLabelMap.js +0 -3
  307. package/out/src/storage/schema/tables/TxLabelMap.js.map +0 -1
  308. package/out/src/storage/schema/tables/User.d.ts.map +0 -1
  309. package/out/src/storage/schema/tables/User.js +0 -3
  310. package/out/src/storage/schema/tables/User.js.map +0 -1
  311. package/out/test/Wallet/support/opers1.man.test.d.ts +0 -2
  312. package/out/test/Wallet/support/opers1.man.test.d.ts.map +0 -1
  313. package/out/test/Wallet/support/opers1.man.test.js.map +0 -1
  314. package/unlock-migrations.sh +0 -41
  315. /package/src/storage/schema/tables/{Certificate.ts → TableCertificate.ts} +0 -0
  316. /package/src/storage/schema/tables/{CertificateField.ts → TableCertificateField.ts} +0 -0
  317. /package/src/storage/schema/tables/{Commission.ts → TableCommission.ts} +0 -0
  318. /package/src/storage/schema/tables/{MonitorEvent.ts → TableMonitorEvent.ts} +0 -0
  319. /package/src/storage/schema/tables/{Output.ts → TableOutput.ts} +0 -0
  320. /package/src/storage/schema/tables/{OutputBasket.ts → TableOutputBasket.ts} +0 -0
  321. /package/src/storage/schema/tables/{OutputTag.ts → TableOutputTag.ts} +0 -0
  322. /package/src/storage/schema/tables/{OutputTagMap.ts → TableOutputTagMap.ts} +0 -0
  323. /package/src/storage/schema/tables/{ProvenTx.ts → TableProvenTx.ts} +0 -0
  324. /package/src/storage/schema/tables/{ProvenTxReq.ts → TableProvenTxReq.ts} +0 -0
  325. /package/src/storage/schema/tables/{SyncState.ts → TableSyncState.ts} +0 -0
  326. /package/src/storage/schema/tables/{Transaction.ts → TableTransaction.ts} +0 -0
  327. /package/src/storage/schema/tables/{TxLabel.ts → TableTxLabel.ts} +0 -0
  328. /package/src/storage/schema/tables/{TxLabelMap.ts → TableTxLabelMap.ts} +0 -0
  329. /package/src/storage/schema/tables/{User.ts → TableUser.ts} +0 -0
@@ -0,0 +1,2289 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StorageIdb = void 0;
4
+ const idb_1 = require("idb");
5
+ const index_client_1 = require("../index.client");
6
+ const StorageProvider_1 = require("./StorageProvider");
7
+ const listActionsIdb_1 = require("./methods/listActionsIdb");
8
+ const listOutputsIdb_1 = require("./methods/listOutputsIdb");
9
+ const reviewStatusIdb_1 = require("./methods/reviewStatusIdb");
10
+ const purgeDataIdb_1 = require("./methods/purgeDataIdb");
11
+ class StorageIdb extends StorageProvider_1.StorageProvider {
12
+ constructor(options) {
13
+ super(options);
14
+ this.allStores = [
15
+ 'certificates',
16
+ 'certificate_fields',
17
+ 'commissions',
18
+ 'monitor_events',
19
+ 'outputs',
20
+ 'output_baskets',
21
+ 'output_tags',
22
+ 'output_tags_map',
23
+ 'proven_txs',
24
+ 'proven_tx_reqs',
25
+ 'sync_states',
26
+ 'transactions',
27
+ 'tx_labels',
28
+ 'tx_labels_map',
29
+ 'users'
30
+ ];
31
+ this.dbName = `wallet-toolbox-${this.chain}net`;
32
+ }
33
+ /**
34
+ * This method must be called at least once before any other method accesses the database,
35
+ * and each time the schema may have updated.
36
+ *
37
+ * If the database has already been created in this context, `storageName` and `storageIdentityKey`
38
+ * are ignored.
39
+ *
40
+ * @param storageName
41
+ * @param storageIdentityKey
42
+ * @returns
43
+ */
44
+ async migrate(storageName, storageIdentityKey) {
45
+ const db = await this.verifyDB(storageName, storageIdentityKey);
46
+ return db.version.toString();
47
+ }
48
+ /**
49
+ * Following initial database initialization, this method verfies that db is ready for use.
50
+ *
51
+ * @throws `WERR_INVALID_OPERATION` if the database has not been initialized by a call to `migrate`.
52
+ *
53
+ * @param storageName
54
+ * @param storageIdentityKey
55
+ *
56
+ * @returns
57
+ */
58
+ async verifyDB(storageName, storageIdentityKey) {
59
+ if (this.db)
60
+ return this.db;
61
+ this.db = await this.initDB(storageName, storageIdentityKey);
62
+ this._settings = (await this.db.getAll('settings'))[0];
63
+ this.whenLastAccess = new Date();
64
+ return this.db;
65
+ }
66
+ /**
67
+ * Convert the standard optional `TrxToken` parameter into either a direct knex database instance,
68
+ * or a Knex.Transaction as appropriate.
69
+ */
70
+ toDbTrx(stores, mode, trx) {
71
+ if (trx) {
72
+ const t = trx;
73
+ return t;
74
+ }
75
+ else {
76
+ if (!this.db)
77
+ throw new Error('not initialized');
78
+ const db = this.db;
79
+ const trx = db.transaction(stores || this.allStores, mode || 'readwrite');
80
+ this.whenLastAccess = new Date();
81
+ return trx;
82
+ }
83
+ }
84
+ /**
85
+ * Called by `makeAvailable` to return storage `TableSettings`.
86
+ * Since this is the first async method that must be called by all clients,
87
+ * it is where async initialization occurs.
88
+ *
89
+ * After initialization, cached settings are returned.
90
+ *
91
+ * @param trx
92
+ */
93
+ async readSettings(trx) {
94
+ await this.verifyDB();
95
+ return this._settings;
96
+ }
97
+ async initDB(storageName, storageIdentityKey) {
98
+ const chain = this.chain;
99
+ const maxOutputScript = 1024;
100
+ const db = await (0, idb_1.openDB)(this.dbName, 1, {
101
+ upgrade(db, oldVersion, newVersion, transaction) {
102
+ if (!db.objectStoreNames.contains('proven_txs')) {
103
+ // proven_txs object store
104
+ const provenTxsStore = db.createObjectStore('proven_txs', {
105
+ keyPath: 'provenTxId',
106
+ autoIncrement: true
107
+ });
108
+ provenTxsStore.createIndex('txid', 'txid', { unique: true });
109
+ }
110
+ if (!db.objectStoreNames.contains('proven_tx_reqs')) {
111
+ // proven_tx_reqs object store
112
+ const provenTxReqsStore = db.createObjectStore('proven_tx_reqs', {
113
+ keyPath: 'provenTxReqId',
114
+ autoIncrement: true
115
+ });
116
+ provenTxReqsStore.createIndex('provenTxId', 'provenTxId');
117
+ provenTxReqsStore.createIndex('txid', 'txid', { unique: true });
118
+ provenTxReqsStore.createIndex('status', 'status');
119
+ provenTxReqsStore.createIndex('batch', 'batch');
120
+ }
121
+ if (!db.objectStoreNames.contains('users')) {
122
+ const users = db.createObjectStore('users', {
123
+ keyPath: 'userId',
124
+ autoIncrement: true
125
+ });
126
+ users.createIndex('identityKey', 'identityKey', { unique: true });
127
+ }
128
+ if (!db.objectStoreNames.contains('certificates')) {
129
+ // certificates object store
130
+ const certificatesStore = db.createObjectStore('certificates', {
131
+ keyPath: 'certificateId',
132
+ autoIncrement: true
133
+ });
134
+ certificatesStore.createIndex('userId', 'userId');
135
+ certificatesStore.createIndex('userId_type_certifier_serialNumber', ['userId', 'type', 'certifier', 'serialNumber'], { unique: true });
136
+ }
137
+ if (!db.objectStoreNames.contains('certificate_fields')) {
138
+ // certificate_fields object store
139
+ const certificateFieldsStore = db.createObjectStore('certificate_fields', {
140
+ keyPath: ['certificateId', 'fieldName'] // Composite key
141
+ });
142
+ certificateFieldsStore.createIndex('userId', 'userId');
143
+ certificateFieldsStore.createIndex('certificateId', 'certificateId');
144
+ }
145
+ if (!db.objectStoreNames.contains('output_baskets')) {
146
+ // output_baskets object store
147
+ const outputBasketsStore = db.createObjectStore('output_baskets', {
148
+ keyPath: 'basketId',
149
+ autoIncrement: true
150
+ });
151
+ outputBasketsStore.createIndex('userId', 'userId');
152
+ outputBasketsStore.createIndex('name_userId', ['name', 'userId'], { unique: true });
153
+ }
154
+ if (!db.objectStoreNames.contains('transactions')) {
155
+ // transactions object store
156
+ const transactionsStore = db.createObjectStore('transactions', {
157
+ keyPath: 'transactionId',
158
+ autoIncrement: true
159
+ });
160
+ transactionsStore.createIndex('userId', 'userId');
161
+ transactionsStore.createIndex('status', 'status'),
162
+ transactionsStore.createIndex('status_userId', ['status', 'userId']);
163
+ transactionsStore.createIndex('provenTxId', 'provenTxId');
164
+ transactionsStore.createIndex('reference', 'reference', { unique: true });
165
+ }
166
+ if (!db.objectStoreNames.contains('commissions')) {
167
+ // commissions object store
168
+ const commissionsStore = db.createObjectStore('commissions', {
169
+ keyPath: 'commissionId',
170
+ autoIncrement: true
171
+ });
172
+ commissionsStore.createIndex('userId', 'userId');
173
+ commissionsStore.createIndex('transactionId', 'transactionId', { unique: true });
174
+ }
175
+ if (!db.objectStoreNames.contains('outputs')) {
176
+ // outputs object store
177
+ const outputsStore = db.createObjectStore('outputs', {
178
+ keyPath: 'outputId',
179
+ autoIncrement: true
180
+ });
181
+ outputsStore.createIndex('userId', 'userId');
182
+ outputsStore.createIndex('transactionId', 'transactionId');
183
+ outputsStore.createIndex('basketId', 'basketId');
184
+ outputsStore.createIndex('spentBy', 'spentBy');
185
+ outputsStore.createIndex('transactionId_vout_userId', ['transactionId', 'vout', 'userId'], { unique: true });
186
+ }
187
+ if (!db.objectStoreNames.contains('output_tags')) {
188
+ // output_tags object store
189
+ const outputTagsStore = db.createObjectStore('output_tags', {
190
+ keyPath: 'outputTagId',
191
+ autoIncrement: true
192
+ });
193
+ outputTagsStore.createIndex('userId', 'userId');
194
+ outputTagsStore.createIndex('tag_userId', ['tag', 'userId'], { unique: true });
195
+ }
196
+ if (!db.objectStoreNames.contains('output_tags_map')) {
197
+ // output_tags_map object store
198
+ const outputTagsMapStore = db.createObjectStore('output_tags_map', {
199
+ keyPath: ['outputTagId', 'outputId']
200
+ });
201
+ outputTagsMapStore.createIndex('outputTagId', 'outputTagId');
202
+ outputTagsMapStore.createIndex('outputId', 'outputId');
203
+ }
204
+ if (!db.objectStoreNames.contains('tx_labels')) {
205
+ // tx_labels object store
206
+ const txLabelsStore = db.createObjectStore('tx_labels', {
207
+ keyPath: 'txLabelId',
208
+ autoIncrement: true
209
+ });
210
+ txLabelsStore.createIndex('userId', 'userId');
211
+ txLabelsStore.createIndex('label_userId', ['label', 'userId'], { unique: true });
212
+ }
213
+ if (!db.objectStoreNames.contains('tx_labels_map')) {
214
+ // tx_labels_map object store
215
+ const txLabelsMapStore = db.createObjectStore('tx_labels_map', {
216
+ keyPath: ['txLabelId', 'transactionId']
217
+ });
218
+ txLabelsMapStore.createIndex('txLabelId', 'txLabelId');
219
+ txLabelsMapStore.createIndex('transactionId', 'transactionId');
220
+ }
221
+ if (!db.objectStoreNames.contains('monitor_events')) {
222
+ // monitor_events object store
223
+ const monitorEventsStore = db.createObjectStore('monitor_events', {
224
+ keyPath: 'id',
225
+ autoIncrement: true
226
+ });
227
+ }
228
+ if (!db.objectStoreNames.contains('sync_states')) {
229
+ // sync_states object store
230
+ const syncStatesStore = db.createObjectStore('sync_states', {
231
+ keyPath: 'syncStateId',
232
+ autoIncrement: true
233
+ });
234
+ syncStatesStore.createIndex('userId', 'userId');
235
+ syncStatesStore.createIndex('refNum', 'refNum', { unique: true });
236
+ syncStatesStore.createIndex('status', 'status');
237
+ }
238
+ if (!db.objectStoreNames.contains('settings')) {
239
+ if (!storageName || !storageIdentityKey) {
240
+ throw new index_client_1.sdk.WERR_INVALID_OPERATION('migrate must be called before first access');
241
+ }
242
+ const settings = db.createObjectStore('settings', {
243
+ keyPath: 'storageIdentityKey'
244
+ });
245
+ const s = {
246
+ created_at: new Date(),
247
+ updated_at: new Date(),
248
+ storageIdentityKey,
249
+ storageName,
250
+ chain,
251
+ dbtype: 'IndexedDB',
252
+ maxOutputScript
253
+ };
254
+ settings.put(s);
255
+ }
256
+ }
257
+ });
258
+ return db;
259
+ }
260
+ //
261
+ // StorageProvider abstract methods
262
+ //
263
+ async reviewStatus(args) {
264
+ return await (0, reviewStatusIdb_1.reviewStatusIdb)(this, args);
265
+ }
266
+ async purgeData(params, trx) {
267
+ return await (0, purgeDataIdb_1.purgeDataIdb)(this, params, trx);
268
+ }
269
+ /**
270
+ * Proceeds in three stages:
271
+ * 1. Find an output that exactly funds the transaction (if exactSatoshis is not undefined).
272
+ * 2. Find an output that overfunds by the least amount (targetSatoshis).
273
+ * 3. Find an output that comes as close to funding as possible (targetSatoshis).
274
+ * 4. Return undefined if no output is found.
275
+ *
276
+ * Outputs must belong to userId and basketId and have spendable true.
277
+ * Their corresponding transaction must have status of 'completed', 'unproven', or 'sending' (if excludeSending is false).
278
+ *
279
+ * @param userId
280
+ * @param basketId
281
+ * @param targetSatoshis
282
+ * @param exactSatoshis
283
+ * @param excludeSending
284
+ * @param transactionId
285
+ * @returns next funding output to add to transaction or undefined if there are none.
286
+ */
287
+ async allocateChangeInput(userId, basketId, targetSatoshis, exactSatoshis, excludeSending, transactionId) {
288
+ const dbTrx = this.toDbTrx(['outputs', 'transactions'], 'readwrite');
289
+ try {
290
+ const txStatus = ['completed', 'unproven'];
291
+ if (!excludeSending)
292
+ txStatus.push('sending');
293
+ const args = {
294
+ partial: { userId, basketId, spendable: true },
295
+ txStatus,
296
+ trx: dbTrx
297
+ };
298
+ const outputs = await this.findOutputs(args);
299
+ let output;
300
+ let scores = [];
301
+ for (const o of outputs) {
302
+ if (exactSatoshis && o.satoshis === exactSatoshis) {
303
+ output = o;
304
+ break;
305
+ }
306
+ const score = o.satoshis - targetSatoshis;
307
+ scores.push({ output: o, score });
308
+ }
309
+ if (!output) {
310
+ // sort scores increasing by score property
311
+ scores = scores.sort((a, b) => a.score - b.score);
312
+ // find the first score that is greater than or equal to 0
313
+ const o = scores.find(s => s.score >= 0);
314
+ if (o) {
315
+ // stage 2 satisfied (minimally funded)
316
+ output = o.output;
317
+ }
318
+ else if (scores.length > 0) {
319
+ // stage 3 satisfied (minimally under-funded)
320
+ output = scores.slice(-1)[0].output;
321
+ }
322
+ else {
323
+ // no available funding outputs
324
+ output = undefined;
325
+ }
326
+ }
327
+ if (output) {
328
+ // mark output as spent by transactionId
329
+ await this.updateOutput(output.outputId, { spendable: false, spentBy: transactionId }, dbTrx);
330
+ }
331
+ return output;
332
+ }
333
+ finally {
334
+ await dbTrx.done;
335
+ }
336
+ }
337
+ async getProvenOrRawTx(txid, trx) {
338
+ const r = {
339
+ proven: undefined,
340
+ rawTx: undefined,
341
+ inputBEEF: undefined
342
+ };
343
+ r.proven = (0, index_client_1.verifyOneOrNone)(await this.findProvenTxs({ partial: { txid: txid }, trx }));
344
+ if (!r.proven) {
345
+ const req = (0, index_client_1.verifyOneOrNone)(await this.findProvenTxReqs({ partial: { txid: txid }, trx }));
346
+ if (req && ['unsent', 'unmined', 'unconfirmed', 'sending', 'nosend', 'completed'].includes(req.status)) {
347
+ r.rawTx = req.rawTx;
348
+ r.inputBEEF = req.inputBEEF;
349
+ }
350
+ }
351
+ return r;
352
+ }
353
+ async getRawTxOfKnownValidTransaction(txid, offset, length, trx) {
354
+ if (!txid)
355
+ return undefined;
356
+ if (!this.isAvailable())
357
+ await this.makeAvailable();
358
+ let rawTx = undefined;
359
+ const r = await this.getProvenOrRawTx(txid, trx);
360
+ if (r.proven)
361
+ rawTx = r.proven.rawTx;
362
+ else
363
+ rawTx = r.rawTx;
364
+ if (rawTx && offset !== undefined && length !== undefined && Number.isInteger(offset) && Number.isInteger(length)) {
365
+ rawTx = rawTx.slice(offset, offset + length);
366
+ }
367
+ return rawTx;
368
+ }
369
+ async getLabelsForTransactionId(transactionId, trx) {
370
+ const maps = await this.findTxLabelMaps({ partial: { transactionId, isDeleted: false }, trx });
371
+ const labelIds = maps.map(m => m.txLabelId);
372
+ const labels = [];
373
+ for (const txLabelId of labelIds) {
374
+ const label = (0, index_client_1.verifyOne)(await this.findTxLabels({ partial: { txLabelId, isDeleted: false }, trx }));
375
+ labels.push(label);
376
+ }
377
+ return labels;
378
+ }
379
+ async getTagsForOutputId(outputId, trx) {
380
+ const maps = await this.findOutputTagMaps({ partial: { outputId, isDeleted: false }, trx });
381
+ const tagIds = maps.map(m => m.outputTagId);
382
+ const tags = [];
383
+ for (const outputTagId of tagIds) {
384
+ const tag = (0, index_client_1.verifyOne)(await this.findOutputTags({ partial: { outputTagId, isDeleted: false }, trx }));
385
+ tags.push(tag);
386
+ }
387
+ return tags;
388
+ }
389
+ async listActions(auth, vargs) {
390
+ if (!auth.userId)
391
+ throw new index_client_1.sdk.WERR_UNAUTHORIZED();
392
+ return await (0, listActionsIdb_1.listActionsIdb)(this, auth, vargs);
393
+ }
394
+ async listOutputs(auth, vargs) {
395
+ if (!auth.userId)
396
+ throw new index_client_1.sdk.WERR_UNAUTHORIZED();
397
+ return await (0, listOutputsIdb_1.listOutputsIdb)(this, auth, vargs);
398
+ }
399
+ async countChangeInputs(userId, basketId, excludeSending) {
400
+ const txStatus = ['completed', 'unproven'];
401
+ if (!excludeSending)
402
+ txStatus.push('sending');
403
+ const args = { partial: { userId, basketId }, txStatus };
404
+ let count = 0;
405
+ await this.filterOutputs(args, r => {
406
+ count++;
407
+ });
408
+ return count;
409
+ }
410
+ async findCertificatesAuth(auth, args) {
411
+ if (!auth.userId || (args.partial.userId && args.partial.userId !== auth.userId))
412
+ throw new index_client_1.sdk.WERR_UNAUTHORIZED();
413
+ args.partial.userId = auth.userId;
414
+ return await this.findCertificates(args);
415
+ }
416
+ async findOutputBasketsAuth(auth, args) {
417
+ if (!auth.userId || (args.partial.userId && args.partial.userId !== auth.userId))
418
+ throw new index_client_1.sdk.WERR_UNAUTHORIZED();
419
+ args.partial.userId = auth.userId;
420
+ return await this.findOutputBaskets(args);
421
+ }
422
+ async findOutputsAuth(auth, args) {
423
+ if (!auth.userId || (args.partial.userId && args.partial.userId !== auth.userId))
424
+ throw new index_client_1.sdk.WERR_UNAUTHORIZED();
425
+ args.partial.userId = auth.userId;
426
+ return await this.findOutputs(args);
427
+ }
428
+ async insertCertificateAuth(auth, certificate) {
429
+ if (!auth.userId || (certificate.userId && certificate.userId !== auth.userId))
430
+ throw new index_client_1.sdk.WERR_UNAUTHORIZED();
431
+ certificate.userId = auth.userId;
432
+ return await this.insertCertificate(certificate);
433
+ }
434
+ //
435
+ // StorageReaderWriter abstract methods
436
+ //
437
+ async dropAllData() {
438
+ await (0, idb_1.deleteDB)(this.dbName);
439
+ }
440
+ async filterOutputTagMaps(args, filtered, userId) {
441
+ var _a, _b, _c, _d;
442
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
443
+ let skipped = 0;
444
+ let count = 0;
445
+ const dbTrx = this.toDbTrx(['output_tags_map'], 'readonly', args.trx);
446
+ let cursor;
447
+ if (((_b = args.partial) === null || _b === void 0 ? void 0 : _b.outputTagId) !== undefined) {
448
+ cursor = await dbTrx.objectStore('output_tags_map').index('outputTagId').openCursor(args.partial.outputTagId);
449
+ }
450
+ else if (((_c = args.partial) === null || _c === void 0 ? void 0 : _c.outputId) !== undefined) {
451
+ cursor = await dbTrx.objectStore('output_tags_map').index('outputId').openCursor(args.partial.outputId);
452
+ }
453
+ else {
454
+ cursor = await dbTrx.objectStore('output_tags_map').openCursor();
455
+ }
456
+ let firstTime = true;
457
+ while (cursor) {
458
+ if (!firstTime)
459
+ cursor = await cursor.continue();
460
+ if (!cursor)
461
+ break;
462
+ firstTime = false;
463
+ const r = cursor.value;
464
+ if (args.since && args.since > r.updated_at)
465
+ continue;
466
+ if (args.tagIds && !args.tagIds.includes(r.outputTagId))
467
+ continue;
468
+ if (args.partial) {
469
+ if (args.partial.outputTagId && r.outputTagId !== args.partial.outputTagId)
470
+ continue;
471
+ if (args.partial.outputId && r.outputId !== args.partial.outputId)
472
+ continue;
473
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
474
+ continue;
475
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
476
+ continue;
477
+ if (args.partial.isDeleted !== undefined && r.isDeleted !== args.partial.isDeleted)
478
+ continue;
479
+ }
480
+ if (userId !== undefined && r.txid) {
481
+ const count = await this.countOutputTags({ partial: { userId, outputTagId: r.outputTagId }, trx: args.trx });
482
+ if (count === 0)
483
+ continue;
484
+ }
485
+ if (skipped < offset) {
486
+ skipped++;
487
+ continue;
488
+ }
489
+ filtered(r);
490
+ count++;
491
+ if (((_d = args.paged) === null || _d === void 0 ? void 0 : _d.limit) && count >= args.paged.limit)
492
+ break;
493
+ }
494
+ if (!args.trx)
495
+ await dbTrx.done;
496
+ }
497
+ async findOutputTagMaps(args) {
498
+ const results = [];
499
+ await this.filterOutputTagMaps(args, r => {
500
+ results.push(this.validateEntity(r));
501
+ });
502
+ return results;
503
+ }
504
+ async filterProvenTxReqs(args, filtered, userId) {
505
+ var _a, _b, _c, _d, _e, _f, _g;
506
+ if (args.partial.rawTx)
507
+ throw new index_client_1.sdk.WERR_INVALID_PARAMETER('args.partial.rawTx', `undefined. ProvenTxReqs may not be found by rawTx value.`);
508
+ if (args.partial.inputBEEF)
509
+ throw new index_client_1.sdk.WERR_INVALID_PARAMETER('args.partial.inputBEEF', `undefined. ProvenTxReqs may not be found by inputBEEF value.`);
510
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
511
+ let skipped = 0;
512
+ let count = 0;
513
+ const dbTrx = this.toDbTrx(['proven_tx_reqs'], 'readonly', args.trx);
514
+ let cursor;
515
+ if ((_b = args.partial) === null || _b === void 0 ? void 0 : _b.provenTxReqId) {
516
+ cursor = await dbTrx.objectStore('proven_tx_reqs').openCursor(args.partial.provenTxReqId);
517
+ }
518
+ else if (((_c = args.partial) === null || _c === void 0 ? void 0 : _c.provenTxId) !== undefined) {
519
+ cursor = await dbTrx.objectStore('proven_tx_reqs').index('provenTxId').openCursor(args.partial.provenTxId);
520
+ }
521
+ else if (((_d = args.partial) === null || _d === void 0 ? void 0 : _d.txid) !== undefined) {
522
+ cursor = await dbTrx.objectStore('proven_tx_reqs').index('txid').openCursor(args.partial.txid);
523
+ }
524
+ else if (((_e = args.partial) === null || _e === void 0 ? void 0 : _e.status) !== undefined) {
525
+ cursor = await dbTrx.objectStore('proven_tx_reqs').index('status').openCursor(args.partial.status);
526
+ }
527
+ else if (((_f = args.partial) === null || _f === void 0 ? void 0 : _f.batch) !== undefined) {
528
+ cursor = await dbTrx.objectStore('proven_tx_reqs').index('batch').openCursor(args.partial.batch);
529
+ }
530
+ else {
531
+ cursor = await dbTrx.objectStore('proven_tx_reqs').openCursor();
532
+ }
533
+ let firstTime = true;
534
+ while (cursor) {
535
+ if (!firstTime)
536
+ cursor = await cursor.continue();
537
+ if (!cursor)
538
+ break;
539
+ firstTime = false;
540
+ const r = cursor.value;
541
+ if (args.since && args.since > r.updated_at)
542
+ continue;
543
+ if (args.partial) {
544
+ if (args.partial.provenTxReqId && r.provenTxReqId !== args.partial.provenTxReqId)
545
+ continue;
546
+ if (args.partial.provenTxId && r.provenTxId !== args.partial.provenTxId)
547
+ continue;
548
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
549
+ continue;
550
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
551
+ continue;
552
+ if (args.partial.status && r.status !== args.partial.status)
553
+ continue;
554
+ if (args.partial.attempts !== undefined && r.attempts !== args.partial.attempts)
555
+ continue;
556
+ if (args.partial.notified !== undefined && r.notified !== args.partial.notified)
557
+ continue;
558
+ if (args.partial.txid && r.txid !== args.partial.txid)
559
+ continue;
560
+ if (args.partial.batch && r.batch !== args.partial.batch)
561
+ continue;
562
+ if (args.partial.history && r.history !== args.partial.history)
563
+ continue;
564
+ if (args.partial.notify && r.notify !== args.partial.notify)
565
+ continue;
566
+ }
567
+ if (userId !== undefined && r.txid) {
568
+ const count = await this.countTransactions({ partial: { userId, txid: r.txid }, trx: args.trx });
569
+ if (count === 0)
570
+ continue;
571
+ }
572
+ if (skipped < offset) {
573
+ skipped++;
574
+ continue;
575
+ }
576
+ filtered(r);
577
+ count++;
578
+ if (((_g = args.paged) === null || _g === void 0 ? void 0 : _g.limit) && count >= args.paged.limit)
579
+ break;
580
+ }
581
+ if (!args.trx)
582
+ await dbTrx.done;
583
+ }
584
+ async findProvenTxReqs(args) {
585
+ const results = [];
586
+ await this.filterProvenTxReqs(args, r => {
587
+ results.push(this.validateEntity(r));
588
+ });
589
+ return results;
590
+ }
591
+ async filterProvenTxs(args, filtered, userId) {
592
+ var _a, _b, _c, _d;
593
+ if (args.partial.rawTx)
594
+ throw new index_client_1.sdk.WERR_INVALID_PARAMETER('args.partial.rawTx', `undefined. ProvenTxs may not be found by rawTx value.`);
595
+ if (args.partial.merklePath)
596
+ throw new index_client_1.sdk.WERR_INVALID_PARAMETER('args.partial.merklePath', `undefined. ProvenTxs may not be found by merklePath value.`);
597
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
598
+ let skipped = 0;
599
+ let count = 0;
600
+ const dbTrx = this.toDbTrx(['proven_txs'], 'readonly', args.trx);
601
+ let cursor;
602
+ if ((_b = args.partial) === null || _b === void 0 ? void 0 : _b.provenTxId) {
603
+ cursor = await dbTrx.objectStore('proven_txs').openCursor(args.partial.provenTxId);
604
+ }
605
+ else if (((_c = args.partial) === null || _c === void 0 ? void 0 : _c.txid) !== undefined) {
606
+ cursor = await dbTrx.objectStore('proven_txs').index('txid').openCursor(args.partial.txid);
607
+ }
608
+ else {
609
+ cursor = await dbTrx.objectStore('proven_txs').openCursor();
610
+ }
611
+ let firstTime = true;
612
+ while (cursor) {
613
+ if (!firstTime)
614
+ cursor = await cursor.continue();
615
+ if (!cursor)
616
+ break;
617
+ firstTime = false;
618
+ const r = cursor.value;
619
+ if (args.since && args.since > r.updated_at)
620
+ continue;
621
+ if (args.partial) {
622
+ if (args.partial.provenTxId && r.provenTxId !== args.partial.provenTxId)
623
+ continue;
624
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
625
+ continue;
626
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
627
+ continue;
628
+ if (args.partial.txid && r.txid !== args.partial.txid)
629
+ continue;
630
+ if (args.partial.height !== undefined && r.height !== args.partial.height)
631
+ continue;
632
+ if (args.partial.index !== undefined && r.index !== args.partial.index)
633
+ continue;
634
+ if (args.partial.blockHash && r.blockHash !== args.partial.blockHash)
635
+ continue;
636
+ if (args.partial.merkleRoot && r.merkleRoot !== args.partial.merkleRoot)
637
+ continue;
638
+ }
639
+ if (userId !== undefined) {
640
+ const count = await this.countTransactions({ partial: { userId, provenTxId: r.provenTxId }, trx: args.trx });
641
+ if (count === 0)
642
+ continue;
643
+ }
644
+ if (skipped < offset) {
645
+ skipped++;
646
+ continue;
647
+ }
648
+ filtered(r);
649
+ count++;
650
+ if (((_d = args.paged) === null || _d === void 0 ? void 0 : _d.limit) && count >= args.paged.limit)
651
+ break;
652
+ }
653
+ if (!args.trx)
654
+ await dbTrx.done;
655
+ }
656
+ async findProvenTxs(args) {
657
+ const results = [];
658
+ await this.filterProvenTxs(args, r => {
659
+ results.push(this.validateEntity(r));
660
+ });
661
+ return results;
662
+ }
663
+ async filterTxLabelMaps(args, filtered, userId) {
664
+ var _a, _b, _c, _d;
665
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
666
+ let skipped = 0;
667
+ let count = 0;
668
+ const dbTrx = this.toDbTrx(['tx_labels_map'], 'readonly', args.trx);
669
+ let cursor;
670
+ if (((_b = args.partial) === null || _b === void 0 ? void 0 : _b.transactionId) !== undefined) {
671
+ cursor = await dbTrx.objectStore('tx_labels_map').index('transactionId').openCursor(args.partial.transactionId);
672
+ }
673
+ else if (((_c = args.partial) === null || _c === void 0 ? void 0 : _c.txLabelId) !== undefined) {
674
+ cursor = await dbTrx.objectStore('tx_labels_map').index('txLabelId').openCursor(args.partial.txLabelId);
675
+ }
676
+ else {
677
+ cursor = await dbTrx.objectStore('tx_labels_map').openCursor();
678
+ }
679
+ let firstTime = true;
680
+ while (cursor) {
681
+ if (!firstTime)
682
+ cursor = await cursor.continue();
683
+ if (!cursor)
684
+ break;
685
+ firstTime = false;
686
+ const r = cursor.value;
687
+ if (args.since && args.since > r.updated_at)
688
+ continue;
689
+ if (args.partial) {
690
+ if (args.partial.txLabelId && r.txLabelId !== args.partial.txLabelId)
691
+ continue;
692
+ if (args.partial.transactionId && r.transactionId !== args.partial.transactionId)
693
+ continue;
694
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
695
+ continue;
696
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
697
+ continue;
698
+ if (args.partial.isDeleted !== undefined && r.isDeleted !== args.partial.isDeleted)
699
+ continue;
700
+ }
701
+ if (userId !== undefined) {
702
+ const count = await this.countTxLabels({ partial: { userId, txLabelId: r.txLabelId }, trx: args.trx });
703
+ if (count === 0)
704
+ continue;
705
+ }
706
+ if (skipped < offset) {
707
+ skipped++;
708
+ continue;
709
+ }
710
+ filtered(r);
711
+ count++;
712
+ if (((_d = args.paged) === null || _d === void 0 ? void 0 : _d.limit) && count >= args.paged.limit)
713
+ break;
714
+ }
715
+ if (!args.trx)
716
+ await dbTrx.done;
717
+ }
718
+ async findTxLabelMaps(args) {
719
+ const results = [];
720
+ await this.filterTxLabelMaps(args, r => {
721
+ results.push(this.validateEntity(r));
722
+ });
723
+ return results;
724
+ }
725
+ async countOutputTagMaps(args) {
726
+ let count = 0;
727
+ await this.filterOutputTagMaps(args, () => {
728
+ count++;
729
+ });
730
+ return count;
731
+ }
732
+ async countProvenTxReqs(args) {
733
+ let count = 0;
734
+ await this.filterProvenTxReqs(args, () => {
735
+ count++;
736
+ });
737
+ return count;
738
+ }
739
+ async countProvenTxs(args) {
740
+ let count = 0;
741
+ await this.filterProvenTxs(args, () => {
742
+ count++;
743
+ });
744
+ return count;
745
+ }
746
+ async countTxLabelMaps(args) {
747
+ let count = 0;
748
+ await this.filterTxLabelMaps(args, () => {
749
+ count++;
750
+ });
751
+ return count;
752
+ }
753
+ async insertCertificate(certificate, trx) {
754
+ const e = await this.validateEntityForInsert(certificate, trx, undefined, ['isDeleted']);
755
+ const fields = e.fields;
756
+ if (e.fields)
757
+ delete e.fields;
758
+ if (e.certificateId === 0)
759
+ delete e.certificateId;
760
+ const dbTrx = this.toDbTrx(['certificates', 'certificate_fields'], 'readwrite', trx);
761
+ const store = dbTrx.objectStore('certificates');
762
+ try {
763
+ const id = Number(await store.add(e));
764
+ certificate.certificateId = id;
765
+ if (fields) {
766
+ for (const field of fields) {
767
+ field.certificateId = certificate.certificateId;
768
+ field.userId = certificate.userId;
769
+ await this.insertCertificateField(field, dbTrx);
770
+ }
771
+ }
772
+ }
773
+ finally {
774
+ if (!trx)
775
+ await dbTrx.done;
776
+ }
777
+ return certificate.certificateId;
778
+ }
779
+ async insertCertificateField(certificateField, trx) {
780
+ const e = await this.validateEntityForInsert(certificateField, trx);
781
+ const dbTrx = this.toDbTrx(['certificate_fields'], 'readwrite', trx);
782
+ const store = dbTrx.objectStore('certificate_fields');
783
+ try {
784
+ await store.add(e);
785
+ }
786
+ finally {
787
+ if (!trx)
788
+ await dbTrx.done;
789
+ }
790
+ }
791
+ async insertCommission(commission, trx) {
792
+ const e = await this.validateEntityForInsert(commission, trx);
793
+ if (e.commissionId === 0)
794
+ delete e.commissionId;
795
+ const dbTrx = this.toDbTrx(['commissions'], 'readwrite', trx);
796
+ const store = dbTrx.objectStore('commissions');
797
+ try {
798
+ const id = Number(await store.add(e));
799
+ commission.commissionId = id;
800
+ }
801
+ finally {
802
+ if (!trx)
803
+ await dbTrx.done;
804
+ }
805
+ return commission.commissionId;
806
+ }
807
+ async insertMonitorEvent(event, trx) {
808
+ const e = await this.validateEntityForInsert(event, trx);
809
+ if (e.id === 0)
810
+ delete e.id;
811
+ const dbTrx = this.toDbTrx(['monitor_events'], 'readwrite', trx);
812
+ const store = dbTrx.objectStore('monitor_events');
813
+ try {
814
+ const id = Number(await store.add(e));
815
+ event.id = id;
816
+ }
817
+ finally {
818
+ if (!trx)
819
+ await dbTrx.done;
820
+ }
821
+ return event.id;
822
+ }
823
+ async insertOutput(output, trx) {
824
+ const e = await this.validateEntityForInsert(output, trx);
825
+ if (e.outputId === 0)
826
+ delete e.outputId;
827
+ const dbTrx = this.toDbTrx(['outputs'], 'readwrite', trx);
828
+ const store = dbTrx.objectStore('outputs');
829
+ try {
830
+ const id = Number(await store.add(e));
831
+ output.outputId = id;
832
+ }
833
+ finally {
834
+ if (!trx)
835
+ await dbTrx.done;
836
+ }
837
+ return output.outputId;
838
+ }
839
+ async insertOutputBasket(basket, trx) {
840
+ const e = await this.validateEntityForInsert(basket, trx, undefined, ['isDeleted']);
841
+ if (e.basketId === 0)
842
+ delete e.basketId;
843
+ const dbTrx = this.toDbTrx(['output_baskets'], 'readwrite', trx);
844
+ const store = dbTrx.objectStore('output_baskets');
845
+ try {
846
+ const id = Number(await store.add(e));
847
+ basket.basketId = id;
848
+ }
849
+ finally {
850
+ if (!trx)
851
+ await dbTrx.done;
852
+ }
853
+ return basket.basketId;
854
+ }
855
+ async insertOutputTag(tag, trx) {
856
+ const e = await this.validateEntityForInsert(tag, trx, undefined, ['isDeleted']);
857
+ if (e.outputTagId === 0)
858
+ delete e.outputTagId;
859
+ const dbTrx = this.toDbTrx(['output_tags'], 'readwrite', trx);
860
+ const store = dbTrx.objectStore('output_tags');
861
+ try {
862
+ const id = Number(await store.add(e));
863
+ tag.outputTagId = id;
864
+ }
865
+ finally {
866
+ if (!trx)
867
+ await dbTrx.done;
868
+ }
869
+ return tag.outputTagId;
870
+ }
871
+ async insertOutputTagMap(tagMap, trx) {
872
+ const e = await this.validateEntityForInsert(tagMap, trx, undefined, ['isDeleted']);
873
+ const dbTrx = this.toDbTrx(['output_tags_map'], 'readwrite', trx);
874
+ const store = dbTrx.objectStore('output_tags_map');
875
+ try {
876
+ await store.add(e);
877
+ }
878
+ finally {
879
+ if (!trx)
880
+ await dbTrx.done;
881
+ }
882
+ }
883
+ async insertProvenTx(tx, trx) {
884
+ const e = await this.validateEntityForInsert(tx, trx);
885
+ if (e.provenTxId === 0)
886
+ delete e.provenTxId;
887
+ const dbTrx = this.toDbTrx(['proven_txs'], 'readwrite', trx);
888
+ const store = dbTrx.objectStore('proven_txs');
889
+ try {
890
+ const id = Number(await store.add(e));
891
+ tx.provenTxId = id;
892
+ }
893
+ finally {
894
+ if (!trx)
895
+ await dbTrx.done;
896
+ }
897
+ return tx.provenTxId;
898
+ }
899
+ async insertProvenTxReq(tx, trx) {
900
+ const e = await this.validateEntityForInsert(tx, trx);
901
+ if (e.provenTxReqId === 0)
902
+ delete e.provenTxReqId;
903
+ const dbTrx = this.toDbTrx(['proven_tx_reqs'], 'readwrite', trx);
904
+ const store = dbTrx.objectStore('proven_tx_reqs');
905
+ try {
906
+ const id = Number(await store.add(e));
907
+ tx.provenTxReqId = id;
908
+ }
909
+ finally {
910
+ if (!trx)
911
+ await dbTrx.done;
912
+ }
913
+ return tx.provenTxReqId;
914
+ }
915
+ async insertSyncState(syncState, trx) {
916
+ const e = await this.validateEntityForInsert(syncState, trx, ['when'], ['init']);
917
+ if (e.syncStateId === 0)
918
+ delete e.syncStateId;
919
+ const dbTrx = this.toDbTrx(['sync_states'], 'readwrite', trx);
920
+ const store = dbTrx.objectStore('sync_states');
921
+ try {
922
+ const id = Number(await store.add(e));
923
+ syncState.syncStateId = id;
924
+ }
925
+ finally {
926
+ if (!trx)
927
+ await dbTrx.done;
928
+ }
929
+ return syncState.syncStateId;
930
+ }
931
+ async insertTransaction(tx, trx) {
932
+ const e = await this.validateEntityForInsert(tx, trx);
933
+ if (e.transactionId === 0)
934
+ delete e.transactionId;
935
+ const dbTrx = this.toDbTrx(['transactions'], 'readwrite', trx);
936
+ const store = dbTrx.objectStore('transactions');
937
+ try {
938
+ const id = Number(await store.add(e));
939
+ tx.transactionId = id;
940
+ }
941
+ finally {
942
+ if (!trx)
943
+ await dbTrx.done;
944
+ }
945
+ return tx.transactionId;
946
+ }
947
+ async insertTxLabel(label, trx) {
948
+ const e = await this.validateEntityForInsert(label, trx, undefined, ['isDeleted']);
949
+ if (e.txLabelId === 0)
950
+ delete e.txLabelId;
951
+ const dbTrx = this.toDbTrx(['tx_labels'], 'readwrite', trx);
952
+ const store = dbTrx.objectStore('tx_labels');
953
+ try {
954
+ const id = Number(await store.add(e));
955
+ label.txLabelId = id;
956
+ }
957
+ finally {
958
+ if (!trx)
959
+ await dbTrx.done;
960
+ }
961
+ return label.txLabelId;
962
+ }
963
+ async insertTxLabelMap(labelMap, trx) {
964
+ const e = await this.validateEntityForInsert(labelMap, trx, undefined, ['isDeleted']);
965
+ const dbTrx = this.toDbTrx(['tx_labels_map'], 'readwrite', trx);
966
+ const store = dbTrx.objectStore('tx_labels_map');
967
+ try {
968
+ await store.add(e);
969
+ }
970
+ finally {
971
+ if (!trx)
972
+ await dbTrx.done;
973
+ }
974
+ }
975
+ async insertUser(user, trx) {
976
+ const e = await this.validateEntityForInsert(user, trx);
977
+ if (e.userId === 0)
978
+ delete e.userId;
979
+ const dbTrx = this.toDbTrx(['users'], 'readwrite', trx);
980
+ const store = dbTrx.objectStore('users');
981
+ try {
982
+ const id = Number(await store.add(e));
983
+ user.userId = id;
984
+ }
985
+ finally {
986
+ if (!trx)
987
+ await dbTrx.done;
988
+ }
989
+ return user.userId;
990
+ }
991
+ async updateIdb(id, update, keyProp, storeName, trx) {
992
+ if (update[keyProp] !== undefined && (Array.isArray(id) || update[keyProp] !== id)) {
993
+ throw new index_client_1.sdk.WERR_INVALID_PARAMETER(`update.${keyProp}`, `undefined`);
994
+ }
995
+ const u = this.validatePartialForUpdate(update);
996
+ const dbTrx = this.toDbTrx([storeName], 'readwrite', trx);
997
+ const store = dbTrx.objectStore(storeName);
998
+ const ids = Array.isArray(id) ? id : [id];
999
+ try {
1000
+ for (const i of ids) {
1001
+ const e = await store.get(i);
1002
+ if (!e)
1003
+ throw new index_client_1.sdk.WERR_INVALID_PARAMETER('id', `an existing record to update ${keyProp} ${i} not found`);
1004
+ const v = {
1005
+ ...e,
1006
+ ...u
1007
+ };
1008
+ const uid = await store.put(v);
1009
+ if (uid !== i)
1010
+ throw new index_client_1.sdk.WERR_INTERNAL(`updated id ${uid} does not match original ${id}`);
1011
+ }
1012
+ }
1013
+ finally {
1014
+ if (!trx)
1015
+ await dbTrx.done;
1016
+ }
1017
+ return 1;
1018
+ }
1019
+ async updateIdbKey(key, update, keyProps, storeName, trx) {
1020
+ if (key.length !== keyProps.length)
1021
+ throw new index_client_1.sdk.WERR_INTERNAL(`key.length ${key.length} !== keyProps.length ${keyProps.length}`);
1022
+ for (let i = 0; i < key.length; i++) {
1023
+ if (update[keyProps[i]] !== undefined && update[keyProps[i]] !== key[i]) {
1024
+ throw new index_client_1.sdk.WERR_INVALID_PARAMETER(`update.${keyProps[i]}`, `undefined`);
1025
+ }
1026
+ }
1027
+ const u = this.validatePartialForUpdate(update);
1028
+ const dbTrx = this.toDbTrx([storeName], 'readwrite', trx);
1029
+ const store = dbTrx.objectStore(storeName);
1030
+ try {
1031
+ const e = await store.get(key);
1032
+ if (!e)
1033
+ throw new index_client_1.sdk.WERR_INVALID_PARAMETER('key', `an existing record to update ${keyProps.join(',')} ${key.join(',')} not found`);
1034
+ const v = {
1035
+ ...e,
1036
+ ...u
1037
+ };
1038
+ const uid = await store.put(v);
1039
+ for (let i = 0; i < key.length; i++) {
1040
+ if (uid[i] !== key[i])
1041
+ throw new index_client_1.sdk.WERR_INTERNAL(`updated key ${uid[i]} does not match original ${key[i]}`);
1042
+ }
1043
+ }
1044
+ finally {
1045
+ if (!trx)
1046
+ await dbTrx.done;
1047
+ }
1048
+ return 1;
1049
+ }
1050
+ async updateCertificate(id, update, trx) {
1051
+ return this.updateIdb(id, update, 'certificateId', 'certificates', trx);
1052
+ }
1053
+ async updateCertificateField(certificateId, fieldName, update, trx) {
1054
+ return this.updateIdbKey([certificateId, fieldName], update, ['certificateId', 'fieldName'], 'certificate_fields', trx);
1055
+ }
1056
+ async updateCommission(id, update, trx) {
1057
+ return this.updateIdb(id, update, 'commissionId', 'commissions', trx);
1058
+ }
1059
+ async updateMonitorEvent(id, update, trx) {
1060
+ return this.updateIdb(id, update, 'id', 'monitor_events', trx);
1061
+ }
1062
+ async updateOutput(id, update, trx) {
1063
+ return this.updateIdb(id, update, 'outputId', 'outputs', trx);
1064
+ }
1065
+ async updateOutputBasket(id, update, trx) {
1066
+ return this.updateIdb(id, update, 'basketId', 'output_baskets', trx);
1067
+ }
1068
+ async updateOutputTag(id, update, trx) {
1069
+ return this.updateIdb(id, update, 'outputTagId', 'output_tags', trx);
1070
+ }
1071
+ async updateProvenTx(id, update, trx) {
1072
+ return this.updateIdb(id, update, 'provenTxId', 'proven_txs', trx);
1073
+ }
1074
+ async updateProvenTxReq(id, update, trx) {
1075
+ return this.updateIdb(id, update, 'provenTxReqId', 'proven_tx_reqs', trx);
1076
+ }
1077
+ async updateSyncState(id, update, trx) {
1078
+ return this.updateIdb(id, update, 'syncStateId', 'sync_states', trx);
1079
+ }
1080
+ async updateTransaction(id, update, trx) {
1081
+ return this.updateIdb(id, update, 'transactionId', 'transactions', trx);
1082
+ }
1083
+ async updateTxLabel(id, update, trx) {
1084
+ return this.updateIdb(id, update, 'txLabelId', 'tx_labels', trx);
1085
+ }
1086
+ async updateUser(id, update, trx) {
1087
+ return this.updateIdb(id, update, 'userId', 'users', trx);
1088
+ }
1089
+ async updateOutputTagMap(outputId, tagId, update, trx) {
1090
+ return this.updateIdbKey([tagId, outputId], update, ['outputTagId', 'outputId'], 'output_tags_map', trx);
1091
+ }
1092
+ async updateTxLabelMap(transactionId, txLabelId, update, trx) {
1093
+ return this.updateIdbKey([txLabelId, transactionId], update, ['txLabelId', 'transactionId'], 'tx_labels_map', trx);
1094
+ }
1095
+ //
1096
+ // StorageReader abstract methods
1097
+ //
1098
+ async destroy() {
1099
+ if (this.db) {
1100
+ this.db.close();
1101
+ }
1102
+ this.db = undefined;
1103
+ this._settings = undefined;
1104
+ }
1105
+ /**
1106
+ * @param scope
1107
+ * @param trx
1108
+ * @returns
1109
+ */
1110
+ async transaction(scope, trx) {
1111
+ if (trx)
1112
+ return await scope(trx);
1113
+ const stores = this.allStores;
1114
+ const db = await this.verifyDB();
1115
+ const tx = db.transaction(stores, 'readwrite');
1116
+ try {
1117
+ const r = await scope(tx);
1118
+ await tx.done;
1119
+ return r;
1120
+ }
1121
+ catch (err) {
1122
+ tx.abort();
1123
+ await tx.done;
1124
+ throw err;
1125
+ }
1126
+ }
1127
+ async filterCertificateFields(args, filtered) {
1128
+ var _a, _b, _c, _d;
1129
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
1130
+ let skipped = 0;
1131
+ let count = 0;
1132
+ const dbTrx = this.toDbTrx(['certificate_fields'], 'readonly', args.trx);
1133
+ let cursor;
1134
+ if (((_b = args.partial) === null || _b === void 0 ? void 0 : _b.certificateId) !== undefined) {
1135
+ cursor = await dbTrx
1136
+ .objectStore('certificate_fields')
1137
+ .index('certificateId')
1138
+ .openCursor(args.partial.certificateId);
1139
+ }
1140
+ else if (((_c = args.partial) === null || _c === void 0 ? void 0 : _c.userId) !== undefined) {
1141
+ cursor = await dbTrx.objectStore('certificate_fields').index('userId').openCursor(args.partial.userId);
1142
+ }
1143
+ else {
1144
+ cursor = await dbTrx.objectStore('certificate_fields').openCursor();
1145
+ }
1146
+ let firstTime = true;
1147
+ while (cursor) {
1148
+ if (!firstTime)
1149
+ cursor = await cursor.continue();
1150
+ if (!cursor)
1151
+ break;
1152
+ firstTime = false;
1153
+ const r = cursor.value;
1154
+ if (args.since && args.since > r.updated_at)
1155
+ continue;
1156
+ if (args.partial) {
1157
+ if (args.partial.userId && r.userId !== args.partial.userId)
1158
+ continue;
1159
+ if (args.partial.certificateId && r.certificateId !== args.partial.certificateId)
1160
+ continue;
1161
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
1162
+ continue;
1163
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
1164
+ continue;
1165
+ if (args.partial.fieldName && r.fieldName !== args.partial.fieldName)
1166
+ continue;
1167
+ if (args.partial.fieldValue && r.fieldValue !== args.partial.fieldValue)
1168
+ continue;
1169
+ if (args.partial.masterKey && r.masterKey !== args.partial.masterKey)
1170
+ continue;
1171
+ }
1172
+ if (skipped < offset) {
1173
+ skipped++;
1174
+ continue;
1175
+ }
1176
+ filtered(r);
1177
+ count++;
1178
+ if (((_d = args.paged) === null || _d === void 0 ? void 0 : _d.limit) && count >= args.paged.limit)
1179
+ break;
1180
+ }
1181
+ if (!args.trx)
1182
+ await dbTrx.done;
1183
+ }
1184
+ async findCertificateFields(args) {
1185
+ const result = [];
1186
+ await this.filterCertificateFields(args, r => {
1187
+ result.push(this.validateEntity(r));
1188
+ });
1189
+ return result;
1190
+ }
1191
+ async filterCertificates(args, filtered) {
1192
+ var _a, _b, _c, _d, _e, _f, _g;
1193
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
1194
+ let skipped = 0;
1195
+ let count = 0;
1196
+ const dbTrx = this.toDbTrx(['certificates'], 'readonly', args.trx);
1197
+ let cursor;
1198
+ if ((_b = args.partial) === null || _b === void 0 ? void 0 : _b.certificateId) {
1199
+ cursor = await dbTrx.objectStore('certificates').openCursor(args.partial.certificateId);
1200
+ }
1201
+ else if (((_c = args.partial) === null || _c === void 0 ? void 0 : _c.userId) !== undefined) {
1202
+ if (((_d = args.partial) === null || _d === void 0 ? void 0 : _d.type) && ((_e = args.partial) === null || _e === void 0 ? void 0 : _e.certifier) && ((_f = args.partial) === null || _f === void 0 ? void 0 : _f.serialNumber)) {
1203
+ cursor = await dbTrx
1204
+ .objectStore('certificates')
1205
+ .index('userId_type_certifier_serialNumber')
1206
+ .openCursor([args.partial.userId, args.partial.type, args.partial.certifier, args.partial.serialNumber]);
1207
+ }
1208
+ else {
1209
+ cursor = await dbTrx.objectStore('certificates').index('userId').openCursor(args.partial.userId);
1210
+ }
1211
+ }
1212
+ else {
1213
+ cursor = await dbTrx.objectStore('certificates').openCursor();
1214
+ }
1215
+ let firstTime = true;
1216
+ while (cursor) {
1217
+ if (!firstTime)
1218
+ cursor = await cursor.continue();
1219
+ if (!cursor)
1220
+ break;
1221
+ firstTime = false;
1222
+ const r = cursor.value;
1223
+ if (args.since && args.since > r.updated_at)
1224
+ continue;
1225
+ if (args.certifiers && !args.certifiers.includes(r.certifier))
1226
+ continue;
1227
+ if (args.types && !args.types.includes(r.type))
1228
+ continue;
1229
+ if (args.partial) {
1230
+ if (args.partial.userId && r.userId !== args.partial.userId)
1231
+ continue;
1232
+ if (args.partial.certificateId && r.certificateId !== args.partial.certificateId)
1233
+ continue;
1234
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
1235
+ continue;
1236
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
1237
+ continue;
1238
+ if (args.partial.type && r.type !== args.partial.type)
1239
+ continue;
1240
+ if (args.partial.serialNumber && r.serialNumber !== args.partial.serialNumber)
1241
+ continue;
1242
+ if (args.partial.certifier && r.certifier !== args.partial.certifier)
1243
+ continue;
1244
+ if (args.partial.subject && r.subject !== args.partial.subject)
1245
+ continue;
1246
+ if (args.partial.verifier && r.verifier !== args.partial.verifier)
1247
+ continue;
1248
+ if (args.partial.revocationOutpoint && r.revocationOutpoint !== args.partial.revocationOutpoint)
1249
+ continue;
1250
+ if (args.partial.signature && r.signature !== args.partial.signature)
1251
+ continue;
1252
+ if (args.partial.isDeleted && r.isDeleted !== args.partial.isDeleted)
1253
+ continue;
1254
+ }
1255
+ if (skipped < offset) {
1256
+ skipped++;
1257
+ continue;
1258
+ }
1259
+ filtered(r);
1260
+ count++;
1261
+ if (((_g = args.paged) === null || _g === void 0 ? void 0 : _g.limit) && count >= args.paged.limit)
1262
+ break;
1263
+ }
1264
+ if (!args.trx)
1265
+ await dbTrx.done;
1266
+ }
1267
+ async findCertificates(args) {
1268
+ const result = [];
1269
+ await this.filterCertificates(args, r => {
1270
+ result.push(this.validateEntity(r));
1271
+ });
1272
+ if (args.includeFields) {
1273
+ for (const c of result) {
1274
+ const fields = await this.findCertificateFields({ partial: { certificateId: c.certificateId }, trx: args.trx });
1275
+ c.fields = fields;
1276
+ }
1277
+ }
1278
+ return result;
1279
+ }
1280
+ async filterCommissions(args, filtered) {
1281
+ var _a, _b, _c, _d, _e;
1282
+ if (args.partial.lockingScript)
1283
+ throw new index_client_1.sdk.WERR_INVALID_PARAMETER('partial.lockingScript', `undefined. Commissions may not be found by lockingScript value.`);
1284
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
1285
+ let skipped = 0;
1286
+ let count = 0;
1287
+ const dbTrx = this.toDbTrx(['commissions'], 'readonly', args.trx);
1288
+ let cursor;
1289
+ if ((_b = args.partial) === null || _b === void 0 ? void 0 : _b.commissionId) {
1290
+ cursor = await dbTrx.objectStore('commissions').openCursor(args.partial.commissionId);
1291
+ }
1292
+ else if (((_c = args.partial) === null || _c === void 0 ? void 0 : _c.userId) !== undefined) {
1293
+ cursor = await dbTrx.objectStore('commissions').index('userId').openCursor(args.partial.userId);
1294
+ }
1295
+ else if (((_d = args.partial) === null || _d === void 0 ? void 0 : _d.transactionId) !== undefined) {
1296
+ cursor = await dbTrx.objectStore('commissions').index('transactionId').openCursor(args.partial.transactionId);
1297
+ }
1298
+ else {
1299
+ cursor = await dbTrx.objectStore('commissions').openCursor();
1300
+ }
1301
+ let firstTime = true;
1302
+ while (cursor) {
1303
+ if (!firstTime)
1304
+ cursor = await cursor.continue();
1305
+ if (!cursor)
1306
+ break;
1307
+ firstTime = false;
1308
+ const r = cursor.value;
1309
+ if (args.since && args.since > r.updated_at)
1310
+ continue;
1311
+ if (args.partial) {
1312
+ if (args.partial.commissionId && r.commissionId !== args.partial.commissionId)
1313
+ continue;
1314
+ if (args.partial.transactionId && r.transactionId !== args.partial.transactionId)
1315
+ continue;
1316
+ if (args.partial.userId && r.userId !== args.partial.userId)
1317
+ continue;
1318
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
1319
+ continue;
1320
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
1321
+ continue;
1322
+ if (args.partial.satoshis !== undefined && r.satoshis !== args.partial.satoshis)
1323
+ continue;
1324
+ if (args.partial.keyOffset && r.keyOffset !== args.partial.keyOffset)
1325
+ continue;
1326
+ if (args.partial.isRedeemed !== undefined && r.isRedeemed !== args.partial.isRedeemed)
1327
+ continue;
1328
+ }
1329
+ if (skipped < offset) {
1330
+ skipped++;
1331
+ continue;
1332
+ }
1333
+ filtered(r);
1334
+ count++;
1335
+ if (((_e = args.paged) === null || _e === void 0 ? void 0 : _e.limit) && count >= args.paged.limit)
1336
+ break;
1337
+ }
1338
+ if (!args.trx)
1339
+ await dbTrx.done;
1340
+ }
1341
+ async findCommissions(args) {
1342
+ const result = [];
1343
+ await this.filterCommissions(args, r => {
1344
+ result.push(this.validateEntity(r));
1345
+ });
1346
+ return result;
1347
+ }
1348
+ async filterMonitorEvents(args, filtered) {
1349
+ var _a, _b, _c;
1350
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
1351
+ let skipped = 0;
1352
+ let count = 0;
1353
+ const dbTrx = this.toDbTrx(['monitor_events'], 'readonly', args.trx);
1354
+ let cursor;
1355
+ if ((_b = args.partial) === null || _b === void 0 ? void 0 : _b.id) {
1356
+ cursor = await dbTrx.objectStore('monitor_events').openCursor(args.partial.id);
1357
+ }
1358
+ else {
1359
+ cursor = await dbTrx.objectStore('monitor_events').openCursor();
1360
+ }
1361
+ let firstTime = true;
1362
+ while (cursor) {
1363
+ if (!firstTime)
1364
+ cursor = await cursor.continue();
1365
+ if (!cursor)
1366
+ break;
1367
+ firstTime = false;
1368
+ const r = cursor.value;
1369
+ if (args.since && args.since > r.updated_at)
1370
+ continue;
1371
+ if (args.partial) {
1372
+ if (args.partial.id && r.id !== args.partial.id)
1373
+ continue;
1374
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
1375
+ continue;
1376
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
1377
+ continue;
1378
+ if (args.partial.event && r.event !== args.partial.event)
1379
+ continue;
1380
+ if (args.partial.details && r.details !== args.partial.details)
1381
+ continue;
1382
+ }
1383
+ if (skipped < offset) {
1384
+ skipped++;
1385
+ continue;
1386
+ }
1387
+ filtered(r);
1388
+ count++;
1389
+ if (((_c = args.paged) === null || _c === void 0 ? void 0 : _c.limit) && count >= args.paged.limit)
1390
+ break;
1391
+ }
1392
+ if (!args.trx)
1393
+ await dbTrx.done;
1394
+ }
1395
+ async findMonitorEvents(args) {
1396
+ const result = [];
1397
+ await this.filterMonitorEvents(args, r => {
1398
+ result.push(this.validateEntity(r));
1399
+ });
1400
+ return result;
1401
+ }
1402
+ async filterOutputBaskets(args, filtered) {
1403
+ var _a, _b, _c, _d, _e;
1404
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
1405
+ let skipped = 0;
1406
+ let count = 0;
1407
+ const dbTrx = this.toDbTrx(['output_baskets'], 'readonly', args.trx);
1408
+ let cursor;
1409
+ if ((_b = args.partial) === null || _b === void 0 ? void 0 : _b.basketId) {
1410
+ cursor = await dbTrx.objectStore('output_baskets').openCursor(args.partial.basketId);
1411
+ }
1412
+ else if (((_c = args.partial) === null || _c === void 0 ? void 0 : _c.userId) !== undefined) {
1413
+ if (((_d = args.partial) === null || _d === void 0 ? void 0 : _d.name) !== undefined) {
1414
+ cursor = await dbTrx
1415
+ .objectStore('output_baskets')
1416
+ .index('name_userId')
1417
+ .openCursor([args.partial.name, args.partial.userId]);
1418
+ }
1419
+ else {
1420
+ cursor = await dbTrx.objectStore('output_baskets').index('userId').openCursor(args.partial.userId);
1421
+ }
1422
+ }
1423
+ else {
1424
+ cursor = await dbTrx.objectStore('output_baskets').openCursor();
1425
+ }
1426
+ let firstTime = true;
1427
+ while (cursor) {
1428
+ if (!firstTime)
1429
+ cursor = await cursor.continue();
1430
+ if (!cursor)
1431
+ break;
1432
+ firstTime = false;
1433
+ const r = cursor.value;
1434
+ if (args.since && args.since > r.updated_at)
1435
+ continue;
1436
+ if (args.partial) {
1437
+ if (args.partial.basketId && r.basketId !== args.partial.basketId)
1438
+ continue;
1439
+ if (args.partial.userId && r.userId !== args.partial.userId)
1440
+ continue;
1441
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
1442
+ continue;
1443
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
1444
+ continue;
1445
+ if (args.partial.name && r.name !== args.partial.name)
1446
+ continue;
1447
+ if (args.partial.numberOfDesiredUTXOs !== undefined &&
1448
+ r.numberOfDesiredUTXOs !== args.partial.numberOfDesiredUTXOs)
1449
+ continue;
1450
+ if (args.partial.minimumDesiredUTXOValue !== undefined &&
1451
+ r.numberOfDesiredSatoshis !== args.partial.minimumDesiredUTXOValue)
1452
+ continue;
1453
+ if (args.partial.isDeleted !== undefined && r.isDeleted !== args.partial.isDeleted)
1454
+ continue;
1455
+ }
1456
+ if (skipped < offset) {
1457
+ skipped++;
1458
+ continue;
1459
+ }
1460
+ filtered(r);
1461
+ count++;
1462
+ if (((_e = args.paged) === null || _e === void 0 ? void 0 : _e.limit) && count >= args.paged.limit)
1463
+ break;
1464
+ }
1465
+ if (!args.trx)
1466
+ await dbTrx.done;
1467
+ }
1468
+ async findOutputBaskets(args) {
1469
+ const result = [];
1470
+ await this.filterOutputBaskets(args, r => {
1471
+ result.push(this.validateEntity(r));
1472
+ });
1473
+ return result;
1474
+ }
1475
+ async filterOutputs(args, filtered, tagIds, isQueryModeAll) {
1476
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
1477
+ // args.txStatus
1478
+ // args.noScript
1479
+ if (args.partial.lockingScript)
1480
+ throw new index_client_1.sdk.WERR_INVALID_PARAMETER('args.partial.lockingScript', `undefined. Outputs may not be found by lockingScript value.`);
1481
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
1482
+ let skipped = 0;
1483
+ let count = 0;
1484
+ const stores = ['outputs'];
1485
+ if (tagIds && tagIds.length > 0) {
1486
+ stores.push('output_tags_map');
1487
+ }
1488
+ if (args.txStatus) {
1489
+ stores.push('transactions');
1490
+ }
1491
+ const dbTrx = this.toDbTrx(stores, 'readonly', args.trx);
1492
+ let cursor;
1493
+ if ((_b = args.partial) === null || _b === void 0 ? void 0 : _b.outputId) {
1494
+ cursor = await dbTrx.objectStore('outputs').openCursor(args.partial.outputId);
1495
+ }
1496
+ else if (((_c = args.partial) === null || _c === void 0 ? void 0 : _c.userId) !== undefined) {
1497
+ if (((_d = args.partial) === null || _d === void 0 ? void 0 : _d.transactionId) && ((_e = args.partial) === null || _e === void 0 ? void 0 : _e.vout) !== undefined) {
1498
+ cursor = await dbTrx
1499
+ .objectStore('outputs')
1500
+ .index('transactionId_vout_userId')
1501
+ .openCursor([args.partial.transactionId, args.partial.vout, args.partial.userId]);
1502
+ }
1503
+ else {
1504
+ cursor = await dbTrx.objectStore('outputs').index('userId').openCursor(args.partial.userId);
1505
+ }
1506
+ }
1507
+ else if (((_f = args.partial) === null || _f === void 0 ? void 0 : _f.transactionId) !== undefined) {
1508
+ cursor = await dbTrx.objectStore('outputs').index('transactionId').openCursor(args.partial.transactionId);
1509
+ }
1510
+ else if (((_g = args.partial) === null || _g === void 0 ? void 0 : _g.basketId) !== undefined) {
1511
+ cursor = await dbTrx.objectStore('outputs').index('basketId').openCursor(args.partial.basketId);
1512
+ }
1513
+ else if (((_h = args.partial) === null || _h === void 0 ? void 0 : _h.spentBy) !== undefined) {
1514
+ cursor = await dbTrx.objectStore('outputs').index('spentBy').openCursor(args.partial.spentBy);
1515
+ }
1516
+ else {
1517
+ cursor = await dbTrx.objectStore('outputs').openCursor();
1518
+ }
1519
+ let firstTime = true;
1520
+ while (cursor) {
1521
+ if (!firstTime)
1522
+ cursor = await cursor.continue();
1523
+ if (!cursor)
1524
+ break;
1525
+ firstTime = false;
1526
+ const r = cursor.value;
1527
+ if (args.since && args.since > r.updated_at)
1528
+ continue;
1529
+ if (args.partial) {
1530
+ if (args.partial.outputId && r.outputId !== args.partial.outputId)
1531
+ continue;
1532
+ if (args.partial.userId && r.userId !== args.partial.userId)
1533
+ continue;
1534
+ if (args.partial.transactionId && r.transactionId !== args.partial.transactionId)
1535
+ continue;
1536
+ if (args.partial.basketId && r.basketId !== args.partial.basketId)
1537
+ continue;
1538
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
1539
+ continue;
1540
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
1541
+ continue;
1542
+ if (args.partial.spendable !== undefined && r.spendable !== args.partial.spendable)
1543
+ continue;
1544
+ if (args.partial.change !== undefined && r.change !== args.partial.change)
1545
+ continue;
1546
+ if (args.partial.outputDescription && r.outputDescription !== args.partial.outputDescription)
1547
+ continue;
1548
+ if (args.partial.vout !== undefined && r.vout !== args.partial.vout)
1549
+ continue;
1550
+ if (args.partial.satoshis !== undefined && r.satoshis !== args.partial.satoshis)
1551
+ continue;
1552
+ if (args.partial.providedBy && r.providedBy !== args.partial.providedBy)
1553
+ continue;
1554
+ if (args.partial.purpose && r.purpose !== args.partial.purpose)
1555
+ continue;
1556
+ if (args.partial.type && r.type !== args.partial.type)
1557
+ continue;
1558
+ if (args.partial.txid && r.txid !== args.partial.txid)
1559
+ continue;
1560
+ if (args.partial.senderIdentityKey && r.senderIdentityKey !== args.partial.senderIdentityKey)
1561
+ continue;
1562
+ if (args.partial.derivationPrefix && r.derivationPrefix !== args.partial.derivationPrefix)
1563
+ continue;
1564
+ if (args.partial.derivationSuffix && r.derivationSuffix !== args.partial.derivationSuffix)
1565
+ continue;
1566
+ if (args.partial.customInstructions && r.customInstructions !== args.partial.customInstructions)
1567
+ continue;
1568
+ if (args.partial.spentBy && r.spentBy !== args.partial.spentBy)
1569
+ continue;
1570
+ if (args.partial.sequenceNumber !== undefined && r.sequenceNumber !== args.partial.sequenceNumber)
1571
+ continue;
1572
+ if (args.partial.scriptLength !== undefined && r.scriptLength !== args.partial.scriptLength)
1573
+ continue;
1574
+ if (args.partial.scriptOffset !== undefined && r.scriptOffset !== args.partial.scriptOffset)
1575
+ continue;
1576
+ }
1577
+ if (args.txStatus !== undefined) {
1578
+ const count = await this.countTransactions({
1579
+ partial: { transactionId: r.transactionId },
1580
+ status: args.txStatus,
1581
+ trx: dbTrx
1582
+ });
1583
+ if (count === 0)
1584
+ continue;
1585
+ }
1586
+ if (tagIds && tagIds.length > 0) {
1587
+ let ids = [...tagIds];
1588
+ await this.filterOutputTagMaps({ partial: { outputId: r.outputId }, trx: dbTrx }, tm => {
1589
+ if (ids.length > 0) {
1590
+ const i = ids.indexOf(tm.outputTagId);
1591
+ if (i >= 0) {
1592
+ if (isQueryModeAll) {
1593
+ ids.splice(i, 1);
1594
+ }
1595
+ else {
1596
+ ids = [];
1597
+ }
1598
+ }
1599
+ }
1600
+ });
1601
+ if (ids.length > 0)
1602
+ continue;
1603
+ }
1604
+ if (skipped < offset) {
1605
+ skipped++;
1606
+ continue;
1607
+ }
1608
+ if (args.noScript === true) {
1609
+ r.script = undefined;
1610
+ }
1611
+ filtered(r);
1612
+ count++;
1613
+ if (((_j = args.paged) === null || _j === void 0 ? void 0 : _j.limit) && count >= args.paged.limit)
1614
+ break;
1615
+ }
1616
+ if (!args.trx)
1617
+ await dbTrx.done;
1618
+ }
1619
+ async findOutputs(args, tagIds, isQueryModeAll) {
1620
+ const results = [];
1621
+ await this.filterOutputs(args, r => {
1622
+ results.push(this.validateEntity(r));
1623
+ }, tagIds, isQueryModeAll);
1624
+ for (const o of results) {
1625
+ if (!args.noScript) {
1626
+ await this.validateOutputScript(o);
1627
+ }
1628
+ else {
1629
+ o.lockingScript = undefined;
1630
+ }
1631
+ }
1632
+ return results;
1633
+ }
1634
+ async filterOutputTags(args, filtered) {
1635
+ var _a, _b, _c, _d, _e;
1636
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
1637
+ let skipped = 0;
1638
+ let count = 0;
1639
+ const dbTrx = this.toDbTrx(['output_tags'], 'readonly', args.trx);
1640
+ let cursor;
1641
+ if ((_b = args.partial) === null || _b === void 0 ? void 0 : _b.outputTagId) {
1642
+ cursor = await dbTrx.objectStore('output_tags').openCursor(args.partial.outputTagId);
1643
+ }
1644
+ else if (((_c = args.partial) === null || _c === void 0 ? void 0 : _c.userId) !== undefined) {
1645
+ if (((_d = args.partial) === null || _d === void 0 ? void 0 : _d.tag) !== undefined) {
1646
+ cursor = await dbTrx
1647
+ .objectStore('output_tags')
1648
+ .index('tag_userId')
1649
+ .openCursor([args.partial.tag, args.partial.userId]);
1650
+ }
1651
+ else {
1652
+ cursor = await dbTrx.objectStore('output_tags').index('userId').openCursor(args.partial.userId);
1653
+ }
1654
+ }
1655
+ else {
1656
+ cursor = await dbTrx.objectStore('output_tags').openCursor();
1657
+ }
1658
+ let firstTime = true;
1659
+ while (cursor) {
1660
+ if (!firstTime)
1661
+ cursor = await cursor.continue();
1662
+ if (!cursor)
1663
+ break;
1664
+ firstTime = false;
1665
+ const r = cursor.value;
1666
+ if (args.since && args.since > r.updated_at)
1667
+ continue;
1668
+ if (args.partial) {
1669
+ if (args.partial.outputTagId && r.outputTagId !== args.partial.outputTagId)
1670
+ continue;
1671
+ if (args.partial.userId && r.userId !== args.partial.userId)
1672
+ continue;
1673
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
1674
+ continue;
1675
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
1676
+ continue;
1677
+ if (args.partial.tag && r.tag !== args.partial.tag)
1678
+ continue;
1679
+ if (args.partial.isDeleted !== undefined && r.isDeleted !== args.partial.isDeleted)
1680
+ continue;
1681
+ }
1682
+ if (skipped < offset) {
1683
+ skipped++;
1684
+ continue;
1685
+ }
1686
+ filtered(r);
1687
+ count++;
1688
+ if (((_e = args.paged) === null || _e === void 0 ? void 0 : _e.limit) && count >= args.paged.limit)
1689
+ break;
1690
+ }
1691
+ if (!args.trx)
1692
+ await dbTrx.done;
1693
+ }
1694
+ async findOutputTags(args) {
1695
+ const result = [];
1696
+ await this.filterOutputTags(args, r => {
1697
+ result.push(this.validateEntity(r));
1698
+ });
1699
+ return result;
1700
+ }
1701
+ async filterSyncStates(args, filtered) {
1702
+ var _a, _b, _c, _d, _e, _f, _g;
1703
+ if (args.partial.syncMap)
1704
+ throw new index_client_1.sdk.WERR_INVALID_PARAMETER('args.partial.syncMap', `undefined. SyncStates may not be found by syncMap value.`);
1705
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
1706
+ let skipped = 0;
1707
+ let count = 0;
1708
+ const dbTrx = this.toDbTrx(['sync_states'], 'readonly', args.trx);
1709
+ let cursor;
1710
+ if ((_b = args.partial) === null || _b === void 0 ? void 0 : _b.syncStateId) {
1711
+ cursor = await dbTrx.objectStore('sync_states').openCursor(args.partial.syncStateId);
1712
+ }
1713
+ else if (((_c = args.partial) === null || _c === void 0 ? void 0 : _c.userId) !== undefined) {
1714
+ cursor = await dbTrx.objectStore('sync_states').index('userId').openCursor(args.partial.userId);
1715
+ }
1716
+ else if (((_d = args.partial) === null || _d === void 0 ? void 0 : _d.refNum) !== undefined) {
1717
+ cursor = await dbTrx.objectStore('sync_states').index('refNum').openCursor(args.partial.refNum);
1718
+ }
1719
+ else if (((_e = args.partial) === null || _e === void 0 ? void 0 : _e.status) !== undefined) {
1720
+ cursor = await dbTrx.objectStore('sync_states').index('status').openCursor(args.partial.status);
1721
+ }
1722
+ else {
1723
+ cursor = await dbTrx.objectStore('sync_states').openCursor();
1724
+ }
1725
+ let firstTime = true;
1726
+ while (cursor) {
1727
+ if (!firstTime)
1728
+ cursor = await cursor.continue();
1729
+ if (!cursor)
1730
+ break;
1731
+ firstTime = false;
1732
+ const r = cursor.value;
1733
+ if (args.since && args.since > r.updated_at)
1734
+ continue;
1735
+ if (args.partial) {
1736
+ if (args.partial.syncStateId && r.syncStateId !== args.partial.syncStateId)
1737
+ continue;
1738
+ if (args.partial.userId && r.userId !== args.partial.userId)
1739
+ continue;
1740
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
1741
+ continue;
1742
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
1743
+ continue;
1744
+ if (args.partial.storageIdentityKey && r.storageIdentityKey !== args.partial.storageIdentityKey)
1745
+ continue;
1746
+ if (args.partial.storageName && r.storageName !== args.partial.storageName)
1747
+ continue;
1748
+ if (args.partial.status && r.status !== args.partial.status)
1749
+ continue;
1750
+ if (args.partial.init !== undefined && r.init !== args.partial.init)
1751
+ continue;
1752
+ if (args.partial.refNum !== undefined && r.refNum !== args.partial.refNum)
1753
+ continue;
1754
+ if (args.partial.when && ((_f = r.when) === null || _f === void 0 ? void 0 : _f.getTime()) !== args.partial.when.getTime())
1755
+ continue;
1756
+ if (args.partial.satoshis !== undefined && r.satoshis !== args.partial.satoshis)
1757
+ continue;
1758
+ if (args.partial.errorLocal && r.errorLocale !== args.partial.errorLocal)
1759
+ continue;
1760
+ if (args.partial.errorOther && r.errorOther !== args.partial.errorOther)
1761
+ continue;
1762
+ }
1763
+ if (skipped < offset) {
1764
+ skipped++;
1765
+ continue;
1766
+ }
1767
+ filtered(r);
1768
+ count++;
1769
+ if (((_g = args.paged) === null || _g === void 0 ? void 0 : _g.limit) && count >= args.paged.limit)
1770
+ break;
1771
+ }
1772
+ if (!args.trx)
1773
+ await dbTrx.done;
1774
+ }
1775
+ async findSyncStates(args) {
1776
+ const result = [];
1777
+ await this.filterSyncStates(args, r => {
1778
+ result.push(this.validateEntity(r));
1779
+ });
1780
+ return result;
1781
+ }
1782
+ async filterTransactions(args, filtered, labelIds, isQueryModeAll) {
1783
+ var _a, _b, _c, _d, _e, _f, _g, _h;
1784
+ if (args.partial.rawTx)
1785
+ throw new index_client_1.sdk.WERR_INVALID_PARAMETER('args.partial.rawTx', `undefined. Transactions may not be found by rawTx value.`);
1786
+ if (args.partial.inputBEEF)
1787
+ throw new index_client_1.sdk.WERR_INVALID_PARAMETER('args.partial.inputBEEF', `undefined. Transactions may not be found by inputBEEF value.`);
1788
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
1789
+ let skipped = 0;
1790
+ let count = 0;
1791
+ const stores = ['transactions'];
1792
+ if (labelIds && labelIds.length > 0) {
1793
+ stores.push('tx_labels_map');
1794
+ }
1795
+ const dbTrx = this.toDbTrx(stores, 'readonly', args.trx);
1796
+ let cursor;
1797
+ if ((_b = args.partial) === null || _b === void 0 ? void 0 : _b.transactionId) {
1798
+ cursor = await dbTrx.objectStore('transactions').openCursor(args.partial.transactionId);
1799
+ }
1800
+ else if (((_c = args.partial) === null || _c === void 0 ? void 0 : _c.userId) !== undefined) {
1801
+ if (((_d = args.partial) === null || _d === void 0 ? void 0 : _d.status) !== undefined) {
1802
+ cursor = await dbTrx
1803
+ .objectStore('transactions')
1804
+ .index('status_userId')
1805
+ .openCursor([args.partial.status, args.partial.userId]);
1806
+ }
1807
+ else {
1808
+ cursor = await dbTrx.objectStore('transactions').index('userId').openCursor(args.partial.userId);
1809
+ }
1810
+ }
1811
+ else if (((_e = args.partial) === null || _e === void 0 ? void 0 : _e.status) !== undefined) {
1812
+ cursor = await dbTrx.objectStore('transactions').index('status').openCursor(args.partial.status);
1813
+ }
1814
+ else if (((_f = args.partial) === null || _f === void 0 ? void 0 : _f.provenTxId) !== undefined) {
1815
+ cursor = await dbTrx.objectStore('transactions').index('provenTxId').openCursor(args.partial.provenTxId);
1816
+ }
1817
+ else if (((_g = args.partial) === null || _g === void 0 ? void 0 : _g.reference) !== undefined) {
1818
+ cursor = await dbTrx.objectStore('transactions').index('reference').openCursor(args.partial.reference);
1819
+ }
1820
+ else {
1821
+ cursor = await dbTrx.objectStore('transactions').openCursor();
1822
+ }
1823
+ let firstTime = true;
1824
+ while (cursor) {
1825
+ if (!firstTime)
1826
+ cursor = await cursor.continue();
1827
+ if (!cursor)
1828
+ break;
1829
+ firstTime = false;
1830
+ const r = cursor.value;
1831
+ if (args.since && args.since > r.updated_at)
1832
+ continue;
1833
+ if (args.status && !args.status.includes(r.status))
1834
+ continue;
1835
+ if (args.partial) {
1836
+ if (args.partial.transactionId && r.transactionId !== args.partial.transactionId)
1837
+ continue;
1838
+ if (args.partial.userId && r.userId !== args.partial.userId)
1839
+ continue;
1840
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
1841
+ continue;
1842
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
1843
+ continue;
1844
+ if (args.partial.provenTxId && r.provenTxId !== args.partial.provenTxId)
1845
+ continue;
1846
+ if (args.partial.status && r.status !== args.partial.status)
1847
+ continue;
1848
+ if (args.partial.reference && r.reference !== args.partial.reference)
1849
+ continue;
1850
+ if (args.partial.isOutgoing !== undefined && r.isOutgoing !== args.partial.isOutgoing)
1851
+ continue;
1852
+ if (args.partial.satoshis !== undefined && r.satoshis !== args.partial.satoshis)
1853
+ continue;
1854
+ if (args.partial.description && r.description !== args.partial.description)
1855
+ continue;
1856
+ if (args.partial.version !== undefined && r.version !== args.partial.version)
1857
+ continue;
1858
+ if (args.partial.lockTime !== undefined && r.lockTime !== args.partial.lockTime)
1859
+ continue;
1860
+ if (args.partial.txid && r.txid !== args.partial.txid)
1861
+ continue;
1862
+ }
1863
+ if (labelIds && labelIds.length > 0) {
1864
+ let ids = [...labelIds];
1865
+ await this.filterTxLabelMaps({ partial: { transactionId: r.transactionId }, trx: dbTrx }, lm => {
1866
+ if (ids.length > 0) {
1867
+ const i = ids.indexOf(lm.txLabelId);
1868
+ if (i >= 0) {
1869
+ if (isQueryModeAll) {
1870
+ ids.splice(i, 1);
1871
+ }
1872
+ else {
1873
+ ids = [];
1874
+ }
1875
+ }
1876
+ }
1877
+ });
1878
+ if (ids.length > 0)
1879
+ continue;
1880
+ }
1881
+ if (skipped < offset) {
1882
+ skipped++;
1883
+ continue;
1884
+ }
1885
+ filtered(r);
1886
+ count++;
1887
+ if (((_h = args.paged) === null || _h === void 0 ? void 0 : _h.limit) && count >= args.paged.limit)
1888
+ break;
1889
+ }
1890
+ if (!args.trx)
1891
+ await dbTrx.done;
1892
+ }
1893
+ async findTransactions(args, labelIds, isQueryModeAll) {
1894
+ const results = [];
1895
+ await this.filterTransactions(args, r => {
1896
+ results.push(this.validateEntity(r));
1897
+ }, labelIds, isQueryModeAll);
1898
+ for (const t of results) {
1899
+ if (!args.noRawTx) {
1900
+ await this.validateRawTransaction(t, args.trx);
1901
+ }
1902
+ else {
1903
+ t.rawTx = undefined;
1904
+ t.inputBEEF = undefined;
1905
+ }
1906
+ }
1907
+ return results;
1908
+ }
1909
+ async filterTxLabels(args, filtered) {
1910
+ var _a, _b, _c, _d, _e;
1911
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
1912
+ let skipped = 0;
1913
+ let count = 0;
1914
+ const dbTrx = this.toDbTrx(['tx_labels'], 'readonly', args.trx);
1915
+ let cursor;
1916
+ if ((_b = args.partial) === null || _b === void 0 ? void 0 : _b.txLabelId) {
1917
+ cursor = await dbTrx.objectStore('tx_labels').openCursor(args.partial.txLabelId);
1918
+ }
1919
+ else if (((_c = args.partial) === null || _c === void 0 ? void 0 : _c.userId) !== undefined) {
1920
+ if (((_d = args.partial) === null || _d === void 0 ? void 0 : _d.label) !== undefined) {
1921
+ cursor = await dbTrx
1922
+ .objectStore('tx_labels')
1923
+ .index('label_userId')
1924
+ .openCursor([args.partial.label, args.partial.userId]);
1925
+ }
1926
+ else {
1927
+ cursor = await dbTrx.objectStore('tx_labels').index('userId').openCursor(args.partial.userId);
1928
+ }
1929
+ }
1930
+ else {
1931
+ cursor = await dbTrx.objectStore('tx_labels').openCursor();
1932
+ }
1933
+ let firstTime = true;
1934
+ while (cursor) {
1935
+ if (!firstTime)
1936
+ cursor = await cursor.continue();
1937
+ if (!cursor)
1938
+ break;
1939
+ firstTime = false;
1940
+ const r = cursor.value;
1941
+ if (args.since && args.since > r.updated_at)
1942
+ continue;
1943
+ if (args.partial) {
1944
+ if (args.partial.txLabelId && r.txLabelId !== args.partial.txLabelId)
1945
+ continue;
1946
+ if (args.partial.userId && r.userId !== args.partial.userId)
1947
+ continue;
1948
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
1949
+ continue;
1950
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
1951
+ continue;
1952
+ if (args.partial.label && r.label !== args.partial.label)
1953
+ continue;
1954
+ if (args.partial.isDeleted !== undefined && r.isDeleted !== args.partial.isDeleted)
1955
+ continue;
1956
+ }
1957
+ if (skipped < offset) {
1958
+ skipped++;
1959
+ continue;
1960
+ }
1961
+ filtered(r);
1962
+ count++;
1963
+ if (((_e = args.paged) === null || _e === void 0 ? void 0 : _e.limit) && count >= args.paged.limit)
1964
+ break;
1965
+ }
1966
+ if (!args.trx)
1967
+ await dbTrx.done;
1968
+ }
1969
+ async findTxLabels(args) {
1970
+ const result = [];
1971
+ await this.filterTxLabels(args, r => {
1972
+ result.push(this.validateEntity(r));
1973
+ });
1974
+ return result;
1975
+ }
1976
+ async filterUsers(args, filtered) {
1977
+ var _a, _b;
1978
+ const offset = ((_a = args.paged) === null || _a === void 0 ? void 0 : _a.offset) || 0;
1979
+ let skipped = 0;
1980
+ let count = 0;
1981
+ const dbTrx = this.toDbTrx(['users'], 'readonly', args.trx);
1982
+ let cursor = await dbTrx.objectStore('users').openCursor();
1983
+ let firstTime = true;
1984
+ while (cursor) {
1985
+ if (!firstTime)
1986
+ cursor = await cursor.continue();
1987
+ if (!cursor)
1988
+ break;
1989
+ firstTime = false;
1990
+ const r = cursor.value;
1991
+ if (args.since && args.since > r.updated_at)
1992
+ continue;
1993
+ if (args.partial) {
1994
+ if (args.partial.userId && r.userId !== args.partial.userId)
1995
+ continue;
1996
+ if (args.partial.created_at && r.created_at.getTime() !== args.partial.created_at.getTime())
1997
+ continue;
1998
+ if (args.partial.updated_at && r.updated_at.getTime() !== args.partial.updated_at.getTime())
1999
+ continue;
2000
+ if (args.partial.identityKey && r.identityKey !== args.partial.identityKey)
2001
+ continue;
2002
+ if (args.partial.activeStorage && r.activeStorage !== args.partial.activeStorage)
2003
+ continue;
2004
+ }
2005
+ if (skipped < offset) {
2006
+ skipped++;
2007
+ continue;
2008
+ }
2009
+ filtered(r);
2010
+ count++;
2011
+ if (((_b = args.paged) === null || _b === void 0 ? void 0 : _b.limit) && count >= args.paged.limit)
2012
+ break;
2013
+ }
2014
+ if (!args.trx)
2015
+ await dbTrx.done;
2016
+ }
2017
+ async findUsers(args) {
2018
+ const result = [];
2019
+ await this.filterUsers(args, r => {
2020
+ result.push(this.validateEntity(r));
2021
+ });
2022
+ return result;
2023
+ }
2024
+ async countCertificateFields(args) {
2025
+ let count = 0;
2026
+ await this.filterCertificateFields(args, () => {
2027
+ count++;
2028
+ });
2029
+ return count;
2030
+ }
2031
+ async countCertificates(args) {
2032
+ let count = 0;
2033
+ await this.filterCertificates(args, () => {
2034
+ count++;
2035
+ });
2036
+ return count;
2037
+ }
2038
+ async countCommissions(args) {
2039
+ let count = 0;
2040
+ await this.filterCommissions(args, () => {
2041
+ count++;
2042
+ });
2043
+ return count;
2044
+ }
2045
+ async countMonitorEvents(args) {
2046
+ let count = 0;
2047
+ await this.filterMonitorEvents(args, () => {
2048
+ count++;
2049
+ });
2050
+ return count;
2051
+ }
2052
+ async countOutputBaskets(args) {
2053
+ let count = 0;
2054
+ await this.filterOutputBaskets(args, () => {
2055
+ count++;
2056
+ });
2057
+ return count;
2058
+ }
2059
+ async countOutputs(args, tagIds, isQueryModeAll) {
2060
+ let count = 0;
2061
+ await this.filterOutputs({ ...args, noScript: true }, () => {
2062
+ count++;
2063
+ }, tagIds, isQueryModeAll);
2064
+ return count;
2065
+ }
2066
+ async countOutputTags(args) {
2067
+ let count = 0;
2068
+ await this.filterOutputTags(args, () => {
2069
+ count++;
2070
+ });
2071
+ return count;
2072
+ }
2073
+ async countSyncStates(args) {
2074
+ let count = 0;
2075
+ await this.filterSyncStates(args, () => {
2076
+ count++;
2077
+ });
2078
+ return count;
2079
+ }
2080
+ async countTransactions(args, labelIds, isQueryModeAll) {
2081
+ let count = 0;
2082
+ await this.filterTransactions({ ...args, noRawTx: true }, () => {
2083
+ count++;
2084
+ }, labelIds, isQueryModeAll);
2085
+ return count;
2086
+ }
2087
+ async countTxLabels(args) {
2088
+ let count = 0;
2089
+ await this.filterTxLabels(args, () => {
2090
+ count++;
2091
+ });
2092
+ return count;
2093
+ }
2094
+ async countUsers(args) {
2095
+ let count = 0;
2096
+ await this.filterUsers(args, () => {
2097
+ count++;
2098
+ });
2099
+ return count;
2100
+ }
2101
+ async getProvenTxsForUser(args) {
2102
+ const results = [];
2103
+ const fargs = {
2104
+ partial: {},
2105
+ since: args.since,
2106
+ paged: args.paged,
2107
+ trx: args.trx
2108
+ };
2109
+ await this.filterProvenTxs(fargs, r => {
2110
+ results.push(this.validateEntity(r));
2111
+ }, args.userId);
2112
+ return results;
2113
+ }
2114
+ async getProvenTxReqsForUser(args) {
2115
+ const results = [];
2116
+ const fargs = {
2117
+ partial: {},
2118
+ since: args.since,
2119
+ paged: args.paged,
2120
+ trx: args.trx
2121
+ };
2122
+ await this.filterProvenTxReqs(fargs, r => {
2123
+ results.push(this.validateEntity(r));
2124
+ }, args.userId);
2125
+ return results;
2126
+ }
2127
+ async getTxLabelMapsForUser(args) {
2128
+ const results = [];
2129
+ const fargs = {
2130
+ partial: {},
2131
+ since: args.since,
2132
+ paged: args.paged,
2133
+ trx: args.trx
2134
+ };
2135
+ await this.filterTxLabelMaps(fargs, r => {
2136
+ results.push(this.validateEntity(r));
2137
+ }, args.userId);
2138
+ return results;
2139
+ }
2140
+ async getOutputTagMapsForUser(args) {
2141
+ const results = [];
2142
+ const fargs = {
2143
+ partial: {},
2144
+ since: args.since,
2145
+ paged: args.paged,
2146
+ trx: args.trx
2147
+ };
2148
+ await this.filterOutputTagMaps(fargs, r => {
2149
+ results.push(this.validateEntity(r));
2150
+ }, args.userId);
2151
+ return results;
2152
+ }
2153
+ async verifyReadyForDatabaseAccess(trx) {
2154
+ if (!this._settings) {
2155
+ this._settings = await this.readSettings();
2156
+ }
2157
+ return this._settings.dbtype;
2158
+ }
2159
+ /**
2160
+ * Helper to force uniform behavior across database engines.
2161
+ * Use to process all individual records with time stamps or number[] retreived from database.
2162
+ */
2163
+ validateEntity(entity, dateFields, booleanFields) {
2164
+ entity.created_at = this.validateDate(entity.created_at);
2165
+ entity.updated_at = this.validateDate(entity.updated_at);
2166
+ if (dateFields) {
2167
+ for (const df of dateFields) {
2168
+ if (entity[df])
2169
+ entity[df] = this.validateDate(entity[df]);
2170
+ }
2171
+ }
2172
+ if (booleanFields) {
2173
+ for (const df of booleanFields) {
2174
+ if (entity[df] !== undefined)
2175
+ entity[df] = !!entity[df];
2176
+ }
2177
+ }
2178
+ for (const key of Object.keys(entity)) {
2179
+ const val = entity[key];
2180
+ if (val === null) {
2181
+ entity[key] = undefined;
2182
+ }
2183
+ else if (Buffer.isBuffer(val) || val instanceof Uint8Array) {
2184
+ entity[key] = Array.from(val);
2185
+ }
2186
+ }
2187
+ return entity;
2188
+ }
2189
+ /**
2190
+ * Helper to force uniform behavior across database engines.
2191
+ * Use to process all arrays of records with time stamps retreived from database.
2192
+ * @returns input `entities` array with contained values validated.
2193
+ */
2194
+ validateEntities(entities, dateFields, booleanFields) {
2195
+ for (let i = 0; i < entities.length; i++) {
2196
+ entities[i] = this.validateEntity(entities[i], dateFields, booleanFields);
2197
+ }
2198
+ return entities;
2199
+ }
2200
+ /**
2201
+ * Helper to force uniform behavior across database engines.
2202
+ * Use to process the update template for entities being updated.
2203
+ */
2204
+ validatePartialForUpdate(update, dateFields, booleanFields) {
2205
+ if (!this.dbtype)
2206
+ throw new index_client_1.sdk.WERR_INTERNAL('must call verifyReadyForDatabaseAccess first');
2207
+ const v = update;
2208
+ if (v.created_at)
2209
+ v.created_at = this.validateEntityDate(v.created_at);
2210
+ if (v.updated_at)
2211
+ v.updated_at = this.validateEntityDate(v.updated_at);
2212
+ if (!v.created_at)
2213
+ delete v.created_at;
2214
+ if (!v.updated_at)
2215
+ v.updated_at = this.validateEntityDate(new Date());
2216
+ if (dateFields) {
2217
+ for (const df of dateFields) {
2218
+ if (v[df])
2219
+ v[df] = this.validateOptionalEntityDate(v[df]);
2220
+ }
2221
+ }
2222
+ if (booleanFields) {
2223
+ for (const df of booleanFields) {
2224
+ if (update[df] !== undefined)
2225
+ update[df] = !!update[df] ? 1 : 0;
2226
+ }
2227
+ }
2228
+ for (const key of Object.keys(v)) {
2229
+ const val = v[key];
2230
+ if (Array.isArray(val) && (val.length === 0 || typeof val[0] === 'number')) {
2231
+ v[key] = Buffer.from(val);
2232
+ }
2233
+ else if (val === undefined) {
2234
+ v[key] = null;
2235
+ }
2236
+ }
2237
+ this.isDirty = true;
2238
+ return v;
2239
+ }
2240
+ /**
2241
+ * Helper to force uniform behavior across database engines.
2242
+ * Use to process new entities being inserted into the database.
2243
+ */
2244
+ async validateEntityForInsert(entity, trx, dateFields, booleanFields) {
2245
+ await this.verifyReadyForDatabaseAccess(trx);
2246
+ const v = { ...entity };
2247
+ v.created_at = this.validateOptionalEntityDate(v.created_at, true);
2248
+ v.updated_at = this.validateOptionalEntityDate(v.updated_at, true);
2249
+ if (!v.created_at)
2250
+ delete v.created_at;
2251
+ if (!v.updated_at)
2252
+ delete v.updated_at;
2253
+ if (dateFields) {
2254
+ for (const df of dateFields) {
2255
+ if (v[df])
2256
+ v[df] = this.validateOptionalEntityDate(v[df]);
2257
+ }
2258
+ }
2259
+ if (booleanFields) {
2260
+ for (const df of booleanFields) {
2261
+ if (entity[df] !== undefined)
2262
+ entity[df] = !!entity[df] ? 1 : 0;
2263
+ }
2264
+ }
2265
+ for (const key of Object.keys(v)) {
2266
+ const val = v[key];
2267
+ if (Array.isArray(val) && (val.length === 0 || typeof val[0] === 'number')) {
2268
+ v[key] = Buffer.from(val);
2269
+ }
2270
+ else if (val === undefined) {
2271
+ v[key] = null;
2272
+ }
2273
+ }
2274
+ this.isDirty = true;
2275
+ return v;
2276
+ }
2277
+ async validateRawTransaction(t, trx) {
2278
+ // if there is no txid or there is a rawTransaction return what we have.
2279
+ if (t.rawTx || !t.txid)
2280
+ return;
2281
+ // rawTransaction is missing, see if we moved it ...
2282
+ const rawTx = await this.getRawTxOfKnownValidTransaction(t.txid, undefined, undefined, trx);
2283
+ if (!rawTx)
2284
+ return;
2285
+ t.rawTx = rawTx;
2286
+ }
2287
+ }
2288
+ exports.StorageIdb = StorageIdb;
2289
+ //# sourceMappingURL=StorageIdb.js.map