@prosopo/provider 0.1.18 → 0.1.19

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 (42) hide show
  1. package/dist/api/admin.d.ts +2 -0
  2. package/dist/api/admin.d.ts.map +1 -0
  3. package/dist/api/admin.js +58 -0
  4. package/dist/api/admin.js.map +1 -0
  5. package/dist/api/captcha.d.ts +10 -0
  6. package/dist/api/captcha.d.ts.map +1 -0
  7. package/dist/api/captcha.js +166 -0
  8. package/dist/api/captcha.js.map +1 -0
  9. package/dist/batch/commitments.d.ts +23 -0
  10. package/dist/batch/commitments.d.ts.map +1 -0
  11. package/dist/batch/commitments.js +145 -0
  12. package/dist/batch/commitments.js.map +1 -0
  13. package/dist/batch/index.d.ts +2 -0
  14. package/dist/batch/index.d.ts.map +1 -0
  15. package/dist/batch/index.js +15 -0
  16. package/dist/batch/index.js.map +1 -0
  17. package/dist/index.d.ts +5 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +18 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/scheduler.d.ts +5 -0
  22. package/dist/scheduler.d.ts.map +1 -0
  23. package/dist/scheduler.js +49 -0
  24. package/dist/scheduler.js.map +1 -0
  25. package/dist/tasks/calculateSolutions.d.ts +10 -0
  26. package/dist/tasks/calculateSolutions.d.ts.map +1 -0
  27. package/dist/tasks/calculateSolutions.js +92 -0
  28. package/dist/tasks/calculateSolutions.js.map +1 -0
  29. package/dist/tasks/index.d.ts +3 -0
  30. package/dist/tasks/index.d.ts.map +1 -0
  31. package/dist/tasks/index.js +16 -0
  32. package/dist/tasks/index.js.map +1 -0
  33. package/dist/tasks/tasks.d.ts +109 -0
  34. package/dist/tasks/tasks.d.ts.map +1 -0
  35. package/dist/tasks/tasks.js +365 -0
  36. package/dist/tasks/tasks.js.map +1 -0
  37. package/dist/util.d.ts +28 -0
  38. package/dist/util.d.ts.map +1 -0
  39. package/dist/util.js +117 -0
  40. package/dist/util.js.map +1 -0
  41. package/package.json +9 -9
  42. package/Dockerfile +0 -12
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=admin.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"admin.d.ts","sourceRoot":"","sources":["../../src/api/admin.ts"],"names":[],"mappings":""}
@@ -0,0 +1,58 @@
1
+ // import { Keyring } from '@polkadot/keyring'
2
+ // import { ProviderEnvironment } from '@prosopo/types-env'
3
+ // import express, { Router } from 'express'
4
+ // import jwt from 'jsonwebtoken'
5
+ export {};
6
+ // /**
7
+ // * Admin router with auth, temp example
8
+ // *
9
+ // * @return {Router} - router stuff
10
+ // * @param {Environment} env - env stuff
11
+ // */
12
+ // export function adminRouter(env: ProviderEnvironment): Router {
13
+ // const keyring = new Keyring({ type: 'sr25519' })
14
+ // const router = express.Router()
15
+ // // Middleware to verify auth token
16
+ // const authMiddleware = (req, res, next) => {
17
+ // const authHeader = req.headers.authorization
18
+ // if (authHeader) {
19
+ // const token = authHeader.split(' ')[1]
20
+ // // Verify token using the public key
21
+ // jwt.verify(token, env.config.account.address, (err, user) => {
22
+ // if (err) {
23
+ // return res.sendStatus(403)
24
+ // }
25
+ // req.user = user
26
+ // next()
27
+ // })
28
+ // } else {
29
+ // res.sendStatus(401)
30
+ // }
31
+ // }
32
+ // // Endpoint to authenticate user using Polkadot network for signing messages
33
+ // router.post('/auth', async (req, res) => {
34
+ // const { message, signature } = req.body
35
+ // try {
36
+ // const address = env.config.account.address
37
+ // const pair = keyring.addFromAddress(env.config.account.address)
38
+ // const isValid = pair.verify(message, signature, address)
39
+ // if (isValid) {
40
+ // const user = { address }
41
+ // // temp jwt, need to verify usage with correct imports and types
42
+ // const accessToken = jwt.sign(user, env.config.account.secret, { expiresIn: '1h' })
43
+ // return res.json({ accessToken })
44
+ // } else {
45
+ // return res.status(401).json({ message: 'bad auth' })
46
+ // }
47
+ // } catch (err) {
48
+ // // todo needs proper prosopo error handling
49
+ // return res.status(500).json({ message: 'An error occurred' })
50
+ // }
51
+ // })
52
+ // // Secure endpoint which requires auth token to access
53
+ // router.get('/secure', authMiddleware, (req, res) => {
54
+ // res.json({ message: 'correct auth' })
55
+ // })
56
+ // return router
57
+ // }
58
+ //# sourceMappingURL=admin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"admin.js","sourceRoot":"","sources":["../../src/api/admin.ts"],"names":[],"mappings":"AAAA,8CAA8C;AAC9C,2DAA2D;AAC3D,4CAA4C;AAC5C,iCAAiC;;AAEjC,MAAM;AACN,0CAA0C;AAC1C,KAAK;AACL,qCAAqC;AACrC,0CAA0C;AAC1C,MAAM;AACN,kEAAkE;AAClE,uDAAuD;AAEvD,sCAAsC;AAEtC,yCAAyC;AACzC,mDAAmD;AACnD,uDAAuD;AAEvD,4BAA4B;AAC5B,qDAAqD;AAErD,mDAAmD;AACnD,6EAA6E;AAC7E,6BAA6B;AAC7B,iDAAiD;AACjD,oBAAoB;AAEpB,kCAAkC;AAClC,yBAAyB;AACzB,iBAAiB;AACjB,mBAAmB;AACnB,kCAAkC;AAClC,YAAY;AACZ,QAAQ;AAER,mFAAmF;AACnF,iDAAiD;AACjD,kDAAkD;AAElD,gBAAgB;AAChB,yDAAyD;AACzD,8EAA8E;AAC9E,uEAAuE;AAEvE,6BAA6B;AAC7B,2CAA2C;AAE3C,mFAAmF;AACnF,qGAAqG;AAErG,mDAAmD;AACnD,uBAAuB;AACvB,uEAAuE;AACvE,gBAAgB;AAChB,0BAA0B;AAC1B,0DAA0D;AAC1D,4EAA4E;AAC5E,YAAY;AACZ,SAAS;AAET,6DAA6D;AAC7D,4DAA4D;AAC5D,gDAAgD;AAChD,SAAS;AAET,oBAAoB;AACpB,IAAI"}
@@ -0,0 +1,10 @@
1
+ import { ProviderEnvironment } from '@prosopo/types-env';
2
+ import { Router } from 'express';
3
+ /**
4
+ * Returns a router connected to the database which can interact with the Proposo protocol
5
+ *
6
+ * @return {Router} - A middleware router that can interact with the Prosopo protocol
7
+ * @param {Environment} env - The Prosopo environment
8
+ */
9
+ export declare function prosopoRouter(env: ProviderEnvironment): Router;
10
+ //# sourceMappingURL=captcha.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"captcha.d.ts","sourceRoot":"","sources":["../../src/api/captcha.ts"],"names":[],"mappings":"AAyBA,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAA;AAMxD,OAAgB,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAEzC;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,mBAAmB,GAAG,MAAM,CAmJ9D"}
@@ -0,0 +1,166 @@
1
+ // Copyright 2021-2023 Prosopo (UK) Ltd.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { ApiParams, ApiPaths, CaptchaSolutionBody, CaptchaStatus, VerifySolutionBody, } from '@prosopo/types';
15
+ import { CaptchaRequestBody } from '@prosopo/types';
16
+ import { ProsopoApiError } from '@prosopo/common';
17
+ import { Tasks } from '../tasks/tasks.js';
18
+ import { parseBlockNumber } from '../util.js';
19
+ import { parseCaptchaAssets } from '@prosopo/datasets';
20
+ import { validateAddress } from '@polkadot/util-crypto';
21
+ import express from 'express';
22
+ /**
23
+ * Returns a router connected to the database which can interact with the Proposo protocol
24
+ *
25
+ * @return {Router} - A middleware router that can interact with the Prosopo protocol
26
+ * @param {Environment} env - The Prosopo environment
27
+ */
28
+ export function prosopoRouter(env) {
29
+ const router = express.Router();
30
+ const tasks = new Tasks(env);
31
+ /**
32
+ * Provides a Captcha puzzle to a Dapp User
33
+ * @param {string} datasetId - Provider datasetId
34
+ * @param {string} userAccount - Dapp User AccountId
35
+ * @param {string} blockNumber - Block number
36
+ * @return {Captcha} - The Captcha data
37
+ */
38
+ router.get(`${ApiPaths.GetCaptchaChallenge}/:${ApiParams.datasetId}/:${ApiParams.user}/:${ApiParams.dapp}/:${ApiParams.blockNumber}`, async (req, res, next) => {
39
+ try {
40
+ const { blockNumber, datasetId, user, dapp } = CaptchaRequestBody.parse(req.params);
41
+ const api = env.api;
42
+ if (api === undefined) {
43
+ throw new Error('api not setup');
44
+ }
45
+ validateAddress(user, false, api.registry.chainSS58);
46
+ const blockNumberParsed = parseBlockNumber(blockNumber);
47
+ await tasks.validateProviderWasRandomlyChosen(user, dapp, datasetId, blockNumberParsed);
48
+ const taskData = await tasks.getRandomCaptchasAndRequestHash(datasetId, user);
49
+ taskData.captchas = taskData.captchas.map((cwp) => ({
50
+ ...cwp,
51
+ captcha: {
52
+ ...cwp.captcha,
53
+ items: cwp.captcha.items.map((item) => parseCaptchaAssets(item, env.assetsResolver)),
54
+ },
55
+ }));
56
+ return res.json(taskData);
57
+ }
58
+ catch (err) {
59
+ // TODO fix error handling
60
+ return next(new ProsopoApiError(err, undefined, 400));
61
+ }
62
+ });
63
+ /**
64
+ * Receives solved CAPTCHA challenges, store to database, and check against solution commitment
65
+ *
66
+ * @param {string} userAccount - Dapp User id
67
+ * @param {string} dappAccount - Dapp Contract AccountId
68
+ * @param {Captcha[]} captchas - The Captcha solutions
69
+ * @return {DappUserSolutionResult} - The Captcha solution result and proof
70
+ */
71
+ router.post(ApiPaths.SubmitCaptchaSolution, async (req, res, next) => {
72
+ let parsed;
73
+ try {
74
+ parsed = CaptchaSolutionBody.parse(req.body);
75
+ }
76
+ catch (err) {
77
+ // TODO fix error handling
78
+ return next(new ProsopoApiError(err, undefined, 400));
79
+ }
80
+ try {
81
+ const result = await tasks.dappUserSolution(parsed[ApiParams.user], parsed[ApiParams.dapp], parsed[ApiParams.requestHash], parsed[ApiParams.captchas], parsed[ApiParams.signature]);
82
+ return res.json({
83
+ status: req.i18n.t(result.solutionApproved ? 'API.CAPTCHA_PASSED' : 'API.CAPTCHA_FAILED'),
84
+ ...result,
85
+ });
86
+ }
87
+ catch (err) {
88
+ // TODO fix error handling
89
+ return next(new ProsopoApiError(err, undefined, 400));
90
+ }
91
+ });
92
+ /**
93
+ * Verifies a user's solution as being approved or not
94
+ *
95
+ * @param {string} userAccount - Dapp User id
96
+ * @param {string} commitmentId - The captcha solution to look up
97
+ */
98
+ router.post(ApiPaths.VerifyCaptchaSolution, async (req, res, next) => {
99
+ let parsed;
100
+ try {
101
+ parsed = VerifySolutionBody.parse(req.body);
102
+ }
103
+ catch (err) {
104
+ // TODO fix error handling
105
+ return next(new ProsopoApiError(err, undefined, 400));
106
+ }
107
+ try {
108
+ let solution;
109
+ let statusMessage = 'API.USER_NOT_VERIFIED';
110
+ if (!parsed.commitmentId) {
111
+ solution = await tasks.getDappUserCommitmentByAccount(parsed.user);
112
+ }
113
+ else {
114
+ solution = await tasks.getDappUserCommitmentById(parsed.commitmentId);
115
+ }
116
+ if (solution) {
117
+ let approved = false;
118
+ if (solution.status === CaptchaStatus.approved) {
119
+ statusMessage = 'API.USER_VERIFIED';
120
+ approved = true;
121
+ }
122
+ return res.json({
123
+ status: req.t(statusMessage),
124
+ solutionApproved: approved,
125
+ commitmentId: solution.id,
126
+ });
127
+ }
128
+ return res.json({
129
+ status: req.t(statusMessage),
130
+ solutionApproved: false,
131
+ });
132
+ }
133
+ catch (err) {
134
+ // TODO fix error handling
135
+ return next(new ProsopoApiError(err, undefined, 400));
136
+ }
137
+ });
138
+ /**
139
+ * Verifies that the provider is running and registered in the contract
140
+ */
141
+ router.get(ApiPaths.GetProviderStatus, async (req, res, next) => {
142
+ try {
143
+ const status = await tasks.providerStatus();
144
+ return res.json({ status });
145
+ }
146
+ catch (err) {
147
+ // TODO fix error handling
148
+ return next(new ProsopoApiError(err, undefined, 400));
149
+ }
150
+ });
151
+ /**
152
+ * Gets public details of the provider
153
+ */
154
+ router.get(ApiPaths.GetProviderDetails, async (req, res, next) => {
155
+ try {
156
+ const details = await tasks.getProviderDetails();
157
+ return res.json(details);
158
+ }
159
+ catch (err) {
160
+ // TODO fix error handling
161
+ return next(new ProsopoApiError(err, undefined, 400));
162
+ }
163
+ });
164
+ return router;
165
+ }
166
+ //# sourceMappingURL=captcha.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"captcha.js","sourceRoot":"","sources":["../../src/api/captcha.ts"],"names":[],"mappings":"AAAA,wCAAwC;AACxC,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AACjC,OAAO,EACH,SAAS,EACT,QAAQ,EACR,mBAAmB,EACnB,aAAa,EAGb,kBAAkB,GACrB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AAEnD,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,EAAE,KAAK,EAAE,MAAM,mBAAmB,CAAA;AAEzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAA;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA;AACvD,OAAO,OAAmB,MAAM,SAAS,CAAA;AAEzC;;;;;GAKG;AACH,MAAM,UAAU,aAAa,CAAC,GAAwB;IAClD,MAAM,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAA;IAC/B,MAAM,KAAK,GAAG,IAAI,KAAK,CAAC,GAAG,CAAC,CAAA;IAE5B;;;;;;OAMG;IACH,MAAM,CAAC,GAAG,CACN,GAAG,QAAQ,CAAC,mBAAmB,KAAK,SAAS,CAAC,SAAS,KA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C,CAAA;SACjE;IACL,CAAC,CAAC,CAAA;IAEF;;OAEG;IACH,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,iBAAiB,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAC5D,IAAI;YACA,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,cAAc,EAAE,CAAA;YAC3C,OAAO,GAAG,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC,CAAA;SAC9B;QAAC,OAAO,GAAG,EAAE;YACV,0BAA0B;YAC1B,OAAO,IAAI,CAAC,IAAI,eAAe,CAAC,GAAY,EAAE,SAAS,EAAE,GAAG,CAAC,CAAC,CAAA;SACjE;IACL,CAAC,CAAC,CAAA;IAEF;;OAEG;IACH,MAAM,CAAC,GAAG,CAAC,QAAQ,CAAC,kBAAkB,EAAE,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE;QAC7D,IAAI;YACA,MAAM,OAAO,GAAG,MAAM,KAAK,CAAC,kBAAkB,EAAE,CAAA;YAChD,OAAO,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,CAAA;SAC3B;QAAC,OAAO,GAAG,EAAE;YACV,0BAA0B;YAC1B,OAAO,IAAI,CAAC,IAAI,eAAe,CAAC,GAAY,EAAE,SAAS,EAAE,GAAG,CAAC,CAAC,CAAA;SACjE;IACL,CAAC,CAAC,CAAA;IAEF,OAAO,MAAM,CAAA;AACjB,CAAC"}
@@ -0,0 +1,23 @@
1
+ /// <reference types="bn.js" />
2
+ import { ArgumentTypes, BatchCommitConfig, Commit, ExtrinsicBatch } from '@prosopo/types';
3
+ import { BN } from '@polkadot/util';
4
+ import { Database, UserCommitmentRecord } from '@prosopo/types-database';
5
+ import { Logger } from '@prosopo/common';
6
+ import { ProsopoCaptchaContract } from '@prosopo/contract';
7
+ import { WeightV2 } from '@polkadot/types/interfaces';
8
+ export declare class BatchCommitmentsTask {
9
+ contract: ProsopoCaptchaContract;
10
+ db: Database;
11
+ batchCommitConfig: BatchCommitConfig;
12
+ logger: Logger;
13
+ private nonce;
14
+ constructor(batchCommitConfig: BatchCommitConfig, contractApi: ProsopoCaptchaContract, db: Database, startNonce: bigint, logger: Logger);
15
+ run(): Promise<void>;
16
+ createExtrinsics(commitments: UserCommitmentRecord[]): Promise<ExtrinsicBatch>;
17
+ extrinsicTooHigh(totalRefTime: BN, totalProofSize: BN, maxBlockWeight: WeightV2): boolean;
18
+ batchIntervalExceeded(): Promise<boolean>;
19
+ getCommitments(): Promise<UserCommitmentRecord[]>;
20
+ flagBatchedCommitments(commitmentIds: ArgumentTypes.Hash[]): Promise<void>;
21
+ convertCommit(commitment: UserCommitmentRecord): Commit;
22
+ }
23
+ //# sourceMappingURL=commitments.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commitments.d.ts","sourceRoot":"","sources":["../../src/batch/commitments.ts"],"names":[],"mappings":";AAcA,OAAO,EACH,aAAa,EACb,iBAAiB,EACjB,MAAM,EACN,cAAc,EAGjB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAA;AACnC,OAAO,EAAE,QAAQ,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAA;AACxE,OAAO,EAAE,MAAM,EAAE,MAAM,iBAAiB,CAAA;AACxC,OAAO,EAAE,sBAAsB,EAA0D,MAAM,mBAAmB,CAAA;AAElH,OAAO,EAAE,QAAQ,EAAE,MAAM,4BAA4B,CAAA;AAOrD,qBAAa,oBAAoB;IAC7B,QAAQ,EAAE,sBAAsB,CAAA;IAChC,EAAE,EAAE,QAAQ,CAAA;IACZ,iBAAiB,EAAE,iBAAiB,CAAA;IACpC,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,KAAK,CAAQ;gBAEjB,iBAAiB,EAAE,iBAAiB,EACpC,WAAW,EAAE,sBAAsB,EACnC,EAAE,EAAE,QAAQ,EACZ,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM;IAQZ,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;IAuDpB,gBAAgB,CAAC,WAAW,EAAE,oBAAoB,EAAE,GAAG,OAAO,CAAC,cAAc,CAAC;IAkFpF,gBAAgB,CAAC,YAAY,EAAE,EAAE,EAAE,cAAc,EAAE,EAAE,EAAE,cAAc,EAAE,QAAQ,GAAG,OAAO;IAOnF,qBAAqB,IAAI,OAAO,CAAC,OAAO,CAAC;IAMzC,cAAc,IAAI,OAAO,CAAC,oBAAoB,EAAE,CAAC;IAKjD,sBAAsB,CAAC,aAAa,EAAE,aAAa,CAAC,IAAI,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAIhF,aAAa,CAAC,UAAU,EAAE,oBAAoB,GAAG,MAAM;CAY1D"}
@@ -0,0 +1,145 @@
1
+ import { ScheduledTaskNames, ScheduledTaskStatus, } from '@prosopo/types';
2
+ import { BN } from '@polkadot/util';
3
+ import { ProsopoContractError, batch, encodeStringArgs, oneUnit } from '@prosopo/contract';
4
+ import { checkIfTaskIsRunning } from '../util.js';
5
+ import { randomAsHex } from '@polkadot/util-crypto';
6
+ const BN_TEN_THOUSAND = new BN(10000);
7
+ const CONTRACT_METHOD_NAME = 'providerCommitMany';
8
+ export class BatchCommitmentsTask {
9
+ constructor(batchCommitConfig, contractApi, db, startNonce, logger) {
10
+ this.contract = contractApi;
11
+ this.db = db;
12
+ this.batchCommitConfig = batchCommitConfig;
13
+ this.logger = logger;
14
+ this.nonce = startNonce;
15
+ }
16
+ async run() {
17
+ // create a task id
18
+ const taskId = randomAsHex(32);
19
+ const taskRunning = await checkIfTaskIsRunning(ScheduledTaskNames.BatchCommitment, this.db);
20
+ // taskRunning and intervalExceeded checks separated over multiple lines to avoid race conditions between providers
21
+ if (!taskRunning) {
22
+ const intervalExceeded = await this.batchIntervalExceeded();
23
+ if (intervalExceeded) {
24
+ try {
25
+ // update last commit time
26
+ await this.db.storeScheduledTaskStatus(taskId, ScheduledTaskNames.BatchCommitment, ScheduledTaskStatus.Running);
27
+ //get commitments
28
+ const commitments = await this.getCommitments();
29
+ if (commitments.length > 0) {
30
+ this.logger.info(`Found ${commitments.length} commitments to commit`);
31
+ // get the extrinsics that are to be batched and an id associated with each one
32
+ const { extrinsics, ids: commitmentIds } = await this.createExtrinsics(commitments);
33
+ // commit and get the Ids of the commitments that were committed on-chain
34
+ await batch(this.contract.contract, this.contract.pair, extrinsics, this.logger);
35
+ // flag commitments as batched
36
+ await this.flagBatchedCommitments(commitmentIds);
37
+ // update last commit time and store the commitmentIds that were batched
38
+ await this.db.storeScheduledTaskStatus(taskId, ScheduledTaskNames.BatchCommitment, ScheduledTaskStatus.Completed, {
39
+ data: {
40
+ commitmentIds: commitments
41
+ .filter((commitment) => commitmentIds.indexOf(commitment.id) > -1)
42
+ .map((c) => c.id),
43
+ },
44
+ });
45
+ }
46
+ }
47
+ catch (e) {
48
+ const err = e;
49
+ this.logger.error(e);
50
+ await this.db.storeScheduledTaskStatus(taskId, ScheduledTaskNames.BatchCommitment, ScheduledTaskStatus.Failed, {
51
+ error: JSON.stringify(e && err.message ? err.message : e),
52
+ });
53
+ }
54
+ }
55
+ }
56
+ }
57
+ async createExtrinsics(commitments) {
58
+ const txs = [];
59
+ const fragment = this.contract.abi.findMessage(CONTRACT_METHOD_NAME);
60
+ const batchedCommitmentIds = [];
61
+ let totalRefTime = new BN(0);
62
+ let totalProofSize = new BN(0);
63
+ let totalFee = new BN(0);
64
+ const maxBlockWeight = this.contract.api.consts.system.blockWeights.maxBlock;
65
+ const commitmentArray = [];
66
+ let extrinsic;
67
+ for (const commitment of commitments) {
68
+ const commit = this.convertCommit(commitment);
69
+ commitmentArray.push(commit);
70
+ const encodedArgs = encodeStringArgs(this.contract.abi, fragment, [commitmentArray]);
71
+ // TODO can we get storage deposit from the provided query method?
72
+ // https://matrix.to/#/!utTuYglskDvqRRMQta:matrix.org/$tELySFxCORlHCHveOknGJBx-MdVe-SxFN8_BsYvcDmI?via=matrix.org&via=t2bot.io&via=cardinal.ems.host
73
+ // const response = await this.contract.query.providerCommitMany(commitmentArray)
74
+ const buildExtrinsicResult = await this.contract.getExtrinsicAndGasEstimates('providerCommitMany', encodedArgs);
75
+ extrinsic = buildExtrinsicResult.extrinsic;
76
+ const { options, storageDeposit } = buildExtrinsicResult;
77
+ let paymentInfo;
78
+ try {
79
+ paymentInfo = (await extrinsic.paymentInfo(this.contract.pair)).partialFee.toBn();
80
+ this.logger.debug(`${CONTRACT_METHOD_NAME} paymentInfo:`, paymentInfo.toNumber());
81
+ }
82
+ catch (e) {
83
+ // TODO https://github.com/polkadot-js/api/issues/5504
84
+ paymentInfo = new BN(0);
85
+ }
86
+ //totalEncodedLength += extrinsic.encodedLength
87
+ totalRefTime = totalRefTime.add(this.contract.api.registry.createType('WeightV2', options.gasLimit).refTime.toBn());
88
+ totalProofSize = totalProofSize.add(this.contract.api.registry.createType('WeightV2', options.gasLimit).proofSize.toBn());
89
+ totalFee = totalFee.add(paymentInfo.add(storageDeposit.asCharge.toBn()));
90
+ const extrinsicTooHigh = this.extrinsicTooHigh(totalRefTime, totalProofSize, maxBlockWeight);
91
+ this.logger.debug('Free balance', '`', (await this.contract.api.query.system.account(this.contract.pair.address)).data.free
92
+ .toBn()
93
+ .div(oneUnit(this.contract.api))
94
+ .toString(), '`', 'UNIT');
95
+ this.logger.debug('Total Fee `', totalFee.div(oneUnit(this.contract.api)).toString(), '`', 'UNIT');
96
+ const feeTooHigh = totalFee.gt((await this.contract.api.query.system.account(this.contract.pair.address)).data.free.toBn());
97
+ // Check if we have a maximum number of transactions that we can successfully submit in a block or if the
98
+ // total fee is more than the provider has left in their account
99
+ if (extrinsicTooHigh || feeTooHigh) {
100
+ const msg = extrinsicTooHigh ? 'Max batch extrinsic percentage reached' : 'Fee too high';
101
+ this.logger.warn(msg);
102
+ break;
103
+ }
104
+ else {
105
+ batchedCommitmentIds.push(commitment.id);
106
+ }
107
+ }
108
+ if (!extrinsic) {
109
+ throw new ProsopoContractError('No extrinsics created');
110
+ }
111
+ txs.push(extrinsic);
112
+ this.logger.info(`${txs.length} transactions will be batched`);
113
+ this.logger.debug('totalRefTime:', totalRefTime.toString());
114
+ this.logger.debug('totalProofSize:', totalProofSize.toString());
115
+ return { extrinsics: txs, ids: batchedCommitmentIds, totalFee, totalRefTime, totalProofSize };
116
+ }
117
+ extrinsicTooHigh(totalRefTime, totalProofSize, maxBlockWeight) {
118
+ return (totalRefTime.mul(BN_TEN_THOUSAND).div(maxBlockWeight.refTime.toBn()).toNumber() / 100 >
119
+ this.batchCommitConfig.maxBatchExtrinsicPercentage);
120
+ }
121
+ async batchIntervalExceeded() {
122
+ //if time since last commit > batchCommitInterval
123
+ const lastTime = await this.db.getLastBatchCommitTime();
124
+ return Date.now() - lastTime.getSeconds() > this.batchCommitConfig.interval;
125
+ }
126
+ async getCommitments() {
127
+ // get commitments that have not yet been batched on-chain
128
+ return await this.db.getUnbatchedDappUserCommitments();
129
+ }
130
+ async flagBatchedCommitments(commitmentIds) {
131
+ await this.db.flagBatchedDappUserCommitments(commitmentIds);
132
+ }
133
+ convertCommit(commitment) {
134
+ const { batched, processed, userSignature, requestedAt, completedAt, ...commit } = commitment;
135
+ return {
136
+ ...commit,
137
+ userSignaturePart1: userSignature.slice(0, userSignature.length / 2),
138
+ userSignaturePart2: userSignature.slice(userSignature.length / 2),
139
+ // to satisfy typescript
140
+ requestedAt: new BN(requestedAt).toNumber(),
141
+ completedAt: new BN(completedAt).toNumber(),
142
+ };
143
+ }
144
+ }
145
+ //# sourceMappingURL=commitments.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"commitments.js","sourceRoot":"","sources":["../../src/batch/commitments.ts"],"names":[],"mappings":"AAcA,OAAO,EAKH,kBAAkB,EAClB,mBAAmB,GACtB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,EAAE,EAAE,MAAM,gBAAgB,CAAA;AAGnC,OAAO,EAA0B,oBAAoB,EAAE,KAAK,EAAE,gBAA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YAC3C,WAAW,EAAE,IAAI,EAAE,CAAC,WAAW,CAAC,CAAC,QAAQ,EAAE;SAC9C,CAAA;IACL,CAAC;CACJ"}
@@ -0,0 +1,2 @@
1
+ export * from './commitments.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/batch/index.ts"],"names":[],"mappings":"AAaA,cAAc,kBAAkB,CAAA"}
@@ -0,0 +1,15 @@
1
+ // Copyright 2021-2023 Prosopo (UK) Ltd.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ export * from './commitments.js';
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/batch/index.ts"],"names":[],"mappings":"AAAA,wCAAwC;AACxC,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AACjC,cAAc,kBAAkB,CAAA"}
@@ -0,0 +1,5 @@
1
+ export * from './tasks/index.js';
2
+ export * from './util.js';
3
+ export * from './batch/index.js';
4
+ export * from './api/captcha.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,18 @@
1
+ // Copyright 2021-2023 Prosopo (UK) Ltd.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ export * from './tasks/index.js';
15
+ export * from './util.js';
16
+ export * from './batch/index.js';
17
+ export * from './api/captcha.js';
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,wCAAwC;AACxC,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AACjC,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,kBAAkB,CAAA;AAChC,cAAc,kBAAkB,CAAA"}
@@ -0,0 +1,5 @@
1
+ import { KeyringPair } from '@polkadot/keyring/types';
2
+ import { ProsopoConfig } from '@prosopo/types';
3
+ export declare function calculateSolutionsScheduler(pair: KeyringPair, config: ProsopoConfig): Promise<void>;
4
+ export declare function batchCommitScheduler(pair: KeyringPair, config: ProsopoConfig): Promise<void>;
5
+ //# sourceMappingURL=scheduler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.d.ts","sourceRoot":"","sources":["../src/scheduler.ts"],"names":[],"mappings":"AAkBA,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AACrD,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAA;AAK9C,wBAAsB,2BAA2B,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,aAAa,iBAYzF;AAED,wBAAsB,oBAAoB,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,aAAa,iBAgBlF"}
@@ -0,0 +1,49 @@
1
+ // Copyright 2021-2022 Prosopo (UK) Ltd.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ // [object Object]
15
+ // SPDX-License-Identifier: Apache-2.0
16
+ import { BatchCommitmentsTask } from './batch/commitments.js';
17
+ import { CalculateSolutionsTask } from './tasks/calculateSolutions.js';
18
+ import { CronJob } from 'cron';
19
+ import { ProsopoEnvError } from '@prosopo/common';
20
+ import { ProviderEnvironment } from '@prosopo/env';
21
+ import { at } from '@prosopo/util';
22
+ export async function calculateSolutionsScheduler(pair, config) {
23
+ const env = new ProviderEnvironment(pair, config);
24
+ await env.isReady();
25
+ const tasks = new CalculateSolutionsTask(env);
26
+ const job = new CronJob(at(process.argv, 2), () => {
27
+ env.logger.debug('CalculateSolutionsTask....');
28
+ tasks.run().catch((err) => {
29
+ env.logger.error(err);
30
+ });
31
+ });
32
+ job.start();
33
+ }
34
+ export async function batchCommitScheduler(pair, config) {
35
+ const env = new ProviderEnvironment(pair, config);
36
+ await env.isReady();
37
+ if (env.db === undefined) {
38
+ throw new ProsopoEnvError('DATABASE.DATABASE_UNDEFINED');
39
+ }
40
+ const tasks = new BatchCommitmentsTask(config.batchCommit, env.getContractInterface(), env.db, 0n, env.logger);
41
+ const job = new CronJob(at(process.argv, 2), () => {
42
+ env.logger.debug('BatchCommitmentsTask....');
43
+ tasks.run().catch((err) => {
44
+ env.logger.error(err);
45
+ });
46
+ });
47
+ job.start();
48
+ }
49
+ //# sourceMappingURL=scheduler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scheduler.js","sourceRoot":"","sources":["../src/scheduler.ts"],"names":[],"mappings":"AAAA,wCAAwC;AACxC,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AACjC,kBAAkB;AAClB,sCAAsC;AACtC,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAA;AAC7D,OAAO,EAAE,sBAAsB,EAAE,MAAM,+BAA+B,CAAA;AACtE,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAA;AAG9B,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAA;AAClD,OAAO,EAAE,EAAE,EAAE,MAAM,eAAe,CAAA;AAElC,MAAM,CAAC,KAAK,UAAU,2BAA2B,CAAC,IAAiB,EAAE,MAAqB;IACtF,MAAM,GAAG,GAAG,IAAI,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IACjD,MAAM,GAAG,CAAC,OAAO,EAAE,CAAA;IACnB,MAAM,KAAK,GAAG,IAAI,sBAAsB,CAAC,GAAG,CAAC,CAAA;IAC7C,MAAM,GAAG,GAAG,IAAI,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE;QAC9C,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,4BAA4B,CAAC,CAAA;QAC9C,KAAK,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACtB,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACzB,CAAC,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;IAEF,GAAG,CAAC,KAAK,EAAE,CAAA;AACf,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAC,IAAiB,EAAE,MAAqB;IAC/E,MAAM,GAAG,GAAG,IAAI,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,CAAA;IACjD,MAAM,GAAG,CAAC,OAAO,EAAE,CAAA;IACnB,IAAI,GAAG,CAAC,EAAE,KAAK,SAAS,EAAE;QACtB,MAAM,IAAI,eAAe,CAAC,6BAA6B,CAAC,CAAA;KAC3D;IAED,MAAM,KAAK,GAAG,IAAI,oBAAoB,CAAC,MAAM,CAAC,WAAW,EAAE,GAAG,CAAC,oBAAoB,EAAE,EAAE,GAAG,CAAC,EAAE,EAAE,EAAE,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IAC9G,MAAM,GAAG,GAAG,IAAI,OAAO,CAAC,EAAE,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,GAAG,EAAE;QAC9C,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,0BAA0B,CAAC,CAAA;QAC5C,KAAK,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;YACtB,GAAG,CAAC,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;QACzB,CAAC,CAAC,CAAA;IACN,CAAC,CAAC,CAAA;IAEF,GAAG,CAAC,KAAK,EAAE,CAAA;AACf,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { ProviderEnvironment } from '@prosopo/types-env';
2
+ import { Tasks } from './tasks.js';
3
+ export declare class CalculateSolutionsTask extends Tasks {
4
+ constructor(env: ProviderEnvironment);
5
+ /**
6
+ * Apply new captcha solutions to captcha dataset and recalculate merkle tree
7
+ */
8
+ run(): Promise<number>;
9
+ }
10
+ //# sourceMappingURL=calculateSolutions.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"calculateSolutions.d.ts","sourceRoot":"","sources":["../../src/tasks/calculateSolutions.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAA;AAExD,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAGlC,qBAAa,sBAAuB,SAAQ,KAAK;gBACjC,GAAG,EAAE,mBAAmB;IAKpC;;OAEG;IACG,GAAG,IAAI,OAAO,CAAC,MAAM,CAAC;CAqE/B"}