@prosopo/datasets 3.1.54 → 3.1.55

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