@prosopo/provider 0.2.13 → 0.2.15
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.
- package/dist/api/admin.js +0 -56
- package/dist/api/admin.js.map +1 -1
- package/dist/api/captcha.d.ts +0 -6
- package/dist/api/captcha.d.ts.map +1 -1
- package/dist/api/captcha.js +24 -83
- package/dist/api/captcha.js.map +1 -1
- package/dist/batch/commitments.d.ts +2 -2
- package/dist/batch/commitments.d.ts.map +1 -1
- package/dist/batch/commitments.js +2 -20
- package/dist/batch/commitments.js.map +1 -1
- package/dist/batch/index.js +0 -13
- package/dist/batch/index.js.map +1 -1
- package/dist/cjs/api/captcha.cjs +23 -31
- package/dist/cjs/batch/commitments.cjs +12 -12
- package/dist/cjs/tasks/tasks.cjs +22 -21
- package/dist/cjs/util.cjs +9 -8
- package/dist/index.js +0 -13
- package/dist/index.js.map +1 -1
- package/dist/scheduler.js +0 -15
- package/dist/scheduler.js.map +1 -1
- package/dist/tasks/calculateSolutions.d.ts +0 -3
- package/dist/tasks/calculateSolutions.d.ts.map +1 -1
- package/dist/tasks/calculateSolutions.js +0 -27
- package/dist/tasks/calculateSolutions.js.map +1 -1
- package/dist/tasks/index.js +0 -13
- package/dist/tasks/index.js.map +1 -1
- package/dist/tasks/tasks.d.ts +3 -66
- package/dist/tasks/tasks.d.ts.map +1 -1
- package/dist/tasks/tasks.js +6 -87
- package/dist/tasks/tasks.js.map +1 -1
- package/dist/tests/accounts.d.ts.map +1 -1
- package/dist/tests/accounts.js +2 -16
- package/dist/tests/accounts.js.map +1 -1
- package/dist/tests/batch/commitments.test.d.ts.map +1 -1
- package/dist/tests/batch/commitments.test.js +12 -77
- package/dist/tests/batch/commitments.test.js.map +1 -1
- package/dist/tests/contract/helpers.test.d.ts.map +1 -1
- package/dist/tests/contract/helpers.test.js +3 -3
- package/dist/tests/contract/helpers.test.js.map +1 -1
- package/dist/tests/dataUtils/DatabaseAccounts.js +0 -1
- package/dist/tests/dataUtils/DatabaseAccounts.js.map +1 -1
- package/dist/tests/dataUtils/DatabasePopulator.d.ts +1 -1
- package/dist/tests/dataUtils/DatabasePopulator.d.ts.map +1 -1
- package/dist/tests/dataUtils/DatabasePopulator.js +10 -16
- package/dist/tests/dataUtils/DatabasePopulator.js.map +1 -1
- package/dist/tests/dataUtils/dapp-example-contract/loadFiles.d.ts +1 -1
- package/dist/tests/dataUtils/dapp-example-contract/loadFiles.d.ts.map +1 -1
- package/dist/tests/dataUtils/dapp-example-contract/loadFiles.js +3 -17
- package/dist/tests/dataUtils/dapp-example-contract/loadFiles.js.map +1 -1
- package/dist/tests/dataUtils/funds.d.ts +1 -16
- package/dist/tests/dataUtils/funds.d.ts.map +1 -1
- package/dist/tests/dataUtils/funds.js +2 -25
- package/dist/tests/dataUtils/funds.js.map +1 -1
- package/dist/tests/dataUtils/populateDatabase.d.ts +1 -1
- package/dist/tests/dataUtils/populateDatabase.d.ts.map +1 -1
- package/dist/tests/dataUtils/populateDatabase.js +0 -1
- package/dist/tests/dataUtils/populateDatabase.js.map +1 -1
- package/dist/tests/tasks/tasks.test.d.ts.map +1 -1
- package/dist/tests/tasks/tasks.test.js +15 -221
- package/dist/tests/tasks/tasks.test.js.map +1 -1
- package/dist/tests/util.test.js +0 -14
- package/dist/tests/util.test.js.map +1 -1
- package/dist/util.d.ts +0 -11
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +3 -33
- package/dist/util.js.map +1 -1
- package/package.json +10 -11
- package/dist/cjs/contracts/captcha/dist/build-extrinsic/captcha.cjs +0 -339
- package/dist/cjs/contracts/captcha/dist/contract-info/captcha.cjs +0 -6
- package/dist/cjs/contracts/captcha/dist/contracts/captcha.cjs +0 -79
- package/dist/cjs/contracts/captcha/dist/data/captcha.json.cjs +0 -3374
- package/dist/cjs/contracts/captcha/dist/event-data/captcha.json.cjs +0 -3
- package/dist/cjs/contracts/captcha/dist/events/captcha.cjs +0 -23
- package/dist/cjs/contracts/captcha/dist/index.cjs +0 -44
- package/dist/cjs/contracts/captcha/dist/mixed-methods/captcha.cjs +0 -470
- package/dist/cjs/contracts/captcha/dist/query/captcha.cjs +0 -468
- package/dist/cjs/contracts/captcha/dist/shared/utils.cjs +0 -31
- package/dist/cjs/contracts/captcha/dist/tx-sign-and-send/captcha.cjs +0 -426
- package/dist/cjs/contracts/captcha/dist/types-arguments/captcha.cjs +0 -62
- package/dist/tests/data/captchas.d.ts +0 -37
- package/dist/tests/data/captchas.d.ts.map +0 -1
- package/dist/tests/data/captchas.js +0 -902
- package/dist/tests/data/captchas.js.map +0 -1
- package/dist/tests/data/captchas1.json +0 -53
- package/dist/tests/data/captchas2.json +0 -69
- package/dist/tests/data/captchas3.json +0 -54
- package/dist/tests/data/captchas4.json +0 -53
package/dist/api/admin.js
CHANGED
|
@@ -1,58 +1,2 @@
|
|
|
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
1
|
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
2
|
//# sourceMappingURL=admin.js.map
|
package/dist/api/admin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"admin.js","sourceRoot":"","sources":["../../src/api/admin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"admin.js","sourceRoot":"","sources":["../../src/api/admin.ts"],"names":[],"mappings":""}
|
package/dist/api/captcha.d.ts
CHANGED
|
@@ -1,10 +1,4 @@
|
|
|
1
1
|
import { ProviderEnvironment } from '@prosopo/types-env';
|
|
2
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
3
|
export declare function prosopoRouter(env: ProviderEnvironment): Router;
|
|
10
4
|
//# sourceMappingURL=captcha.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"captcha.d.ts","sourceRoot":"","sources":["../../src/api/captcha.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"captcha.d.ts","sourceRoot":"","sources":["../../src/api/captcha.ts"],"names":[],"mappings":"AA0BA,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAA;AAKxD,OAAgB,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAQzC,wBAAgB,aAAa,CAAC,GAAG,EAAE,mBAAmB,GAAG,MAAM,CAmJ9D"}
|
package/dist/api/captcha.js
CHANGED
|
@@ -1,41 +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
1
|
import { ApiParams, ApiPaths, CaptchaSolutionBody, VerifySolutionBody, } from '@prosopo/types';
|
|
15
2
|
import { CaptchaRequestBody } from '@prosopo/types';
|
|
16
|
-
import { CaptchaStatus } from '@prosopo/captcha-contract';
|
|
3
|
+
import { CaptchaStatus } from '@prosopo/captcha-contract/types-returns';
|
|
17
4
|
import { ProsopoApiError } from '@prosopo/common';
|
|
18
5
|
import { Tasks } from '../tasks/tasks.js';
|
|
19
6
|
import { parseBlockNumber } from '../util.js';
|
|
20
7
|
import { parseCaptchaAssets } from '@prosopo/datasets';
|
|
21
|
-
import { validateAddress } from '@polkadot/util-crypto';
|
|
8
|
+
import { validateAddress } from '@polkadot/util-crypto/address';
|
|
22
9
|
import express from 'express';
|
|
23
|
-
/**
|
|
24
|
-
* Returns a router connected to the database which can interact with the Proposo protocol
|
|
25
|
-
*
|
|
26
|
-
* @return {Router} - A middleware router that can interact with the Prosopo protocol
|
|
27
|
-
* @param {Environment} env - The Prosopo environment
|
|
28
|
-
*/
|
|
29
10
|
export function prosopoRouter(env) {
|
|
30
11
|
const router = express.Router();
|
|
31
12
|
const tasks = new Tasks(env);
|
|
32
|
-
/**
|
|
33
|
-
* Provides a Captcha puzzle to a Dapp User
|
|
34
|
-
* @param {string} datasetId - Provider datasetId
|
|
35
|
-
* @param {string} userAccount - Dapp User AccountId
|
|
36
|
-
* @param {string} blockNumber - Block number
|
|
37
|
-
* @return {Captcha} - The Captcha data
|
|
38
|
-
*/
|
|
39
13
|
router.get(`${ApiPaths.GetCaptchaChallenge}/:${ApiParams.datasetId}/:${ApiParams.user}/:${ApiParams.dapp}/:${ApiParams.blockNumber}`, async (req, res, next) => {
|
|
40
14
|
try {
|
|
41
15
|
const { blockNumber, datasetId, user, dapp } = CaptchaRequestBody.parse(req.params);
|
|
@@ -47,35 +21,28 @@ export function prosopoRouter(env) {
|
|
|
47
21
|
const blockNumberParsed = parseBlockNumber(blockNumber);
|
|
48
22
|
await tasks.validateProviderWasRandomlyChosen(user, dapp, datasetId, blockNumberParsed);
|
|
49
23
|
const taskData = await tasks.getRandomCaptchasAndRequestHash(datasetId, user);
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
24
|
+
const captchaResponse = {
|
|
25
|
+
captchas: taskData.captchas.map((cwp) => ({
|
|
26
|
+
...cwp,
|
|
27
|
+
captcha: {
|
|
28
|
+
...cwp.captcha,
|
|
29
|
+
items: cwp.captcha.items.map((item) => parseCaptchaAssets(item, env.assetsResolver)),
|
|
30
|
+
},
|
|
31
|
+
})),
|
|
32
|
+
requestHash: taskData.requestHash,
|
|
33
|
+
};
|
|
34
|
+
return res.json(captchaResponse);
|
|
58
35
|
}
|
|
59
36
|
catch (err) {
|
|
60
|
-
// TODO fix error handling
|
|
61
37
|
return next(new ProsopoApiError(err, undefined, 400));
|
|
62
38
|
}
|
|
63
39
|
});
|
|
64
|
-
/**
|
|
65
|
-
* Receives solved CAPTCHA challenges, store to database, and check against solution commitment
|
|
66
|
-
*
|
|
67
|
-
* @param {string} userAccount - Dapp User id
|
|
68
|
-
* @param {string} dappAccount - Dapp Contract AccountId
|
|
69
|
-
* @param {Captcha[]} captchas - The Captcha solutions
|
|
70
|
-
* @return {DappUserSolutionResult} - The Captcha solution result and proof
|
|
71
|
-
*/
|
|
72
40
|
router.post(ApiPaths.SubmitCaptchaSolution, async (req, res, next) => {
|
|
73
41
|
let parsed;
|
|
74
42
|
try {
|
|
75
43
|
parsed = CaptchaSolutionBody.parse(req.body);
|
|
76
44
|
}
|
|
77
45
|
catch (err) {
|
|
78
|
-
// TODO fix error handling
|
|
79
46
|
return next(new ProsopoApiError(err, undefined, 400));
|
|
80
47
|
}
|
|
81
48
|
try {
|
|
@@ -86,79 +53,53 @@ export function prosopoRouter(env) {
|
|
|
86
53
|
});
|
|
87
54
|
}
|
|
88
55
|
catch (err) {
|
|
89
|
-
// TODO fix error handling
|
|
90
56
|
return next(new ProsopoApiError(err, undefined, 400));
|
|
91
57
|
}
|
|
92
58
|
});
|
|
93
|
-
/**
|
|
94
|
-
* Verifies a user's solution as being approved or not
|
|
95
|
-
*
|
|
96
|
-
* @param {string} userAccount - Dapp User id
|
|
97
|
-
* @param {string} commitmentId - The captcha solution to look up
|
|
98
|
-
*/
|
|
99
59
|
router.post(ApiPaths.VerifyCaptchaSolution, async (req, res, next) => {
|
|
100
60
|
let parsed;
|
|
101
61
|
try {
|
|
102
62
|
parsed = VerifySolutionBody.parse(req.body);
|
|
103
63
|
}
|
|
104
64
|
catch (err) {
|
|
105
|
-
// TODO fix error handling
|
|
106
65
|
return next(new ProsopoApiError(err, undefined, 400));
|
|
107
66
|
}
|
|
108
67
|
try {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
68
|
+
const solution = await (parsed.commitmentId
|
|
69
|
+
? tasks.getDappUserCommitmentById(parsed.commitmentId)
|
|
70
|
+
: tasks.getDappUserCommitmentByAccount(parsed.user));
|
|
71
|
+
if (!solution) {
|
|
72
|
+
return res.json({ status: req.t('API.USER_NOT_VERIFIED'), solutionApproved: false });
|
|
113
73
|
}
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
if (solution) {
|
|
118
|
-
let approved = false;
|
|
119
|
-
if (solution.status === CaptchaStatus.approved) {
|
|
120
|
-
statusMessage = 'API.USER_VERIFIED';
|
|
121
|
-
approved = true;
|
|
122
|
-
}
|
|
123
|
-
return res.json({
|
|
124
|
-
status: req.t(statusMessage),
|
|
125
|
-
solutionApproved: approved,
|
|
126
|
-
commitmentId: solution.id,
|
|
127
|
-
});
|
|
74
|
+
const msSinceCompleted = new Date().getTime() - solution.completedAt;
|
|
75
|
+
if (parsed.maxVerifiedTime && msSinceCompleted > parsed.maxVerifiedTime) {
|
|
76
|
+
return res.json({ status: req.t('API.USER_NOT_VERIFIED'), solutionApproved: false });
|
|
128
77
|
}
|
|
78
|
+
const isApproved = solution.status === CaptchaStatus.approved;
|
|
129
79
|
return res.json({
|
|
130
|
-
status: req.t(
|
|
131
|
-
solutionApproved:
|
|
80
|
+
status: req.t(isApproved ? 'API.USER_VERIFIED' : 'API.USER_NOT_VERIFIED'),
|
|
81
|
+
solutionApproved: isApproved,
|
|
132
82
|
});
|
|
133
83
|
}
|
|
134
84
|
catch (err) {
|
|
135
|
-
// TODO fix error handling
|
|
136
85
|
return next(new ProsopoApiError(err, undefined, 400));
|
|
137
86
|
}
|
|
138
87
|
});
|
|
139
|
-
/**
|
|
140
|
-
* Verifies that the provider is running and registered in the contract
|
|
141
|
-
*/
|
|
142
88
|
router.get(ApiPaths.GetProviderStatus, async (req, res, next) => {
|
|
143
89
|
try {
|
|
144
90
|
const status = await tasks.providerStatus();
|
|
145
91
|
return res.json({ status });
|
|
146
92
|
}
|
|
147
93
|
catch (err) {
|
|
148
|
-
// TODO fix error handling
|
|
149
94
|
return next(new ProsopoApiError(err, undefined, 400));
|
|
150
95
|
}
|
|
151
96
|
});
|
|
152
|
-
/**
|
|
153
|
-
* Gets public details of the provider
|
|
154
|
-
*/
|
|
155
97
|
router.get(ApiPaths.GetProviderDetails, async (req, res, next) => {
|
|
156
98
|
try {
|
|
157
99
|
const details = await tasks.getProviderDetails();
|
|
158
100
|
return res.json(details);
|
|
159
101
|
}
|
|
160
102
|
catch (err) {
|
|
161
|
-
// TODO fix error handling
|
|
162
103
|
return next(new ProsopoApiError(err, undefined, 400));
|
|
163
104
|
}
|
|
164
105
|
});
|
package/dist/api/captcha.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"captcha.js","sourceRoot":"","sources":["../../src/api/captcha.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"captcha.js","sourceRoot":"","sources":["../../src/api/captcha.ts"],"names":[],"mappings":"AAaA,OAAO,EACH,SAAS,EACT,QAAQ,EAER,mBAAmB,EAGnB,kBAAkB,GACrB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAA;AAEnD,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAA;AACvE,OAAO,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AAEjD,OAAO,EA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C,CAAA;SAC3B;QAAC,OAAO,GAAG,EAAE;YAEV,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"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
|
-
import { BN } from '@polkadot/util';
|
|
2
|
+
import { BN } from '@polkadot/util/bn';
|
|
3
3
|
import { BatchCommitConfigOutput, ExtrinsicBatch } from '@prosopo/types';
|
|
4
|
-
import { Commit, Hash } from '@prosopo/captcha-contract';
|
|
4
|
+
import { Commit, Hash } from '@prosopo/captcha-contract/types-returns';
|
|
5
5
|
import { Database, UserCommitmentRecord } from '@prosopo/types-database';
|
|
6
6
|
import { Logger } from '@prosopo/common';
|
|
7
7
|
import { ProsopoCaptchaContract } from '@prosopo/contract';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commitments.d.ts","sourceRoot":"","sources":["../../src/batch/commitments.ts"],"names":[],"mappings":";AAcA,OAAO,EAAE,EAAE,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"commitments.d.ts","sourceRoot":"","sources":["../../src/batch/commitments.ts"],"names":[],"mappings":";AAcA,OAAO,EAAE,EAAE,EAAE,MAAM,mBAAmB,CAAA;AACtC,OAAO,EAAE,uBAAuB,EAAE,cAAc,EAA2C,MAAM,gBAAgB,CAAA;AACjH,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,yCAAyC,CAAA;AACtE,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,uBAAuB,CAAA;IAC1C,MAAM,EAAE,MAAM,CAAA;IACd,OAAO,CAAC,KAAK,CAAQ;gBAEjB,iBAAiB,EAAE,uBAAuB,EAC1C,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,IAAI,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAIlE,aAAa,CAAC,UAAU,EAAE,oBAAoB,GAAG,MAAM;CAW1D"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { BN } from '@polkadot/util';
|
|
1
|
+
import { BN } from '@polkadot/util/bn';
|
|
2
2
|
import { ScheduledTaskNames, ScheduledTaskStatus } from '@prosopo/types';
|
|
3
3
|
import { ProsopoContractError, batch, encodeStringArgs, oneUnit } from '@prosopo/contract';
|
|
4
4
|
import { checkIfTaskIsRunning } from '../util.js';
|
|
5
|
-
import { randomAsHex } from '@polkadot/util-crypto';
|
|
5
|
+
import { randomAsHex } from '@polkadot/util-crypto/random';
|
|
6
6
|
const BN_TEN_THOUSAND = new BN(10000);
|
|
7
7
|
const CONTRACT_METHOD_NAME = 'providerCommitMany';
|
|
8
8
|
export class BatchCommitmentsTask {
|
|
@@ -14,27 +14,19 @@ export class BatchCommitmentsTask {
|
|
|
14
14
|
this.nonce = startNonce;
|
|
15
15
|
}
|
|
16
16
|
async run() {
|
|
17
|
-
// create a task id
|
|
18
17
|
const taskId = randomAsHex(32);
|
|
19
18
|
const taskRunning = await checkIfTaskIsRunning(ScheduledTaskNames.BatchCommitment, this.db);
|
|
20
|
-
// taskRunning and intervalExceeded checks separated over multiple lines to avoid race conditions between providers
|
|
21
19
|
if (!taskRunning) {
|
|
22
20
|
const intervalExceeded = await this.batchIntervalExceeded();
|
|
23
21
|
if (intervalExceeded) {
|
|
24
22
|
try {
|
|
25
|
-
// update last commit time
|
|
26
23
|
await this.db.storeScheduledTaskStatus(taskId, ScheduledTaskNames.BatchCommitment, ScheduledTaskStatus.Running);
|
|
27
|
-
//get commitments
|
|
28
24
|
const commitments = await this.getCommitments();
|
|
29
25
|
if (commitments.length > 0) {
|
|
30
26
|
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
27
|
const { extrinsics, ids: commitmentIds } = await this.createExtrinsics(commitments);
|
|
33
|
-
// commit and get the Ids of the commitments that were committed on-chain
|
|
34
28
|
await batch(this.contract.contract, this.contract.pair, extrinsics, this.logger);
|
|
35
|
-
// flag commitments as batched
|
|
36
29
|
await this.flagBatchedCommitments(commitmentIds);
|
|
37
|
-
// update last commit time and store the commitmentIds that were batched
|
|
38
30
|
await this.db.storeScheduledTaskStatus(taskId, ScheduledTaskNames.BatchCommitment, ScheduledTaskStatus.Completed, {
|
|
39
31
|
data: {
|
|
40
32
|
commitmentIds: commitments
|
|
@@ -68,9 +60,6 @@ export class BatchCommitmentsTask {
|
|
|
68
60
|
const commit = this.convertCommit(commitment);
|
|
69
61
|
commitmentArray.push(commit);
|
|
70
62
|
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
63
|
const buildExtrinsicResult = await this.contract.getExtrinsicAndGasEstimates('providerCommitMany', encodedArgs);
|
|
75
64
|
extrinsic = buildExtrinsicResult.extrinsic;
|
|
76
65
|
const { options, storageDeposit } = buildExtrinsicResult;
|
|
@@ -80,10 +69,8 @@ export class BatchCommitmentsTask {
|
|
|
80
69
|
this.logger.debug(`${CONTRACT_METHOD_NAME} paymentInfo:`, paymentInfo.toNumber());
|
|
81
70
|
}
|
|
82
71
|
catch (e) {
|
|
83
|
-
// TODO https://github.com/polkadot-js/api/issues/5504
|
|
84
72
|
paymentInfo = new BN(0);
|
|
85
73
|
}
|
|
86
|
-
//totalEncodedLength += extrinsic.encodedLength
|
|
87
74
|
totalRefTime = totalRefTime.add(this.contract.api.registry.createType('WeightV2', options.gasLimit).refTime.toBn());
|
|
88
75
|
totalProofSize = totalProofSize.add(this.contract.api.registry.createType('WeightV2', options.gasLimit).proofSize.toBn());
|
|
89
76
|
totalFee = totalFee.add(paymentInfo.add(storageDeposit.asCharge.toBn()));
|
|
@@ -94,8 +81,6 @@ export class BatchCommitmentsTask {
|
|
|
94
81
|
.toString(), '`', 'UNIT');
|
|
95
82
|
this.logger.debug('Total Fee `', totalFee.div(oneUnit(this.contract.api)).toString(), '`', 'UNIT');
|
|
96
83
|
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
84
|
if (extrinsicTooHigh || feeTooHigh) {
|
|
100
85
|
const msg = extrinsicTooHigh ? 'Max batch extrinsic percentage reached' : 'Fee too high';
|
|
101
86
|
this.logger.warn(msg);
|
|
@@ -119,12 +104,10 @@ export class BatchCommitmentsTask {
|
|
|
119
104
|
this.batchCommitConfig.maxBatchExtrinsicPercentage);
|
|
120
105
|
}
|
|
121
106
|
async batchIntervalExceeded() {
|
|
122
|
-
//if time since last commit > batchCommitInterval
|
|
123
107
|
const lastTime = await this.db.getLastBatchCommitTime();
|
|
124
108
|
return Date.now() - lastTime.getSeconds() > this.batchCommitConfig.interval;
|
|
125
109
|
}
|
|
126
110
|
async getCommitments() {
|
|
127
|
-
// get commitments that have not yet been batched on-chain
|
|
128
111
|
return await this.db.getUnbatchedDappUserCommitments();
|
|
129
112
|
}
|
|
130
113
|
async flagBatchedCommitments(commitmentIds) {
|
|
@@ -135,7 +118,6 @@ export class BatchCommitmentsTask {
|
|
|
135
118
|
return {
|
|
136
119
|
...commit,
|
|
137
120
|
userSignature,
|
|
138
|
-
// to satisfy typescript
|
|
139
121
|
requestedAt: new BN(requestedAt).toNumber(),
|
|
140
122
|
completedAt: new BN(completedAt).toNumber(),
|
|
141
123
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commitments.js","sourceRoot":"","sources":["../../src/batch/commitments.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,EAAE,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"commitments.js","sourceRoot":"","sources":["../../src/batch/commitments.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,EAAE,EAAE,MAAM,mBAAmB,CAAA;AACtC,OAAO,EAA2C,kBAAkB,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAA;AAIjH,OAAO,EAA0B,oBAAoB,EAAE,KAAK,EAAE,gBAAgB,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAGlH,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAA;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAA;AAE1D,MAAM,eAAe,GAAG,IAAI,EAAE,CAAC,KAAM,CAAC,CAAA;AACtC,MAAM,oBAAoB,GAAG,oB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G,EAAE,GAAG,QAAQ,CAAC,UAAU,EAAE,GAAG,IAAI,CAAC,iBAAiB,CAAC,QAAQ,CAAA;IAC/E,CAAC;IAED,KAAK,CAAC,cAAc;QAEhB,OAAO,MAAM,IAAI,CAAC,EAAE,CAAC,+BAA+B,EAAE,CAAA;IAC1D,CAAC;IAED,KAAK,CAAC,sBAAsB,CAAC,aAAqB;QAC9C,MAAM,IAAI,CAAC,EAAE,CAAC,8BAA8B,CAAC,aAAa,CAAC,CAAA;IAC/D,CAAC;IAED,aAAa,CAAC,UAAgC;QAC1C,MAAM,EAAE,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,WAAW,EAAE,WAAW,EAAE,GAAG,MAAM,EAAE,GAAG,UAAU,CAAA;QAE7F,OAAO;YACH,GAAG,MAAM;YACT,aAAa;YAEb,WAAW,EAAE,IAAI,EAAE,CAAC,WAAW,CAAC,CAAC,QAAQ,EAAE;YAC3C,WAAW,EAAE,IAAI,EAAE,CAAC,WAAW,CAAC,CAAC,QAAQ,EAAE;SAC9C,CAAA;IACL,CAAC;CACJ"}
|
package/dist/batch/index.js
CHANGED
|
@@ -1,15 +1,2 @@
|
|
|
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
1
|
export * from './commitments.js';
|
|
15
2
|
//# sourceMappingURL=index.js.map
|
package/dist/batch/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/batch/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/batch/index.ts"],"names":[],"mappings":"AAaA,cAAc,kBAAkB,CAAA"}
|
package/dist/cjs/api/captcha.cjs
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const types = require("@prosopo/types");
|
|
4
|
-
require("
|
|
4
|
+
const typesReturns = require("@prosopo/captcha-contract/types-returns");
|
|
5
5
|
const common = require("@prosopo/common");
|
|
6
6
|
const tasks = require("../tasks/tasks.cjs");
|
|
7
7
|
const util = require("../util.cjs");
|
|
8
8
|
const datasets = require("@prosopo/datasets");
|
|
9
|
-
const
|
|
9
|
+
const address = require("@polkadot/util-crypto/address");
|
|
10
10
|
const express = require("express");
|
|
11
|
-
const captcha = require("../contracts/captcha/dist/types-arguments/captcha.cjs");
|
|
12
11
|
function prosopoRouter(env) {
|
|
13
12
|
const router = express.Router();
|
|
14
13
|
const tasks$1 = new tasks.Tasks(env);
|
|
@@ -21,18 +20,21 @@ function prosopoRouter(env) {
|
|
|
21
20
|
if (api === void 0) {
|
|
22
21
|
throw new Error("api not setup");
|
|
23
22
|
}
|
|
24
|
-
|
|
23
|
+
address.validateAddress(user, false, api.registry.chainSS58);
|
|
25
24
|
const blockNumberParsed = util.parseBlockNumber(blockNumber);
|
|
26
25
|
await tasks$1.validateProviderWasRandomlyChosen(user, dapp, datasetId, blockNumberParsed);
|
|
27
26
|
const taskData = await tasks$1.getRandomCaptchasAndRequestHash(datasetId, user);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
27
|
+
const captchaResponse = {
|
|
28
|
+
captchas: taskData.captchas.map((cwp) => ({
|
|
29
|
+
...cwp,
|
|
30
|
+
captcha: {
|
|
31
|
+
...cwp.captcha,
|
|
32
|
+
items: cwp.captcha.items.map((item) => datasets.parseCaptchaAssets(item, env.assetsResolver))
|
|
33
|
+
}
|
|
34
|
+
})),
|
|
35
|
+
requestHash: taskData.requestHash
|
|
36
|
+
};
|
|
37
|
+
return res.json(captchaResponse);
|
|
36
38
|
} catch (err) {
|
|
37
39
|
return next(new common.ProsopoApiError(err, void 0, 400));
|
|
38
40
|
}
|
|
@@ -69,28 +71,18 @@ function prosopoRouter(env) {
|
|
|
69
71
|
return next(new common.ProsopoApiError(err, void 0, 400));
|
|
70
72
|
}
|
|
71
73
|
try {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
solution = await tasks$1.getDappUserCommitmentByAccount(parsed.user);
|
|
76
|
-
} else {
|
|
77
|
-
solution = await tasks$1.getDappUserCommitmentById(parsed.commitmentId);
|
|
74
|
+
const solution = await (parsed.commitmentId ? tasks$1.getDappUserCommitmentById(parsed.commitmentId) : tasks$1.getDappUserCommitmentByAccount(parsed.user));
|
|
75
|
+
if (!solution) {
|
|
76
|
+
return res.json({ status: req.t("API.USER_NOT_VERIFIED"), solutionApproved: false });
|
|
78
77
|
}
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
statusMessage = "API.USER_VERIFIED";
|
|
83
|
-
approved = true;
|
|
84
|
-
}
|
|
85
|
-
return res.json({
|
|
86
|
-
status: req.t(statusMessage),
|
|
87
|
-
solutionApproved: approved,
|
|
88
|
-
commitmentId: solution.id
|
|
89
|
-
});
|
|
78
|
+
const msSinceCompleted = (/* @__PURE__ */ new Date()).getTime() - solution.completedAt;
|
|
79
|
+
if (parsed.maxVerifiedTime && msSinceCompleted > parsed.maxVerifiedTime) {
|
|
80
|
+
return res.json({ status: req.t("API.USER_NOT_VERIFIED"), solutionApproved: false });
|
|
90
81
|
}
|
|
82
|
+
const isApproved = solution.status === typesReturns.CaptchaStatus.approved;
|
|
91
83
|
return res.json({
|
|
92
|
-
status: req.t(
|
|
93
|
-
solutionApproved:
|
|
84
|
+
status: req.t(isApproved ? "API.USER_VERIFIED" : "API.USER_NOT_VERIFIED"),
|
|
85
|
+
solutionApproved: isApproved
|
|
94
86
|
});
|
|
95
87
|
} catch (err) {
|
|
96
88
|
return next(new common.ProsopoApiError(err, void 0, 400));
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const
|
|
3
|
+
const bn = require("@polkadot/util/bn");
|
|
4
4
|
const types = require("@prosopo/types");
|
|
5
5
|
const contract = require("@prosopo/contract");
|
|
6
|
-
const util
|
|
7
|
-
const
|
|
8
|
-
const BN_TEN_THOUSAND = new
|
|
6
|
+
const util = require("../util.cjs");
|
|
7
|
+
const random = require("@polkadot/util-crypto/random");
|
|
8
|
+
const BN_TEN_THOUSAND = new bn.BN(1e4);
|
|
9
9
|
const CONTRACT_METHOD_NAME = "providerCommitMany";
|
|
10
10
|
class BatchCommitmentsTask {
|
|
11
11
|
constructor(batchCommitConfig, contractApi, db, startNonce, logger) {
|
|
@@ -16,8 +16,8 @@ class BatchCommitmentsTask {
|
|
|
16
16
|
this.nonce = startNonce;
|
|
17
17
|
}
|
|
18
18
|
async run() {
|
|
19
|
-
const taskId =
|
|
20
|
-
const taskRunning = await util
|
|
19
|
+
const taskId = random.randomAsHex(32);
|
|
20
|
+
const taskRunning = await util.checkIfTaskIsRunning(types.ScheduledTaskNames.BatchCommitment, this.db);
|
|
21
21
|
if (!taskRunning) {
|
|
22
22
|
const intervalExceeded = await this.batchIntervalExceeded();
|
|
23
23
|
if (intervalExceeded) {
|
|
@@ -63,9 +63,9 @@ class BatchCommitmentsTask {
|
|
|
63
63
|
const txs = [];
|
|
64
64
|
const fragment = this.contract.abi.findMessage(CONTRACT_METHOD_NAME);
|
|
65
65
|
const batchedCommitmentIds = [];
|
|
66
|
-
let totalRefTime = new
|
|
67
|
-
let totalProofSize = new
|
|
68
|
-
let totalFee = new
|
|
66
|
+
let totalRefTime = new bn.BN(0);
|
|
67
|
+
let totalProofSize = new bn.BN(0);
|
|
68
|
+
let totalFee = new bn.BN(0);
|
|
69
69
|
const maxBlockWeight = this.contract.api.consts.system.blockWeights.maxBlock;
|
|
70
70
|
const commitmentArray = [];
|
|
71
71
|
let extrinsic;
|
|
@@ -84,7 +84,7 @@ class BatchCommitmentsTask {
|
|
|
84
84
|
paymentInfo = (await extrinsic.paymentInfo(this.contract.pair)).partialFee.toBn();
|
|
85
85
|
this.logger.debug(`${CONTRACT_METHOD_NAME} paymentInfo:`, paymentInfo.toNumber());
|
|
86
86
|
} catch (e) {
|
|
87
|
-
paymentInfo = new
|
|
87
|
+
paymentInfo = new bn.BN(0);
|
|
88
88
|
}
|
|
89
89
|
totalRefTime = totalRefTime.add(
|
|
90
90
|
this.contract.api.registry.createType("WeightV2", options.gasLimit).refTime.toBn()
|
|
@@ -146,8 +146,8 @@ class BatchCommitmentsTask {
|
|
|
146
146
|
...commit,
|
|
147
147
|
userSignature,
|
|
148
148
|
// to satisfy typescript
|
|
149
|
-
requestedAt: new
|
|
150
|
-
completedAt: new
|
|
149
|
+
requestedAt: new bn.BN(requestedAt).toNumber(),
|
|
150
|
+
completedAt: new bn.BN(completedAt).toNumber()
|
|
151
151
|
};
|
|
152
152
|
}
|
|
153
153
|
}
|