@bsv/wallet-toolbox 1.6.2 → 1.6.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 (85) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/docs/client.md +315 -2500
  3. package/docs/services.md +15 -4
  4. package/docs/wallet.md +315 -2500
  5. package/mobile/out/src/services/chaintracker/chaintracks/Ingest/BulkIngestorCDN.d.ts.map +1 -1
  6. package/mobile/out/src/services/chaintracker/chaintracks/Ingest/BulkIngestorCDN.js +1 -2
  7. package/mobile/out/src/services/chaintracker/chaintracks/Ingest/BulkIngestorCDN.js.map +1 -1
  8. package/mobile/out/src/services/chaintracker/chaintracks/index.client.d.ts +0 -3
  9. package/mobile/out/src/services/chaintracker/chaintracks/index.client.d.ts.map +1 -1
  10. package/mobile/out/src/services/chaintracker/chaintracks/index.client.js +0 -3
  11. package/mobile/out/src/services/chaintracker/chaintracks/index.client.js.map +1 -1
  12. package/mobile/out/src/services/chaintracker/chaintracks/util/BulkFileDataManager.d.ts.map +1 -1
  13. package/mobile/out/src/services/chaintracker/chaintracks/util/BulkFileDataManager.js +2 -3
  14. package/mobile/out/src/services/chaintracker/chaintracks/util/BulkFileDataManager.js.map +1 -1
  15. package/mobile/package-lock.json +2 -2
  16. package/mobile/package.json +1 -1
  17. package/out/src/services/chaintracker/chaintracks/Ingest/BulkIngestorCDN.d.ts.map +1 -1
  18. package/out/src/services/chaintracker/chaintracks/Ingest/BulkIngestorCDN.js +1 -2
  19. package/out/src/services/chaintracker/chaintracks/Ingest/BulkIngestorCDN.js.map +1 -1
  20. package/out/src/services/chaintracker/chaintracks/index.all.d.ts +3 -0
  21. package/out/src/services/chaintracker/chaintracks/index.all.d.ts.map +1 -1
  22. package/out/src/services/chaintracker/chaintracks/index.all.js +3 -0
  23. package/out/src/services/chaintracker/chaintracks/index.all.js.map +1 -1
  24. package/out/src/services/chaintracker/chaintracks/index.client.d.ts +0 -3
  25. package/out/src/services/chaintracker/chaintracks/index.client.d.ts.map +1 -1
  26. package/out/src/services/chaintracker/chaintracks/index.client.js +0 -3
  27. package/out/src/services/chaintracker/chaintracks/index.client.js.map +1 -1
  28. package/out/src/services/chaintracker/chaintracks/util/BulkFileDataManager.d.ts.map +1 -1
  29. package/out/src/services/chaintracker/chaintracks/util/BulkFileDataManager.js +2 -3
  30. package/out/src/services/chaintracker/chaintracks/util/BulkFileDataManager.js.map +1 -1
  31. package/out/src/storage/__test/getBeefForTransaction.test.js.map +1 -1
  32. package/out/test/storage/idb/update.test.js +1 -1
  33. package/out/test/storage/idb/update.test.js.map +1 -1
  34. package/out/tsconfig.all.tsbuildinfo +1 -1
  35. package/package.json +1 -1
  36. package/src/services/chaintracker/chaintracks/Ingest/BulkIngestorCDN.ts +1 -2
  37. package/src/services/chaintracker/chaintracks/index.all.ts +4 -0
  38. package/src/services/chaintracker/chaintracks/index.client.ts +0 -3
  39. package/src/services/chaintracker/chaintracks/util/BulkFileDataManager.ts +2 -3
  40. package/src/storage/__test/getBeefForTransaction.test.ts +2 -5
  41. package/test/storage/idb/update.test.ts +1 -1
  42. package/mobile/out/src/Setup.d.ts +0 -267
  43. package/mobile/out/src/Setup.d.ts.map +0 -1
  44. package/mobile/out/src/Setup.js +0 -408
  45. package/mobile/out/src/Setup.js.map +0 -1
  46. package/mobile/out/src/services/chaintracker/chaintracks/ChaintracksService.d.ts +0 -30
  47. package/mobile/out/src/services/chaintracker/chaintracks/ChaintracksService.d.ts.map +0 -1
  48. package/mobile/out/src/services/chaintracker/chaintracks/ChaintracksService.js +0 -149
  49. package/mobile/out/src/services/chaintracker/chaintracks/ChaintracksService.js.map +0 -1
  50. package/mobile/out/src/services/chaintracker/chaintracks/Ingest/BulkIngestorWhatsOnChainWs.d.ts +0 -23
  51. package/mobile/out/src/services/chaintracker/chaintracks/Ingest/BulkIngestorWhatsOnChainWs.d.ts.map +0 -1
  52. package/mobile/out/src/services/chaintracker/chaintracks/Ingest/BulkIngestorWhatsOnChainWs.js +0 -57
  53. package/mobile/out/src/services/chaintracker/chaintracks/Ingest/BulkIngestorWhatsOnChainWs.js.map +0 -1
  54. package/mobile/out/src/services/chaintracker/chaintracks/Ingest/LiveIngestorWhatsOnChainWs.d.ts +0 -16
  55. package/mobile/out/src/services/chaintracker/chaintracks/Ingest/LiveIngestorWhatsOnChainWs.d.ts.map +0 -1
  56. package/mobile/out/src/services/chaintracker/chaintracks/Ingest/LiveIngestorWhatsOnChainWs.js +0 -53
  57. package/mobile/out/src/services/chaintracker/chaintracks/Ingest/LiveIngestorWhatsOnChainWs.js.map +0 -1
  58. package/mobile/out/src/storage/StorageKnex.d.ts +0 -179
  59. package/mobile/out/src/storage/StorageKnex.d.ts.map +0 -1
  60. package/mobile/out/src/storage/StorageKnex.js +0 -1215
  61. package/mobile/out/src/storage/StorageKnex.js.map +0 -1
  62. package/mobile/out/src/storage/methods/listActionsKnex.d.ts +0 -6
  63. package/mobile/out/src/storage/methods/listActionsKnex.d.ts.map +0 -1
  64. package/mobile/out/src/storage/methods/listActionsKnex.js +0 -198
  65. package/mobile/out/src/storage/methods/listActionsKnex.js.map +0 -1
  66. package/mobile/out/src/storage/methods/listOutputsKnex.d.ts +0 -6
  67. package/mobile/out/src/storage/methods/listOutputsKnex.d.ts.map +0 -1
  68. package/mobile/out/src/storage/methods/listOutputsKnex.js +0 -241
  69. package/mobile/out/src/storage/methods/listOutputsKnex.js.map +0 -1
  70. package/mobile/out/src/storage/methods/purgeData.d.ts +0 -4
  71. package/mobile/out/src/storage/methods/purgeData.d.ts.map +0 -1
  72. package/mobile/out/src/storage/methods/purgeData.js +0 -207
  73. package/mobile/out/src/storage/methods/purgeData.js.map +0 -1
  74. package/mobile/out/src/storage/methods/reviewStatus.d.ts +0 -20
  75. package/mobile/out/src/storage/methods/reviewStatus.d.ts.map +0 -1
  76. package/mobile/out/src/storage/methods/reviewStatus.js +0 -84
  77. package/mobile/out/src/storage/methods/reviewStatus.js.map +0 -1
  78. package/mobile/out/src/storage/schema/KnexMigrations.d.ts +0 -39
  79. package/mobile/out/src/storage/schema/KnexMigrations.d.ts.map +0 -1
  80. package/mobile/out/src/storage/schema/KnexMigrations.js +0 -410
  81. package/mobile/out/src/storage/schema/KnexMigrations.js.map +0 -1
  82. package/mobile/out/test/utils/TestUtilsWalletStorage.d.ts +0 -522
  83. package/mobile/out/test/utils/TestUtilsWalletStorage.d.ts.map +0 -1
  84. package/mobile/out/test/utils/TestUtilsWalletStorage.js +0 -1956
  85. package/mobile/out/test/utils/TestUtilsWalletStorage.js.map +0 -1
