@prosopo/database 0.2.32 → 0.2.36
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.
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
|
3
3
|
const common = require("@prosopo/common");
|
|
4
4
|
const types = require("@prosopo/types");
|
|
5
5
|
const typesDatabase = require("@prosopo/types-database");
|
|
6
|
-
const
|
|
6
|
+
const typesReturns = require("@prosopo/captcha-contract/types-returns");
|
|
7
7
|
const mongodb = require("mongodb");
|
|
8
8
|
const is = require("@polkadot/util/is");
|
|
9
9
|
const mongoose = require("mongoose");
|
|
@@ -56,10 +56,13 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
56
56
|
};
|
|
57
57
|
this.connection.once("open", resolve).on("error", (e) => {
|
|
58
58
|
var _a;
|
|
59
|
-
this.logger.warn(`mongoose connection error`);
|
|
60
|
-
this.logger.error(e);
|
|
61
59
|
if (attempt === MAX_RETRIES) {
|
|
62
|
-
reject(
|
|
60
|
+
reject(
|
|
61
|
+
new common.ProsopoDBError("DATABASE.CONNECT_ERROR", {
|
|
62
|
+
context: { url: this.url, error: e, attempt },
|
|
63
|
+
logger: this.logger
|
|
64
|
+
})
|
|
65
|
+
);
|
|
63
66
|
} else {
|
|
64
67
|
(_a = this.connection) == null ? void 0 : _a.removeAllListeners("error");
|
|
65
68
|
}
|
|
@@ -78,10 +81,10 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
78
81
|
}
|
|
79
82
|
/** Close connection to the database */
|
|
80
83
|
async close() {
|
|
81
|
-
this.logger.
|
|
84
|
+
this.logger.debug(`Closing connection to ${this.url}`);
|
|
82
85
|
await new Promise(async (resolve, reject) => {
|
|
83
86
|
mongoose.connection.close().then(() => {
|
|
84
|
-
this.logger.
|
|
87
|
+
this.logger.debug(`Connection to ${this.url} closed`);
|
|
85
88
|
resolve();
|
|
86
89
|
}).catch(reject);
|
|
87
90
|
});
|
|
@@ -93,7 +96,7 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
93
96
|
async storeDataset(dataset) {
|
|
94
97
|
var _a, _b, _c;
|
|
95
98
|
try {
|
|
96
|
-
this.logger.
|
|
99
|
+
this.logger.debug(`Storing dataset in database`);
|
|
97
100
|
const parsedDataset = types.DatasetWithIdsAndTreeSchema.parse(dataset);
|
|
98
101
|
const datasetDoc = {
|
|
99
102
|
datasetId: parsedDataset.datasetId,
|
|
@@ -107,14 +110,14 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
107
110
|
{ $set: datasetDoc },
|
|
108
111
|
{ upsert: true }
|
|
109
112
|
));
|
|
110
|
-
const captchaDocs = parsedDataset.captchas.map(({ solution, ...
|
|
111
|
-
...
|
|
113
|
+
const captchaDocs = parsedDataset.captchas.map(({ solution, ...captcha }, index) => ({
|
|
114
|
+
...captcha,
|
|
112
115
|
datasetId: parsedDataset.datasetId,
|
|
113
116
|
datasetContentId: parsedDataset.datasetContentId,
|
|
114
117
|
index,
|
|
115
118
|
solved: !!(solution == null ? void 0 : solution.length)
|
|
116
119
|
}));
|
|
117
|
-
this.logger.
|
|
120
|
+
this.logger.debug(`Inserting captcha records`);
|
|
118
121
|
if (captchaDocs.length) {
|
|
119
122
|
await ((_b = this.tables) == null ? void 0 : _b.captcha.bulkWrite(
|
|
120
123
|
captchaDocs.map((captchaDoc) => ({
|
|
@@ -126,15 +129,15 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
126
129
|
}))
|
|
127
130
|
));
|
|
128
131
|
}
|
|
129
|
-
const captchaSolutionDocs = parsedDataset.captchas.filter(({ solution }) => solution == null ? void 0 : solution.length).map((
|
|
130
|
-
captchaId:
|
|
131
|
-
captchaContentId:
|
|
132
|
-
solution:
|
|
133
|
-
salt:
|
|
132
|
+
const captchaSolutionDocs = parsedDataset.captchas.filter(({ solution }) => solution == null ? void 0 : solution.length).map((captcha) => ({
|
|
133
|
+
captchaId: captcha.captchaId,
|
|
134
|
+
captchaContentId: captcha.captchaContentId,
|
|
135
|
+
solution: captcha.solution,
|
|
136
|
+
salt: captcha.salt,
|
|
134
137
|
datasetId: parsedDataset.datasetId,
|
|
135
138
|
datasetContentId: parsedDataset.datasetContentId
|
|
136
139
|
}));
|
|
137
|
-
this.logger.
|
|
140
|
+
this.logger.debug(`Inserting solution records`);
|
|
138
141
|
if (captchaSolutionDocs.length) {
|
|
139
142
|
await ((_c = this.tables) == null ? void 0 : _c.solution.bulkWrite(
|
|
140
143
|
captchaSolutionDocs.map((captchaSolutionDoc) => ({
|
|
@@ -146,11 +149,22 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
146
149
|
}))
|
|
147
150
|
));
|
|
148
151
|
}
|
|
149
|
-
this.logger.
|
|
152
|
+
this.logger.debug(`Dataset stored in database`);
|
|
150
153
|
} catch (err) {
|
|
151
|
-
throw new common.
|
|
154
|
+
throw new common.ProsopoDBError("DATABASE.DATASET_LOAD_FAILED", {
|
|
155
|
+
context: { failedFuncName: this.storeDataset.name, error: err },
|
|
156
|
+
logger: this.logger
|
|
157
|
+
});
|
|
152
158
|
}
|
|
153
159
|
}
|
|
160
|
+
/** @description Get solutions for a dataset
|
|
161
|
+
* @param {string} datasetId
|
|
162
|
+
*/
|
|
163
|
+
async getSolutions(datasetId) {
|
|
164
|
+
var _a;
|
|
165
|
+
const docs = await ((_a = this.tables) == null ? void 0 : _a.solution.find({ datasetId }).lean());
|
|
166
|
+
return docs ? docs : [];
|
|
167
|
+
}
|
|
154
168
|
/** @description Get a dataset from the database
|
|
155
169
|
* @param {string} datasetId
|
|
156
170
|
*/
|
|
@@ -186,7 +200,9 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
186
200
|
})
|
|
187
201
|
};
|
|
188
202
|
}
|
|
189
|
-
throw new common.
|
|
203
|
+
throw new common.ProsopoDBError("DATABASE.DATASET_GET_FAILED", {
|
|
204
|
+
context: { failedFuncName: this.getDataset.name, datasetId }
|
|
205
|
+
});
|
|
190
206
|
}
|
|
191
207
|
/**
|
|
192
208
|
* @description Get random captchas that are solved or not solved
|
|
@@ -197,7 +213,9 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
197
213
|
async getRandomCaptcha(solved, datasetId, size) {
|
|
198
214
|
var _a;
|
|
199
215
|
if (!is.isHex(datasetId)) {
|
|
200
|
-
throw new common.
|
|
216
|
+
throw new common.ProsopoDBError("DATABASE.INVALID_HASH", {
|
|
217
|
+
context: { failedFuncName: this.getRandomCaptcha.name, datasetId }
|
|
218
|
+
});
|
|
201
219
|
}
|
|
202
220
|
const sampleSize = size ? Math.abs(Math.trunc(size)) : 1;
|
|
203
221
|
const cursor = (_a = this.tables) == null ? void 0 : _a.captcha.aggregate([
|
|
@@ -218,16 +236,9 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
218
236
|
if (docs && docs.length) {
|
|
219
237
|
return docs.map(({ _id, ...keepAttrs }) => keepAttrs);
|
|
220
238
|
}
|
|
221
|
-
throw new common.
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
{},
|
|
225
|
-
{
|
|
226
|
-
solved,
|
|
227
|
-
datasetId,
|
|
228
|
-
size
|
|
229
|
-
}
|
|
230
|
-
);
|
|
239
|
+
throw new common.ProsopoDBError("DATABASE.CAPTCHA_GET_FAILED", {
|
|
240
|
+
context: { failedFuncName: this.getRandomCaptcha.name, solved, datasetId, size }
|
|
241
|
+
});
|
|
231
242
|
}
|
|
232
243
|
/**
|
|
233
244
|
* @description Get captchas by id
|
|
@@ -240,22 +251,28 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
240
251
|
if (docs && docs.length) {
|
|
241
252
|
return docs.map(({ _id, ...keepAttrs }) => keepAttrs);
|
|
242
253
|
}
|
|
243
|
-
throw new common.
|
|
254
|
+
throw new common.ProsopoDBError("DATABASE.CAPTCHA_GET_FAILED", {
|
|
255
|
+
context: { failedFuncName: this.getCaptchaById.name, captchaId }
|
|
256
|
+
});
|
|
244
257
|
}
|
|
245
258
|
/**
|
|
246
259
|
* @description Update a captcha
|
|
247
260
|
* @param {Captcha} captcha
|
|
248
261
|
* @param {string} datasetId the id of the data set
|
|
249
262
|
*/
|
|
250
|
-
async updateCaptcha(
|
|
263
|
+
async updateCaptcha(captcha, datasetId) {
|
|
251
264
|
var _a;
|
|
252
265
|
if (!is.isHex(datasetId)) {
|
|
253
|
-
throw new common.
|
|
266
|
+
throw new common.ProsopoDBError("DATABASE.INVALID_HASH", {
|
|
267
|
+
context: { failedFuncName: this.updateCaptcha.name, datasetId }
|
|
268
|
+
});
|
|
254
269
|
}
|
|
255
270
|
try {
|
|
256
|
-
await ((_a = this.tables) == null ? void 0 : _a.captcha.updateOne({ datasetId }, { $set:
|
|
271
|
+
await ((_a = this.tables) == null ? void 0 : _a.captcha.updateOne({ datasetId }, { $set: captcha }, { upsert: false }));
|
|
257
272
|
} catch (err) {
|
|
258
|
-
throw new common.
|
|
273
|
+
throw new common.ProsopoDBError("DATABASE.CAPTCHA_UPDATE_FAILED", {
|
|
274
|
+
context: { failedFuncName: this.getDatasetDetails.name, error: err }
|
|
275
|
+
});
|
|
259
276
|
}
|
|
260
277
|
}
|
|
261
278
|
/**
|
|
@@ -271,13 +288,17 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
271
288
|
async getDatasetDetails(datasetId) {
|
|
272
289
|
var _a;
|
|
273
290
|
if (!is.isHex(datasetId)) {
|
|
274
|
-
throw new common.
|
|
291
|
+
throw new common.ProsopoDBError("DATABASE.INVALID_HASH", {
|
|
292
|
+
context: { failedFuncName: this.getDatasetDetails.name, datasetId }
|
|
293
|
+
});
|
|
275
294
|
}
|
|
276
295
|
const doc = await ((_a = this.tables) == null ? void 0 : _a.dataset.findOne({ datasetId }).lean());
|
|
277
296
|
if (doc) {
|
|
278
297
|
return doc;
|
|
279
298
|
}
|
|
280
|
-
throw new common.
|
|
299
|
+
throw new common.ProsopoDBError("DATABASE.DATASET_GET_FAILED", {
|
|
300
|
+
context: { failedFuncName: this.getDatasetDetails.name, datasetId }
|
|
301
|
+
});
|
|
281
302
|
}
|
|
282
303
|
/**
|
|
283
304
|
* @description Store a Dapp User's captcha solution commitment
|
|
@@ -293,15 +314,15 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
293
314
|
commitmentRecord,
|
|
294
315
|
{ upsert: true }
|
|
295
316
|
));
|
|
296
|
-
const ops = captchas.map((
|
|
317
|
+
const ops = captchas.map((captcha) => ({
|
|
297
318
|
updateOne: {
|
|
298
|
-
filter: { commitmentId: commit.id, captchaId:
|
|
319
|
+
filter: { commitmentId: commit.id, captchaId: captcha.captchaId },
|
|
299
320
|
update: {
|
|
300
321
|
$set: {
|
|
301
|
-
captchaId:
|
|
302
|
-
captchaContentId:
|
|
303
|
-
salt:
|
|
304
|
-
solution:
|
|
322
|
+
captchaId: captcha.captchaId,
|
|
323
|
+
captchaContentId: captcha.captchaContentId,
|
|
324
|
+
salt: captcha.salt,
|
|
325
|
+
solution: captcha.solution,
|
|
305
326
|
commitmentId: commit.id,
|
|
306
327
|
processed: false
|
|
307
328
|
}
|
|
@@ -358,7 +379,9 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
358
379
|
async storeDappUserPending(userAccount, requestHash, salt, deadlineTimestamp, requestedAtBlock) {
|
|
359
380
|
var _a;
|
|
360
381
|
if (!is.isHex(requestHash)) {
|
|
361
|
-
throw new common.
|
|
382
|
+
throw new common.ProsopoDBError("DATABASE.INVALID_HASH", {
|
|
383
|
+
context: { failedFuncName: this.storeDappUserPending.name, requestHash }
|
|
384
|
+
});
|
|
362
385
|
}
|
|
363
386
|
const pendingRecord = {
|
|
364
387
|
accountId: userAccount,
|
|
@@ -376,13 +399,17 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
376
399
|
async getDappUserPending(requestHash) {
|
|
377
400
|
var _a;
|
|
378
401
|
if (!is.isHex(requestHash)) {
|
|
379
|
-
throw new common.ProsopoEnvError("DATABASE.INVALID_HASH",
|
|
402
|
+
throw new common.ProsopoEnvError("DATABASE.INVALID_HASH", {
|
|
403
|
+
context: { failedFuncName: this.getDappUserPending.name, requestHash }
|
|
404
|
+
});
|
|
380
405
|
}
|
|
381
406
|
const doc = await ((_a = this.tables) == null ? void 0 : _a.pending.findOne({ requestHash }).lean());
|
|
382
407
|
if (doc) {
|
|
383
408
|
return doc;
|
|
384
409
|
}
|
|
385
|
-
throw new common.ProsopoEnvError("DATABASE.PENDING_RECORD_NOT_FOUND",
|
|
410
|
+
throw new common.ProsopoEnvError("DATABASE.PENDING_RECORD_NOT_FOUND", {
|
|
411
|
+
context: { failedFuncName: this.getDappUserPending.name, requestHash }
|
|
412
|
+
});
|
|
386
413
|
}
|
|
387
414
|
/**
|
|
388
415
|
* @description Update a Dapp User's pending record
|
|
@@ -390,7 +417,9 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
390
417
|
async updateDappUserPendingStatus(userAccount, requestHash, approve) {
|
|
391
418
|
var _a;
|
|
392
419
|
if (!is.isHex(requestHash)) {
|
|
393
|
-
throw new common.ProsopoEnvError("DATABASE.INVALID_HASH",
|
|
420
|
+
throw new common.ProsopoEnvError("DATABASE.INVALID_HASH", {
|
|
421
|
+
context: { failedFuncName: this.updateDappUserPendingStatus.name, requestHash }
|
|
422
|
+
});
|
|
394
423
|
}
|
|
395
424
|
await ((_a = this.tables) == null ? void 0 : _a.pending.updateOne(
|
|
396
425
|
{ requestHash },
|
|
@@ -457,17 +486,14 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
457
486
|
if (docs && docs.length) {
|
|
458
487
|
return docs[0]._id;
|
|
459
488
|
}
|
|
460
|
-
throw new common.
|
|
489
|
+
throw new common.ProsopoDBError("DATABASE.DATASET_WITH_SOLUTIONS_GET_FAILED");
|
|
461
490
|
}
|
|
462
491
|
async getRandomSolvedCaptchasFromSingleDataset(datasetId, size) {
|
|
463
492
|
var _a;
|
|
464
493
|
if (!is.isHex(datasetId)) {
|
|
465
|
-
throw new common.
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
{},
|
|
469
|
-
datasetId
|
|
470
|
-
);
|
|
494
|
+
throw new common.ProsopoDBError("DATABASE.INVALID_HASH", {
|
|
495
|
+
context: { failedFuncName: this.getRandomSolvedCaptchasFromSingleDataset.name, datasetId }
|
|
496
|
+
});
|
|
471
497
|
}
|
|
472
498
|
const sampleSize = size ? Math.abs(Math.trunc(size)) : 1;
|
|
473
499
|
const cursor = (_a = this.tables) == null ? void 0 : _a.solution.aggregate([
|
|
@@ -485,7 +511,9 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
485
511
|
if (docs && docs.length) {
|
|
486
512
|
return docs;
|
|
487
513
|
}
|
|
488
|
-
throw new common.
|
|
514
|
+
throw new common.ProsopoDBError("DATABASE.SOLUTION_GET_FAILED", {
|
|
515
|
+
context: { failedFuncName: this.getRandomSolvedCaptchasFromSingleDataset.name, datasetId, size }
|
|
516
|
+
});
|
|
489
517
|
}
|
|
490
518
|
/**
|
|
491
519
|
* @description Get dapp user solution by ID
|
|
@@ -503,7 +531,9 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
503
531
|
if (doc) {
|
|
504
532
|
return doc;
|
|
505
533
|
}
|
|
506
|
-
throw new common.
|
|
534
|
+
throw new common.ProsopoDBError("DATABASE.SOLUTION_GET_FAILED", {
|
|
535
|
+
context: { failedFuncName: this.getCaptchaById.name, commitmentId }
|
|
536
|
+
});
|
|
507
537
|
}
|
|
508
538
|
/**
|
|
509
539
|
* @description Get dapp user commitment by user account
|
|
@@ -533,11 +563,11 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
533
563
|
try {
|
|
534
564
|
await ((_b = (_a = this.tables) == null ? void 0 : _a.commitment) == null ? void 0 : _b.findOneAndUpdate(
|
|
535
565
|
{ id: commitmentId },
|
|
536
|
-
{ $set: { status:
|
|
566
|
+
{ $set: { status: typesReturns.CaptchaStatus.approved } },
|
|
537
567
|
{ upsert: false }
|
|
538
568
|
).lean());
|
|
539
569
|
} catch (err) {
|
|
540
|
-
throw new common.
|
|
570
|
+
throw new common.ProsopoDBError("DATABASE.SOLUTION_APPROVE_FAILED", { context: { error: err, commitmentId } });
|
|
541
571
|
}
|
|
542
572
|
}
|
|
543
573
|
/**
|
|
@@ -549,7 +579,7 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
549
579
|
try {
|
|
550
580
|
await ((_b = (_a = this.tables) == null ? void 0 : _a.usersolution) == null ? void 0 : _b.updateMany({ captchaId: { $in: captchaIds } }, { $set: { processed: true } }, { upsert: false }).lean());
|
|
551
581
|
} catch (err) {
|
|
552
|
-
throw new common.
|
|
582
|
+
throw new common.ProsopoDBError("DATABASE.SOLUTION_FLAG_FAILED", { context: { error: err, captchaIds } });
|
|
553
583
|
}
|
|
554
584
|
}
|
|
555
585
|
/**
|
|
@@ -562,7 +592,7 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
562
592
|
const distinctCommitmentIds = [...new Set(commitmentIds)];
|
|
563
593
|
await ((_b = (_a = this.tables) == null ? void 0 : _a.commitment) == null ? void 0 : _b.updateMany({ id: { $in: distinctCommitmentIds } }, { $set: { processed: true } }, { upsert: false }).lean());
|
|
564
594
|
} catch (err) {
|
|
565
|
-
throw new common.
|
|
595
|
+
throw new common.ProsopoDBError("DATABASE.COMMITMENT_FLAG_FAILED", { context: { error: err, commitmentIds } });
|
|
566
596
|
}
|
|
567
597
|
}
|
|
568
598
|
/**
|
|
@@ -575,7 +605,7 @@ class ProsopoDatabase extends common.AsyncFactory {
|
|
|
575
605
|
const distinctCommitmentIds = [...new Set(commitmentIds)];
|
|
576
606
|
await ((_b = (_a = this.tables) == null ? void 0 : _a.commitment) == null ? void 0 : _b.updateMany({ id: { $in: distinctCommitmentIds } }, { $set: { batched: true } }, { upsert: false }).lean());
|
|
577
607
|
} catch (err) {
|
|
578
|
-
throw new common.
|
|
608
|
+
throw new common.ProsopoDBError("DATABASE.COMMITMENT_FLAG_FAILED", { context: { error: err, commitmentIds } });
|
|
579
609
|
}
|
|
580
610
|
}
|
|
581
611
|
/**
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const common = require("@prosopo/common");
|
|
4
|
+
const mongoose = require("mongoose");
|
|
5
|
+
const logger = common.getLoggerDefault();
|
|
6
|
+
const MAX_RETRIES = 3;
|
|
7
|
+
const captchaEventSchema = new mongoose.Schema({
|
|
8
|
+
touchEvents: [
|
|
9
|
+
{
|
|
10
|
+
x: Number,
|
|
11
|
+
y: Number,
|
|
12
|
+
timestamp: Number
|
|
13
|
+
}
|
|
14
|
+
],
|
|
15
|
+
mouseEvents: [
|
|
16
|
+
{
|
|
17
|
+
x: Number,
|
|
18
|
+
y: Number,
|
|
19
|
+
timestamp: Number
|
|
20
|
+
}
|
|
21
|
+
],
|
|
22
|
+
keyboardEvents: [
|
|
23
|
+
{
|
|
24
|
+
key: String,
|
|
25
|
+
timestamp: Number,
|
|
26
|
+
isShiftKey: { type: Boolean, required: false },
|
|
27
|
+
isCtrlKey: { type: Boolean, required: false }
|
|
28
|
+
}
|
|
29
|
+
],
|
|
30
|
+
accountId: String
|
|
31
|
+
});
|
|
32
|
+
let CaptchaEvent;
|
|
33
|
+
try {
|
|
34
|
+
CaptchaEvent = mongoose.model("CaptchaEvent");
|
|
35
|
+
} catch (error) {
|
|
36
|
+
CaptchaEvent = mongoose.model("CaptchaEvent", captchaEventSchema);
|
|
37
|
+
}
|
|
38
|
+
const addCaptchaEventRecord = async (record) => {
|
|
39
|
+
try {
|
|
40
|
+
const newRecord = new CaptchaEvent(record);
|
|
41
|
+
await newRecord.save();
|
|
42
|
+
logger.info("Record added successfully");
|
|
43
|
+
} catch (error) {
|
|
44
|
+
logger.error("Error adding record to the database:", error);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
const saveCaptchaEvent = async (events, accountId, atlasUri) => {
|
|
48
|
+
return new Promise((resolve, reject) => {
|
|
49
|
+
const connection = mongoose.createConnection(atlasUri);
|
|
50
|
+
for (let attempt = 1; attempt <= MAX_RETRIES; attempt++) {
|
|
51
|
+
connection.once("open", resolve).on("error", (e) => {
|
|
52
|
+
logger.warn(`Mongoose connection error`);
|
|
53
|
+
logger.error(e);
|
|
54
|
+
if (attempt === MAX_RETRIES) {
|
|
55
|
+
reject(
|
|
56
|
+
new common.ProsopoDBError("DATABASE.CONNECT_ERROR", {
|
|
57
|
+
context: { failedFuncName: saveCaptchaEvent.name, db: "events database" },
|
|
58
|
+
logger
|
|
59
|
+
})
|
|
60
|
+
);
|
|
61
|
+
} else {
|
|
62
|
+
connection == null ? void 0 : connection.removeAllListeners("error");
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
const captchaEventData = {
|
|
67
|
+
...events,
|
|
68
|
+
accountId
|
|
69
|
+
};
|
|
70
|
+
addCaptchaEventRecord(captchaEventData).then(() => logger.info("Captcha event data saved")).catch((error) => logger.error("Error saving captcha event data:", error)).finally(() => connection.close());
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
exports.saveCaptchaEvent = saveCaptchaEvent;
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const index = require("./databases/index.cjs");
|
|
4
|
+
require("./eventsDatabase/index.cjs");
|
|
5
|
+
const eventsDatabase = require("./eventsDatabase/eventsDatabase.cjs");
|
|
4
6
|
exports.Databases = index.Databases;
|
|
7
|
+
exports.saveCaptchaEvent = eventsDatabase.saveCaptchaEvent;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prosopo/database",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.36",
|
|
4
4
|
"description": "Prosopo database plugins for provider",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -37,11 +37,11 @@
|
|
|
37
37
|
"homepage": "https://github.com/prosopo/captcha#readme",
|
|
38
38
|
"dependencies": {
|
|
39
39
|
"@polkadot/util": "^12.5.1",
|
|
40
|
-
"@prosopo/captcha-contract": "0.2.
|
|
41
|
-
"@prosopo/common": "0.2.
|
|
42
|
-
"@prosopo/config": "0.2.
|
|
43
|
-
"@prosopo/types": "0.2.
|
|
44
|
-
"@prosopo/types-database": "0.2.
|
|
40
|
+
"@prosopo/captcha-contract": "0.2.36",
|
|
41
|
+
"@prosopo/common": "0.2.36",
|
|
42
|
+
"@prosopo/config": "0.2.36",
|
|
43
|
+
"@prosopo/types": "0.2.36",
|
|
44
|
+
"@prosopo/types-database": "0.2.36",
|
|
45
45
|
"mongodb": "5.8.0",
|
|
46
46
|
"mongodb-memory-server": "^8.7.2",
|
|
47
47
|
"mongoose": "^7.3.3"
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
exports.Error = void 0;
|
|
4
|
-
(function(Error) {
|
|
5
|
-
Error["notAuthorised"] = "NotAuthorised";
|
|
6
|
-
Error["transferFailed"] = "TransferFailed";
|
|
7
|
-
Error["setCodeHashFailed"] = "SetCodeHashFailed";
|
|
8
|
-
Error["invalidDestination"] = "InvalidDestination";
|
|
9
|
-
Error["unknownMessage"] = "UnknownMessage";
|
|
10
|
-
Error["providerAccountExists"] = "ProviderAccountExists";
|
|
11
|
-
Error["providerExists"] = "ProviderExists";
|
|
12
|
-
Error["providerAccountDoesNotExist"] = "ProviderAccountDoesNotExist";
|
|
13
|
-
Error["providerDoesNotExist"] = "ProviderDoesNotExist";
|
|
14
|
-
Error["providerInsufficientFunds"] = "ProviderInsufficientFunds";
|
|
15
|
-
Error["providerInactive"] = "ProviderInactive";
|
|
16
|
-
Error["providerUrlUsed"] = "ProviderUrlUsed";
|
|
17
|
-
Error["dappExists"] = "DappExists";
|
|
18
|
-
Error["dappDoesNotExist"] = "DappDoesNotExist";
|
|
19
|
-
Error["dappInactive"] = "DappInactive";
|
|
20
|
-
Error["dappInsufficientFunds"] = "DappInsufficientFunds";
|
|
21
|
-
Error["captchaDataDoesNotExist"] = "CaptchaDataDoesNotExist";
|
|
22
|
-
Error["commitDoesNotExist"] = "CommitDoesNotExist";
|
|
23
|
-
Error["dappUserDoesNotExist"] = "DappUserDoesNotExist";
|
|
24
|
-
Error["noActiveProviders"] = "NoActiveProviders";
|
|
25
|
-
Error["datasetIdSolutionsSame"] = "DatasetIdSolutionsSame";
|
|
26
|
-
Error["codeNotFound"] = "CodeNotFound";
|
|
27
|
-
Error["unknown"] = "Unknown";
|
|
28
|
-
Error["invalidContract"] = "InvalidContract";
|
|
29
|
-
Error["invalidPayee"] = "InvalidPayee";
|
|
30
|
-
Error["invalidCaptchaStatus"] = "InvalidCaptchaStatus";
|
|
31
|
-
Error["noCorrectCaptcha"] = "NoCorrectCaptcha";
|
|
32
|
-
Error["notEnoughActiveProviders"] = "NotEnoughActiveProviders";
|
|
33
|
-
Error["providerFeeTooHigh"] = "ProviderFeeTooHigh";
|
|
34
|
-
Error["commitAlreadyExists"] = "CommitAlreadyExists";
|
|
35
|
-
Error["notAuthor"] = "NotAuthor";
|
|
36
|
-
})(exports.Error || (exports.Error = {}));
|
|
37
|
-
exports.LangError = void 0;
|
|
38
|
-
(function(LangError) {
|
|
39
|
-
LangError["couldNotReadInput"] = "CouldNotReadInput";
|
|
40
|
-
})(exports.LangError || (exports.LangError = {}));
|
|
41
|
-
exports.Payee = void 0;
|
|
42
|
-
(function(Payee) {
|
|
43
|
-
Payee["provider"] = "Provider";
|
|
44
|
-
Payee["dapp"] = "Dapp";
|
|
45
|
-
})(exports.Payee || (exports.Payee = {}));
|
|
46
|
-
exports.DappPayee = void 0;
|
|
47
|
-
(function(DappPayee) {
|
|
48
|
-
DappPayee["provider"] = "Provider";
|
|
49
|
-
DappPayee["dapp"] = "Dapp";
|
|
50
|
-
DappPayee["any"] = "Any";
|
|
51
|
-
})(exports.DappPayee || (exports.DappPayee = {}));
|
|
52
|
-
exports.GovernanceStatus = void 0;
|
|
53
|
-
(function(GovernanceStatus) {
|
|
54
|
-
GovernanceStatus["active"] = "Active";
|
|
55
|
-
GovernanceStatus["inactive"] = "Inactive";
|
|
56
|
-
})(exports.GovernanceStatus || (exports.GovernanceStatus = {}));
|
|
57
|
-
exports.CaptchaStatus = void 0;
|
|
58
|
-
(function(CaptchaStatus) {
|
|
59
|
-
CaptchaStatus["pending"] = "Pending";
|
|
60
|
-
CaptchaStatus["approved"] = "Approved";
|
|
61
|
-
CaptchaStatus["disapproved"] = "Disapproved";
|
|
62
|
-
})(exports.CaptchaStatus || (exports.CaptchaStatus = {}));
|