@prosopo/datasets 3.0.8 → 3.0.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.
Files changed (49) hide show
  1. package/CHANGELOG.md +31 -0
  2. package/dist/captcha/captcha.js +173 -154
  3. package/dist/captcha/dataset.js +83 -69
  4. package/dist/captcha/index.js +27 -5
  5. package/dist/captcha/merkle.js +112 -109
  6. package/dist/captcha/util.js +19 -18
  7. package/dist/index.js +31 -3
  8. package/dist/tests/mocks/data/captchas.js +1039 -1033
  9. package/dist/tests/mocks/data/captchas.json +886 -886
  10. package/dist/tests/mocks/data/captchas1.json +132 -132
  11. package/dist/tests/mocks/data/captchas2.json +175 -175
  12. package/dist/tests/mocks/data/captchas3.json +133 -133
  13. package/dist/tests/mocks/data/captchas4.json +132 -132
  14. package/package.json +15 -12
  15. package/vite.cjs.config.ts +4 -1
  16. package/vite.esm.config.ts +20 -0
  17. package/dist/captcha/captcha.d.ts +0 -21
  18. package/dist/captcha/captcha.d.ts.map +0 -1
  19. package/dist/captcha/captcha.js.map +0 -1
  20. package/dist/captcha/dataset.d.ts +0 -8
  21. package/dist/captcha/dataset.d.ts.map +0 -1
  22. package/dist/captcha/dataset.js.map +0 -1
  23. package/dist/captcha/index.d.ts +0 -5
  24. package/dist/captcha/index.d.ts.map +0 -1
  25. package/dist/captcha/index.js.map +0 -1
  26. package/dist/captcha/merkle.d.ts +0 -20
  27. package/dist/captcha/merkle.d.ts.map +0 -1
  28. package/dist/captcha/merkle.js.map +0 -1
  29. package/dist/captcha/util.d.ts +0 -2
  30. package/dist/captcha/util.d.ts.map +0 -1
  31. package/dist/captcha/util.js.map +0 -1
  32. package/dist/index.d.ts +0 -3
  33. package/dist/index.d.ts.map +0 -1
  34. package/dist/index.js.map +0 -1
  35. package/dist/tests/captcha.unit.test.d.ts +0 -2
  36. package/dist/tests/captcha.unit.test.d.ts.map +0 -1
  37. package/dist/tests/captcha.unit.test.js +0 -357
  38. package/dist/tests/captcha.unit.test.js.map +0 -1
  39. package/dist/tests/dataset.unit.test.d.ts +0 -2
  40. package/dist/tests/dataset.unit.test.d.ts.map +0 -1
  41. package/dist/tests/dataset.unit.test.js +0 -126
  42. package/dist/tests/dataset.unit.test.js.map +0 -1
  43. package/dist/tests/merkle.unit.test.d.ts +0 -2
  44. package/dist/tests/merkle.unit.test.d.ts.map +0 -1
  45. package/dist/tests/merkle.unit.test.js +0 -171
  46. package/dist/tests/merkle.unit.test.js.map +0 -1
  47. package/dist/tests/mocks/data/captchas.d.ts +0 -24
  48. package/dist/tests/mocks/data/captchas.d.ts.map +0 -1
  49. package/dist/tests/mocks/data/captchas.js.map +0 -1
@@ -1,124 +1,127 @@
1
1
  import { ProsopoError, hexHashArray } from "@prosopo/common";
2
2
  import { at } from "@prosopo/util";
3
3
  class MerkleNode {
4
- constructor(hash) {
5
- this.hash = hash;
6
- this.parent = null;
7
- }
4
+ constructor(hash) {
5
+ this.hash = hash;
6
+ this.parent = null;
7
+ }
8
8
  }