@@ -1,1215 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.StorageKnex = void 0;
4
- const tables_1 = require("./schema/tables");
5
- const KnexMigrations_1 = require("./schema/KnexMigrations");
6
- const StorageProvider_1 = require("./StorageProvider");
7
- const purgeData_1 = require("./methods/purgeData");
8
- const listActionsKnex_1 = require("./methods/listActionsKnex");
9
- const listOutputsKnex_1 = require("./methods/listOutputsKnex");
10
- const reviewStatus_1 = require("./methods/reviewStatus");
11
- const WERR_errors_1 = require("../sdk/WERR_errors");
12
- const utilityHelpers_1 = require("../utility/utilityHelpers");
13
- class StorageKnex extends StorageProvider_1.StorageProvider {
14
- constructor(options) {
15
- super(options);
16
- this._verifiedReadyForDatabaseAccess = false;
17
- if (!options.knex)
18
- throw new WERR_errors_1.WERR_INVALID_PARAMETER('options.knex', `valid`);
19
- this.knex = options.knex;
20
- }
21
- async readSettings() {
22
- return this.validateEntity((0, utilityHelpers_1.verifyOne)(await this.toDb(undefined)('settings')));
23
- }
24
- async getProvenOrRawTx(txid, trx) {
25
- const k = this.toDb(trx);
26
- const r = {
27
- proven: undefined,
28
- rawTx: undefined,
29
- inputBEEF: undefined
30
- };
31
- r.proven = (0, utilityHelpers_1.verifyOneOrNone)(await this.findProvenTxs({ partial: { txid: txid } }));
32
- if (!r.proven) {
33
- const reqRawTx = (0, utilityHelpers_1.verifyOneOrNone)(await k('proven_tx_reqs')
34
- .where('txid', txid)
35
- .whereIn('status', ['unsent', 'unmined', 'unconfirmed', 'sending', 'nosend', 'completed'])
36
- .select('rawTx', 'inputBEEF'));
37
- if (reqRawTx) {
38
- r.rawTx = Array.from(reqRawTx.rawTx);
39
- r.inputBEEF = Array.from(reqRawTx.inputBEEF);
40
- }
41
- }
42
- return r;
43
- }
44
- dbTypeSubstring(source, fromOffset, forLength) {
45
- if (this.dbtype === 'MySQL')
46
- return `substring(${source} from ${fromOffset} for ${forLength})`;
47
- return `substr(${source}, ${fromOffset}, ${forLength})`;
48
- }
49
- async getRawTxOfKnownValidTransaction(txid, offset, length, trx) {
50
- if (!txid)
51
- return undefined;
52
- if (!this.isAvailable())
53
- await this.makeAvailable();
54
- let rawTx = undefined;
55
- if (Number.isInteger(offset) && Number.isInteger(length)) {
56
- let rs = await this.toDb(trx).raw(`select ${this.dbTypeSubstring('rawTx', offset + 1, length)} as rawTx from proven_txs where txid = '${txid}'`);
57
- if (this.dbtype === 'MySQL')
58
- rs = rs[0];
59
- const r = (0, utilityHelpers_1.verifyOneOrNone)(rs);
60
- if (r && r.rawTx) {
61
- rawTx = Array.from(r.rawTx);
62
- }
63
- else {
64
- let rs = await this.toDb(trx).raw(`select ${this.dbTypeSubstring('rawTx', offset + 1, length)} as rawTx from proven_tx_reqs where txid = '${txid}' and status in ('unsent', 'nosend', 'sending', 'unmined', 'completed', 'unfail')`);
65
- if (this.dbtype === 'MySQL')
66
- rs = rs[0];
67
- const r = (0, utilityHelpers_1.verifyOneOrNone)(rs);
68
- if (r && r.rawTx) {
69
- rawTx = Array.from(r.rawTx);
70
- }
71
- }
72
- }
73
- else {
74
- const r = await this.getProvenOrRawTx(txid, trx);
75
- if (r.proven)
76
- rawTx = r.proven.rawTx;
77
- else
78
- rawTx = r.rawTx;
79
- }
80
- return rawTx;
81
- }
82
- getProvenTxsForUserQuery(args) {
83
- const k = this.toDb(args.trx);
84
- let q = k('proven_txs').where(function () {
85
- this.whereExists(k
86
- .select('*')
87
- .from('transactions')
88
- .whereRaw(`proven_txs.provenTxId = transactions.provenTxId and transactions.userId = ${args.userId}`));
89
- });
90
- if (args.paged) {
91
- q = q.limit(args.paged.limit);
92
- q = q.offset(args.paged.offset || 0);
93
- }
94
- if (args.since)
95
- q = q.where('updated_at', '>=', this.validateDateForWhere(args.since));
96
- return q;
97
- }
98
- async getProvenTxsForUser(args) {
99
- const q = this.getProvenTxsForUserQuery(args);
100
- const rs = await q;
101
- return this.validateEntities(rs);
102
- }
103
- getProvenTxReqsForUserQuery(args) {
104
- const k = this.toDb(args.trx);
105
- let q = k('proven_tx_reqs').where(function () {
106
- this.whereExists(k
107
- .select('*')
108
- .from('transactions')
109
- .whereRaw(`proven_tx_reqs.txid = transactions.txid and transactions.userId = ${args.userId}`));
110
- });
111
- if (args.paged) {
112
- q = q.limit(args.paged.limit);
113
- q = q.offset(args.paged.offset || 0);
114
- }
115
- if (args.since)
116
- q = q.where('updated_at', '>=', this.validateDateForWhere(args.since));
117
- return q;
118
- }
119
- async getProvenTxReqsForUser(args) {
120
- const q = this.getProvenTxReqsForUserQuery(args);
121
- const rs = await q;
122
- return this.validateEntities(rs, undefined, ['notified']);
123
- }
124
- getTxLabelMapsForUserQuery(args) {
125
- const k = this.toDb(args.trx);
126
- let q = k('tx_labels_map').whereExists(k
127
- .select('*')
128
- .from('tx_labels')
129
- .whereRaw(`tx_labels.txLabelId = tx_labels_map.txLabelId and tx_labels.userId = ${args.userId}`));
130
- if (args.since)
131
- q = q.where('updated_at', '>=', this.validateDateForWhere(args.since));
132
- if (args.paged) {
133
- q = q.limit(args.paged.limit);
134
- q = q.offset(args.paged.offset || 0);
135
- }
136
- return q;
137
- }
138
- async getTxLabelMapsForUser(args) {
139
- const q = this.getTxLabelMapsForUserQuery(args);
140
- const rs = await q;
141
- return this.validateEntities(rs, undefined, ['isDeleted']);
142
- }
143
- getOutputTagMapsForUserQuery(args) {
144
- const k = this.toDb(args.trx);
145
- let q = k('output_tags_map').whereExists(k
146
- .select('*')
147
- .from('output_tags')
148
- .whereRaw(`output_tags.outputTagId = output_tags_map.outputTagId and output_tags.userId = ${args.userId}`));
149
- if (args.since)
150
- q = q.where('updated_at', '>=', this.validateDateForWhere(args.since));
151
- if (args.paged) {
152
- q = q.limit(args.paged.limit);
153
- q = q.offset(args.paged.offset || 0);
154
- }
155
- return q;
156
- }
157
- async getOutputTagMapsForUser(args) {
158
- const q = this.getOutputTagMapsForUserQuery(args);
159
- const rs = await q;
160
- return this.validateEntities(rs, undefined, ['isDeleted']);
161
- }
162
- async listActions(auth, vargs) {
163
- if (!auth.userId)
164
- throw new WERR_errors_1.WERR_UNAUTHORIZED();
165
- return await (0, listActionsKnex_1.listActions)(this, auth, vargs);
166
- }
167
- async listOutputs(auth, vargs) {
168
- if (!auth.userId)
169
- throw new WERR_errors_1.WERR_UNAUTHORIZED();
170
- return await (0, listOutputsKnex_1.listOutputs)(this, auth, vargs);
171
- }
172
- async insertProvenTx(tx, trx) {
173
- const e = await this.validateEntityForInsert(tx, trx);
174
- if (e.provenTxId === 0)
175
- delete e.provenTxId;
176
- const [id] = await this.toDb(trx)('proven_txs').insert(e);
177
- tx.provenTxId = id;
178
- return tx.provenTxId;
179
- }
180
- async insertProvenTxReq(tx, trx) {
181
- const e = await this.validateEntityForInsert(tx, trx);
182
- if (e.provenTxReqId === 0)
183
- delete e.provenTxReqId;
184
- const [id] = await this.toDb(trx)('proven_tx_reqs').insert(e);
185
- tx.provenTxReqId = id;
186
- return tx.provenTxReqId;
187
- }
188
- async insertUser(user, trx) {
189
- const e = await this.validateEntityForInsert(user, trx);
190
- if (e.userId === 0)
191
- delete e.userId;
192
- const [id] = await this.toDb(trx)('users').insert(e);
193
- user.userId = id;
194
- return user.userId;
195
- }
196
- async insertCertificateAuth(auth, certificate) {
197
- if (!auth.userId || (certificate.userId && certificate.userId !== auth.userId))
198
- throw new WERR_errors_1.WERR_UNAUTHORIZED();
199
- certificate.userId = auth.userId;
200
- return await this.insertCertificate(certificate);
201
- }
202
- async insertCertificate(certificate, trx) {
203
- const e = await this.validateEntityForInsert(certificate, trx, undefined, ['isDeleted']);
204
- const fields = e.fields;
205
- if (e.fields)
206
- delete e.fields;
207
- if (e.certificateId === 0)
208
- delete e.certificateId;
209
- const [id] = await this.toDb(trx)('certificates').insert(e);
210
- certificate.certificateId = id;
211
- if (fields) {
212
- for (const field of fields) {
213
- field.certificateId = id;
214
- field.userId = certificate.userId;
215
- await this.insertCertificateField(field, trx);
216
- }
217
- }
218
- return certificate.certificateId;
219
- }
220
- async insertCertificateField(certificateField, trx) {
221
- const e = await this.validateEntityForInsert(certificateField, trx);
222
- await this.toDb(trx)('certificate_fields').insert(e);
223
- }
224
- async insertOutputBasket(basket, trx) {
225
- const e = await this.validateEntityForInsert(basket, trx, undefined, ['isDeleted']);
226
- if (e.basketId === 0)
227
- delete e.basketId;
228
- const [id] = await this.toDb(trx)('output_baskets').insert(e);
229
- basket.basketId = id;
230
- return basket.basketId;
231
- }
232
- async insertTransaction(tx, trx) {
233
- const e = await this.validateEntityForInsert(tx, trx);
234
- if (e.transactionId === 0)
235
- delete e.transactionId;
236
- const [id] = await this.toDb(trx)('transactions').insert(e);
237
- tx.transactionId = id;
238
- return tx.transactionId;
239
- }
240
- async insertCommission(commission, trx) {
241
- const e = await this.validateEntityForInsert(commission, trx);
242
- if (e.commissionId === 0)
243
- delete e.commissionId;
244
- const [id] = await this.toDb(trx)('commissions').insert(e);
245
- commission.commissionId = id;
246
- return commission.commissionId;
247
- }
248
- async insertOutput(output, trx) {
249
- try {
250
- const e = await this.validateEntityForInsert(output, trx);
251
- if (e.outputId === 0)
252
- delete e.outputId;
253
- const [id] = await this.toDb(trx)('outputs').insert(e);
254
- output.outputId = id;
255
- return output.outputId;
256
- }
257
- catch (e) {
258
- throw e;
259
- }
260
- }
261
- async insertOutputTag(tag, trx) {
262
- const e = await this.validateEntityForInsert(tag, trx, undefined, ['isDeleted']);
263
- if (e.outputTagId === 0)
264
- delete e.outputTagId;
265
- const [id] = await this.toDb(trx)('output_tags').insert(e);
266
- tag.outputTagId = id;
267
- return tag.outputTagId;
268
- }
269
- async insertOutputTagMap(tagMap, trx) {
270
- const e = await this.validateEntityForInsert(tagMap, trx, undefined, ['isDeleted']);
271
- const [id] = await this.toDb(trx)('output_tags_map').insert(e);
272
- }
273
- async insertTxLabel(label, trx) {
274
- const e = await this.validateEntityForInsert(label, trx, undefined, ['isDeleted']);
275
- if (e.txLabelId === 0)
276
- delete e.txLabelId;
277
- const [id] = await this.toDb(trx)('tx_labels').insert(e);
278
- label.txLabelId = id;
279
- return label.txLabelId;
280
- }
281
- async insertTxLabelMap(labelMap, trx) {
282
- const e = await this.validateEntityForInsert(labelMap, trx, undefined, ['isDeleted']);
283
- const [id] = await this.toDb(trx)('tx_labels_map').insert(e);
284
- }
285
- async insertMonitorEvent(event, trx) {
286
- const e = await this.validateEntityForInsert(event, trx);
287
- if (e.id === 0)
288
- delete e.id;
289
- const [id] = await this.toDb(trx)('monitor_events').insert(e);
290
- event.id = id;
291
- return event.id;
292
- }
293
- async insertSyncState(syncState, trx) {
294
- const e = await this.validateEntityForInsert(syncState, trx, ['when'], ['init']);
295
- if (e.syncStateId === 0)
296
- delete e.syncStateId;
297
- const [id] = await this.toDb(trx)('sync_states').insert(e);
298
- syncState.syncStateId = id;
299
- return syncState.syncStateId;
300
- }
301
- async updateCertificateField(certificateId, fieldName, update, trx) {
302
- await this.verifyReadyForDatabaseAccess(trx);
303
- return await this.toDb(trx)('certificate_fields')
304
- .where({ certificateId, fieldName })
305
- .update(this.validatePartialForUpdate(update));
306
- }
307
- async updateCertificate(id, update, trx) {
308
- await this.verifyReadyForDatabaseAccess(trx);
309
- return await this.toDb(trx)('certificates')
310
- .where({ certificateId: id })
311
- .update(this.validatePartialForUpdate(update, undefined, ['isDeleted']));
312
- }
313
- async updateCommission(id, update, trx) {
314
- await this.verifyReadyForDatabaseAccess(trx);
315
- return await this.toDb(trx)('commissions')
316
- .where({ commissionId: id })
317
- .update(this.validatePartialForUpdate(update));
318
- }
319
- async updateOutputBasket(id, update, trx) {
320
- await this.verifyReadyForDatabaseAccess(trx);
321
- return await this.toDb(trx)('output_baskets')
322
- .where({ basketId: id })
323
- .update(this.validatePartialForUpdate(update, undefined, ['isDeleted']));
324
- }
325
- async updateOutput(id, update, trx) {
326
- await this.verifyReadyForDatabaseAccess(trx);
327
- return await this.toDb(trx)('outputs')
328
- .where({ outputId: id })
329
- .update(this.validatePartialForUpdate(update));
330
- }
331
- async updateOutputTagMap(outputId, tagId, update, trx) {
332
- await this.verifyReadyForDatabaseAccess(trx);
333
- return await this.toDb(trx)('output_tags_map')
334
- .where({ outputId, outputTagId: tagId })
335
- .update(this.validatePartialForUpdate(update, undefined, ['isDeleted']));
336
- }
337
- async updateOutputTag(id, update, trx) {
338
- await this.verifyReadyForDatabaseAccess(trx);
339
- return await this.toDb(trx)('output_tags')
340
- .where({ outputTagId: id })
341
- .update(this.validatePartialForUpdate(update, undefined, ['isDeleted']));
342
- }
343
- async updateProvenTxReq(id, update, trx) {
344
- await this.verifyReadyForDatabaseAccess(trx);
345
- let r;
346
- if (Array.isArray(id)) {
347
- r = await this.toDb(trx)('proven_tx_reqs')
348
- .whereIn('provenTxReqId', id)
349
- .update(this.validatePartialForUpdate(update));
350
- }
351
- else if (Number.isInteger(id)) {
352
- r = await this.toDb(trx)('proven_tx_reqs')
353
- .where({ provenTxReqId: id })
354
- .update(this.validatePartialForUpdate(update));
355
- }
356
- else {
357
- throw new WERR_errors_1.WERR_INVALID_PARAMETER('id', 'transactionId or array of transactionId');
358
- }
359
- return r;
360
- }
361
- async updateProvenTx(id, update, trx) {
362
- await this.verifyReadyForDatabaseAccess(trx);
363
- return await this.toDb(trx)('proven_txs')
364
- .where({ provenTxId: id })
365
- .update(this.validatePartialForUpdate(update));
366
- }
367
- async updateSyncState(id, update, trx) {
368
- await this.verifyReadyForDatabaseAccess(trx);
369
- return await this.toDb(trx)('sync_states')
370
- .where({ syncStateId: id })
371
- .update(this.validatePartialForUpdate(update, ['when'], ['init']));
372
- }
373
- async updateTransaction(id, update, trx) {
374
- await this.verifyReadyForDatabaseAccess(trx);
375
- let r;
376
- if (Array.isArray(id)) {
377
- r = await this.toDb(trx)('transactions')
378
- .whereIn('transactionId', id)
379
- .update(await this.validatePartialForUpdate(update));
380
- }
381
- else if (Number.isInteger(id)) {
382
- r = await this.toDb(trx)('transactions')
383
- .where({ transactionId: id })
384
- .update(await this.validatePartialForUpdate(update));
385
- }
386
- else {
387
- throw new WERR_errors_1.WERR_INVALID_PARAMETER('id', 'transactionId or array of transactionId');
388
- }
389
- return r;
390
- }
391
- async updateTxLabelMap(transactionId, txLabelId, update, trx) {
392
- await this.verifyReadyForDatabaseAccess(trx);
393
- return await this.toDb(trx)('tx_labels_map')
394
- .where({ transactionId, txLabelId })
395
- .update(this.validatePartialForUpdate(update, undefined, ['isDeleted']));
396
- }
397
- async updateTxLabel(id, update, trx) {
398
- await this.verifyReadyForDatabaseAccess(trx);
399
- return await this.toDb(trx)('tx_labels')
400
- .where({ txLabelId: id })
401
- .update(this.validatePartialForUpdate(update, undefined, ['isDeleted']));
402
- }
403
- async updateUser(id, update, trx) {
404
- await this.verifyReadyForDatabaseAccess(trx);
405
- return await this.toDb(trx)('users').where({ userId: id }).update(this.validatePartialForUpdate(update));
406
- }
407
- async updateMonitorEvent(id, update, trx) {
408
- await this.verifyReadyForDatabaseAccess(trx);
409
- return await this.toDb(trx)('monitor_events')
410
- .where({ id })
411
- .update(this.validatePartialForUpdate(update));
412
- }
413
- setupQuery(table, args) {
414
- let q = this.toDb(args.trx)(table);
415
- if (args.partial && Object.keys(args.partial).length > 0)
416
- q.where(args.partial);
417
- if (args.since)
418
- q.where('updated_at', '>=', this.validateDateForWhere(args.since));
419
- if (args.orderDescending) {
420
- let sortColumn = '';
421
- switch (table) {
422
- case 'certificates':
423
- sortColumn = 'certificateId';
424
- break;
425
- case 'commissions':
426
- sortColumn = 'commissionId';
427
- break;
428
- case 'output_baskets':
429
- sortColumn = 'basketId';
430
- break;
431
- case 'outputs':
432
- sortColumn = 'outputId';
433
- break;
434
- case 'output_tags':
435
- sortColumn = 'outputTagId';
436
- break;
437
- case 'proven_tx_reqs':
438
- sortColumn = 'provenTxReqId';
439
- break;
440
- case 'proven_txs':
441
- sortColumn = 'provenTxId';
442
- break;
443
- case 'sync_states':
444
- sortColumn = 'syncStateId';
445
- break;
446
- case 'transactions':
447
- sortColumn = 'transactionId';
448
- break;
449
- case 'tx_labels':
450
- sortColumn = 'txLabelId';
451
- break;
452
- case 'users':
453
- sortColumn = 'userId';
454
- break;
455
- case 'monitor_events':
456
- sortColumn = 'id';
457
- break;
458
- default:
459
- break;
460
- }
461
- if (sortColumn !== '') {
462
- q.orderBy(sortColumn, 'desc');
463
- }
464
- }
465
- if (args.paged) {
466
- q.limit(args.paged.limit);
467
- q.offset(args.paged.offset || 0);
468
- }
469
- return q;
470
- }
471
- findCertificateFieldsQuery(args) {
472
- return this.setupQuery('certificate_fields', args);
473
- }
474
- findCertificatesQuery(args) {
475
- const q = this.setupQuery('certificates', args);
476
- if (args.certifiers && args.certifiers.length > 0)
477
- q.whereIn('certifier', args.certifiers);
478
- if (args.types && args.types.length > 0)
479
- q.whereIn('type', args.types);
480
- return q;
481
- }
482
- findCommissionsQuery(args) {
483
- if (args.partial.lockingScript)
484
- throw new WERR_errors_1.WERR_INVALID_PARAMETER('partial.lockingScript', `undefined. Commissions may not be found by lockingScript value.`);
485
- return this.setupQuery('commissions', args);
486
- }
487
- findOutputBasketsQuery(args) {
488
- return this.setupQuery('output_baskets', args);
489
- }
490
- findOutputsQuery(args, count) {
491
- if (args.partial.lockingScript)
492
- throw new WERR_errors_1.WERR_INVALID_PARAMETER('args.partial.lockingScript', `undefined. Outputs may not be found by lockingScript value.`);
493
- const q = this.setupQuery('outputs', args);
494
- if (args.txStatus && args.txStatus.length > 0) {
495
- q.whereRaw(`(select status from transactions where transactions.transactionId = outputs.transactionId) in (${args.txStatus.map(s => `'${s}'`).join(',')})`);
496
- }
497
- if (args.noScript && !count) {
498
- const columns = tables_1.outputColumnsWithoutLockingScript.map(c => `outputs.${c}`);
499
- q.select(columns);
500
- }
501
- return q;
502
- }
503
- findOutputTagMapsQuery(args) {
504
- const q = this.setupQuery('output_tags_map', args);
505
- if (args.tagIds && args.tagIds.length > 0)
506
- q.whereIn('outputTagId', args.tagIds);
507
- return q;
508
- }
509
- findOutputTagsQuery(args) {
510
- return this.setupQuery('output_tags', args);
511
- }
512
- findProvenTxReqsQuery(args) {
513
- if (args.partial.rawTx)
514
- throw new WERR_errors_1.WERR_INVALID_PARAMETER('args.partial.rawTx', `undefined. ProvenTxReqs may not be found by rawTx value.`);
515
- if (args.partial.inputBEEF)
516
- throw new WERR_errors_1.WERR_INVALID_PARAMETER('args.partial.inputBEEF', `undefined. ProvenTxReqs may not be found by inputBEEF value.`);
517
- const q = this.setupQuery('proven_tx_reqs', args);
518
- if (args.status && args.status.length > 0)
519
- q.whereIn('status', args.status);
520
- if (args.txids) {
521
- const txids = args.txids.filter(txid => txid !== undefined);
522
- if (txids.length > 0)
523
- q.whereIn('txid', txids);
524
- }
525
- return q;
526
- }
527
- findProvenTxsQuery(args) {
528
- if (args.partial.rawTx)
529
- throw new WERR_errors_1.WERR_INVALID_PARAMETER('args.partial.rawTx', `undefined. ProvenTxs may not be found by rawTx value.`);
530
- if (args.partial.merklePath)
531
- throw new WERR_errors_1.WERR_INVALID_PARAMETER('args.partial.merklePath', `undefined. ProvenTxs may not be found by merklePath value.`);
532
- return this.setupQuery('proven_txs', args);
533
- }
534
- findSyncStatesQuery(args) {
535
- return this.setupQuery('sync_states', args);
536
- }
537
- findTransactionsQuery(args, count) {
538
- if (args.partial.rawTx)
539
- throw new WERR_errors_1.WERR_INVALID_PARAMETER('args.partial.rawTx', `undefined. Transactions may not be found by rawTx value.`);
540
- if (args.partial.inputBEEF)
541
- throw new WERR_errors_1.WERR_INVALID_PARAMETER('args.partial.inputBEEF', `undefined. Transactions may not be found by inputBEEF value.`);
542
- const q = this.setupQuery('transactions', args);
543
- if (args.status && args.status.length > 0)
544
- q.whereIn('status', args.status);
545
- if (args.noRawTx && !count) {
546
- const columns = tables_1.transactionColumnsWithoutRawTx.map(c => `transactions.${c}`);
547
- q.select(columns);
548
- }
549
- return q;
550
- }
551
- findTxLabelMapsQuery(args) {
552
- const q = this.setupQuery('tx_labels_map', args);
553
- if (args.labelIds && args.labelIds.length > 0)
554
- q.whereIn('txLabelId', args.labelIds);
555
- return q;
556
- }
557
- findTxLabelsQuery(args) {
558
- return this.setupQuery('tx_labels', args);
559
- }
560
- findUsersQuery(args) {
561
- return this.setupQuery('users', args);
562
- }
563
- findMonitorEventsQuery(args) {
564
- return this.setupQuery('monitor_events', args);
565
- }
566
- async findCertificatesAuth(auth, args) {
567
- if (!auth.userId || (args.partial.userId && args.partial.userId !== auth.userId))
568
- throw new WERR_errors_1.WERR_UNAUTHORIZED();
569
- args.partial.userId = auth.userId;
570
- return await this.findCertificates(args);
571
- }
572
- async findOutputBasketsAuth(auth, args) {
573
- if (!auth.userId || (args.partial.userId && args.partial.userId !== auth.userId))
574
- throw new WERR_errors_1.WERR_UNAUTHORIZED();
575
- args.partial.userId = auth.userId;
576
- return await this.findOutputBaskets(args);
577
- }
578
- async findOutputsAuth(auth, args) {
579
- if (!auth.userId || (args.partial.userId && args.partial.userId !== auth.userId))
580
- throw new WERR_errors_1.WERR_UNAUTHORIZED();
581
- args.partial.userId = auth.userId;
582
- return await this.findOutputs(args);
583
- }
584
- async findCertificateFields(args) {
585
- return this.validateEntities(await this.findCertificateFieldsQuery(args));
586
- }
587
- async findCertificates(args) {
588
- const q = this.findCertificatesQuery(args);
589
- let r = await q;
590
- r = this.validateEntities(r, undefined, ['isDeleted']);
591
- if (args.includeFields) {
592
- for (const c of r) {
593
- c.fields = this.validateEntities(await this.findCertificateFields({
594
- partial: { certificateId: c.certificateId, userId: c.userId },
595
- trx: args.trx
596
- }));
597
- }
598
- }
599
- return r;
600
- }
601
- async findCommissions(args) {
602
- const q = this.findCommissionsQuery(args);
603
- const r = await q;
604
- return this.validateEntities(r, undefined, ['isRedeemed']);
605
- }
606
- async findOutputBaskets(args) {
607
- const q = this.findOutputBasketsQuery(args);
608
- const r = await q;
609
- return this.validateEntities(r, undefined, ['isDeleted']);
610
- }
611
- async findOutputs(args) {
612
- const q = this.findOutputsQuery(args);
613
- const r = await q;
614
- if (!args.noScript) {
615
- for (const o of r) {
616
- await this.validateOutputScript(o, args.trx);
617
- }
618
- }
619
- return this.validateEntities(r, undefined, ['spendable', 'change']);
620
- }
621
- async findOutputTagMaps(args) {
622
- const q = this.findOutputTagMapsQuery(args);
623
- const r = await q;
624
- return this.validateEntities(r, undefined, ['isDeleted']);
625
- }
626
- async findOutputTags(args) {
627
- const q = this.findOutputTagsQuery(args);
628
- const r = await q;
629
- return this.validateEntities(r, undefined, ['isDeleted']);
630
- }
631
- async findProvenTxReqs(args) {
632
- const q = this.findProvenTxReqsQuery(args);
633
- const r = await q;
634
- return this.validateEntities(r, undefined, ['notified']);
635
- }
636
- async findProvenTxs(args) {
637
- const q = this.findProvenTxsQuery(args);
638
- const r = await q;
639
- return this.validateEntities(r);
640
- }
641
- async findSyncStates(args) {
642
- const q = this.findSyncStatesQuery(args);
643
- const r = await q;
644
- return this.validateEntities(r, ['when'], ['init']);
645
- }
646
- async findTransactions(args) {
647
- const q = this.findTransactionsQuery(args);
648
- const r = await q;
649
- if (!args.noRawTx) {
650
- for (const t of r) {
651
- await this.validateRawTransaction(t, args.trx);
652
- }
653
- }
654
- return this.validateEntities(r, undefined, ['isOutgoing']);
655
- }
656
- async findTxLabelMaps(args) {
657
- const q = this.findTxLabelMapsQuery(args);
658
- const r = await q;
659
- return this.validateEntities(r, undefined, ['isDeleted']);
660
- }
661
- async findTxLabels(args) {
662
- const q = this.findTxLabelsQuery(args);
663
- const r = await q;
664
- return this.validateEntities(r, undefined, ['isDeleted']);
665
- }
666
- async findUsers(args) {
667
- const q = this.findUsersQuery(args);
668
- const r = await q;
669
- return this.validateEntities(r);
670
- }
671
- async findMonitorEvents(args) {
672
- const q = this.findMonitorEventsQuery(args);
673
- const r = await q;
674
- return this.validateEntities(r, ['when'], undefined);
675
- }
676
- async getCount(q) {
677
- q.count();
678
- const r = await q;
679
- return r[0]['count(*)'];
680
- }
681
- async countCertificateFields(args) {
682
- return await this.getCount(this.findCertificateFieldsQuery(args));
683
- }
684
- async countCertificates(args) {
685
- return await this.getCount(this.findCertificatesQuery(args));
686
- }
687
- async countCommissions(args) {
688
- return await this.getCount(this.findCommissionsQuery(args));
689
- }
690
- async countOutputBaskets(args) {
691
- return await this.getCount(this.findOutputBasketsQuery(args));
692
- }
693
- async countOutputs(args) {
694
- return await this.getCount(this.findOutputsQuery(args, true));
695
- }
696
- async countOutputTagMaps(args) {
697
- return await this.getCount(this.findOutputTagMapsQuery(args));
698
- }
699
- async countOutputTags(args) {
700
- return await this.getCount(this.findOutputTagsQuery(args));
701
- }
702
- async countProvenTxReqs(args) {
703
- return await this.getCount(this.findProvenTxReqsQuery(args));
704
- }
705
- async countProvenTxs(args) {
706
- return await this.getCount(this.findProvenTxsQuery(args));
707
- }
708
- async countSyncStates(args) {
709
- return await this.getCount(this.findSyncStatesQuery(args));
710
- }
711
- async countTransactions(args) {
712
- return await this.getCount(this.findTransactionsQuery(args, true));
713
- }
714
- async countTxLabelMaps(args) {
715
- return await this.getCount(this.findTxLabelMapsQuery(args));
716
- }
717
- async countTxLabels(args) {
718
- return await this.getCount(this.findTxLabelsQuery(args));
719
- }
720
- async countUsers(args) {
721
- return await this.getCount(this.findUsersQuery(args));
722
- }
723
- async countMonitorEvents(args) {
724
- return await this.getCount(this.findMonitorEventsQuery(args));
725
- }
726
- async destroy() {
727
- var _a;
728
- await ((_a = this.knex) === null || _a === void 0 ? void 0 : _a.destroy());
729
- }
730
- async migrate(storageName, storageIdentityKey) {
731
- const config = {
732
- migrationSource: new KnexMigrations_1.KnexMigrations(this.chain, storageName, storageIdentityKey, 1024)
733
- };
734
- await this.knex.migrate.latest(config);
735
- const version = await this.knex.migrate.currentVersion(config);
736
- return version;
737
- }
738
- async dropAllData() {
739
- // Only using migrations to migrate down, don't need valid properties for settings table.
740
- const config = {
741
- migrationSource: new KnexMigrations_1.KnexMigrations('test', '', '', 1024)
742
- };
743
- const count = Object.keys(config.migrationSource.migrations).length;
744
- for (let i = 0; i < count; i++) {
745
- try {
746
- const r = await this.knex.migrate.down(config);
747
- if (!r) {
748
- console.error(`Migration returned falsy result await this.knex.migrate.down(config)`);
749
- break;
750
- }
751
- }
752
- catch (eu) {
753
- break;
754
- }
755
- }
756
- }
757
- async transaction(scope, trx) {
758
- if (trx)
759
- return await scope(trx);
760
- return await this.knex.transaction(async (knextrx) => {
761
- const trx = knextrx;
762
- return await scope(trx);
763
- });
764
- }
765
- /**
766
- * Convert the standard optional `TrxToken` parameter into either a direct knex database instance,
767
- * or a Knex.Transaction as appropriate.
768
- */
769
- toDb(trx) {
770
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
771
- const db = !trx ? this.knex : trx;
772
- this.whenLastAccess = new Date();
773
- return db;
774
- }
775
- async validateRawTransaction(t, trx) {
776
- // if there is no txid or there is a rawTransaction return what we have.
777
- if (t.rawTx || !t.txid)
778
- return;
779
- // rawTransaction is missing, see if we moved it ...
780
- const rawTx = await this.getRawTxOfKnownValidTransaction(t.txid, undefined, undefined, trx);
781
- if (!rawTx)
782
- return;
783
- t.rawTx = rawTx;
784
- }
785
- /**
786
- * Make sure database is ready for access:
787
- *
788
- * - dateScheme is known
789
- * - foreign key constraints are enabled
790
- *
791
- * @param trx
792
- */
793
- async verifyReadyForDatabaseAccess(trx) {
794
- if (!this._settings) {
795
- this._settings = await this.readSettings();
796
- }
797
- if (!this._verifiedReadyForDatabaseAccess) {
798
- // Make sure foreign key constraint checking is turned on in SQLite.
799
- if (this._settings.dbtype === 'SQLite') {
800
- await this.toDb(trx).raw('PRAGMA foreign_keys = ON;');
801
- }
802
- this._verifiedReadyForDatabaseAccess = true;
803
- }
804
- return this._settings.dbtype;
805
- }
806
- /**
807
- * Helper to force uniform behavior across database engines.
808
- * Use to process the update template for entities being updated.
809
- */
810
- validatePartialForUpdate(update, dateFields, booleanFields) {
811
- if (!this.dbtype)
812
- throw new WERR_errors_1.WERR_INTERNAL('must call verifyReadyForDatabaseAccess first');
813
- const v = update;
814
- if (v.created_at)
815
- v.created_at = this.validateEntityDate(v.created_at);
816
- if (v.updated_at)
817
- v.updated_at = this.validateEntityDate(v.updated_at);
818
- if (!v.created_at)
819
- delete v.created_at;
820
- if (!v.updated_at)
821
- v.updated_at = this.validateEntityDate(new Date());
822
- if (dateFields) {
823
- for (const df of dateFields) {
824
- if (v[df])
825
- v[df] = this.validateOptionalEntityDate(v[df]);
826
- }
827
- }
828
- if (booleanFields) {
829
- for (const df of booleanFields) {
830
- if (update[df] !== undefined)
831
- update[df] = !!update[df] ? 1 : 0;
832
- }
833
- }
834
- for (const key of Object.keys(v)) {
835
- const val = v[key];
836
- if (Array.isArray(val) && (val.length === 0 || typeof val[0] === 'number')) {
837
- v[key] = Buffer.from(val);
838
- }
839
- else if (val === undefined) {
840
- v[key] = null;
841
- }
842
- }
843
- this.isDirty = true;
844
- return v;
845
- }
846
- /**
847
- * Helper to force uniform behavior across database engines.
848
- * Use to process new entities being inserted into the database.
849
- */
850
- async validateEntityForInsert(entity, trx, dateFields, booleanFields) {
851
- await this.verifyReadyForDatabaseAccess(trx);
852
- const v = { ...entity };
853
- v.created_at = this.validateOptionalEntityDate(v.created_at, true);
854
- v.updated_at = this.validateOptionalEntityDate(v.updated_at, true);
855
- if (!v.created_at)
856
- delete v.created_at;
857
- if (!v.updated_at)
858
- delete v.updated_at;
859
- if (dateFields) {
860
- for (const df of dateFields) {
861
- if (v[df])
862
- v[df] = this.validateOptionalEntityDate(v[df]);
863
- }
864
- }
865
- if (booleanFields) {
866
- for (const df of booleanFields) {
867
- if (entity[df] !== undefined)
868
- entity[df] = !!entity[df] ? 1 : 0;
869
- }
870
- }
871
- for (const key of Object.keys(v)) {
872
- const val = v[key];
873
- if (Array.isArray(val) && (val.length === 0 || typeof val[0] === 'number')) {
874
- v[key] = Buffer.from(val);
875
- }
876
- else if (val === undefined) {
877
- v[key] = null;
878
- }
879
- }
880
- this.isDirty = true;
881
- return v;
882
- }
883
- async getLabelsForTransactionId(transactionId, trx) {
884
- if (transactionId === undefined)
885
- return [];
886
- const labels = await this.toDb(trx)('tx_labels')
887
- .join('tx_labels_map', 'tx_labels_map.txLabelId', 'tx_labels.txLabelId')
888
- .where('tx_labels_map.transactionId', transactionId)
889
- .whereNot('tx_labels_map.isDeleted', true)
890
- .whereNot('tx_labels.isDeleted', true);
891
- return this.validateEntities(labels, undefined, ['isDeleted']);
892
- }
893
- async getTagsForOutputId(outputId, trx) {
894
- const tags = await this.toDb(trx)('output_tags')
895
- .join('output_tags_map', 'output_tags_map.outputTagId', 'output_tags.outputTagId')
896
- .where('output_tags_map.outputId', outputId)
897
- .whereNot('output_tags_map.isDeleted', true)
898
- .whereNot('output_tags.isDeleted', true);
899
- return this.validateEntities(tags, undefined, ['isDeleted']);
900
- }
901
- async purgeData(params, trx) {
902
- return await (0, purgeData_1.purgeData)(this, params, trx);
903
- }
904
- async reviewStatus(args) {
905
- return await (0, reviewStatus_1.reviewStatus)(this, args);
906
- }
907
- /**
908
- * Counts the outputs for userId in basketId that are spendable: true
909
- * AND whose transaction status is one of:
910
- * - completed
911
- * - unproven
912
- * - sending (if excludeSending is false)
913
- */
914
- async countChangeInputs(userId, basketId, excludeSending) {
915
- const status = ['completed', 'unproven'];
916
- if (!excludeSending)
917
- status.push('sending');
918
- const statusText = status.map(s => `'${s}'`).join(',');
919
- const txStatusCondition = `(SELECT status FROM transactions WHERE outputs.transactionId = transactions.transactionId) in (${statusText})`;
920
- let q = this.knex('outputs').where({ userId, spendable: true, basketId }).whereRaw(txStatusCondition);
921
- const count = await this.getCount(q);
922
- return count;
923
- }
924
- /**
925
- * Finds closest matching available change output to use as input for new transaction.
926
- *
927
- * Transactionally allocate the output such that
928
- */
929
- async allocateChangeInput(userId, basketId, targetSatoshis, exactSatoshis, excludeSending, transactionId) {
930
- const status = ['completed', 'unproven'];
931
- if (!excludeSending)
932
- status.push('sending');
933
- const statusText = status.map(s => `'${s}'`).join(',');
934
- const r = await this.knex.transaction(async (trx) => {
935
- const txStatusCondition = `AND (SELECT status FROM transactions WHERE outputs.transactionId = transactions.transactionId) in (${statusText})`;
936
- let outputId;
937
- const setOutputId = async (rawQuery) => {
938
- let oidr = await trx.raw(rawQuery);
939
- outputId = undefined;
940
- if (!oidr['outputId'] && oidr.length > 0)
941
- oidr = oidr[0];
942
- if (!oidr['outputId'] && oidr.length > 0)
943
- oidr = oidr[0];
944
- if (oidr['outputId'])
945
- outputId = Number(oidr['outputId']);
946
- };
947
- if (exactSatoshis !== undefined) {
948
- // Find outputId of output that with exactSatoshis
949
- await setOutputId(`
950
- SELECT outputId
951
- FROM outputs
952
- WHERE userId = ${userId}
953
- AND spendable = 1
954
- AND basketId = ${basketId}
955
- ${txStatusCondition}
956
- AND satoshis = ${exactSatoshis}
957
- LIMIT 1;
958
- `);
959
- }
960
- if (outputId === undefined) {
961
- // Find outputId of output that would at least fund targetSatoshis
962
- await setOutputId(`
963
- SELECT outputId
964
- FROM outputs
965
- WHERE userId = ${userId}
966
- AND spendable = 1
967
- AND basketId = ${basketId}
968
- ${txStatusCondition}
969
- AND satoshis - ${targetSatoshis} = (
970
- SELECT MIN(satoshis - ${targetSatoshis})
971
- FROM outputs
972
- WHERE userId = ${userId}
973
- AND spendable = 1
974
- AND basketId = ${basketId}
975
- ${txStatusCondition}
976
- AND satoshis - ${targetSatoshis} >= 0
977
- )
978
- LIMIT 1;
979
- `);
980
- }
981
- if (outputId === undefined) {
982
- // Find outputId of output that would add the most fund targetSatoshis
983
- await setOutputId(`
984
- SELECT outputId
985
- FROM outputs
986
- WHERE userId = ${userId}
987
- AND spendable = 1
988
- AND basketId = ${basketId}
989
- ${txStatusCondition}
990
- AND satoshis - ${targetSatoshis} = (
991
- SELECT MAX(satoshis - ${targetSatoshis})
992
- FROM outputs
993
- WHERE userId = ${userId}
994
- AND spendable = 1
995
- AND basketId = ${basketId}
996
- ${txStatusCondition}
997
- AND satoshis - ${targetSatoshis} < 0
998
- )
999
- LIMIT 1;
1000
- `);
1001
- }
1002
- if (outputId === undefined)
1003
- return undefined;
1004
- await this.updateOutput(outputId, {
1005
- spendable: false,
1006
- spentBy: transactionId
1007
- }, trx);
1008
- const r = (0, utilityHelpers_1.verifyTruthy)(await this.findOutputById(outputId, trx));
1009
- return r;
1010
- });
1011
- return r;
1012
- }
1013
- /**
1014
- * Helper to force uniform behavior across database engines.
1015
- * Use to process all individual records with time stamps retreived from database.
1016
- */
1017
- validateEntity(entity, dateFields, booleanFields) {
1018
- entity.created_at = this.validateDate(entity.created_at);
1019
- entity.updated_at = this.validateDate(entity.updated_at);
1020
- if (dateFields) {
1021
- for (const df of dateFields) {
1022
- if (entity[df])
1023
- entity[df] = this.validateDate(entity[df]);
1024
- }
1025
- }
1026
- if (booleanFields) {
1027
- for (const df of booleanFields) {
1028
- if (entity[df] !== undefined)
1029
- entity[df] = !!entity[df];
1030
- }
1031
- }
1032
- for (const key of Object.keys(entity)) {
1033
- const val = entity[key];
1034
- if (val === null) {
1035
- entity[key] = undefined;
1036
- }
1037
- else if (Buffer.isBuffer(val)) {
1038
- entity[key] = Array.from(val);
1039
- }
1040
- }
1041
- return entity;
1042
- }
1043
- /**
1044
- * Helper to force uniform behavior across database engines.
1045
- * Use to process all arrays of records with time stamps retreived from database.
1046
- * @returns input `entities` array with contained values validated.
1047
- */
1048
- validateEntities(entities, dateFields, booleanFields) {
1049
- for (let i = 0; i < entities.length; i++) {
1050
- entities[i] = this.validateEntity(entities[i], dateFields, booleanFields);
1051
- }
1052
- return entities;
1053
- }
1054
- async adminStats(adminIdentityKey) {
1055
- if (this.dbtype !== 'MySQL')
1056
- throw new WERR_errors_1.WERR_NOT_IMPLEMENTED('adminStats, only MySQL is supported');
1057
- const monitorEvent = (0, utilityHelpers_1.verifyOneOrNone)(await this.findMonitorEvents({
1058
- partial: { event: 'MonitorCallHistory' },
1059
- orderDescending: true,
1060
- paged: { limit: 1 }
1061
- }));
1062
- const monitorStats = monitorEvent ? JSON.parse(monitorEvent.details) : undefined;
1063
- const servicesStats = this.getServices().getServicesCallHistory(true);
1064
- await this.insertMonitorEvent({
1065
- event: 'ServicesCallHistory',
1066
- details: JSON.stringify(servicesStats),
1067
- created_at: new Date(),
1068
- updated_at: new Date(),
1069
- id: 0
1070
- });
1071
- const one_day_ago = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString();
1072
- const one_week_ago = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000).toISOString();
1073
- const one_month_ago = new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString();
1074
- const [[{ usersDay, usersMonth, usersWeek, usersTotal, transactionsDay, transactionsMonth, transactionsWeek, transactionsTotal, txCompletedDay, txCompletedMonth, txCompletedWeek, txCompletedTotal, txFailedDay, txFailedMonth, txFailedWeek, txFailedTotal, txUnprocessedDay, txUnprocessedMonth, txUnprocessedWeek, txUnprocessedTotal, txSendingDay, txSendingMonth, txSendingWeek, txSendingTotal, txUnprovenDay, txUnprovenMonth, txUnprovenWeek, txUnprovenTotal, txUnsignedDay, txUnsignedMonth, txUnsignedWeek, txUnsignedTotal, txNosendDay, txNosendMonth, txNosendWeek, txNosendTotal, txNonfinalDay, txNonfinalMonth, txNonfinalWeek, txNonfinalTotal, txUnfailDay, txUnfailMonth, txUnfailWeek, txUnfailTotal, satoshisDefaultDay, satoshisDefaultMonth, satoshisDefaultWeek, satoshisDefaultTotal, satoshisOtherDay, satoshisOtherMonth, satoshisOtherWeek, satoshisOtherTotal, basketsDay, basketsMonth, basketsWeek, basketsTotal, labelsDay, labelsMonth, labelsWeek, labelsTotal, tagsDay, tagsMonth, tagsWeek, tagsTotal }]] = await this.knex.raw(`
1075
- select
1076
- (select count(*) from users where created_at > '${one_day_ago}') as usersDay,
1077
- (select count(*) from users where created_at > '${one_week_ago}') as usersWeek,
1078
- (select count(*) from users where created_at > '${one_month_ago}') as usersMonth,
1079
- (select count(*) from users) as usersTotal,
1080
- (select count(*) from transactions where created_at > '${one_day_ago}') as transactionsDay,
1081
- (select count(*) from transactions where created_at > '${one_week_ago}') as transactionsWeek,
1082
- (select count(*) from transactions where created_at > '${one_month_ago}') as transactionsMonth,
1083
- (select count(*) from transactions) as transactionsTotal,
1084
- (select count(*) from transactions where status = 'completed' and created_at > '${one_day_ago}') as txCompletedDay,
1085
- (select count(*) from transactions where status = 'completed' and created_at > '${one_week_ago}') as txCompletedWeek,
1086
- (select count(*) from transactions where status = 'completed' and created_at > '${one_month_ago}') as txCompletedMonth,
1087
- (select count(*) from transactions where status = 'completed') as txCompletedTotal,
1088
- (select count(*) from transactions where status = 'failed' and created_at > '${one_day_ago}') as txFailedDay,
1089
- (select count(*) from transactions where status = 'failed' and created_at > '${one_week_ago}') as txFailedWeek,
1090
- (select count(*) from transactions where status = 'failed' and created_at > '${one_month_ago}') as txFailedMonth,
1091
- (select count(*) from transactions where status = 'failed') as txFailedTotal,
1092
- (select count(*) from transactions where status = 'unprocessed' and created_at > '${one_day_ago}') as txUnprocessedDay,
1093
- (select count(*) from transactions where status = 'unprocessed' and created_at > '${one_week_ago}') as txUnprocessedWeek,
1094
- (select count(*) from transactions where status = 'unprocessed' and created_at > '${one_month_ago}') as txUnprocessedMonth,
1095
- (select count(*) from transactions where status = 'unprocessed') as txUnprocessedTotal,
1096
- (select count(*) from transactions where status = 'sending' and created_at > '${one_day_ago}') as txSendingDay,
1097
- (select count(*) from transactions where status = 'sending' and created_at > '${one_week_ago}') as txSendingWeek,
1098
- (select count(*) from transactions where status = 'sending' and created_at > '${one_month_ago}') as txSendingMonth,
1099
- (select count(*) from transactions where status = 'sending') as txSendingTotal,
1100
- (select count(*) from transactions where status = 'unproven' and created_at > '${one_day_ago}') as txUnprovenDay,
1101
- (select count(*) from transactions where status = 'unproven' and created_at > '${one_week_ago}') as txUnprovenWeek,
1102
- (select count(*) from transactions where status = 'unproven' and created_at > '${one_month_ago}') as txUnprovenMonth,
1103
- (select count(*) from transactions where status = 'unproven') as txUnprovenTotal,
1104
- (select count(*) from transactions where status = 'unsigned' and created_at > '${one_day_ago}') as txUnsignedDay,
1105
- (select count(*) from transactions where status = 'unsigned' and created_at > '${one_week_ago}') as txUnsignedWeek,
1106
- (select count(*) from transactions where status = 'unsigned' and created_at > '${one_month_ago}') as txUnsignedMonth,
1107
- (select count(*) from transactions where status = 'unsigned') as txUnsignedTotal,
1108
- (select count(*) from transactions where status = 'nosend' and created_at > '${one_day_ago}') as txNosendDay,
1109
- (select count(*) from transactions where status = 'nosend' and created_at > '${one_week_ago}') as txNosendWeek,
1110
- (select count(*) from transactions where status = 'nosend' and created_at > '${one_month_ago}') as txNosendMonth,
1111
- (select count(*) from transactions where status = 'nosend') as txNosendTotal,
1112
- (select count(*) from transactions where status = 'nonfinal' and created_at > '${one_day_ago}') as txNonfinalDay,
1113
- (select count(*) from transactions where status = 'nonfinal' and created_at > '${one_week_ago}') as txNonfinalWeek,
1114
- (select count(*) from transactions where status = 'nonfinal' and created_at > '${one_month_ago}') as txNonfinalMonth,
1115
- (select count(*) from transactions where status = 'nonfinal') as txNonfinalTotal,
1116
- (select count(*) from transactions where status = 'unfail' and created_at > '${one_day_ago}') as txUnfailDay,
1117
- (select count(*) from transactions where status = 'unfail' and created_at > '${one_week_ago}') as txUnfailWeek,
1118
- (select count(*) from transactions where status = 'unfail' and created_at > '${one_month_ago}') as txUnfailMonth,
1119
- (select count(*) from transactions where status = 'unfail') as txUnfailTotal,
1120
- (select sum(satoshis) from outputs where spendable = 1 and \`change\` = 1 and created_at > '${one_day_ago}') as satoshisDefaultDay,
1121
- (select sum(satoshis) from outputs where spendable = 1 and \`change\` = 1 and created_at > '${one_week_ago}') as satoshisDefaultWeek,
1122
- (select sum(satoshis) from outputs where spendable = 1 and \`change\` = 1 and created_at > '${one_month_ago}') as satoshisDefaultMonth,
1123
- (select sum(satoshis) from outputs where spendable = 1 and \`change\` = 1) as satoshisDefaultTotal,
1124
- (select sum(satoshis) from outputs where spendable = 1 and \`change\` = 0 and not basketId is null and created_at > '${one_day_ago}') as satoshisOtherDay,
1125
- (select sum(satoshis) from outputs where spendable = 1 and \`change\` = 0 and not basketId is null and created_at > '${one_week_ago}') as satoshisOtherWeek,
1126
- (select sum(satoshis) from outputs where spendable = 1 and \`change\` = 0 and not basketId is null and created_at > '${one_month_ago}') as satoshisOtherMonth,
1127
- (select sum(satoshis) from outputs where spendable = 1 and \`change\` = 0 and not basketId is null) as satoshisOtherTotal,
1128
- (select count(*) from output_baskets where created_at > '${one_day_ago}') as basketsDay,
1129
- (select count(*) from output_baskets where created_at > '${one_week_ago}') as basketsWeek,
1130
- (select count(*) from output_baskets where created_at > '${one_month_ago}') as basketsMonth,
1131
- (select count(*) from output_baskets) as basketsTotal,
1132
- (select count(*) from tx_labels where created_at > '${one_day_ago}') as labelsDay,
1133
- (select count(*) from tx_labels where created_at > '${one_week_ago}') as labelsWeek,
1134
- (select count(*) from tx_labels where created_at > '${one_month_ago}') as labelsMonth,
1135
- (select count(*) from tx_labels) as labelsTotal,
1136
- (select count(*) from output_tags where created_at > '${one_day_ago}') as tagsDay,
1137
- (select count(*) from output_tags where created_at > '${one_week_ago}') as tagsWeek,
1138
- (select count(*) from output_tags where created_at > '${one_month_ago}') as tagsMonth,
1139
- (select count(*) from output_tags) as tagsTotal
1140
- `);
1141
- const r = {
1142
- monitorStats,
1143
- servicesStats,
1144
- requestedBy: adminIdentityKey,
1145
- when: new Date().toISOString(),
1146
- usersDay,
1147
- usersWeek,
1148
- usersMonth,
1149
- usersTotal,
1150
- transactionsDay,
1151
- transactionsWeek,
1152
- transactionsMonth,
1153
- transactionsTotal,
1154
- txCompletedDay,
1155
- txCompletedWeek,
1156
- txCompletedMonth,
1157
- txCompletedTotal,
1158
- txFailedDay,
1159
- txFailedWeek,
1160
- txFailedMonth,
1161
- txFailedTotal,
1162
- txUnprocessedDay,
1163
- txUnprocessedWeek,
1164
- txUnprocessedMonth,
1165
- txUnprocessedTotal,
1166
- txSendingDay,
1167
- txSendingWeek,
1168
- txSendingMonth,
1169
- txSendingTotal,
1170
- txUnprovenDay,
1171
- txUnprovenWeek,
1172
- txUnprovenMonth,
1173
- txUnprovenTotal,
1174
- txUnsignedDay,
1175
- txUnsignedWeek,
1176
- txUnsignedMonth,
1177
- txUnsignedTotal,
1178
- txNosendDay,
1179
- txNosendWeek,
1180
- txNosendMonth,
1181
- txNosendTotal,
1182
- txNonfinalDay,
1183
- txNonfinalWeek,
1184
- txNonfinalMonth,
1185
- txNonfinalTotal,
1186
- txUnfailDay,
1187
- txUnfailWeek,
1188
- txUnfailMonth,
1189
- txUnfailTotal,
1190
- satoshisDefaultDay: Number(satoshisDefaultDay),
1191
- satoshisDefaultWeek: Number(satoshisDefaultWeek),
1192
- satoshisDefaultMonth: Number(satoshisDefaultMonth),
1193
- satoshisDefaultTotal: Number(satoshisDefaultTotal),
1194
- satoshisOtherDay: Number(satoshisOtherDay),
1195
- satoshisOtherWeek: Number(satoshisOtherWeek),
1196
- satoshisOtherMonth: Number(satoshisOtherMonth),
1197
- satoshisOtherTotal: Number(satoshisOtherTotal),
1198
- basketsDay,
1199
- basketsWeek,
1200
- basketsMonth,
1201
- basketsTotal,
1202
- labelsDay,
1203
- labelsWeek,
1204
- labelsMonth,
1205
- labelsTotal,
1206
- tagsDay,
1207
- tagsWeek,
1208
- tagsMonth,
1209
- tagsTotal
1210
- };
1211
- return r;
1212
- }
1213
- }
1214
- exports.StorageKnex = StorageKnex;
1215
- //# sourceMappingURL=StorageKnex.js.map