@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.
- package/CHANGELOG.md +31 -0
- package/dist/captcha/captcha.js +173 -154
- package/dist/captcha/dataset.js +83 -69
- package/dist/captcha/index.js +27 -5
- package/dist/captcha/merkle.js +112 -109
- package/dist/captcha/util.js +19 -18
- package/dist/index.js +31 -3
- package/dist/tests/mocks/data/captchas.js +1039 -1033
- package/dist/tests/mocks/data/captchas.json +886 -886
- package/dist/tests/mocks/data/captchas1.json +132 -132
- package/dist/tests/mocks/data/captchas2.json +175 -175
- package/dist/tests/mocks/data/captchas3.json +133 -133
- package/dist/tests/mocks/data/captchas4.json +132 -132
- package/package.json +15 -12
- package/vite.cjs.config.ts +4 -1
- package/vite.esm.config.ts +20 -0
- package/dist/captcha/captcha.d.ts +0 -21
- package/dist/captcha/captcha.d.ts.map +0 -1
- package/dist/captcha/captcha.js.map +0 -1
- package/dist/captcha/dataset.d.ts +0 -8
- package/dist/captcha/dataset.d.ts.map +0 -1
- package/dist/captcha/dataset.js.map +0 -1
- package/dist/captcha/index.d.ts +0 -5
- package/dist/captcha/index.d.ts.map +0 -1
- package/dist/captcha/index.js.map +0 -1
- package/dist/captcha/merkle.d.ts +0 -20
- package/dist/captcha/merkle.d.ts.map +0 -1
- package/dist/captcha/merkle.js.map +0 -1
- package/dist/captcha/util.d.ts +0 -2
- package/dist/captcha/util.d.ts.map +0 -1
- package/dist/captcha/util.js.map +0 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/tests/captcha.unit.test.d.ts +0 -2
- package/dist/tests/captcha.unit.test.d.ts.map +0 -1
- package/dist/tests/captcha.unit.test.js +0 -357
- package/dist/tests/captcha.unit.test.js.map +0 -1
- package/dist/tests/dataset.unit.test.d.ts +0 -2
- package/dist/tests/dataset.unit.test.d.ts.map +0 -1
- package/dist/tests/dataset.unit.test.js +0 -126
- package/dist/tests/dataset.unit.test.js.map +0 -1
- package/dist/tests/merkle.unit.test.d.ts +0 -2
- package/dist/tests/merkle.unit.test.d.ts.map +0 -1
- package/dist/tests/merkle.unit.test.js +0 -171
- package/dist/tests/merkle.unit.test.js.map +0 -1
- package/dist/tests/mocks/data/captchas.d.ts +0 -24
- package/dist/tests/mocks/data/captchas.d.ts.map +0 -1
- package/dist/tests/mocks/data/captchas.js.map +0 -1
package/dist/captcha/merkle.js
CHANGED
|
@@ -1,124 +1,127 @@
|
|
|
1
1
|
import { ProsopoError, hexHashArray } from "@prosopo/common";
|
|
2
2
|
import { at } from "@prosopo/util";
|
|
3
3
|
class MerkleNode {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
constructor(hash) {
|
|
5
|
+
this.hash = hash;
|
|
6
|
+
this.parent = null;
|
|
7
|
+
}
|
|
8
8
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
21
|
+
});
|
|
24
22
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
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
|
-
|
|
124
|
+
export {
|
|
125
|
+
CaptchaMerkleTree,
|
|
126
|
+
verifyProof
|
|
127
|
+
};
|
package/dist/captcha/util.js
CHANGED
|
@@ -1,22 +1,23 @@
|
|
|
1
1
|
import { ProsopoDatasetError, ProsopoEnvError } from "@prosopo/common";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
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
|
-
|
|
21
|
+
export {
|
|
22
|
+
downloadImage
|
|
23
|
+
};
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,31 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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
|
+
};
|