9
- export class CaptchaMerkleTree {
10
- constructor() {
11
- this.leaves = [];
12
- this.layers = [];
13
- }
14
- getRoot() {
15
- if (this.root === undefined) {
16
- throw new ProsopoError("DATASET.MERKLE_ERROR", {
17
- context: {
18
- error: "root undefined",
19
- failedFuncName: this.getRoot.name,
20
- },
21
- });
9
+ class CaptchaMerkleTree {
10
+ constructor() {
11
+ this.leaves = [];
12
+ this.layers = [];
13
+ }
14
+ getRoot() {
15
+ if (this.root === void 0) {
16
+ throw new ProsopoError("DATASET.MERKLE_ERROR", {
17
+ context: {
18
+ error: "root undefined",
19
+ failedFuncName: this.getRoot.name
22
20
  }
23
- return this.root;
21
+ });
24
22
  }
25
- build(leaves) {
26
- if (this.layers.length) {
27
- this.layers = [];
28
- }
29
- const layerZero = [];
30
- for (const leaf of leaves) {
31
- const node = new MerkleNode(leaf);
32
- this.leaves.push(node);
33
- layerZero.push(node.hash);
34
- }
35
- this.layers.push(layerZero);
36
- this.root = this.buildMerkleTree(this.leaves)[0];
23
+ return this.root;
24
+ }
25
+ build(leaves) {
26
+ if (this.layers.length) {
27
+ this.layers = [];
37
28
  }
38
- buildMerkleTree(leaves) {
39
- const numLeaves = leaves.length;
40
- if (numLeaves === 1) {
41
- return leaves;
42
- }
43
- const parents = [];
44
- let leafIndex = 0;
45
- const newLayer = [];
46
- while (leafIndex < numLeaves) {
47
- const leftChild = leaves[leafIndex];
48
- if (leftChild === undefined) {
49
- throw new ProsopoError("DEVELOPER.GENERAL", {
50
- context: { error: "leftChild undefined" },
51
- });
52
- }
53
- const rightChild = leafIndex + 1 < numLeaves ? at(leaves, leafIndex + 1) : leftChild;
54
- const parentNode = this.createParent(leftChild, rightChild);
55
- newLayer.push(parentNode.hash);
56
- parents.push(parentNode);
57
- leafIndex += 2;
58
- }
59
- this.layers.push(newLayer);
60
- return this.buildMerkleTree(parents);
29
+ const layerZero = [];
30
+ for (const leaf of leaves) {
31
+ const node = new MerkleNode(leaf);
32
+ this.leaves.push(node);
33
+ layerZero.push(node.hash);
61
34
  }
62
- createParent(leftChild, rightChild) {
63
- const parent = new MerkleNode(hexHashArray([leftChild.hash, rightChild.hash]));
64
- leftChild.parent = parent.hash;
65
- rightChild.parent = parent.hash;
66
- return parent;
35
+ this.layers.push(layerZero);
36
+ this.root = this.buildMerkleTree(this.leaves)[0];
37
+ }
38
+ buildMerkleTree(leaves) {
39
+ const numLeaves = leaves.length;
40
+ if (numLeaves === 1) {
41
+ return leaves;
67
42
  }
68
- proof(leafHash) {
69
- const proofTree = [];
70
- let layerNum = 0;
71
- while (layerNum < this.layers.length - 1) {
72
- const layer = this.layers[layerNum];
73
- if (layer === undefined) {
74
- throw new ProsopoError("DATASET.MERKLE_ERROR", {
75
- context: {
76
- error: "layer undefined",
77
- failedFuncName: this.proof.name,
78
- layerNum,
79
- },
80
- });
81
- }
82
- const leafIndex = layer.indexOf(leafHash);
83
- let partnerIndex = leafIndex % 2 && leafIndex > 0 ? leafIndex - 1 : leafIndex + 1;
84
- if (partnerIndex > layer.length - 1) {
85
- partnerIndex = leafIndex;
86
- }
87
- const pair = [leafHash];
88
- const partner = at(layer, partnerIndex);
89
- if (partnerIndex > leafIndex) {
90
- pair.push(partner);
91
- }
92
- else {
93
- pair.unshift(partner);
94
- }
95
- proofTree.push([at(pair, 0), at(pair, 1)]);
96
- layerNum += 1;
97
- leafHash = hexHashArray(pair);
98
- }
99
- const last = at(this.layers, this.layers.length - 1);
100
- return [...proofTree, [at(last, 0)]];
43
+ const parents = [];
44
+ let leafIndex = 0;
45
+ const newLayer = [];
46
+ while (leafIndex < numLeaves) {
47
+ const leftChild = leaves[leafIndex];
48
+ if (leftChild === void 0) {
49
+ throw new ProsopoError("DEVELOPER.GENERAL", {
50
+ context: { error: "leftChild undefined" }
51
+ });
52
+ }
53
+ const rightChild = leafIndex + 1 < numLeaves ? at(leaves, leafIndex + 1) : leftChild;
54
+ const parentNode = this.createParent(leftChild, rightChild);
55
+ newLayer.push(parentNode.hash);
56
+ parents.push(parentNode);
57
+ leafIndex += 2;
58
+ }
59
+ this.layers.push(newLayer);
60
+ return this.buildMerkleTree(parents);
61
+ }
62
+ createParent(leftChild, rightChild) {
63
+ const parent = new MerkleNode(
64
+ hexHashArray([leftChild.hash, rightChild.hash])
65
+ );
66
+ leftChild.parent = parent.hash;
67
+ rightChild.parent = parent.hash;
68
+ return parent;
69
+ }
70
+ proof(leafHash) {
71
+ const proofTree = [];
72
+ let layerNum = 0;
73
+ while (layerNum < this.layers.length - 1) {
74
+ const layer = this.layers[layerNum];
75
+ if (layer === void 0) {
76
+ throw new ProsopoError("DATASET.MERKLE_ERROR", {
77
+ context: {
78
+ error: "layer undefined",
79
+ failedFuncName: this.proof.name,
80
+ layerNum
81
+ }
82
+ });
83
+ }
84
+ const leafIndex = layer.indexOf(leafHash);
85
+ let partnerIndex = leafIndex % 2 && leafIndex > 0 ? leafIndex - 1 : leafIndex + 1;
86
+ if (partnerIndex > layer.length - 1) {
87
+ partnerIndex = leafIndex;
88
+ }
89
+ const pair = [leafHash];
90
+ const partner = at(layer, partnerIndex);
91
+ if (partnerIndex > leafIndex) {
92
+ pair.push(partner);
93
+ } else {
94
+ pair.unshift(partner);
95
+ }
96
+ proofTree.push([at(pair, 0), at(pair, 1)]);
97
+ layerNum += 1;
98
+ leafHash = hexHashArray(pair);
101
99
  }
100
+ const last = at(this.layers, this.layers.length - 1);
101
+ return [...proofTree, [at(last, 0)]];
102
+ }
102
103
  }
103
- export function verifyProof(leaf, proof) {
104
- try {
105
- if (at(proof, 0).indexOf(leaf) === -1) {
106
- return false;
107
- }
108
- for (const [layerIndex, layer] of proof.entries()) {
109
- leaf = hexHashArray(layer);
110
- if (at(proof, layerIndex + 1).indexOf(leaf) === -1) {
111
- return false;
112
- }
113
- const last = at(proof, proof.length - 1);
114
- if (leaf === at(last, 0)) {
115
- return true;
116
- }
117
- }
118
- return false;
104
+ function verifyProof(leaf, proof) {
105
+ try {
106
+ if (at(proof, 0).indexOf(leaf) === -1) {
107
+ return false;
119
108
  }
120
- catch (err) {
109
+ for (const [layerIndex, layer] of proof.entries()) {
110
+ leaf = hexHashArray(layer);
111
+ if (at(proof, layerIndex + 1).indexOf(leaf) === -1) {
121
112
  return false;
113
+ }
114
+ const last = at(proof, proof.length - 1);
115
+ if (leaf === at(last, 0)) {
116
+ return true;
117
+ }
122
118
  }
119
+ return false;
120
+ } catch (err) {
121
+ return false;
122
+ }
123
123
  }
124
- //# sourceMappingURL=merkle.js.map
124
+ export {
125
+ CaptchaMerkleTree,
126
+ verifyProof
127
+ };
@@ -1,22 +1,23 @@
1
1
  import { ProsopoDatasetError, ProsopoEnvError } from "@prosopo/common";
2
- export async function downloadImage(url) {
3
- try {
4
- const response = await fetch(url);
5
- if (!response.ok) {
6
- throw new ProsopoDatasetError("API.BAD_REQUEST", {
7
- context: {
8
- error: `Network response was not ok, status: ${response.status}`,
9
- url,
10
- },
11
- });
2
+ async function downloadImage(url) {
3
+ try {
4
+ const response = await fetch(url);
5
+ if (!response.ok) {
6
+ throw new ProsopoDatasetError("API.BAD_REQUEST", {
7
+ context: {
8
+ error: `Network response was not ok, status: ${response.status}`,
9
+ url
12
10
  }
13
- const buffer = await response.arrayBuffer();
14
- return new Uint8Array(buffer);
15
- }
16
- catch (err) {
17
- throw new ProsopoEnvError("DATABASE.IMAGE_GET_FAILED", {
18
- context: { error: err },
19
- });
11
+ });
20
12
  }
13
+ const buffer = await response.arrayBuffer();
14
+ return new Uint8Array(buffer);
15
+ } catch (err) {
16
+ throw new ProsopoEnvError("DATABASE.IMAGE_GET_FAILED", {
17
+ context: { error: err }
18
+ });
19
+ }
21
20
  }
22
- //# sourceMappingURL=util.js.map
21
+ export {
22
+ downloadImage
23
+ };
package/dist/index.js CHANGED
@@ -1,3 +1,31 @@
1
- export * from "./captcha/index.js";
2
- export { datasetWithSolutionHashes, datasetWithIndexSolutions, } from "./tests/mocks/data/captchas.js";
3
- //# sourceMappingURL=index.js.map
1
+ import "./captcha/index.js";
2
+ import { datasetWithIndexSolutions, datasetWithSolutionHashes } from "./tests/mocks/data/captchas.js";
3
+ import { NO_SOLUTION_VALUE, captchaSort, compareCaptchaSolutions, computeCaptchaHash, computeCaptchaSolutionHash, computeItemHash, computePendingRequestHash, getSolutionValueToHash, matchItemsToSolutions, parseAndSortCaptchaSolutions, parseCaptchaAssets, parseCaptchaDataset, sortAndComputeHashes } from "./captcha/captcha.js";
4
+ import { CaptchaMerkleTree, verifyProof } from "./captcha/merkle.js";
5
+ import { downloadImage } from "./captcha/util.js";
6
+ import { addSolutionHashesToDataset, buildCaptchaTree, buildDataset, hashDatasetItems, validateDatasetContent } from "./captcha/dataset.js";
7
+ export {
8
+ CaptchaMerkleTree,
9
+ NO_SOLUTION_VALUE,
10
+ addSolutionHashesToDataset,
11
+ buildCaptchaTree,
12
+ buildDataset,
13
+ captchaSort,
14
+ compareCaptchaSolutions,
15
+ computeCaptchaHash,
16
+ computeCaptchaSolutionHash,
17
+ computeItemHash,
18
+ computePendingRequestHash,
19
+ datasetWithIndexSolutions,
20
+ datasetWithSolutionHashes,
21
+ downloadImage,
22
+ getSolutionValueToHash,
23
+ hashDatasetItems,
24
+ matchItemsToSolutions,
25
+ parseAndSortCaptchaSolutions,
26
+ parseCaptchaAssets,
27
+ parseCaptchaDataset,
28
+ sortAndComputeHashes,
29
+ validateDatasetContent,
30
+ verifyProof
31
+ };