@prosopo/datasets 0.1.9 → 0.1.10
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/.eslintrc.js +32 -20
- package/.prettierrc.js +7 -0
- package/dist/js/captcha/captcha.d.ts +14 -11
- package/dist/js/captcha/captcha.d.ts.map +1 -1
- package/dist/js/captcha/captcha.js +50 -62
- package/dist/js/captcha/captcha.js.map +1 -1
- package/dist/js/captcha/dataset.d.ts +2 -2
- package/dist/js/captcha/dataset.d.ts.map +1 -1
- package/dist/js/captcha/dataset.js +15 -7
- package/dist/js/captcha/dataset.js.map +1 -1
- package/dist/js/captcha/merkle.d.ts +5 -5
- package/dist/js/captcha/merkle.d.ts.map +1 -1
- package/dist/js/captcha/merkle.js +18 -16
- package/dist/js/captcha/merkle.js.map +1 -1
- package/dist/js/captcha/util.d.ts +4 -0
- package/dist/js/captcha/util.d.ts.map +1 -1
- package/dist/js/captcha/util.js +24 -1
- package/dist/js/captcha/util.js.map +1 -1
- package/dist/js/types/assets.d.ts.map +1 -1
- package/dist/js/types/captcha.d.ts +14 -14
- package/dist/js/types/captcha.d.ts.map +1 -1
- package/dist/js/types/captcha.js +6 -6
- package/dist/js/types/captcha.js.map +1 -1
- package/dist/js/types/dataset.d.ts +6 -7
- package/dist/js/types/dataset.d.ts.map +1 -1
- package/dist/js/types/dataset.js +2 -2
- package/dist/js/types/dataset.js.map +1 -1
- package/dist/js/types/error.d.ts +14 -0
- package/dist/js/types/error.d.ts.map +1 -0
- package/dist/js/types/error.js +51 -0
- package/dist/js/types/error.js.map +1 -0
- package/dist/js/types/index.d.ts +1 -0
- package/dist/js/types/index.d.ts.map +1 -1
- package/dist/js/types/index.js +1 -0
- package/dist/js/types/index.js.map +1 -1
- package/dist/js/types/merkle.d.ts +5 -11
- package/dist/js/types/merkle.d.ts.map +1 -1
- package/dist/js/types/merkle.js +1 -7
- package/dist/js/types/merkle.js.map +1 -1
- package/package.json +3 -3
- package/tsconfig.json +6 -1
- package/captchas.json +0 -184
package/.eslintrc.js
CHANGED
|
@@ -1,24 +1,36 @@
|
|
|
1
1
|
module.exports = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
env: {
|
|
3
|
+
browser: true,
|
|
4
|
+
es2021: true,
|
|
5
5
|
},
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier', 'plugin:prettier/recommended'],
|
|
7
|
+
parser: '@typescript-eslint/parser',
|
|
8
|
+
parserOptions: {
|
|
9
|
+
ecmaVersion: 'latest',
|
|
10
|
+
sourceType: 'module',
|
|
11
|
+
},
|
|
12
|
+
plugins: ['unused-imports', '@typescript-eslint', 'prettier'],
|
|
13
|
+
rules: {
|
|
14
|
+
'no-unused-vars': 'off',
|
|
15
|
+
'unused-imports/no-unused-imports': 'error',
|
|
16
|
+
'unused-imports/no-unused-vars': [
|
|
17
|
+
'warn',
|
|
18
|
+
{
|
|
19
|
+
vars: 'all',
|
|
20
|
+
varsIgnorePattern: '^_',
|
|
21
|
+
args: 'after-used',
|
|
22
|
+
argsIgnorePattern: '^_',
|
|
23
|
+
},
|
|
24
|
+
],
|
|
25
|
+
//"indent": ["error", 4],
|
|
26
|
+
//"indent": "off",
|
|
27
|
+
'sort-imports': [
|
|
28
|
+
'error',
|
|
29
|
+
{
|
|
30
|
+
ignoreDeclarationSort: true,
|
|
31
|
+
allowSeparatedGroups: false,
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
'prettier/prettier': ['error'],
|
|
14
35
|
},
|
|
15
|
-
"plugins": [
|
|
16
|
-
"@typescript-eslint"
|
|
17
|
-
],
|
|
18
|
-
"rules": {
|
|
19
|
-
"indent": ["error", 4],
|
|
20
|
-
"no-empty": "warn",
|
|
21
|
-
"@typescript-eslint/ban-ts-comment": "warn",
|
|
22
|
-
"@typescript-eslint/no-empty-function": "warn"
|
|
23
|
-
}
|
|
24
36
|
}
|
package/.prettierrc.js
ADDED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AssetsResolver, Captcha, CaptchaSolution, CaptchaWithoutId, DatasetRaw, HashedSolution, Item, RawSolution } from '../types/index';
|
|
1
|
+
import { AssetsResolver, Captcha, CaptchaSolution, CaptchaWithoutId, DatasetRaw, HashedItem, HashedSolution, Item, RawSolution } from '../types/index';
|
|
2
2
|
/**
|
|
3
3
|
* Parse a dataset
|
|
4
4
|
* @return {JSON} captcha dataset, stored in JSON
|
|
@@ -8,9 +8,12 @@ export declare function parseCaptchaDataset(datasetJSON: JSON): DatasetRaw;
|
|
|
8
8
|
/**
|
|
9
9
|
* Make sure captcha solutions are in the correct format
|
|
10
10
|
* @param {JSON} captchaJSON captcha solutions received from the api
|
|
11
|
-
* @return {CaptchaSolution[]} an array of parsed captcha solutions
|
|
11
|
+
* @return {CaptchaSolution[]} an array of parsed and sorted captcha solutions
|
|
12
12
|
*/
|
|
13
|
-
export declare function
|
|
13
|
+
export declare function parseAndSortCaptchaSolutions(captchaJSON: JSON): CaptchaSolution[];
|
|
14
|
+
export declare function captchaSort<T extends {
|
|
15
|
+
captchaId: string;
|
|
16
|
+
}>(a: T, b: T): number;
|
|
14
17
|
export declare function sortAndComputeHashes(received: CaptchaSolution[], stored: Captcha[]): {
|
|
15
18
|
captchaId: string;
|
|
16
19
|
hash: string;
|
|
@@ -31,20 +34,20 @@ export declare function compareCaptchaSolutions(received: CaptchaSolution[], sto
|
|
|
31
34
|
* @return {string} the hex string hash
|
|
32
35
|
*/
|
|
33
36
|
export declare function computeCaptchaHash(captcha: CaptchaWithoutId, includeSolution: boolean | undefined, includeSalt: boolean | undefined, sortItemHashes: boolean): string;
|
|
34
|
-
export declare function
|
|
35
|
-
|
|
37
|
+
export declare function computeItemHash(item: Item): Promise<HashedItem>;
|
|
38
|
+
/**
|
|
39
|
+
* Converts an indexed captcha solution to a hashed captcha solution or simply returns the hash if it is already hashed
|
|
40
|
+
* @return {HashedSolution[]}
|
|
41
|
+
* @param {RawSolution[] | HashedSolution[]} solutions
|
|
42
|
+
* @param {Item[]} items
|
|
43
|
+
*/
|
|
44
|
+
export declare function matchItemsToSolutions(solutions: RawSolution[] | HashedSolution[], items: Item[] | undefined): HashedSolution[];
|
|
36
45
|
/**
|
|
37
46
|
* Create a unique solution commitment
|
|
38
47
|
* @param {CaptchaSolution} captcha
|
|
39
48
|
* @return {string} the hex string hash
|
|
40
49
|
*/
|
|
41
50
|
export declare function computeCaptchaSolutionHash(captcha: CaptchaSolution): string;
|
|
42
|
-
/**
|
|
43
|
-
* Map a Captcha to a Captcha solution (drop items, target, etc.)
|
|
44
|
-
* @param {Captcha} captcha
|
|
45
|
-
* @return {CaptchaSolution}
|
|
46
|
-
*/
|
|
47
|
-
export declare function convertCaptchaToCaptchaSolution(captcha: Captcha): CaptchaSolution;
|
|
48
51
|
/**
|
|
49
52
|
* Compute hash for an array of captcha ids, userAccount, and salt, which serves as the identifier for a pending request
|
|
50
53
|
* @param {string[]} captchaIds
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"captcha.d.ts","sourceRoot":"","sources":["../../../src/js/captcha/captcha.ts"],"names":[],"mappings":"AAeA,OAAO,EACH,cAAc,EACd,OAAO,EACP,eAAe,EAEf,gBAAgB,EAChB,UAAU,EAEV,cAAc,EACd,IAAI,EACJ,WAAW,EACd,MAAM,gBAAgB,
|
|
1
|
+
{"version":3,"file":"captcha.d.ts","sourceRoot":"","sources":["../../../src/js/captcha/captcha.ts"],"names":[],"mappings":"AAeA,OAAO,EACH,cAAc,EACd,OAAO,EACP,eAAe,EAEf,gBAAgB,EAChB,UAAU,EAEV,UAAU,EACV,cAAc,EACd,IAAI,EACJ,WAAW,EACd,MAAM,gBAAgB,CAAA;AAOvB;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,WAAW,EAAE,IAAI,GAAG,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7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.parseCaptchaAssets = exports.computePendingRequestHash = exports.
|
|
3
|
+
exports.parseCaptchaAssets = exports.computePendingRequestHash = exports.computeCaptchaSolutionHash = exports.matchItemsToSolutions = exports.computeItemHash = exports.computeCaptchaHash = exports.compareCaptchaSolutions = exports.sortAndComputeHashes = exports.captchaSort = exports.parseAndSortCaptchaSolutions = exports.parseCaptchaDataset = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
4
5
|
// Copyright (C) 2021-2022 Prosopo (UK) Ltd.
|
|
5
6
|
// This file is part of provider <https://github.com/prosopo/provider>.
|
|
6
7
|
//
|
|
@@ -18,7 +19,8 @@ exports.parseCaptchaAssets = exports.computePendingRequestHash = exports.convert
|
|
|
18
19
|
// along with provider. If not, see <http://www.gnu.org/licenses/>.
|
|
19
20
|
const index_1 = require("../types/index");
|
|
20
21
|
const util_1 = require("./util");
|
|
21
|
-
const
|
|
22
|
+
const error_1 = require("../types/error");
|
|
23
|
+
const util_2 = require("@polkadot/util");
|
|
22
24
|
// import {encodeAddress} from "@polkadot/util-crypto";
|
|
23
25
|
/**
|
|
24
26
|
* Parse a dataset
|
|
@@ -30,33 +32,36 @@ function parseCaptchaDataset(datasetJSON) {
|
|
|
30
32
|
return index_1.DatasetSchema.parse(datasetJSON);
|
|
31
33
|
}
|
|
32
34
|
catch (err) {
|
|
33
|
-
throw new
|
|
35
|
+
throw new error_1.ProsopoEnvError(err, 'ERRORS.DATASET.PARSE_ERROR');
|
|
34
36
|
}
|
|
35
37
|
}
|
|
36
38
|
exports.parseCaptchaDataset = parseCaptchaDataset;
|
|
37
39
|
/**
|
|
38
40
|
* Make sure captcha solutions are in the correct format
|
|
39
41
|
* @param {JSON} captchaJSON captcha solutions received from the api
|
|
40
|
-
* @return {CaptchaSolution[]} an array of parsed captcha solutions
|
|
42
|
+
* @return {CaptchaSolution[]} an array of parsed and sorted captcha solutions
|
|
41
43
|
*/
|
|
42
|
-
function
|
|
44
|
+
function parseAndSortCaptchaSolutions(captchaJSON) {
|
|
43
45
|
try {
|
|
44
|
-
|
|
46
|
+
const parsed = index_1.CaptchaSolutionArraySchema.parse(captchaJSON);
|
|
47
|
+
parsed.map((captcha) => (Object.assign(Object.assign({}, captcha), { solution: captcha.solution.sort() })));
|
|
48
|
+
return parsed;
|
|
45
49
|
}
|
|
46
50
|
catch (err) {
|
|
47
|
-
throw new
|
|
51
|
+
throw new error_1.ProsopoEnvError(err, 'ERRORS.CAPTCHA.PARSE_ERROR');
|
|
48
52
|
}
|
|
49
53
|
}
|
|
50
|
-
exports.
|
|
54
|
+
exports.parseAndSortCaptchaSolutions = parseAndSortCaptchaSolutions;
|
|
51
55
|
function captchaSort(a, b) {
|
|
52
56
|
return a.captchaId.localeCompare(b.captchaId);
|
|
53
57
|
}
|
|
58
|
+
exports.captchaSort = captchaSort;
|
|
54
59
|
function sortAndComputeHashes(received, stored) {
|
|
55
60
|
received.sort(captchaSort);
|
|
56
61
|
stored.sort(captchaSort);
|
|
57
|
-
return stored.map(({ salt, items = [], target =
|
|
62
|
+
return stored.map(({ salt, items = [], target = '', captchaId, solved }, index) => {
|
|
58
63
|
if (captchaId != received[index].captchaId) {
|
|
59
|
-
throw new
|
|
64
|
+
throw new error_1.ProsopoEnvError('CAPTCHA.ID_MISMATCH');
|
|
60
65
|
}
|
|
61
66
|
return {
|
|
62
67
|
hash: computeCaptchaHash({
|
|
@@ -94,85 +99,68 @@ exports.compareCaptchaSolutions = compareCaptchaSolutions;
|
|
|
94
99
|
*/
|
|
95
100
|
function computeCaptchaHash(captcha, includeSolution = false, includeSalt = false, sortItemHashes) {
|
|
96
101
|
try {
|
|
97
|
-
const itemHashes = captcha.items.map((item, index) =>
|
|
98
|
-
|
|
102
|
+
const itemHashes = captcha.items.map((item, index) => {
|
|
103
|
+
if (item.hash) {
|
|
104
|
+
return item.hash;
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
throw new error_1.ProsopoEnvError('CAPTCHA.MISSING_ITEM_HASH', computeCaptchaHash.name, undefined, index);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
return (0, util_1.hexHashArray)([
|
|
99
111
|
captcha.target,
|
|
100
112
|
...(includeSolution && captcha.solution ? captcha.solution.sort() : []),
|
|
101
113
|
includeSalt ? captcha.salt : '',
|
|
102
114
|
sortItemHashes ? itemHashes.sort() : itemHashes,
|
|
103
|
-
]
|
|
115
|
+
]);
|
|
104
116
|
}
|
|
105
117
|
catch (err) {
|
|
106
|
-
throw new
|
|
118
|
+
throw new error_1.ProsopoEnvError(err);
|
|
107
119
|
}
|
|
108
120
|
}
|
|
109
121
|
exports.computeCaptchaHash = computeCaptchaHash;
|
|
110
|
-
function
|
|
111
|
-
return
|
|
112
|
-
if (item.type ===
|
|
113
|
-
return Object.assign(Object.assign({}, item), { hash: (0, util_1.hexHash)(
|
|
122
|
+
function computeItemHash(item) {
|
|
123
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
124
|
+
if (item.type === 'text') {
|
|
125
|
+
return Object.assign(Object.assign({}, item), { hash: (0, util_1.hexHash)(item.data) });
|
|
126
|
+
}
|
|
127
|
+
else if (item.type === 'image') {
|
|
128
|
+
return Object.assign(Object.assign({}, item), { hash: (0, util_1.hexHash)(yield (0, util_1.downloadImage)(item.data)) });
|
|
114
129
|
}
|
|
115
130
|
else {
|
|
116
|
-
throw new
|
|
131
|
+
throw new error_1.ProsopoEnvError('CAPTCHA.INVALID_ITEM_FORMAT');
|
|
117
132
|
}
|
|
118
133
|
});
|
|
119
134
|
}
|
|
120
|
-
exports.
|
|
135
|
+
exports.computeItemHash = computeItemHash;
|
|
136
|
+
/**
|
|
137
|
+
* Converts an indexed captcha solution to a hashed captcha solution or simply returns the hash if it is already hashed
|
|
138
|
+
* @return {HashedSolution[]}
|
|
139
|
+
* @param {RawSolution[] | HashedSolution[]} solutions
|
|
140
|
+
* @param {Item[]} items
|
|
141
|
+
*/
|
|
121
142
|
function matchItemsToSolutions(solutions, items) {
|
|
122
|
-
return (solutions === null || solutions === void 0 ? void 0 : solutions.map((solution) => {
|
|
123
|
-
const hash = items
|
|
143
|
+
return ((solutions === null || solutions === void 0 ? void 0 : solutions.map((solution) => {
|
|
144
|
+
const hash = items && items[solution] && items[solution].hash ? items[solution].hash : solution;
|
|
124
145
|
if (!hash) {
|
|
125
|
-
throw new
|
|
146
|
+
throw new error_1.ProsopoEnvError('CAPTCHA.MISSING_ITEM_HASH');
|
|
147
|
+
}
|
|
148
|
+
if (!(0, util_2.isHex)(hash)) {
|
|
149
|
+
throw new error_1.ProsopoEnvError('CAPTCHA.INVALID_ITEM_HASH');
|
|
126
150
|
}
|
|
127
151
|
return hash;
|
|
128
|
-
})) || [];
|
|
152
|
+
})) || []);
|
|
129
153
|
}
|
|
130
154
|
exports.matchItemsToSolutions = matchItemsToSolutions;
|
|
131
|
-
// export function hashSolutions<T>(solutions: T[]): T[] {
|
|
132
|
-
// try {
|
|
133
|
-
// return solutions?.map(({ solution, ...rest }: any) => ({
|
|
134
|
-
// ...rest,
|
|
135
|
-
// solution: hashSolutionRaw(solution),
|
|
136
|
-
// }));
|
|
137
|
-
// } catch (err) {
|
|
138
|
-
// console.error(err);
|
|
139
|
-
// // @ts-ignore
|
|
140
|
-
// return arg0;
|
|
141
|
-
// }
|
|
142
|
-
// }
|
|
143
155
|
/**
|
|
144
156
|
* Create a unique solution commitment
|
|
145
157
|
* @param {CaptchaSolution} captcha
|
|
146
158
|
* @return {string} the hex string hash
|
|
147
159
|
*/
|
|
148
160
|
function computeCaptchaSolutionHash(captcha) {
|
|
149
|
-
return (0, util_1.
|
|
161
|
+
return (0, util_1.hexHashArray)([captcha.captchaId, captcha.captchaContentId, [...captcha.solution].sort(), captcha.salt]);
|
|
150
162
|
}
|
|
151
163
|
exports.computeCaptchaSolutionHash = computeCaptchaSolutionHash;
|
|
152
|
-
// /**
|
|
153
|
-
// * Compute hashes for an array of captchas
|
|
154
|
-
// * @param {Captcha[]} captchas
|
|
155
|
-
// * @return {Promise<CaptchaSolution[]>} captchasWithHashes
|
|
156
|
-
// */
|
|
157
|
-
// export async function computeCaptchaHashes(captchas: CaptchaWithoutId[]): Promise<CaptchaSolution[]> {
|
|
158
|
-
// const captchasWithHashes: CaptchaSolution[] = [];
|
|
159
|
-
// for (const captcha of captchas) {
|
|
160
|
-
// const captchaId = await computeCaptchaHash(captcha);
|
|
161
|
-
// const captchaWithId: Captcha = {captchaId, ...captcha};
|
|
162
|
-
// const captchaSol = convertCaptchaToCaptchaSolution(captchaWithId);
|
|
163
|
-
// captchasWithHashes.push(captchaSol);
|
|
164
|
-
// }
|
|
165
|
-
// return captchasWithHashes;
|
|
166
|
-
// }
|
|
167
|
-
/**
|
|
168
|
-
* Map a Captcha to a Captcha solution (drop items, target, etc.)
|
|
169
|
-
* @param {Captcha} captcha
|
|
170
|
-
* @return {CaptchaSolution}
|
|
171
|
-
*/
|
|
172
|
-
function convertCaptchaToCaptchaSolution(captcha) {
|
|
173
|
-
return { captchaId: captcha.captchaId, captchaContentId: captcha.captchaContentId, salt: captcha.salt, solution: captcha.solution || [] };
|
|
174
|
-
}
|
|
175
|
-
exports.convertCaptchaToCaptchaSolution = convertCaptchaToCaptchaSolution;
|
|
176
164
|
/**
|
|
177
165
|
* Compute hash for an array of captcha ids, userAccount, and salt, which serves as the identifier for a pending request
|
|
178
166
|
* @param {string[]} captchaIds
|
|
@@ -181,7 +169,7 @@ exports.convertCaptchaToCaptchaSolution = convertCaptchaToCaptchaSolution;
|
|
|
181
169
|
* @return {string}
|
|
182
170
|
*/
|
|
183
171
|
function computePendingRequestHash(captchaIds, userAccount, salt) {
|
|
184
|
-
return (0, util_1.
|
|
172
|
+
return (0, util_1.hexHashArray)([...captchaIds.sort(), userAccount, salt]);
|
|
185
173
|
}
|
|
186
174
|
exports.computePendingRequestHash = computePendingRequestHash;
|
|
187
175
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"captcha.js","sourceRoot":"","sources":["../../../src/js/captcha/captcha.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"captcha.js","sourceRoot":"","sources":["../../../src/js/captcha/captcha.ts"],"names":[],"mappings":";;;;AAAA,4CAA4C;AAC5C,uEAAuE;AACvE,EAAE;AACF,mEAAmE;AACnE,uEAAuE;AACvE,oEAAoE;AACpE,sCAAsC;AACtC,EAAE;AACF,8DAA8D;AAC9D,iEAAiE;AACjE,gEAAgE;AAChE,+CAA+C;AAC/C,EAAE;AACF,oEAAoE;AACpE,oEAAoE;AACpE,0CAYuB;AACvB,iCAA6D;AAC7D,0CAAgD;AAChD,yCAAsC;AAEtC,uDAAuD;AAEvD;;;;GAIG;AACH,SAAgB,mBAAmB,CAAC,WAAiB;IACjD,IAAI;QACA,OAAO,qBAAa,CAAC,KAAK,CAAC,WAAW,CAAC,CAAA;KAC1C;IAAC,OAAO,GAAG,EAAE;QACV,MAAM,IAAI,uBAAe,CAAC,GAAG,EAAE,4BAA4B,CAAC,CAAA;KAC/D;AACL,CAAC;AAND,kDAMC;AAED;;;;GAIG;AACH,SAAgB,4BAA4B,CAAC,WAAiB;IAC1D,IAAI;QACA,MAAM,MAAM,GAAG,kCAA0B,CAAC,KAAK,CAAC,WAAW,CAAC,CAAA;QAC5D,MAAM,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,iCAAM,OAAO,KAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,IAAG,CAAC,CAAA;QAC5E,OAAO,MAAM,CAAA;KAChB;IAAC,OAAO,GAAG,EAAE;QACV,MAAM,IAAI,uBAAe,CAAC,GAAG,EAAE,4BAA4B,CAAC,CAAA;KAC/D;AACL,CAAC;AARD,oEAQC;AAED,SAAgB,WAAW,CAAkC,CAAI,EAAE,CAAI;IACnE,OAAO,CAAC,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,C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AAU,EAAE,cAA0C;IACrF,uCAAY,IAAI,KAAE,IAAI,EAAE,CAAA,cAAc,aAAd,cAAc,uBAAd,cAAc,CAAE,YAAY,CAAC,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,KAAI,IAAI,CAAC,IAAI,IAAE;AAC3F,CAAC;AAFD,gDAEC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Dataset, DatasetRaw } from
|
|
2
|
-
import { CaptchaMerkleTree } from
|
|
1
|
+
import { Dataset, DatasetRaw } from '../types/dataset';
|
|
2
|
+
import { CaptchaMerkleTree } from './merkle';
|
|
3
3
|
export declare function buildDataset(datasetRaw: DatasetRaw): Promise<Dataset>;
|
|
4
4
|
export declare function buildCaptchaTree(dataset: Dataset, includeSolution: boolean, includeSalt: boolean, sortItemHashes: boolean): Promise<CaptchaMerkleTree>;
|
|
5
5
|
export declare function addItemHashesAndSolutionHashesToDataset(datasetRaw: DatasetRaw): Promise<Dataset>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dataset.d.ts","sourceRoot":"","sources":["../../../src/js/captcha/dataset.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"dataset.d.ts","sourceRoot":"","sources":["../../../src/js/captcha/dataset.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAGtD,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAA;AAG5C,wBAAsB,YAAY,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAwB3E;AAED,wBAAsB,gBAAgB,CAClC,OAAO,EAAE,OAAO,EAChB,eAAe,EAAE,OAAO,EACxB,WAAW,EAAE,OAAO,EACpB,cAAc,EAAE,OAAO,GACxB,OAAO,CAAC,iBAAiB,CAAC,CAY5B;AAED,wBAAsB,uCAAuC,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAgBtG"}
|
|
@@ -4,11 +4,18 @@ exports.addItemHashesAndSolutionHashesToDataset = exports.buildCaptchaTree = exp
|
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const captcha_1 = require("./captcha");
|
|
6
6
|
const merkle_1 = require("./merkle");
|
|
7
|
-
const
|
|
7
|
+
const error_1 = require("../types/error");
|
|
8
8
|
function buildDataset(datasetRaw) {
|
|
9
9
|
var _a, _b;
|
|
10
10
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
11
11
|
const dataset = yield addItemHashesAndSolutionHashesToDataset(datasetRaw);
|
|
12
|
+
// console.log(dataset.captchas[0])
|
|
13
|
+
// dataset.captchas.map((captcha: CaptchaWithoutId) => {
|
|
14
|
+
// if (captcha.target === 'car') {
|
|
15
|
+
// console.log(captcha)
|
|
16
|
+
// }
|
|
17
|
+
// })
|
|
18
|
+
// process.exit(0)
|
|
12
19
|
const contentTree = yield buildCaptchaTree(dataset, false, false, true);
|
|
13
20
|
const solutionTree = yield buildCaptchaTree(dataset, true, true, false);
|
|
14
21
|
dataset.captchas = dataset.captchas.map((captcha, index) => (Object.assign(Object.assign({}, captcha), { captchaId: solutionTree.leaves[index].hash, captchaContentId: contentTree.leaves[index].hash })));
|
|
@@ -24,22 +31,23 @@ function buildCaptchaTree(dataset, includeSolution, includeSalt, sortItemHashes)
|
|
|
24
31
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
25
32
|
try {
|
|
26
33
|
const tree = new merkle_1.CaptchaMerkleTree();
|
|
27
|
-
const
|
|
34
|
+
const datasetWithItemHashes = Object.assign({}, dataset);
|
|
35
|
+
const captchaHashes = datasetWithItemHashes.captchas.map((captcha) => (0, captcha_1.computeCaptchaHash)(captcha, includeSolution, includeSalt, sortItemHashes));
|
|
28
36
|
tree.build(captchaHashes);
|
|
29
37
|
return tree;
|
|
30
38
|
}
|
|
31
39
|
catch (err) {
|
|
32
|
-
throw new
|
|
40
|
+
throw new error_1.ProsopoEnvError('DATASET.HASH_ERROR');
|
|
33
41
|
}
|
|
34
42
|
});
|
|
35
43
|
}
|
|
36
44
|
exports.buildCaptchaTree = buildCaptchaTree;
|
|
37
45
|
function addItemHashesAndSolutionHashesToDataset(datasetRaw) {
|
|
38
46
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
return Object.assign(Object.assign({}, datasetRaw), { captchas: datasetRaw.captchas.map((captcha) => {
|
|
40
|
-
const items = (0, captcha_1.
|
|
41
|
-
return Object.assign(Object.assign({}, captcha), { items, solution: (0, captcha_1.matchItemsToSolutions)(captcha.solution, items) });
|
|
42
|
-
}) });
|
|
47
|
+
return Object.assign(Object.assign({}, datasetRaw), { captchas: yield Promise.all(datasetRaw.captchas.map((captcha) => tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
const items = yield Promise.all(captcha.items.map((item) => tslib_1.__awaiter(this, void 0, void 0, function* () { return yield (0, captcha_1.computeItemHash)(item); })));
|
|
49
|
+
return Object.assign(Object.assign(Object.assign({}, captcha), { items }), (captcha.solution !== undefined && { solution: (0, captcha_1.matchItemsToSolutions)(captcha.solution, items) }));
|
|
50
|
+
}))) });
|
|
43
51
|
});
|
|
44
52
|
}
|
|
45
53
|
exports.addItemHashesAndSolutionHashesToDataset = addItemHashesAndSolutionHashesToDataset;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dataset.js","sourceRoot":"","sources":["../../../src/js/captcha/dataset.ts"],"names":[],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"dataset.js","sourceRoot":"","sources":["../../../src/js/captcha/dataset.ts"],"names":[],"mappings":";;;;AAEA,uCAAsF;AACtF,qCAA4C;AAC5C,0CAAgD;AAEhD,SAAsB,YAAY,CAAC,UAAsB;;;QACrD,MAAM,OAAO,GAAG,MAAM,uCAAuC,CAAC,UAAU,CAAC,CAAA;QACzE,mCAAmC;QACnC,wDAAwD;QACxD,sCAAsC;QACtC,+BAA+B;QAC/B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zB,OAAO,IAAI,CAAA;SACd;QAAC,OAAO,GAAG,EAAE;YACV,MAAM,IAAI,uBAAe,CAAC,oBAAoB,CAAC,CAAA;SAClD;IACL,CAAC;CAAA;AAjBD,4CAiBC;AAED,SAAsB,uCAAuC,CAAC,UAAsB;;QAChF,OAAO,gCACA,UAAU,KACb,QAAQ,EAAE,MAAM,OAAO,CAAC,GAAG,CACvB,UAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAO,OAAO,EAAE,EAAE;gBACtC,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAO,IAAI,EAAE,EAAE,wDAAC,OAAA,MAAM,IAAA,yBAAe,EAAC,IAAI,CAAC,CAAA,GAAA,CAAC,CAAC,CAAA;gBAE/F,qDACO,OAAO,KACV,KAAK,KAEF,CAAC,OAAO,CAAC,QAAQ,KAAK,SAAS,IAAI,EAAE,QAAQ,EAAE,IAAA,+BAAqB,EAAC,OAAO,CAAC,QAAQ,EAAE,KAAK,CAAC,EAAE,CAAC,EACtG;YACL,CAAC,CAAA,CAAC,CACL,GACO,CAAA;IAChB,CAAC;CAAA;AAhBD,0FAgBC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { MerkleNodeInterface } from '../types/merkle';
|
|
1
|
+
import { MerkleLayer, MerkleLeaf, MerkleNodeInterface, MerkleProof } from '../types/merkle';
|
|
2
2
|
declare class MerkleNode implements MerkleNodeInterface {
|
|
3
3
|
hash: string;
|
|
4
4
|
parent: string | null;
|
|
@@ -6,14 +6,14 @@ declare class MerkleNode implements MerkleNodeInterface {
|
|
|
6
6
|
}
|
|
7
7
|
export declare class CaptchaMerkleTree {
|
|
8
8
|
leaves: MerkleNode[];
|
|
9
|
-
layers:
|
|
9
|
+
layers: MerkleLayer[];
|
|
10
10
|
root: MerkleNode | undefined;
|
|
11
11
|
constructor();
|
|
12
12
|
build(leaves: string[]): void;
|
|
13
|
-
buildMerkleTree(leaves:
|
|
13
|
+
buildMerkleTree(leaves: MerkleNode[]): any;
|
|
14
14
|
createParent(leftChild: any, rightChild: any): MerkleNode;
|
|
15
|
-
proof(leafHash:
|
|
15
|
+
proof(leafHash: MerkleLeaf): MerkleProof;
|
|
16
16
|
}
|
|
17
|
-
export declare function verifyProof(leaf:
|
|
17
|
+
export declare function verifyProof(leaf: MerkleLeaf, proof: MerkleProof): boolean;
|
|
18
18
|
export {};
|
|
19
19
|
//# sourceMappingURL=merkle.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merkle.d.ts","sourceRoot":"","sources":["../../../src/js/captcha/merkle.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;
|
|
1
|
+
{"version":3,"file":"merkle.d.ts","sourceRoot":"","sources":["../../../src/js/captcha/merkle.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,mBAAmB,EAAE,WAAW,EAAoB,MAAM,iBAAiB,CAAA;AAE7G,cAAM,UAAW,YAAW,mBAAmB;IAC3C,IAAI,EAAE,MAAM,CAAA;IAEZ,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;gBAET,IAAI,KAAA;CAInB;AAED,qBAAa,iBAAiB;IAC1B,MAAM,EAAE,UAAU,EAAE,CAAA;IAEpB,MAAM,EAAE,WAAW,EAAE,CAAA;IAErB,IAAI,EAAE,UAAU,GAAG,SAAS,CAAA;;IAO5B,KAAK,CAAC,MAAM,EAAE,MAAM,EAAE;IAetB,eAAe,CAAC,MAAM,EAAE,UAAU,EAAE;IAyBpC,YAAY,CAAC,SAAS,KAAA,EAAE,UAAU,KAAA,GAAG,UAAU;IAO/C,KAAK,CAAC,QAAQ,EAAE,UAAU,GAAG,WAAW;CA0B3C;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,WAAW,GAAG,OAAO,CAkBzE"}
|
|
@@ -53,7 +53,7 @@ class CaptchaMerkleTree {
|
|
|
53
53
|
const newLayer = [];
|
|
54
54
|
while (leafIndex < numLeaves) {
|
|
55
55
|
const leftChild = leaves[leafIndex];
|
|
56
|
-
const rightChild =
|
|
56
|
+
const rightChild = leafIndex + 1 < numLeaves ? leaves[leafIndex + 1] : leftChild;
|
|
57
57
|
const parentNode = this.createParent(leftChild, rightChild);
|
|
58
58
|
newLayer.push(parentNode.hash);
|
|
59
59
|
parents.push(parentNode);
|
|
@@ -63,7 +63,7 @@ class CaptchaMerkleTree {
|
|
|
63
63
|
return this.buildMerkleTree(parents);
|
|
64
64
|
}
|
|
65
65
|
createParent(leftChild, rightChild) {
|
|
66
|
-
const parent = new MerkleNode((0, util_1.
|
|
66
|
+
const parent = new MerkleNode((0, util_1.hexHashArray)([leftChild.hash, rightChild.hash]));
|
|
67
67
|
leftChild.parent = parent;
|
|
68
68
|
rightChild.parent = parent;
|
|
69
69
|
return parent;
|
|
@@ -71,27 +71,29 @@ class CaptchaMerkleTree {
|
|
|
71
71
|
proof(leafHash) {
|
|
72
72
|
const proofTree = [];
|
|
73
73
|
let layerNum = 0;
|
|
74
|
-
while (layerNum < this.layers.length) {
|
|
74
|
+
while (layerNum < this.layers.length - 1) {
|
|
75
75
|
const leafIndex = this.layers[layerNum].indexOf(leafHash);
|
|
76
76
|
// if layer 0 leaf index is 3, it should be partnered with 2: [L0,L1],[L2,L3],[L3,L4],...
|
|
77
77
|
// layer one pairs looks like [L0L1, L2L3], [L3L4, L5L6],...etc
|
|
78
|
-
|
|
78
|
+
let partnerIndex = leafIndex % 2 && leafIndex > 0 ? leafIndex - 1 : leafIndex + 1;
|
|
79
|
+
// if there are an odd number of leaves in the layer, the last leaf is duplicated
|
|
80
|
+
if (partnerIndex > this.layers[layerNum].length - 1) {
|
|
81
|
+
partnerIndex = leafIndex;
|
|
82
|
+
}
|
|
79
83
|
const pair = [leafHash];
|
|
80
84
|
const layer = this.layers[layerNum];
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
pair.unshift(layer[partnerIndex]);
|
|
88
|
-
}
|
|
85
|
+
// determine whether the leaf sits on the left or the right of its partner
|
|
86
|
+
if (partnerIndex > leafIndex) {
|
|
87
|
+
pair.push(layer[partnerIndex]);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
pair.unshift(layer[partnerIndex]);
|
|
89
91
|
}
|
|
90
|
-
proofTree.push(pair);
|
|
92
|
+
proofTree.push([pair[0], pair[1]]);
|
|
91
93
|
layerNum += 1;
|
|
92
|
-
leafHash = (0, util_1.
|
|
94
|
+
leafHash = (0, util_1.hexHashArray)(pair);
|
|
93
95
|
}
|
|
94
|
-
return proofTree;
|
|
96
|
+
return [...proofTree, [this.layers[this.layers.length - 1][0]]];
|
|
95
97
|
}
|
|
96
98
|
}
|
|
97
99
|
exports.CaptchaMerkleTree = CaptchaMerkleTree;
|
|
@@ -101,7 +103,7 @@ function verifyProof(leaf, proof) {
|
|
|
101
103
|
return false;
|
|
102
104
|
}
|
|
103
105
|
for (const [layerIndex, layer] of proof.entries()) {
|
|
104
|
-
leaf = (0, util_1.
|
|
106
|
+
leaf = (0, util_1.hexHashArray)(layer);
|
|
105
107
|
if (proof[layerIndex + 1].indexOf(leaf) === -1) {
|
|
106
108
|
return false;
|
|
107
109
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merkle.js","sourceRoot":"","sources":["../../../src/js/captcha/merkle.ts"],"names":[],"mappings":";;;AAAA,4CAA4C;AAC5C,uEAAuE;AACvE,EAAE;AACF,mEAAmE;AACnE,uEAAuE;AACvE,oEAAoE;AACpE,sCAAsC;AACtC,EAAE;AACF,8DAA8D;AAC9D,iEAAiE;AACjE,gEAAgE;AAChE,+CAA+C;AAC/C,EAAE;AACF,oEAAoE;AACpE,oEAAoE;AACpE,
|
|
1
|
+
{"version":3,"file":"merkle.js","sourceRoot":"","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AAO,KAAK,CAAA;SACf;QACD,KAAK,MAAM,CAAC,UAAU,EAAE,KAAK,CAAC,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE;YAC/C,IAAI,GAAG,IAAA,mBAAY,EAAC,KAAK,CAAC,CAAA;YAC1B,IAAI,KAAK,CAAC,UAAU,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE;gBAC5C,OAAO,KAAK,CAAA;aACf;YACD,IAAI,IAAI,KAAK,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE;gBACrC,OAAO,IAAI,CAAA;aACd;SACJ;QACD,OAAO,KAAK,CAAA;KACf;IAAC,OAAO,GAAG,EAAE;QACV,OAAO,KAAK,CAAA;KACf;AACL,CAAC;AAlBD,kCAkBC"}
|
|
@@ -1,3 +1,7 @@
|
|
|
1
1
|
export declare const HEX_HASH_BIT_LENGTH = 256;
|
|
2
|
+
export declare const ARRAY_JOINER = "";
|
|
2
3
|
export declare function hexHash(data: string | Uint8Array, bitLength?: 256 | 512 | 64 | 128 | 384 | undefined): string;
|
|
4
|
+
export declare function hexHashArray<T>(arr: T[]): string;
|
|
5
|
+
export declare function arrayJoin<T>(arr: T[], joiner?: string): string;
|
|
6
|
+
export declare function downloadImage(url: string): Promise<Uint8Array>;
|
|
3
7
|
//# sourceMappingURL=util.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../src/js/captcha/util.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../../src/js/captcha/util.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,mBAAmB,MAAM,CAAA;AACtC,eAAO,MAAM,YAAY,KAAK,CAAA;AAE9B,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,UAAU,EAAE,SAAS,CAAC,EAAE,GAAG,GAAG,GAAG,GAAG,EAAE,GAAG,GAAG,GAAG,GAAG,GAAG,SAAS,GAAG,MAAM,CAG7G;AAED,wBAAgB,YAAY,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,MAAM,CAEhD;AAED,wBAAgB,SAAS,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,MAAM,CAE9D;AAED,wBAAsB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAQpE"}
|
package/dist/js/captcha/util.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hexHash = exports.HEX_HASH_BIT_LENGTH = void 0;
|
|
3
|
+
exports.downloadImage = exports.arrayJoin = exports.hexHashArray = exports.hexHash = exports.ARRAY_JOINER = exports.HEX_HASH_BIT_LENGTH = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
4
5
|
// Copyright (C) 2021-2022 Prosopo (UK) Ltd.
|
|
5
6
|
// This file is part of provider <https://github.com/prosopo/provider>.
|
|
6
7
|
//
|
|
@@ -17,10 +18,32 @@ exports.hexHash = exports.HEX_HASH_BIT_LENGTH = void 0;
|
|
|
17
18
|
// You should have received a copy of the GNU General Public License
|
|
18
19
|
// along with provider. If not, see <http://www.gnu.org/licenses/>.
|
|
19
20
|
const util_crypto_1 = require("@polkadot/util-crypto");
|
|
21
|
+
const axios_1 = tslib_1.__importDefault(require("axios"));
|
|
22
|
+
const index_1 = require("../types/index");
|
|
20
23
|
exports.HEX_HASH_BIT_LENGTH = 256;
|
|
24
|
+
exports.ARRAY_JOINER = '';
|
|
21
25
|
function hexHash(data, bitLength) {
|
|
22
26
|
// default bit length is 256
|
|
23
27
|
return (0, util_crypto_1.blake2AsHex)(data, bitLength);
|
|
24
28
|
}
|
|
25
29
|
exports.hexHash = hexHash;
|
|
30
|
+
function hexHashArray(arr) {
|
|
31
|
+
return hexHash(arrayJoin(arr));
|
|
32
|
+
}
|
|
33
|
+
exports.hexHashArray = hexHashArray;
|
|
34
|
+
function arrayJoin(arr, joiner) {
|
|
35
|
+
return arr.join(joiner ? joiner : exports.ARRAY_JOINER);
|
|
36
|
+
}
|
|
37
|
+
exports.arrayJoin = arrayJoin;
|
|
38
|
+
function downloadImage(url) {
|
|
39
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
try {
|
|
41
|
+
return new Uint8Array((yield axios_1.default.get(url, { url, method: 'GET', responseType: 'arraybuffer' })).data);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
throw new index_1.ProsopoEnvError(error, downloadImage.name);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
exports.downloadImage = downloadImage;
|
|
26
49
|
//# sourceMappingURL=util.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../../src/js/captcha/util.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../../src/js/captcha/util.ts"],"names":[],"mappings":";;;;AAAA,4CAA4C;AAC5C,uEAAuE;AACvE,EAAE;AACF,mEAAmE;AACnE,uEAAuE;AACvE,oEAAoE;AACpE,sCAAsC;AACtC,EAAE;AACF,8DAA8D;AAC9D,iEAAiE;AACjE,gEAAgE;AAChE,+CAA+C;AAC/C,EAAE;AACF,oEAAoE;AACpE,oEAAoE;AACpE,uDAAmD;AACnD,0DAA0B;AAC1B,0CAAgD;AAEnC,QAAA,mBAAmB,GAAG,GAAG,CAAA;AACzB,QAAA,YAAY,GAAG,EAAE,CAAA;AAE9B,SAAgB,OAAO,CAAC,IAAyB,EAAE,SAAkD;IACjG,4BAA4B;IAC5B,OAAO,IAAA,yBAAW,EAAC,IAAI,EAAE,SAAS,CAAC,CAAA;AACvC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