@prosopo/datasets 0.1.17 → 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.
- package/dist/captcha/captcha.d.ts +1 -1
- package/dist/captcha/captcha.d.ts.map +1 -1
- package/dist/captcha/captcha.js +47 -23
- package/dist/captcha/captcha.js.map +1 -1
- package/dist/captcha/dataset.d.ts.map +1 -1
- package/dist/captcha/dataset.js +71 -62
- package/dist/captcha/dataset.js.map +1 -1
- package/dist/captcha/merkle.d.ts +3 -3
- package/dist/captcha/merkle.d.ts.map +1 -1
- package/dist/captcha/merkle.js +25 -15
- package/dist/captcha/merkle.js.map +1 -1
- package/dist/captcha/util.d.ts.map +1 -1
- package/dist/captcha/util.js +8 -10
- package/dist/captcha/util.js.map +1 -1
- package/package.json +7 -5
|
@@ -39,7 +39,7 @@ export declare function computeCaptchaHash(captcha: CaptchaWithoutId, includeSol
|
|
|
39
39
|
* solution
|
|
40
40
|
* @param solution
|
|
41
41
|
*/
|
|
42
|
-
export declare function getSolutionValueToHash(solution?: HashedSolution[] | RawSolution[]):
|
|
42
|
+
export declare function getSolutionValueToHash(solution?: HashedSolution[] | RawSolution[]): number[] | string[];
|
|
43
43
|
/** Compute the hash of a captcha item, downloading the image if necessary
|
|
44
44
|
* @param {Item} item
|
|
45
45
|
* @return {Promise<HashedItem>} the hex string hash of the item
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"captcha.d.ts","sourceRoot":"","sources":["../../src/captcha/captcha.ts"],"names":[],"mappings":"AAaA,OAAO,EACH,cAAc,EACd,OAAO,EACP,eAAe,EAEf,gBAAgB,EAChB,UAAU,EAEV,UAAU,EACV,cAAc,EACd,IAAI,EACJ,WAAW,EACd,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"captcha.d.ts","sourceRoot":"","sources":["../../src/captcha/captcha.ts"],"names":[],"mappings":"AAaA,OAAO,EACH,cAAc,EACd,OAAO,EACP,eAA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;AAED;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,eAAe,UAGlE;AAED;;;;;;GAMG;AACH,wBAAgB,yBAAyB,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,WAAW,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAEzG;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,IAAI,EAAE,cAAc,EAAE,cAAc,GAAG,SAAS;;;;;EAExF"}
|
package/dist/captcha/captcha.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { __awaiter } from "tslib";
|
|
2
1
|
// Copyright 2021-2023 Prosopo (UK) Ltd.
|
|
3
2
|
//
|
|
4
3
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -14,6 +13,7 @@ import { __awaiter } from "tslib";
|
|
|
14
13
|
// limitations under the License.
|
|
15
14
|
import { CaptchaSolutionArraySchema, DatasetSchema, } from '@prosopo/types';
|
|
16
15
|
import { ProsopoEnvError, hexHash, hexHashArray } from '@prosopo/common';
|
|
16
|
+
import { at } from '@prosopo/util';
|
|
17
17
|
import { downloadImage } from './util.js';
|
|
18
18
|
import { isHex } from '@polkadot/util';
|
|
19
19
|
export const NO_SOLUTION_VALUE = 'NO_SOLUTION';
|
|
@@ -24,10 +24,29 @@ export const NO_SOLUTION_VALUE = 'NO_SOLUTION';
|
|
|
24
24
|
*/
|
|
25
25
|
export function parseCaptchaDataset(datasetJSON) {
|
|
26
26
|
try {
|
|
27
|
-
|
|
27
|
+
const result = DatasetSchema.parse(datasetJSON);
|
|
28
|
+
const result2 = {
|
|
29
|
+
format: result.format,
|
|
30
|
+
captchas: result.captchas.map((captcha) => {
|
|
31
|
+
return {
|
|
32
|
+
...captcha,
|
|
33
|
+
solution: captcha.solution || [],
|
|
34
|
+
unlabelled: captcha.unlabelled || [],
|
|
35
|
+
};
|
|
36
|
+
}),
|
|
37
|
+
};
|
|
38
|
+
if (result.datasetId !== undefined)
|
|
39
|
+
result2.datasetId = result.datasetId;
|
|
40
|
+
if (result.contentTree !== undefined)
|
|
41
|
+
result2.contentTree = result.contentTree;
|
|
42
|
+
if (result.datasetContentId !== undefined)
|
|
43
|
+
result2.datasetContentId = result.datasetContentId;
|
|
44
|
+
if (result.solutionTree !== undefined)
|
|
45
|
+
result2.solutionTree = result.solutionTree;
|
|
46
|
+
return result2;
|
|
28
47
|
}
|
|
29
48
|
catch (err) {
|
|
30
|
-
throw new ProsopoEnvError(err
|
|
49
|
+
throw new ProsopoEnvError(err);
|
|
31
50
|
}
|
|
32
51
|
}
|
|
33
52
|
/**
|
|
@@ -37,10 +56,14 @@ export function parseCaptchaDataset(datasetJSON) {
|
|
|
37
56
|
*/
|
|
38
57
|
export function parseAndSortCaptchaSolutions(captchaJSON) {
|
|
39
58
|
try {
|
|
40
|
-
return CaptchaSolutionArraySchema.parse(captchaJSON).map((captcha) => (
|
|
59
|
+
return CaptchaSolutionArraySchema.parse(captchaJSON).map((captcha) => ({
|
|
60
|
+
...captcha,
|
|
61
|
+
solution: captcha.solution.sort(),
|
|
62
|
+
}));
|
|
41
63
|
}
|
|
42
64
|
catch (err) {
|
|
43
|
-
|
|
65
|
+
// TODO fix / improve error handling
|
|
66
|
+
throw new ProsopoEnvError(err);
|
|
44
67
|
}
|
|
45
68
|
}
|
|
46
69
|
export function captchaSort(a, b) {
|
|
@@ -50,12 +73,13 @@ export function sortAndComputeHashes(received, stored) {
|
|
|
50
73
|
received.sort(captchaSort);
|
|
51
74
|
stored.sort(captchaSort);
|
|
52
75
|
return stored.map(({ salt, items = [], target = '', captchaId, solved }, index) => {
|
|
53
|
-
|
|
76
|
+
const item = at(received, index);
|
|
77
|
+
if (captchaId != item.captchaId) {
|
|
54
78
|
throw new ProsopoEnvError('CAPTCHA.ID_MISMATCH');
|
|
55
79
|
}
|
|
56
80
|
return {
|
|
57
81
|
hash: computeCaptchaHash({
|
|
58
|
-
solution: solved ?
|
|
82
|
+
solution: solved ? item.solution : [],
|
|
59
83
|
salt,
|
|
60
84
|
items,
|
|
61
85
|
target,
|
|
@@ -105,6 +129,7 @@ export function computeCaptchaHash(captcha, includeSolution = false, includeSalt
|
|
|
105
129
|
]);
|
|
106
130
|
}
|
|
107
131
|
catch (err) {
|
|
132
|
+
// TODO fix / improve error handling
|
|
108
133
|
throw new ProsopoEnvError(err);
|
|
109
134
|
}
|
|
110
135
|
}
|
|
@@ -119,18 +144,16 @@ export function getSolutionValueToHash(solution) {
|
|
|
119
144
|
* @param {Item} item
|
|
120
145
|
* @return {Promise<HashedItem>} the hex string hash of the item
|
|
121
146
|
*/
|
|
122
|
-
export function computeItemHash(item) {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
133
|
-
});
|
|
147
|
+
export async function computeItemHash(item) {
|
|
148
|
+
if (item.type === 'text') {
|
|
149
|
+
return { ...item, hash: hexHash(item.data) };
|
|
150
|
+
}
|
|
151
|
+
else if (item.type === 'image') {
|
|
152
|
+
return { ...item, hash: hexHash(await downloadImage(item.data)) };
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
throw new ProsopoEnvError('CAPTCHA.INVALID_ITEM_FORMAT');
|
|
156
|
+
}
|
|
134
157
|
}
|
|
135
158
|
/**
|
|
136
159
|
* Converts an indexed captcha solution to a hashed captcha solution or simply returns the hash if it is already hashed
|
|
@@ -139,8 +162,9 @@ export function computeItemHash(item) {
|
|
|
139
162
|
* @param {Item[]} items
|
|
140
163
|
*/
|
|
141
164
|
export function matchItemsToSolutions(solutions, items) {
|
|
142
|
-
return (
|
|
143
|
-
const
|
|
165
|
+
return (solutions?.map((solution) => {
|
|
166
|
+
const index = parseInt(solution.toString());
|
|
167
|
+
const hash = items && items[index] && (items[index]?.hash || solution);
|
|
144
168
|
if (!hash) {
|
|
145
169
|
throw new ProsopoEnvError('CAPTCHA.MISSING_ITEM_HASH');
|
|
146
170
|
}
|
|
@@ -148,7 +172,7 @@ export function matchItemsToSolutions(solutions, items) {
|
|
|
148
172
|
throw new ProsopoEnvError('CAPTCHA.INVALID_ITEM_HASH');
|
|
149
173
|
}
|
|
150
174
|
return hash;
|
|
151
|
-
})
|
|
175
|
+
}) || []);
|
|
152
176
|
}
|
|
153
177
|
/**
|
|
154
178
|
* Create a unique solution commitment
|
|
@@ -173,6 +197,6 @@ export function computePendingRequestHash(captchaIds, userAccount, salt) {
|
|
|
173
197
|
* Parse the image items in a captcha and pass back a URI if they exist
|
|
174
198
|
*/
|
|
175
199
|
export function parseCaptchaAssets(item, assetsResolver) {
|
|
176
|
-
return
|
|
200
|
+
return { ...item, path: assetsResolver?.resolveAsset(item.data).getURL() || item.data };
|
|
177
201
|
}
|
|
178
202
|
//# sourceMappingURL=captcha.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"captcha.js","sourceRoot":"","sources":["../../src/captcha/captcha.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"captcha.js","sourceRoot":"","sources":["../../src/captcha/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,EAIH,0BAA0B,EAG1B,aAAa,GAKhB,MAAM,gBAAgB,CAAA;AACvB,OAAO,EAAE,eAAe,EAAE,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AACxE,OAAO,EAAE,EAAE,EAAE,MAAM,eAAe,CAAA;AAClC,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AACzC,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAA;AAEtC,MAAM,CAAC,MAAM,iBAAiB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AAS,EAAE,OAAO,CAAC,gBAAgB,EAAE,CAAC,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC,IAAI,EAAE,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC,CAAA;AAClH,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,yBAAyB,CAAC,UAAoB,EAAE,WAAmB,EAAE,IAAY;IAC7F,OAAO,YAAY,CAAC,CAAC,GAAG,UAAU,CAAC,IAAI,EAAE,EAAE,WAAW,EAAE,IAAI,CAAC,CAAC,CAAA;AAClE,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAAC,IAAU,EAAE,cAA0C;IACrF,OAAO,EAAE,GAAG,IAAI,EAAE,IAAI,EAAE,cAAc,EAAE,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,IAAI,IAAI,CAAC,IAAI,EAAE,CAAA;AAC3F,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dataset.d.ts","sourceRoot":"","sources":["../../src/captcha/dataset.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAoB,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAC/E,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;
|
|
1
|
+
{"version":3,"file":"dataset.d.ts","sourceRoot":"","sources":["../../src/captcha/dataset.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAoB,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAA;AAC/E,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAO/C,wBAAsB,gBAAgB,CAAC,UAAU,EAAE,OAAO,GAAG,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,CAQpG;AAED;;;GAGG;AACH,wBAAsB,sBAAsB,CAAC,eAAe,EAAE,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAqBvF;AAED,wBAAsB,YAAY,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAqB3E;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,0BAA0B,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,CAiBzF"}
|
package/dist/captcha/dataset.js
CHANGED
|
@@ -1,77 +1,86 @@
|
|
|
1
|
-
import { __awaiter } from "tslib";
|
|
2
1
|
import { CaptchaMerkleTree } from './merkle.js';
|
|
3
|
-
import { ProsopoEnvError } from '@prosopo/common';
|
|
2
|
+
import { ProsopoEnvError, getLogger } from '@prosopo/common';
|
|
3
|
+
import { at } from '@prosopo/util';
|
|
4
4
|
import { computeCaptchaHash, computeItemHash, matchItemsToSolutions } from './captcha.js';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
const logger = getLogger(`Info`, `dataset.ts`);
|
|
6
|
+
export async function hashDatasetItems(datasetRaw) {
|
|
7
|
+
return datasetRaw.captchas.map(async (captcha) => {
|
|
8
|
+
const items = await Promise.all(captcha.items.map(async (item) => computeItemHash(item)));
|
|
9
|
+
return {
|
|
10
|
+
...captcha,
|
|
11
|
+
items,
|
|
12
|
+
};
|
|
11
13
|
});
|
|
12
14
|
}
|
|
13
15
|
/**
|
|
14
16
|
* Take a dataset and hash all the items, making sure that the existing captchaIds and item hashes are correct
|
|
15
17
|
* @param datasetOriginal
|
|
16
18
|
*/
|
|
17
|
-
export function validateDatasetContent(datasetOriginal) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
return false;
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
return hashes.every((hash) => hash);
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
export function buildDataset(datasetRaw) {
|
|
37
|
-
var _a, _b;
|
|
38
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
-
const dataset = yield addSolutionHashesToDataset(datasetRaw);
|
|
40
|
-
const contentTree = yield buildCaptchaTree(dataset, false, false, true);
|
|
41
|
-
const solutionTree = yield buildCaptchaTree(dataset, true, true, false);
|
|
42
|
-
dataset.captchas = dataset.captchas.map((captcha, index) => {
|
|
43
|
-
var _a, _b;
|
|
44
|
-
return (Object.assign(Object.assign({}, captcha), { captchaId: solutionTree.leaves[index].hash, captchaContentId: contentTree.leaves[index].hash, datasetId: (_a = solutionTree.root) === null || _a === void 0 ? void 0 : _a.hash, datasetContentId: (_b = contentTree.root) === null || _b === void 0 ? void 0 : _b.hash }));
|
|
45
|
-
});
|
|
46
|
-
dataset.solutionTree = solutionTree.layers;
|
|
47
|
-
dataset.contentTree = contentTree.layers;
|
|
48
|
-
dataset.datasetId = (_a = solutionTree.root) === null || _a === void 0 ? void 0 : _a.hash;
|
|
49
|
-
dataset.datasetContentId = (_b = contentTree.root) === null || _b === void 0 ? void 0 : _b.hash;
|
|
50
|
-
return dataset;
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
export function buildCaptchaTree(dataset, includeSolution, includeSalt, sortItemHashes) {
|
|
54
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
55
|
-
try {
|
|
56
|
-
const tree = new CaptchaMerkleTree();
|
|
57
|
-
const datasetWithItemHashes = Object.assign({}, dataset);
|
|
58
|
-
const captchaHashes = datasetWithItemHashes.captchas.map((captcha) => computeCaptchaHash(captcha, includeSolution, includeSalt, sortItemHashes));
|
|
59
|
-
tree.build(captchaHashes);
|
|
60
|
-
return tree;
|
|
19
|
+
export async function validateDatasetContent(datasetOriginal) {
|
|
20
|
+
const captchaPromises = await hashDatasetItems(datasetOriginal);
|
|
21
|
+
const captchas = await Promise.all(captchaPromises);
|
|
22
|
+
const dataset = {
|
|
23
|
+
...datasetOriginal,
|
|
24
|
+
captchas,
|
|
25
|
+
};
|
|
26
|
+
// compare each of the Item hashes in each of the captchas in the dataset to each of the item hashes in each of the
|
|
27
|
+
// captchas in datasetOriginal
|
|
28
|
+
const hashes = dataset.captchas.map((captcha) => {
|
|
29
|
+
const captchaRaw = datasetOriginal.captchas.find((captchaRaw) => 'captchaId' in captchaRaw ? captchaRaw.captchaId === captcha.captchaId : false);
|
|
30
|
+
if (captchaRaw) {
|
|
31
|
+
return captcha.items.every((item, index) => item.hash === at(captchaRaw.items, index).hash);
|
|
61
32
|
}
|
|
62
|
-
|
|
63
|
-
|
|
33
|
+
else {
|
|
34
|
+
return false;
|
|
64
35
|
}
|
|
65
36
|
});
|
|
37
|
+
return hashes.every((hash) => hash);
|
|
38
|
+
}
|
|
39
|
+
export async function buildDataset(datasetRaw) {
|
|
40
|
+
logger.info(`Adding solution hashes to dataset`);
|
|
41
|
+
const dataset = await addSolutionHashesToDataset(datasetRaw);
|
|
42
|
+
logger.info(`Building dataset merkle trees`);
|
|
43
|
+
const contentTree = await buildCaptchaTree(dataset, false, false, true);
|
|
44
|
+
const solutionTree = await buildCaptchaTree(dataset, true, true, false);
|
|
45
|
+
dataset.captchas = dataset.captchas.map((captcha, index) => ({
|
|
46
|
+
...captcha,
|
|
47
|
+
captchaId: at(solutionTree.leaves, index).hash,
|
|
48
|
+
captchaContentId: at(contentTree.leaves, index).hash,
|
|
49
|
+
datasetId: solutionTree.root?.hash,
|
|
50
|
+
datasetContentId: contentTree.root?.hash,
|
|
51
|
+
}));
|
|
52
|
+
dataset.solutionTree = solutionTree.layers;
|
|
53
|
+
dataset.contentTree = contentTree.layers;
|
|
54
|
+
dataset.datasetId = solutionTree.root?.hash;
|
|
55
|
+
dataset.datasetContentId = contentTree.root?.hash;
|
|
56
|
+
return dataset;
|
|
57
|
+
}
|
|
58
|
+
export async function buildCaptchaTree(dataset, includeSolution, includeSalt, sortItemHashes) {
|
|
59
|
+
try {
|
|
60
|
+
const tree = new CaptchaMerkleTree();
|
|
61
|
+
const datasetWithItemHashes = { ...dataset };
|
|
62
|
+
const captchaHashes = datasetWithItemHashes.captchas.map((captcha) => computeCaptchaHash(captcha, includeSolution, includeSalt, sortItemHashes));
|
|
63
|
+
tree.build(captchaHashes);
|
|
64
|
+
return tree;
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
throw new ProsopoEnvError('DATASET.HASH_ERROR');
|
|
68
|
+
}
|
|
66
69
|
}
|
|
67
|
-
export function addSolutionHashesToDataset(datasetRaw) {
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
export async function addSolutionHashesToDataset(datasetRaw) {
|
|
71
|
+
const captchaPromises = datasetRaw.captchas.map(async (captcha) => {
|
|
72
|
+
//const items = await Promise.all(captcha.items.map(async (item) => await computeItemHash(item)))
|
|
73
|
+
return {
|
|
74
|
+
...captcha,
|
|
75
|
+
items: captcha.items,
|
|
76
|
+
// some captcha challenges will not have a solution
|
|
77
|
+
...(captcha.solution !== undefined && { solution: matchItemsToSolutions(captcha.solution, captcha.items) }),
|
|
78
|
+
};
|
|
75
79
|
});
|
|
80
|
+
const captchas = await Promise.all(captchaPromises);
|
|
81
|
+
return {
|
|
82
|
+
...datasetRaw,
|
|
83
|
+
captchas,
|
|
84
|
+
};
|
|
76
85
|
}
|
|
77
86
|
//# sourceMappingURL=dataset.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dataset.js","sourceRoot":"","sources":["../../src/captcha/dataset.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"dataset.js","sourceRoot":"","sources":["../../src/captcha/dataset.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE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,CAAC,CAAC,CAAA;IAEF,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAA;IAEnD,OAAO;QACH,GAAG,UAAU;QACb,QAAQ;KACX,CAAA;AACL,CAAC"}
|
package/dist/captcha/merkle.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { MerkleLayer, MerkleLeaf, MerkleNodeInterface, MerkleProof } from '@pros
|
|
|
2
2
|
declare class MerkleNode implements MerkleNodeInterface {
|
|
3
3
|
hash: string;
|
|
4
4
|
parent: string | null;
|
|
5
|
-
constructor(hash:
|
|
5
|
+
constructor(hash: string);
|
|
6
6
|
}
|
|
7
7
|
export declare class CaptchaMerkleTree {
|
|
8
8
|
leaves: MerkleNode[];
|
|
@@ -10,8 +10,8 @@ export declare class CaptchaMerkleTree {
|
|
|
10
10
|
root: MerkleNode | undefined;
|
|
11
11
|
constructor();
|
|
12
12
|
build(leaves: string[]): void;
|
|
13
|
-
buildMerkleTree(leaves: MerkleNode[]):
|
|
14
|
-
createParent(leftChild:
|
|
13
|
+
buildMerkleTree(leaves: MerkleNode[]): MerkleNode[];
|
|
14
|
+
createParent(leftChild: MerkleNode, rightChild: MerkleNode): MerkleNode;
|
|
15
15
|
proof(leafHash: MerkleLeaf): MerkleProof;
|
|
16
16
|
}
|
|
17
17
|
export declare function verifyProof(leaf: MerkleLeaf, proof: MerkleProof): boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merkle.d.ts","sourceRoot":"","sources":["../../src/captcha/merkle.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,mBAAmB,EAAE,WAAW,EAAoB,MAAM,gBAAgB,CAAA;
|
|
1
|
+
{"version":3,"file":"merkle.d.ts","sourceRoot":"","sources":["../../src/captcha/merkle.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,mBAAmB,EAAE,WAAW,EAAoB,MAAM,gBAAgB,CAAA;AAI5G,cAAM,UAAW,YAAW,mBAAmB;IAC3C,IAAI,EAAE,MAAM,CAAA;IAEZ,MAAM,EAAE,MAAM,GAAG,IAAI,CAAA;gBAET,IAAI,EAAE,MAAM;CAI3B;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,GAAG,UAAU,EAAE;IA4BnD,YAAY,CAAC,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,GAAG,UAAU;IAOvE,KAAK,CAAC,QAAQ,EAAE,UAAU,GAAG,WAAW;CA+B3C;AAED,wBAAgB,WAAW,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,WAAW,GAAG,OAAO,CAmBzE"}
|
package/dist/captcha/merkle.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { at } from '@prosopo/util';
|
|
1
2
|
import { hexHashArray } from '@prosopo/common';
|
|
2
3
|
class MerkleNode {
|
|
3
4
|
constructor(hash) {
|
|
@@ -22,20 +23,23 @@ export class CaptchaMerkleTree {
|
|
|
22
23
|
layerZero.push(node.hash);
|
|
23
24
|
}
|
|
24
25
|
this.layers.push(layerZero);
|
|
25
|
-
this.root = this.buildMerkleTree(this.leaves);
|
|
26
|
+
this.root = this.buildMerkleTree(this.leaves)[0];
|
|
26
27
|
}
|
|
27
28
|
buildMerkleTree(leaves) {
|
|
28
29
|
// Builds the Merkle tree from a list of leaves. In case of an odd number of leaves, the last leaf is duplicated.
|
|
29
30
|
const numLeaves = leaves.length;
|
|
30
31
|
if (numLeaves === 1) {
|
|
31
|
-
return leaves
|
|
32
|
+
return leaves;
|
|
32
33
|
}
|
|
33
34
|
const parents = [];
|
|
34
35
|
let leafIndex = 0;
|
|
35
36
|
const newLayer = [];
|
|
36
37
|
while (leafIndex < numLeaves) {
|
|
37
38
|
const leftChild = leaves[leafIndex];
|
|
38
|
-
|
|
39
|
+
if (leftChild === undefined) {
|
|
40
|
+
throw new Error('leftChild undefined');
|
|
41
|
+
}
|
|
42
|
+
const rightChild = leafIndex + 1 < numLeaves ? at(leaves, leafIndex + 1) : leftChild;
|
|
39
43
|
const parentNode = this.createParent(leftChild, rightChild);
|
|
40
44
|
newLayer.push(parentNode.hash);
|
|
41
45
|
parents.push(parentNode);
|
|
@@ -46,49 +50,55 @@ export class CaptchaMerkleTree {
|
|
|
46
50
|
}
|
|
47
51
|
createParent(leftChild, rightChild) {
|
|
48
52
|
const parent = new MerkleNode(hexHashArray([leftChild.hash, rightChild.hash]));
|
|
49
|
-
leftChild.parent = parent;
|
|
50
|
-
rightChild.parent = parent;
|
|
53
|
+
leftChild.parent = parent.hash;
|
|
54
|
+
rightChild.parent = parent.hash;
|
|
51
55
|
return parent;
|
|
52
56
|
}
|
|
53
57
|
proof(leafHash) {
|
|
54
58
|
const proofTree = [];
|
|
55
59
|
let layerNum = 0;
|
|
56
60
|
while (layerNum < this.layers.length - 1) {
|
|
57
|
-
const
|
|
61
|
+
const layer = this.layers[layerNum];
|
|
62
|
+
if (layer === undefined) {
|
|
63
|
+
throw new Error('layer undefined');
|
|
64
|
+
}
|
|
65
|
+
const leafIndex = layer.indexOf(leafHash);
|
|
58
66
|
// if layer 0 leaf index is 3, it should be partnered with 2: [L0,L1],[L2,L3],[L3,L4],...
|
|
59
67
|
// layer one pairs looks like [L0L1, L2L3], [L3L4, L5L6],...etc
|
|
60
68
|
let partnerIndex = leafIndex % 2 && leafIndex > 0 ? leafIndex - 1 : leafIndex + 1;
|
|
61
69
|
// if there are an odd number of leaves in the layer, the last leaf is duplicated
|
|
62
|
-
if (partnerIndex >
|
|
70
|
+
if (partnerIndex > layer.length - 1) {
|
|
63
71
|
partnerIndex = leafIndex;
|
|
64
72
|
}
|
|
65
73
|
const pair = [leafHash];
|
|
66
|
-
const layer = this.layers[layerNum];
|
|
67
74
|
// determine whether the leaf sits on the left or the right of its partner
|
|
75
|
+
const partner = at(layer, partnerIndex);
|
|
68
76
|
if (partnerIndex > leafIndex) {
|
|
69
|
-
pair.push(
|
|
77
|
+
pair.push(partner);
|
|
70
78
|
}
|
|
71
79
|
else {
|
|
72
|
-
pair.unshift(
|
|
80
|
+
pair.unshift(partner);
|
|
73
81
|
}
|
|
74
|
-
proofTree.push([pair
|
|
82
|
+
proofTree.push([at(pair, 0), at(pair, 1)]);
|
|
75
83
|
layerNum += 1;
|
|
76
84
|
leafHash = hexHashArray(pair);
|
|
77
85
|
}
|
|
78
|
-
|
|
86
|
+
const last = at(this.layers, this.layers.length - 1);
|
|
87
|
+
return [...proofTree, [at(last, 0)]];
|
|
79
88
|
}
|
|
80
89
|
}
|
|
81
90
|
export function verifyProof(leaf, proof) {
|
|
82
91
|
try {
|
|
83
|
-
if (proof
|
|
92
|
+
if (at(proof, 0).indexOf(leaf) === -1) {
|
|
84
93
|
return false;
|
|
85
94
|
}
|
|
86
95
|
for (const [layerIndex, layer] of proof.entries()) {
|
|
87
96
|
leaf = hexHashArray(layer);
|
|
88
|
-
if (proof
|
|
97
|
+
if (at(proof, layerIndex + 1).indexOf(leaf) === -1) {
|
|
89
98
|
return false;
|
|
90
99
|
}
|
|
91
|
-
|
|
100
|
+
const last = at(proof, proof.length - 1);
|
|
101
|
+
if (leaf === at(last, 0)) {
|
|
92
102
|
return true;
|
|
93
103
|
}
|
|
94
104
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"merkle.js","sourceRoot":"","sources":["../../src/captcha/merkle.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE9C,MAAM,UAAU;IAKZ,YAAY,
|
|
1
|
+
{"version":3,"file":"merkle.js","sourceRoot":"","sources":["../../src/captcha/merkle.ts"],"names":[],"mappings":"A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I,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE;YACnC,OAAO,KAAK,CAAA;SACf;QACD,KAAK,MAAM,CAAC,UAAU,EAAE,KAAK,CAAC,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE;YAC/C,IAAI,GAAG,YAAY,CAAC,KAAK,CAAC,CAAA;YAC1B,IAAI,EAAE,CAAC,KAAK,EAAE,UAAU,GAAG,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE;gBAChD,OAAO,KAAK,CAAA;aACf;YACD,MAAM,IAAI,GAAG,EAAE,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAA;YACxC,IAAI,IAAI,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE;gBACtB,OAAO,IAAI,CAAA;aACd;SACJ;QACD,OAAO,KAAK,CAAA;KACf;IAAC,OAAO,GAAG,EAAE;QACV,OAAO,KAAK,CAAA;KACf;AACL,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/captcha/util.ts"],"names":[],"mappings":"AAgBA,wBAAsB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,
|
|
1
|
+
{"version":3,"file":"util.d.ts","sourceRoot":"","sources":["../../src/captcha/util.ts"],"names":[],"mappings":"AAgBA,wBAAsB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CASpE"}
|
package/dist/captcha/util.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { __awaiter } from "tslib";
|
|
2
1
|
// Copyright 2021-2023 Prosopo (UK) Ltd.
|
|
3
2
|
//
|
|
4
3
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -14,14 +13,13 @@ import { __awaiter } from "tslib";
|
|
|
14
13
|
// limitations under the License.
|
|
15
14
|
import { ProsopoEnvError } from '@prosopo/common';
|
|
16
15
|
import client from 'axios';
|
|
17
|
-
export function downloadImage(url) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
});
|
|
16
|
+
export async function downloadImage(url) {
|
|
17
|
+
try {
|
|
18
|
+
return new Uint8Array((await client.default.get(url, { url, method: 'GET', responseType: 'arraybuffer' })).data);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
// TODO fix/improve error handling
|
|
22
|
+
throw new ProsopoEnvError(error);
|
|
23
|
+
}
|
|
26
24
|
}
|
|
27
25
|
//# sourceMappingURL=util.js.map
|
package/dist/captcha/util.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/captcha/util.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"util.js","sourceRoot":"","sources":["../../src/captcha/util.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,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAA;AACjD,OAAO,MAAM,MAAM,OAAO,CAAA;AAE1B,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,GAAW;IAC3C,IAAI;QACA,OAAO,IAAI,UAAU,CACjB,CAAC,MAAM,MAAM,CAAC,OAAO,CAAC,GAAG,CAAc,GAAG,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,EAAE,aAAa,EAAE,CAAC,CAAC,CAAC,IAAI,CACzG,CAAA;KACJ;IAAC,OAAO,KAAK,EAAE;QACZ,kCAAkC;QAClC,MAAM,IAAI,eAAe,CAAC,KAAc,CAAC,CAAA;KAC5C;AACL,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prosopo/datasets",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.19",
|
|
4
4
|
"author": "PROSOPO LIMITED <info@prosopo.io>",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"private": false,
|
|
@@ -32,14 +32,16 @@
|
|
|
32
32
|
},
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"@polkadot/util": "^12.3.2",
|
|
35
|
-
"@prosopo/common": "^0.1.
|
|
36
|
-
"@prosopo/contract": "^0.1.
|
|
37
|
-
"@prosopo/types": "^0.1.
|
|
35
|
+
"@prosopo/common": "^0.1.19",
|
|
36
|
+
"@prosopo/contract": "^0.1.19",
|
|
37
|
+
"@prosopo/types": "^0.1.19",
|
|
38
|
+
"@prosopo/util": "^0.1.19",
|
|
38
39
|
"axios": "^0.27.2"
|
|
39
40
|
},
|
|
40
41
|
"devDependencies": {
|
|
41
42
|
"@types/mocha": "^10.0.0",
|
|
42
|
-
"
|
|
43
|
+
"tslib": "^2.6.2",
|
|
44
|
+
"typescript": "^5.1.6"
|
|
43
45
|
},
|
|
44
46
|
"repository": {
|
|
45
47
|
"type": "git",
|