@prosopo/datasets 0.1.18 → 0.2.0

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.
@@ -0,0 +1,78 @@
1
+ import { AssetsResolver, Captcha, CaptchaSolution, CaptchaWithoutId, DatasetRaw, HashedItem, HashedSolution, Item, RawSolution } from '@prosopo/types';
2
+ export declare const NO_SOLUTION_VALUE = "NO_SOLUTION";
3
+ /**
4
+ * Parse a dataset
5
+ * @return {JSON} captcha dataset, stored in JSON
6
+ * @param datasetJSON
7
+ */
8
+ export declare function parseCaptchaDataset(datasetJSON: JSON): DatasetRaw;
9
+ /**
10
+ * Make sure captcha solutions are in the correct format
11
+ * @param {JSON} captchaJSON captcha solutions received from the api
12
+ * @return {CaptchaSolution[]} an array of parsed and sorted captcha solutions
13
+ */
14
+ export declare function parseAndSortCaptchaSolutions(captchaJSON: CaptchaSolution[]): CaptchaSolution[];
15
+ export declare function captchaSort<T extends {
16
+ captchaId: string;
17
+ }>(a: T, b: T): number;
18
+ export declare function sortAndComputeHashes(received: CaptchaSolution[], stored: Captcha[]): {
19
+ captchaId: string;
20
+ hash: string;
21
+ }[];
22
+ /**
23
+ * Take an array of CaptchaSolutions and Captchas and check if the solutions are the same for each pair
24
+ * @param {CaptchaSolution[]} received
25
+ * @param {Captcha[]} stored
26
+ * @return {boolean}
27
+ */
28
+ export declare function compareCaptchaSolutions(received: CaptchaSolution[], stored: Captcha[]): boolean;
29
+ /**
30
+ * Compute the hash of various types of captcha
31
+ * @param {Captcha} captcha
32
+ * @param {boolean} includeSolution
33
+ * @param {boolean} includeSalt
34
+ * @param {boolean} sortItemHashes
35
+ * @return {string} the hex string hash
36
+ */
37
+ export declare function computeCaptchaHash(captcha: CaptchaWithoutId, includeSolution: boolean | undefined, includeSalt: boolean | undefined, sortItemHashes: boolean): string;
38
+ /** Return the sorted solution value or ['NO_SOLUTION'] if there is no solution. Ensures that an empty array is a valid
39
+ * solution
40
+ * @param solution
41
+ */
42
+ export declare function getSolutionValueToHash(solution?: HashedSolution[] | RawSolution[]): number[] | string[];
43
+ /** Compute the hash of a captcha item, downloading the image if necessary
44
+ * @param {Item} item
45
+ * @return {Promise<HashedItem>} the hex string hash of the item
46
+ */
47
+ export declare function computeItemHash(item: Item): Promise<HashedItem>;
48
+ /**
49
+ * Converts an indexed captcha solution to a hashed captcha solution or simply returns the hash if it is already hashed
50
+ * @return {HashedSolution[]}
51
+ * @param {RawSolution[] | HashedSolution[]} solutions
52
+ * @param {Item[]} items
53
+ */
54
+ export declare function matchItemsToSolutions(solutions: RawSolution[] | HashedSolution[], items: HashedItem[] | undefined): HashedSolution[];
55
+ /**
56
+ * Create a unique solution commitment
57
+ * @param {CaptchaSolution} captcha
58
+ * @return {string} the hex string hash
59
+ */
60
+ export declare function computeCaptchaSolutionHash(captcha: CaptchaSolution): string;
61
+ /**
62
+ * Compute hash for an array of captcha ids, userAccount, and salt, which serves as the identifier for a pending request
63
+ * @param {string[]} captchaIds
64
+ * @param {string} userAccount
65
+ * @param {string} salt
66
+ * @return {string}
67
+ */
68
+ export declare function computePendingRequestHash(captchaIds: string[], userAccount: string, salt: string): string;
69
+ /**
70
+ * Parse the image items in a captcha and pass back a URI if they exist
71
+ */
72
+ export declare function parseCaptchaAssets(item: Item, assetsResolver: AssetsResolver | undefined): {
73
+ path: string;
74
+ type: import("@prosopo/types").CaptchaItemTypes;
75
+ data: string;
76
+ hash: string;
77
+ };
78
+ //# sourceMappingURL=captcha.d.ts.map
@@ -0,0 +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;AAMvB,eAAO,MAAM,iBAAiB,gBAAgB,CAAA;AAE9C;;;;GAIG;AACH,wB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,IAAI,EAAE,MAAM,GAAG,MAAM,CAEzG;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,IAAI,EAAE,cAAc,EAAE,cAAc,GAAG,SAAS;;;;;EAExF"}
@@ -0,0 +1,202 @@
1
+ // Copyright 2021-2023 Prosopo (UK) Ltd.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { CaptchaSolutionArraySchema, DatasetSchema, } from '@prosopo/types';
15
+ import { ProsopoEnvError, hexHash, hexHashArray } from '@prosopo/common';
16
+ import { at } from '@prosopo/util';
17
+ import { downloadImage } from './util.js';
18
+ import { isHex } from '@polkadot/util';
19
+ export const NO_SOLUTION_VALUE = 'NO_SOLUTION';
20
+ /**
21
+ * Parse a dataset
22
+ * @return {JSON} captcha dataset, stored in JSON
23
+ * @param datasetJSON
24
+ */
25
+ export function parseCaptchaDataset(datasetJSON) {
26
+ try {
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;
47
+ }
48
+ catch (err) {
49
+ throw new ProsopoEnvError(err);
50
+ }
51
+ }
52
+ /**
53
+ * Make sure captcha solutions are in the correct format
54
+ * @param {JSON} captchaJSON captcha solutions received from the api
55
+ * @return {CaptchaSolution[]} an array of parsed and sorted captcha solutions
56
+ */
57
+ export function parseAndSortCaptchaSolutions(captchaJSON) {
58
+ try {
59
+ return CaptchaSolutionArraySchema.parse(captchaJSON).map((captcha) => ({
60
+ ...captcha,
61
+ solution: captcha.solution.sort(),
62
+ }));
63
+ }
64
+ catch (err) {
65
+ // TODO fix / improve error handling
66
+ throw new ProsopoEnvError(err);
67
+ }
68
+ }
69
+ export function captchaSort(a, b) {
70
+ return a.captchaId.localeCompare(b.captchaId);
71
+ }
72
+ export function sortAndComputeHashes(received, stored) {
73
+ received.sort(captchaSort);
74
+ stored.sort(captchaSort);
75
+ return stored.map(({ salt, items = [], target = '', captchaId, solved }, index) => {
76
+ const item = at(received, index);
77
+ if (captchaId != item.captchaId) {
78
+ throw new ProsopoEnvError('CAPTCHA.ID_MISMATCH');
79
+ }
80
+ return {
81
+ hash: computeCaptchaHash({
82
+ solution: solved ? item.solution : [],
83
+ salt,
84
+ items,
85
+ target,
86
+ }, true, true, false),
87
+ captchaId,
88
+ };
89
+ });
90
+ }
91
+ /**
92
+ * Take an array of CaptchaSolutions and Captchas and check if the solutions are the same for each pair
93
+ * @param {CaptchaSolution[]} received
94
+ * @param {Captcha[]} stored
95
+ * @return {boolean}
96
+ */
97
+ export function compareCaptchaSolutions(received, stored) {
98
+ if (received.length && stored.length && received.length === stored.length) {
99
+ const hashes = sortAndComputeHashes(received, stored);
100
+ return hashes.every(({ hash, captchaId }) => hash === captchaId);
101
+ }
102
+ return false;
103
+ }
104
+ /**
105
+ * Compute the hash of various types of captcha
106
+ * @param {Captcha} captcha
107
+ * @param {boolean} includeSolution
108
+ * @param {boolean} includeSalt
109
+ * @param {boolean} sortItemHashes
110
+ * @return {string} the hex string hash
111
+ */
112
+ export function computeCaptchaHash(captcha, includeSolution = false, includeSalt = false, sortItemHashes) {
113
+ try {
114
+ const itemHashes = captcha.items.map((item, index) => {
115
+ if (item.hash) {
116
+ return item.hash;
117
+ }
118
+ else {
119
+ throw new ProsopoEnvError('CAPTCHA.MISSING_ITEM_HASH', computeCaptchaHash.name, undefined, index);
120
+ }
121
+ });
122
+ return hexHashArray([
123
+ captcha.target,
124
+ // empty array hashes as empty string, undefined solution results in the array [`NO_SOLUTION`]
125
+ // [undefined] also hashes as empty string, which is why we don't use it
126
+ ...(includeSolution ? getSolutionValueToHash(captcha.solution) : []),
127
+ includeSalt ? captcha.salt : '',
128
+ sortItemHashes ? itemHashes.sort() : itemHashes,
129
+ ]);
130
+ }
131
+ catch (err) {
132
+ // TODO fix / improve error handling
133
+ throw new ProsopoEnvError(err);
134
+ }
135
+ }
136
+ /** Return the sorted solution value or ['NO_SOLUTION'] if there is no solution. Ensures that an empty array is a valid
137
+ * solution
138
+ * @param solution
139
+ */
140
+ export function getSolutionValueToHash(solution) {
141
+ return solution !== undefined ? solution.sort() : [NO_SOLUTION_VALUE];
142
+ }
143
+ /** Compute the hash of a captcha item, downloading the image if necessary
144
+ * @param {Item} item
145
+ * @return {Promise<HashedItem>} the hex string hash of the item
146
+ */
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
+ }
157
+ }
158
+ /**
159
+ * Converts an indexed captcha solution to a hashed captcha solution or simply returns the hash if it is already hashed
160
+ * @return {HashedSolution[]}
161
+ * @param {RawSolution[] | HashedSolution[]} solutions
162
+ * @param {Item[]} items
163
+ */
164
+ export function matchItemsToSolutions(solutions, items) {
165
+ return (solutions?.map((solution) => {
166
+ const index = parseInt(solution.toString());
167
+ const hash = items && items[index] && (items[index]?.hash || solution);
168
+ if (!hash) {
169
+ throw new ProsopoEnvError('CAPTCHA.MISSING_ITEM_HASH');
170
+ }
171
+ if (!isHex(hash)) {
172
+ throw new ProsopoEnvError('CAPTCHA.INVALID_ITEM_HASH');
173
+ }
174
+ return hash;
175
+ }) || []);
176
+ }
177
+ /**
178
+ * Create a unique solution commitment
179
+ * @param {CaptchaSolution} captcha
180
+ * @return {string} the hex string hash
181
+ */
182
+ export function computeCaptchaSolutionHash(captcha) {
183
+ // TODO: should the captchaContentId be validated?
184
+ return hexHashArray([captcha.captchaId, captcha.captchaContentId, [...captcha.solution].sort(), captcha.salt]);
185
+ }
186
+ /**
187
+ * Compute hash for an array of captcha ids, userAccount, and salt, which serves as the identifier for a pending request
188
+ * @param {string[]} captchaIds
189
+ * @param {string} userAccount
190
+ * @param {string} salt
191
+ * @return {string}
192
+ */
193
+ export function computePendingRequestHash(captchaIds, userAccount, salt) {
194
+ return hexHashArray([...captchaIds.sort(), userAccount, salt]);
195
+ }
196
+ /**
197
+ * Parse the image items in a captcha and pass back a URI if they exist
198
+ */
199
+ export function parseCaptchaAssets(item, assetsResolver) {
200
+ return { ...item, path: assetsResolver?.resolveAsset(item.data).getURL() || item.data };
201
+ }
202
+ //# sourceMappingURL=captcha.js.map
@@ -0,0 +1 @@
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,OAA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ACL,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,0BAA0B,CAAC,OAAwB;IAC/D,kDAAkD;IAClD,OAAO,YAAY,CAAC,CAAC,OAAO,CAAC,SAAS,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"}
@@ -0,0 +1,12 @@
1
+ import { Captcha, Dataset, DatasetRaw } from '@prosopo/types';
2
+ import { CaptchaMerkleTree } from './merkle.js';
3
+ export declare function hashDatasetItems(datasetRaw: Dataset | DatasetRaw): Promise<Promise<Captcha>[]>;
4
+ /**
5
+ * Take a dataset and hash all the items, making sure that the existing captchaIds and item hashes are correct
6
+ * @param datasetOriginal
7
+ */
8
+ export declare function validateDatasetContent(datasetOriginal: Dataset): Promise<boolean>;
9
+ export declare function buildDataset(datasetRaw: DatasetRaw): Promise<Dataset>;
10
+ export declare function buildCaptchaTree(dataset: Dataset, includeSolution: boolean, includeSalt: boolean, sortItemHashes: boolean): Promise<CaptchaMerkleTree>;
11
+ export declare function addSolutionHashesToDataset(datasetRaw: DatasetRaw): Promise<Dataset>;
12
+ //# sourceMappingURL=dataset.d.ts.map
@@ -0,0 +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;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"}
@@ -0,0 +1,86 @@
1
+ import { CaptchaMerkleTree } from './merkle.js';
2
+ import { ProsopoEnvError, getLogger } from '@prosopo/common';
3
+ import { at } from '@prosopo/util';
4
+ import { computeCaptchaHash, computeItemHash, matchItemsToSolutions } from './captcha.js';
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
+ };
13
+ });
14
+ }
15
+ /**
16
+ * Take a dataset and hash all the items, making sure that the existing captchaIds and item hashes are correct
17
+ * @param datasetOriginal
18
+ */
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);
32
+ }
33
+ else {
34
+ return false;
35
+ }
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
+ }
69
+ }
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
+ };
79
+ });
80
+ const captchas = await Promise.all(captchaPromises);
81
+ return {
82
+ ...datasetRaw,
83
+ captchas,
84
+ };
85
+ }
86
+ //# sourceMappingURL=dataset.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dataset.js","sourceRoot":"","sources":["../../src/captcha/dataset.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAC/C,OAAO,EAAE,eAAe,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAA;AAC5D,OAAO,EAAE,EAAE,EAAE,MAAM,eAAe,CAAA;AAClC,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAA;AAEzF,MAAM,MAAM,GAAG,SAAS,CAAC,MAAM,EAAE,YAAY,CAAC,CAAA;AAE9C,MAAM,CAAC,KAAK,UAAU,gBAAgB,CAAC,UAAgC;IACnE,OAAO,UAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;QAC7C,MAAM,KAAK,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,eAAe,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;QACzF,OAAO;YACH,GAAG,OAAO;YACV,KAAK;SACG,CAAA;IAChB,CAAC,CAAC,CAAA;AACN,CAAC;AAED;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAAC,eAAwB;IACjE,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O,GAAG,EAAE;QACV,MAAM,IAAI,eAAe,CAAC,oBAAoB,CAAC,CAAA;KAClD;AACL,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAAC,UAAsB;IACnE,MAAM,eAAe,GAAG,UAAU,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE;QAC9D,iGAAiG;QACjG,OAAO;YACH,GAAG,OAAO;YACV,KAAK,EAAE,OAAO,CAAC,KAAK;YACpB,mDAAmD;YACnD,GAAG,CAAC,OAAO,CAAC,QAAQ,KAAK,SAAS,IAAI,EAAE,QAAQ,EAAE,qBAAqB,CAAC,OAAO,CAAC,QAAQ,EAAE,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;SAC9G,CAAA;IACL,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"}
@@ -0,0 +1,5 @@
1
+ export * from './captcha.js';
2
+ export * from './merkle.js';
3
+ export * from './util.js';
4
+ export * from './dataset.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/captcha/index.ts"],"names":[],"mappings":"AAaA,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,cAAc,CAAA"}
@@ -0,0 +1,18 @@
1
+ // Copyright 2021-2023 Prosopo (UK) Ltd.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ export * from './captcha.js';
15
+ export * from './merkle.js';
16
+ export * from './util.js';
17
+ export * from './dataset.js';
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/captcha/index.ts"],"names":[],"mappings":"AAAA,wCAAwC;AACxC,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AACjC,cAAc,cAAc,CAAA;AAC5B,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,cAAc,CAAA"}
@@ -0,0 +1,19 @@
1
+ import { MerkleLayer, MerkleLeaf, MerkleNodeInterface, MerkleProof } from '@prosopo/types';
2
+ declare class MerkleNode implements MerkleNodeInterface {
3
+ hash: string;
4
+ parent: string | null;
5
+ constructor(hash: string);
6
+ }
7
+ export declare class CaptchaMerkleTree {
8
+ leaves: MerkleNode[];
9
+ layers: MerkleLayer[];
10
+ root: MerkleNode | undefined;
11
+ constructor();
12
+ build(leaves: string[]): void;
13
+ buildMerkleTree(leaves: MerkleNode[]): MerkleNode[];
14
+ createParent(leftChild: MerkleNode, rightChild: MerkleNode): MerkleNode;
15
+ proof(leafHash: MerkleLeaf): MerkleProof;
16
+ }
17
+ export declare function verifyProof(leaf: MerkleLeaf, proof: MerkleProof): boolean;
18
+ export {};
19
+ //# sourceMappingURL=merkle.d.ts.map
@@ -0,0 +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;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"}
@@ -0,0 +1,111 @@
1
+ import { at } from '@prosopo/util';
2
+ import { hexHashArray } from '@prosopo/common';
3
+ class MerkleNode {
4
+ constructor(hash) {
5
+ this.hash = hash;
6
+ this.parent = null;
7
+ }
8
+ }
9
+ export class CaptchaMerkleTree {
10
+ constructor() {
11
+ this.leaves = [];
12
+ this.layers = [];
13
+ }
14
+ build(leaves) {
15
+ // allow rebuild
16
+ if (this.layers.length) {
17
+ this.layers = [];
18
+ }
19
+ const layerZero = [];
20
+ for (const leaf of leaves) {
21
+ const node = new MerkleNode(leaf);
22
+ this.leaves.push(node);
23
+ layerZero.push(node.hash);
24
+ }
25
+ this.layers.push(layerZero);
26
+ this.root = this.buildMerkleTree(this.leaves)[0];
27
+ }
28
+ buildMerkleTree(leaves) {
29
+ // Builds the Merkle tree from a list of leaves. In case of an odd number of leaves, the last leaf is duplicated.
30
+ const numLeaves = leaves.length;
31
+ if (numLeaves === 1) {
32
+ return leaves;
33
+ }
34
+ const parents = [];
35
+ let leafIndex = 0;
36
+ const newLayer = [];
37
+ while (leafIndex < numLeaves) {
38
+ const leftChild = leaves[leafIndex];
39
+ if (leftChild === undefined) {
40
+ throw new Error('leftChild undefined');
41
+ }
42
+ const rightChild = leafIndex + 1 < numLeaves ? at(leaves, leafIndex + 1) : leftChild;
43
+ const parentNode = this.createParent(leftChild, rightChild);
44
+ newLayer.push(parentNode.hash);
45
+ parents.push(parentNode);
46
+ leafIndex += 2;
47
+ }
48
+ this.layers.push(newLayer);
49
+ return this.buildMerkleTree(parents);
50
+ }
51
+ createParent(leftChild, rightChild) {
52
+ const parent = new MerkleNode(hexHashArray([leftChild.hash, rightChild.hash]));
53
+ leftChild.parent = parent.hash;
54
+ rightChild.parent = parent.hash;
55
+ return parent;
56
+ }
57
+ proof(leafHash) {
58
+ const proofTree = [];
59
+ let layerNum = 0;
60
+ while (layerNum < this.layers.length - 1) {
61
+ const layer = this.layers[layerNum];
62
+ if (layer === undefined) {
63
+ throw new Error('layer undefined');
64
+ }
65
+ const leafIndex = layer.indexOf(leafHash);
66
+ // if layer 0 leaf index is 3, it should be partnered with 2: [L0,L1],[L2,L3],[L3,L4],...
67
+ // layer one pairs looks like [L0L1, L2L3], [L3L4, L5L6],...etc
68
+ let partnerIndex = leafIndex % 2 && leafIndex > 0 ? leafIndex - 1 : leafIndex + 1;
69
+ // if there are an odd number of leaves in the layer, the last leaf is duplicated
70
+ if (partnerIndex > layer.length - 1) {
71
+ partnerIndex = leafIndex;
72
+ }
73
+ const pair = [leafHash];
74
+ // determine whether the leaf sits on the left or the right of its partner
75
+ const partner = at(layer, partnerIndex);
76
+ if (partnerIndex > leafIndex) {
77
+ pair.push(partner);
78
+ }
79
+ else {
80
+ pair.unshift(partner);
81
+ }
82
+ proofTree.push([at(pair, 0), at(pair, 1)]);
83
+ layerNum += 1;
84
+ leafHash = hexHashArray(pair);
85
+ }
86
+ const last = at(this.layers, this.layers.length - 1);
87
+ return [...proofTree, [at(last, 0)]];
88
+ }
89
+ }
90
+ export function verifyProof(leaf, proof) {
91
+ try {
92
+ if (at(proof, 0).indexOf(leaf) === -1) {
93
+ return false;
94
+ }
95
+ for (const [layerIndex, layer] of proof.entries()) {
96
+ leaf = hexHashArray(layer);
97
+ if (at(proof, layerIndex + 1).indexOf(leaf) === -1) {
98
+ return false;
99
+ }
100
+ const last = at(proof, proof.length - 1);
101
+ if (leaf === at(last, 0)) {
102
+ return true;
103
+ }
104
+ }
105
+ return false;
106
+ }
107
+ catch (err) {
108
+ return false;
109
+ }
110
+ }
111
+ //# sourceMappingURL=merkle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"merkle.js","sourceRoot":"","sources":["../../src/captcha/merkle.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,EAAE,EAAE,MAAM,eAAe,CAAA;AAClC,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAA;AAE9C,MAAM,UAAU;IAKZ,YAAY,IAAY;QACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;QAChB,IAAI,CAAC,MAAM,GAAG,IAAI,CAAA;IACtB,CAAC;CACJ;AAED,MAAM,OAAO,iBAAiB;IAO1B;QACI,IAAI,CAAC,MAAM,GAAG,EAAE,CAAA;QAChB,IAAI,CAAC,MAAM,GAAG,EAAE,CAAA;IACpB,CAAC;IAED,KAAK,CAAC,MAAgB;QAClB,gBAAgB;QAChB,IAAI,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE;YACpB,IAAI,CAAC,MAAM,GAAG,EAAE,CAAA;SACnB;QACD,MAAM,SAAS,GAAa,EAAE,CAAA;QAC9B,KAAK,MAAM,IAAI,IAAI,MAAM,EAAE;YACvB,MAAM,IAAI,GAAG,IAAI,UAAU,CAAC,IAAI,CAAC,CAAA;YACjC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;YACtB,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;SAC5B;QACD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,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ceRoot":"","sources":["../../src/captcha/util.ts"],"names":[],"mappings":"AAgBA,wBAAsB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CASpE"}
@@ -0,0 +1,25 @@
1
+ // Copyright 2021-2023 Prosopo (UK) Ltd.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ import { ProsopoEnvError } from '@prosopo/common';
15
+ import client from 'axios';
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
+ }
24
+ }
25
+ //# sourceMappingURL=util.js.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,2 @@
1
+ export * from './captcha/index.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,oBAAoB,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,15 @@
1
+ // Copyright 2021-2023 Prosopo (UK) Ltd.
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ export * from './captcha/index.js';
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,wCAAwC;AACxC,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,iDAAiD;AACjD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AACjC,cAAc,oBAAoB,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prosopo/datasets",
3
- "version": "0.1.18",
3
+ "version": "0.2.0",
4
4
  "author": "PROSOPO LIMITED <info@prosopo.io>",
5
5
  "license": "Apache-2.0",
6
6
  "private": false,
@@ -32,10 +32,10 @@
32
32
  },
33
33
  "dependencies": {
34
34
  "@polkadot/util": "^12.3.2",
35
- "@prosopo/common": "^0.1.18",
36
- "@prosopo/contract": "^0.1.18",
37
- "@prosopo/types": "^0.1.18",
38
- "@prosopo/util": "^0.1.18",
35
+ "@prosopo/common": "0.2.0",
36
+ "@prosopo/contract": "0.2.0",
37
+ "@prosopo/types": "0.2.0",
38
+ "@prosopo/util": "0.2.0",
39
39
  "axios": "^0.27.2"
40
40
  },
41
41
  "devDependencies": {