@prosopo/datasets 3.0.42 → 3.0.43

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 (72) hide show
  1. package/CHANGELOG.md +20 -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/cjs/captcha/captcha.cjs +195 -0
  8. package/dist/cjs/captcha/dataset.cjs +94 -0
  9. package/dist/cjs/captcha/index.cjs +27 -0
  10. package/dist/cjs/captcha/merkle.cjs +128 -0
  11. package/dist/cjs/captcha/util.cjs +23 -0
  12. package/dist/cjs/index.cjs +31 -0
  13. package/dist/cjs/tests/mocks/data/captchas.cjs +1044 -0
  14. package/dist/index.js +31 -3
  15. package/dist/tests/mocks/data/captchas.js +1039 -1033
  16. package/dist/tests/mocks/data/captchas.json +886 -886
  17. package/dist/tests/mocks/data/captchas1.json +132 -132
  18. package/dist/tests/mocks/data/captchas2.json +175 -175
  19. package/dist/tests/mocks/data/captchas3.json +133 -133
  20. package/dist/tests/mocks/data/captchas4.json +132 -132
  21. package/package.json +7 -7
  22. package/coverage/base.css +0 -224
  23. package/coverage/block-navigation.js +0 -87
  24. package/coverage/clover.xml +0 -493
  25. package/coverage/coverage-final.json +0 -7
  26. package/coverage/favicon.png +0 -0
  27. package/coverage/index.html +0 -131
  28. package/coverage/prettify.css +0 -1
  29. package/coverage/prettify.js +0 -2
  30. package/coverage/sort-arrow-sprite.png +0 -0
  31. package/coverage/sorter.js +0 -210
  32. package/coverage/src/captcha/captcha.ts.html +0 -1093
  33. package/coverage/src/captcha/dataset.ts.html +0 -490
  34. package/coverage/src/captcha/index.html +0 -176
  35. package/coverage/src/captcha/index.ts.html +0 -136
  36. package/coverage/src/captcha/merkle.ts.html +0 -610
  37. package/coverage/src/captcha/util.ts.html +0 -187
  38. package/coverage/src/index.html +0 -116
  39. package/coverage/src/index.ts.html +0 -139
  40. package/dist/captcha/captcha.d.ts +0 -21
  41. package/dist/captcha/captcha.d.ts.map +0 -1
  42. package/dist/captcha/captcha.js.map +0 -1
  43. package/dist/captcha/dataset.d.ts +0 -8
  44. package/dist/captcha/dataset.d.ts.map +0 -1
  45. package/dist/captcha/dataset.js.map +0 -1
  46. package/dist/captcha/index.d.ts +0 -5
  47. package/dist/captcha/index.d.ts.map +0 -1
  48. package/dist/captcha/index.js.map +0 -1
  49. package/dist/captcha/merkle.d.ts +0 -20
  50. package/dist/captcha/merkle.d.ts.map +0 -1
  51. package/dist/captcha/merkle.js.map +0 -1
  52. package/dist/captcha/util.d.ts +0 -2
  53. package/dist/captcha/util.d.ts.map +0 -1
  54. package/dist/captcha/util.js.map +0 -1
  55. package/dist/index.d.ts +0 -3
  56. package/dist/index.d.ts.map +0 -1
  57. package/dist/index.js.map +0 -1
  58. package/dist/tests/captcha.unit.test.d.ts +0 -2
  59. package/dist/tests/captcha.unit.test.d.ts.map +0 -1
  60. package/dist/tests/captcha.unit.test.js +0 -357
  61. package/dist/tests/captcha.unit.test.js.map +0 -1
  62. package/dist/tests/dataset.unit.test.d.ts +0 -2
  63. package/dist/tests/dataset.unit.test.d.ts.map +0 -1
  64. package/dist/tests/dataset.unit.test.js +0 -126
  65. package/dist/tests/dataset.unit.test.js.map +0 -1
  66. package/dist/tests/merkle.unit.test.d.ts +0 -2
  67. package/dist/tests/merkle.unit.test.d.ts.map +0 -1
  68. package/dist/tests/merkle.unit.test.js +0 -171
  69. package/dist/tests/merkle.unit.test.js.map +0 -1
  70. package/dist/tests/mocks/data/captchas.d.ts +0 -24
  71. package/dist/tests/mocks/data/captchas.d.ts.map +0 -1
  72. package/dist/tests/mocks/data/captchas.js.map +0 -1
@@ -0,0 +1,128 @@
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
+ }
107
+ 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
+ }
126
+ }
127
+ exports.CaptchaMerkleTree = CaptchaMerkleTree;
128
+ exports.verifyProof = verifyProof;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const common = require("@prosopo/common");
4
+ 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
+ }
22
+ }
23
+ exports.downloadImage = downloadImage;
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+ 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;