@prosopo/procaptcha 0.1.19 → 0.2.1
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/dist/api/HttpClientBase.js +1 -1
- package/dist/api/HttpClientBase.js.map +1 -1
- package/dist/cjs/api/Extension.cjs +4 -0
- package/dist/cjs/api/ExtensionWeb2.cjs +88 -0
- package/dist/cjs/api/ExtensionWeb3.cjs +25 -0
- package/dist/cjs/api/HttpClientBase.cjs +17 -0
- package/dist/cjs/api/errors.cjs +32 -0
- package/dist/cjs/api/handlers.cjs +11 -0
- package/dist/cjs/api/index.cjs +6 -0
- package/dist/cjs/datasets/dist/captcha/captcha.cjs +142 -0
- package/dist/cjs/datasets/dist/captcha/dataset.cjs +87 -0
- package/dist/cjs/datasets/dist/captcha/index.cjs +27 -0
- package/dist/cjs/datasets/dist/captcha/merkle.cjs +106 -0
- package/dist/cjs/datasets/dist/captcha/util.cjs +12 -0
- package/dist/cjs/datasets/dist/index.cjs +2 -0
- package/dist/cjs/index.cjs +18 -0
- package/dist/cjs/modules/Manager.cjs +369 -0
- package/dist/cjs/modules/ProsopoCaptchaApi.cjs +96 -0
- package/dist/cjs/modules/canvas.cjs +374 -0
- package/dist/cjs/modules/index.cjs +8 -0
- package/dist/cjs/modules/storage.cjs +22 -0
- package/dist/cjs/types/api.cjs +1 -0
- package/dist/cjs/types/client.cjs +1 -0
- package/dist/cjs/types/contract.cjs +1 -0
- package/dist/cjs/types/index.cjs +7 -0
- package/dist/cjs/types/manager.cjs +11 -0
- package/dist/cjs/utils/index.cjs +4 -0
- package/dist/cjs/utils/utils.cjs +6 -0
- package/package.json +12 -11
- package/vite.cjs.config.ts +6 -0
|
@@ -20,7 +20,7 @@ export class HttpClientBase {
|
|
|
20
20
|
};
|
|
21
21
|
this.errorHandler = (error) => Promise.reject(error.response);
|
|
22
22
|
baseURL = baseURL + prefix;
|
|
23
|
-
this.axios = axios.
|
|
23
|
+
this.axios = axios.create({ baseURL });
|
|
24
24
|
this.axios.interceptors.response.use(this.responseHandler, this.errorHandler);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"HttpClientBase.js","sourceRoot":"","sources":["../../src/api/HttpClientBase.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,KAAuC,MAAM,OAAO,CAAA;AAE3D,MAAM,OAAO,cAAc;IAGvB,YAAY,OAAe,EAAE,MAAM,GAAG,EAAE;QAM9B,oBAAe,GAAG,CAAC,QAAuB,EAAE,EAAE;YACpD,OAAO,CAAC,GAAG,CAAC,aAAa,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;YAC5C,OAAO,QAAQ,CAAC,IAAI,CAAA;QACxB,CAAC,CAAA;QAES,iBAAY,GAAG,CAAC,KAAU,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;QAVnE,OAAO,GAAG,OAAO,GAAG,MAAM,CAAA;QAC1B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,
|
|
1
|
+
{"version":3,"file":"HttpClientBase.js","sourceRoot":"","sources":["../../src/api/HttpClientBase.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,KAAuC,MAAM,OAAO,CAAA;AAE3D,MAAM,OAAO,cAAc;IAGvB,YAAY,OAAe,EAAE,MAAM,GAAG,EAAE;QAM9B,oBAAe,GAAG,CAAC,QAAuB,EAAE,EAAE;YACpD,OAAO,CAAC,GAAG,CAAC,aAAa,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;YAC5C,OAAO,QAAQ,CAAC,IAAI,CAAA;QACxB,CAAC,CAAA;QAES,iBAAY,GAAG,CAAC,KAAU,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAA;QAVnE,OAAO,GAAG,OAAO,GAAG,MAAM,CAAA;QAC1B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,CAAA;QACtC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,eAAe,EAAE,IAAI,CAAC,YAAY,CAAC,CAAA;IACjF,CAAC;CAQJ;AAED,eAAe,cAAc,CAAA"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const api = require("@polkadot/api");
|
|
3
|
+
const ws = require("@polkadot/rpc-provider/ws");
|
|
4
|
+
const utilCrypto = require("@polkadot/util-crypto");
|
|
5
|
+
const bip39 = require("@polkadot/util-crypto/mnemonic/bip39");
|
|
6
|
+
const Manager = require("../modules/Manager.cjs");
|
|
7
|
+
const common = require("@prosopo/common");
|
|
8
|
+
const canvas = require("../modules/canvas.cjs");
|
|
9
|
+
const util = require("@polkadot/util");
|
|
10
|
+
const Extension = require("./Extension.cjs");
|
|
11
|
+
const FingerprintJS = require("@fingerprintjs/fingerprintjs");
|
|
12
|
+
const Signer = require("@polkadot/extension-base/page/Signer");
|
|
13
|
+
class ExtWeb2 extends Extension {
|
|
14
|
+
async getAccount(config) {
|
|
15
|
+
const network = Manager.getNetwork(config);
|
|
16
|
+
const wsProvider = new ws.WsProvider(network.endpoint);
|
|
17
|
+
const account = await this.createAccount(wsProvider);
|
|
18
|
+
const extension = await this.createExtension(account);
|
|
19
|
+
return {
|
|
20
|
+
account,
|
|
21
|
+
extension
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
async createExtension(account) {
|
|
25
|
+
const signer = new Signer(async () => {
|
|
26
|
+
return;
|
|
27
|
+
});
|
|
28
|
+
signer.signRaw = async (payload) => {
|
|
29
|
+
const signature = account.keypair.sign(payload.data);
|
|
30
|
+
return {
|
|
31
|
+
id: 1,
|
|
32
|
+
// the id of the request to sign. This should be incremented each time and adjust the signature, but we're hacking around this. Hence the signature will always be the same given the same payload.
|
|
33
|
+
signature: util.u8aToHex(signature)
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
return {
|
|
37
|
+
accounts: {
|
|
38
|
+
get: async () => {
|
|
39
|
+
return [account];
|
|
40
|
+
},
|
|
41
|
+
subscribe: () => {
|
|
42
|
+
return () => {
|
|
43
|
+
return;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
name: "procaptcha-web2",
|
|
48
|
+
version: "0.1.11",
|
|
49
|
+
signer
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
async createAccount(wsProvider) {
|
|
53
|
+
const params = {
|
|
54
|
+
area: { width: 300, height: 300 },
|
|
55
|
+
offsetParameter: 2001000001,
|
|
56
|
+
multiplier: 15e3,
|
|
57
|
+
fontSizeFactor: 1.5,
|
|
58
|
+
maxShadowBlur: 50,
|
|
59
|
+
numberOfRounds: 5,
|
|
60
|
+
seed: 42
|
|
61
|
+
};
|
|
62
|
+
const browserEntropy = await this.getFingerprint();
|
|
63
|
+
const canvasEntropy = canvas.picassoCanvas(params.numberOfRounds, params.seed, params);
|
|
64
|
+
const entropy = common.hexHash([canvasEntropy, browserEntropy].join(""), 128).slice(2);
|
|
65
|
+
const u8Entropy = util.stringToU8a(entropy);
|
|
66
|
+
const mnemonic = bip39.entropyToMnemonic(u8Entropy);
|
|
67
|
+
const api$1 = await api.ApiPromise.create({ provider: wsProvider });
|
|
68
|
+
const type = "sr25519";
|
|
69
|
+
const keyring = new api.Keyring({ type, ss58Format: api$1.registry.chainSS58 });
|
|
70
|
+
await utilCrypto.cryptoWaitReady();
|
|
71
|
+
const keypair = keyring.addFromMnemonic(mnemonic);
|
|
72
|
+
const address = keypair.address.length === 42 ? keypair.address : utilCrypto.encodeAddress(utilCrypto.decodeAddress(keypair.address), api$1.registry.chainSS58);
|
|
73
|
+
return {
|
|
74
|
+
address,
|
|
75
|
+
type,
|
|
76
|
+
name: address,
|
|
77
|
+
keypair
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
async getFingerprint() {
|
|
81
|
+
const fpPromise = FingerprintJS.load();
|
|
82
|
+
const fp = await fpPromise;
|
|
83
|
+
const result = await fp.get();
|
|
84
|
+
const { screenFrame, ...componentsReduced } = result.components;
|
|
85
|
+
return FingerprintJS.hashComponents(componentsReduced);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
module.exports = ExtWeb2;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const errors = require("./errors.cjs");
|
|
3
|
+
const extensionDapp = require("@polkadot/extension-dapp");
|
|
4
|
+
const Extension = require("./Extension.cjs");
|
|
5
|
+
class ExtWeb3 extends Extension {
|
|
6
|
+
async getAccount(config) {
|
|
7
|
+
const { dappName, userAccountAddress: address } = config;
|
|
8
|
+
if (!address) {
|
|
9
|
+
throw new errors.AccountNotFoundError("No account address provided");
|
|
10
|
+
}
|
|
11
|
+
const extensions = await extensionDapp.web3Enable(dappName);
|
|
12
|
+
if (extensions.length === 0) {
|
|
13
|
+
throw new errors.ExtensionNotFoundError();
|
|
14
|
+
}
|
|
15
|
+
for (const extension of extensions) {
|
|
16
|
+
const accounts = await extension.accounts.get();
|
|
17
|
+
const account = accounts.find((account2) => account2.address === address);
|
|
18
|
+
if (account) {
|
|
19
|
+
return { account, extension };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
throw new errors.AccountNotFoundError("No account found matching " + address);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
module.exports = ExtWeb3;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const client = require("axios");
|
|
4
|
+
class HttpClientBase {
|
|
5
|
+
constructor(baseURL, prefix = "") {
|
|
6
|
+
this.responseHandler = (response) => {
|
|
7
|
+
console.log("API REQUEST", response.request);
|
|
8
|
+
return response.data;
|
|
9
|
+
};
|
|
10
|
+
this.errorHandler = (error) => Promise.reject(error.response);
|
|
11
|
+
baseURL = baseURL + prefix;
|
|
12
|
+
this.axios = client.create({ baseURL });
|
|
13
|
+
this.axios.interceptors.response.use(this.responseHandler, this.errorHandler);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.HttpClientBase = HttpClientBase;
|
|
17
|
+
exports.default = HttpClientBase;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
class NoExtensionsFoundError extends Error {
|
|
4
|
+
constructor(msg) {
|
|
5
|
+
super(msg || "No extensions found");
|
|
6
|
+
}
|
|
7
|
+
}
|
|
8
|
+
class AccountCreationUnsupportedError extends Error {
|
|
9
|
+
constructor(msg) {
|
|
10
|
+
super(msg || "Account creation is not supported");
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
class AccountNotFoundError extends Error {
|
|
14
|
+
constructor(msg) {
|
|
15
|
+
super(msg || "No account found");
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
class ExtensionNotFoundError extends Error {
|
|
19
|
+
constructor(msg) {
|
|
20
|
+
super(msg || "No extensions found");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
class ExpiredError extends Error {
|
|
24
|
+
constructor(msg) {
|
|
25
|
+
super(msg || "The challenge has expired");
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.AccountCreationUnsupportedError = AccountCreationUnsupportedError;
|
|
29
|
+
exports.AccountNotFoundError = AccountNotFoundError;
|
|
30
|
+
exports.ExpiredError = ExpiredError;
|
|
31
|
+
exports.ExtensionNotFoundError = ExtensionNotFoundError;
|
|
32
|
+
exports.NoExtensionsFoundError = NoExtensionsFoundError;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
class ProsopoApiError extends Error {
|
|
4
|
+
constructor(error, context, ...params) {
|
|
5
|
+
super(error ? error.message : "Unknown API Error");
|
|
6
|
+
this.name = context && `${ProsopoApiError.name}@${context}` || ProsopoApiError.name;
|
|
7
|
+
console.error("\n********************* ERROR *********************\n");
|
|
8
|
+
console.error(this.cause, this.stack, ...params);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.ProsopoApiError = ProsopoApiError;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
require("./ExtensionWeb3.cjs");
|
|
4
|
+
require("./ExtensionWeb2.cjs");
|
|
5
|
+
const HttpClientBase = require("./HttpClientBase.cjs");
|
|
6
|
+
exports.HttpClientBase = HttpClientBase.HttpClientBase;
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const types = require("@prosopo/types");
|
|
4
|
+
const common = require("@prosopo/common");
|
|
5
|
+
const util = require("@prosopo/util");
|
|
6
|
+
const util$1 = require("./util.cjs");
|
|
7
|
+
const util$2 = require("@polkadot/util");
|
|
8
|
+
const NO_SOLUTION_VALUE = "NO_SOLUTION";
|
|
9
|
+
function parseCaptchaDataset(datasetJSON) {
|
|
10
|
+
try {
|
|
11
|
+
const result = types.DatasetSchema.parse(datasetJSON);
|
|
12
|
+
const result2 = {
|
|
13
|
+
format: result.format,
|
|
14
|
+
captchas: result.captchas.map((captcha) => {
|
|
15
|
+
return {
|
|
16
|
+
...captcha,
|
|
17
|
+
solution: captcha.solution || [],
|
|
18
|
+
unlabelled: captcha.unlabelled || []
|
|
19
|
+
};
|
|
20
|
+
})
|
|
21
|
+
};
|
|
22
|
+
if (result.datasetId !== void 0)
|
|
23
|
+
result2.datasetId = result.datasetId;
|
|
24
|
+
if (result.contentTree !== void 0)
|
|
25
|
+
result2.contentTree = result.contentTree;
|
|
26
|
+
if (result.datasetContentId !== void 0)
|
|
27
|
+
result2.datasetContentId = result.datasetContentId;
|
|
28
|
+
if (result.solutionTree !== void 0)
|
|
29
|
+
result2.solutionTree = result.solutionTree;
|
|
30
|
+
return result2;
|
|
31
|
+
} catch (err) {
|
|
32
|
+
throw new common.ProsopoEnvError(err);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function parseAndSortCaptchaSolutions(captchaJSON) {
|
|
36
|
+
try {
|
|
37
|
+
return types.CaptchaSolutionArraySchema.parse(captchaJSON).map((captcha) => ({
|
|
38
|
+
...captcha,
|
|
39
|
+
solution: captcha.solution.sort()
|
|
40
|
+
}));
|
|
41
|
+
} catch (err) {
|
|
42
|
+
throw new common.ProsopoEnvError(err);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function captchaSort(a, b) {
|
|
46
|
+
return a.captchaId.localeCompare(b.captchaId);
|
|
47
|
+
}
|
|
48
|
+
function sortAndComputeHashes(received, stored) {
|
|
49
|
+
received.sort(captchaSort);
|
|
50
|
+
stored.sort(captchaSort);
|
|
51
|
+
return stored.map(({ salt, items = [], target = "", captchaId, solved }, index) => {
|
|
52
|
+
const item = util.at(received, index);
|
|
53
|
+
if (captchaId != item.captchaId) {
|
|
54
|
+
throw new common.ProsopoEnvError("CAPTCHA.ID_MISMATCH");
|
|
55
|
+
}
|
|
56
|
+
return {
|
|
57
|
+
hash: computeCaptchaHash({
|
|
58
|
+
solution: solved ? item.solution : [],
|
|
59
|
+
salt,
|
|
60
|
+
items,
|
|
61
|
+
target
|
|
62
|
+
}, true, true, false),
|
|
63
|
+
captchaId
|
|
64
|
+
};
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
function compareCaptchaSolutions(received, stored) {
|
|
68
|
+
if (received.length && stored.length && received.length === stored.length) {
|
|
69
|
+
const hashes = sortAndComputeHashes(received, stored);
|
|
70
|
+
return hashes.every(({ hash, captchaId }) => hash === captchaId);
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
function computeCaptchaHash(captcha, includeSolution = false, includeSalt = false, sortItemHashes) {
|
|
75
|
+
try {
|
|
76
|
+
const itemHashes = captcha.items.map((item, index) => {
|
|
77
|
+
if (item.hash) {
|
|
78
|
+
return item.hash;
|
|
79
|
+
} else {
|
|
80
|
+
throw new common.ProsopoEnvError("CAPTCHA.MISSING_ITEM_HASH", computeCaptchaHash.name, void 0, index);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
return common.hexHashArray([
|
|
84
|
+
captcha.target,
|
|
85
|
+
// empty array hashes as empty string, undefined solution results in the array [`NO_SOLUTION`]
|
|
86
|
+
// [undefined] also hashes as empty string, which is why we don't use it
|
|
87
|
+
...includeSolution ? getSolutionValueToHash(captcha.solution) : [],
|
|
88
|
+
includeSalt ? captcha.salt : "",
|
|
89
|
+
sortItemHashes ? itemHashes.sort() : itemHashes
|
|
90
|
+
]);
|
|
91
|
+
} catch (err) {
|
|
92
|
+
throw new common.ProsopoEnvError(err);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
function getSolutionValueToHash(solution) {
|
|
96
|
+
return solution !== void 0 ? solution.sort() : [NO_SOLUTION_VALUE];
|
|
97
|
+
}
|
|
98
|
+
async function computeItemHash(item) {
|
|
99
|
+
if (item.type === "text") {
|
|
100
|
+
return { ...item, hash: common.hexHash(item.data) };
|
|
101
|
+
} else if (item.type === "image") {
|
|
102
|
+
return { ...item, hash: common.hexHash(await util$1.downloadImage(item.data)) };
|
|
103
|
+
} else {
|
|
104
|
+
throw new common.ProsopoEnvError("CAPTCHA.INVALID_ITEM_FORMAT");
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
function matchItemsToSolutions(solutions, items) {
|
|
108
|
+
return (solutions == null ? void 0 : solutions.map((solution) => {
|
|
109
|
+
var _a;
|
|
110
|
+
const index = parseInt(solution.toString());
|
|
111
|
+
const hash = items && items[index] && (((_a = items[index]) == null ? void 0 : _a.hash) || solution);
|
|
112
|
+
if (!hash) {
|
|
113
|
+
throw new common.ProsopoEnvError("CAPTCHA.MISSING_ITEM_HASH");
|
|
114
|
+
}
|
|
115
|
+
if (!util$2.isHex(hash)) {
|
|
116
|
+
throw new common.ProsopoEnvError("CAPTCHA.INVALID_ITEM_HASH");
|
|
117
|
+
}
|
|
118
|
+
return hash;
|
|
119
|
+
})) || [];
|
|
120
|
+
}
|
|
121
|
+
function computeCaptchaSolutionHash(captcha) {
|
|
122
|
+
return common.hexHashArray([captcha.captchaId, captcha.captchaContentId, [...captcha.solution].sort(), captcha.salt]);
|
|
123
|
+
}
|
|
124
|
+
function computePendingRequestHash(captchaIds, userAccount, salt) {
|
|
125
|
+
return common.hexHashArray([...captchaIds.sort(), userAccount, salt]);
|
|
126
|
+
}
|
|
127
|
+
function parseCaptchaAssets(item, assetsResolver) {
|
|
128
|
+
return { ...item, path: (assetsResolver == null ? void 0 : assetsResolver.resolveAsset(item.data).getURL()) || item.data };
|
|
129
|
+
}
|
|
130
|
+
exports.NO_SOLUTION_VALUE = NO_SOLUTION_VALUE;
|
|
131
|
+
exports.captchaSort = captchaSort;
|
|
132
|
+
exports.compareCaptchaSolutions = compareCaptchaSolutions;
|
|
133
|
+
exports.computeCaptchaHash = computeCaptchaHash;
|
|
134
|
+
exports.computeCaptchaSolutionHash = computeCaptchaSolutionHash;
|
|
135
|
+
exports.computeItemHash = computeItemHash;
|
|
136
|
+
exports.computePendingRequestHash = computePendingRequestHash;
|
|
137
|
+
exports.getSolutionValueToHash = getSolutionValueToHash;
|
|
138
|
+
exports.matchItemsToSolutions = matchItemsToSolutions;
|
|
139
|
+
exports.parseAndSortCaptchaSolutions = parseAndSortCaptchaSolutions;
|
|
140
|
+
exports.parseCaptchaAssets = parseCaptchaAssets;
|
|
141
|
+
exports.parseCaptchaDataset = parseCaptchaDataset;
|
|
142
|
+
exports.sortAndComputeHashes = sortAndComputeHashes;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const merkle = require("./merkle.cjs");
|
|
4
|
+
const common = require("@prosopo/common");
|
|
5
|
+
const util = require("@prosopo/util");
|
|
6
|
+
const captcha = require("./captcha.cjs");
|
|
7
|
+
const logger = common.getLogger(`Info`, `dataset.ts`);
|
|
8
|
+
async function hashDatasetItems(datasetRaw) {
|
|
9
|
+
return datasetRaw.captchas.map(async (captcha$1) => {
|
|
10
|
+
const items = await Promise.all(captcha$1.items.map(async (item) => captcha.computeItemHash(item)));
|
|
11
|
+
return {
|
|
12
|
+
...captcha$1,
|
|
13
|
+
items
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
async function validateDatasetContent(datasetOriginal) {
|
|
18
|
+
const captchaPromises = await hashDatasetItems(datasetOriginal);
|
|
19
|
+
const captchas = await Promise.all(captchaPromises);
|
|
20
|
+
const dataset = {
|
|
21
|
+
...datasetOriginal,
|
|
22
|
+
captchas
|
|
23
|
+
};
|
|
24
|
+
const hashes = dataset.captchas.map((captcha2) => {
|
|
25
|
+
const captchaRaw = datasetOriginal.captchas.find((captchaRaw2) => "captchaId" in captchaRaw2 ? captchaRaw2.captchaId === captcha2.captchaId : false);
|
|
26
|
+
if (captchaRaw) {
|
|
27
|
+
return captcha2.items.every((item, index) => item.hash === util.at(captchaRaw.items, index).hash);
|
|
28
|
+
} else {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
return hashes.every((hash) => hash);
|
|
33
|
+
}
|
|
34
|
+
async function buildDataset(datasetRaw) {
|
|
35
|
+
var _a, _b;
|
|
36
|
+
logger.info(`Adding solution hashes to dataset`);
|
|
37
|
+
const dataset = await addSolutionHashesToDataset(datasetRaw);
|
|
38
|
+
logger.info(`Building dataset merkle trees`);
|
|
39
|
+
const contentTree = await buildCaptchaTree(dataset, false, false, true);
|
|
40
|
+
const solutionTree = await buildCaptchaTree(dataset, true, true, false);
|
|
41
|
+
dataset.captchas = dataset.captchas.map((captcha2, index) => {
|
|
42
|
+
var _a2, _b2;
|
|
43
|
+
return {
|
|
44
|
+
...captcha2,
|
|
45
|
+
captchaId: util.at(solutionTree.leaves, index).hash,
|
|
46
|
+
captchaContentId: util.at(contentTree.leaves, index).hash,
|
|
47
|
+
datasetId: (_a2 = solutionTree.root) == null ? void 0 : _a2.hash,
|
|
48
|
+
datasetContentId: (_b2 = contentTree.root) == null ? void 0 : _b2.hash
|
|
49
|
+
};
|
|
50
|
+
});
|
|
51
|
+
dataset.solutionTree = solutionTree.layers;
|
|
52
|
+
dataset.contentTree = contentTree.layers;
|
|
53
|
+
dataset.datasetId = (_a = solutionTree.root) == null ? void 0 : _a.hash;
|
|
54
|
+
dataset.datasetContentId = (_b = contentTree.root) == null ? void 0 : _b.hash;
|
|
55
|
+
return dataset;
|
|
56
|
+
}
|
|
57
|
+
async function buildCaptchaTree(dataset, includeSolution, includeSalt, sortItemHashes) {
|
|
58
|
+
try {
|
|
59
|
+
const tree = new merkle.CaptchaMerkleTree();
|
|
60
|
+
const datasetWithItemHashes = { ...dataset };
|
|
61
|
+
const captchaHashes = datasetWithItemHashes.captchas.map((captcha$1) => captcha.computeCaptchaHash(captcha$1, includeSolution, includeSalt, sortItemHashes));
|
|
62
|
+
tree.build(captchaHashes);
|
|
63
|
+
return tree;
|
|
64
|
+
} catch (err) {
|
|
65
|
+
throw new common.ProsopoEnvError("DATASET.HASH_ERROR");
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async function addSolutionHashesToDataset(datasetRaw) {
|
|
69
|
+
const captchaPromises = datasetRaw.captchas.map(async (captcha$1) => {
|
|
70
|
+
return {
|
|
71
|
+
...captcha$1,
|
|
72
|
+
items: captcha$1.items,
|
|
73
|
+
// some captcha challenges will not have a solution
|
|
74
|
+
...captcha$1.solution !== void 0 && { solution: captcha.matchItemsToSolutions(captcha$1.solution, captcha$1.items) }
|
|
75
|
+
};
|
|
76
|
+
});
|
|
77
|
+
const captchas = await Promise.all(captchaPromises);
|
|
78
|
+
return {
|
|
79
|
+
...datasetRaw,
|
|
80
|
+
captchas
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
exports.addSolutionHashesToDataset = addSolutionHashesToDataset;
|
|
84
|
+
exports.buildCaptchaTree = buildCaptchaTree;
|
|
85
|
+
exports.buildDataset = buildDataset;
|
|
86
|
+
exports.hashDatasetItems = hashDatasetItems;
|
|
87
|
+
exports.validateDatasetContent = validateDatasetContent;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const captcha = require("./captcha.cjs");
|
|
4
|
+
const merkle = require("./merkle.cjs");
|
|
5
|
+
const util = require("./util.cjs");
|
|
6
|
+
const dataset = require("./dataset.cjs");
|
|
7
|
+
exports.NO_SOLUTION_VALUE = captcha.NO_SOLUTION_VALUE;
|
|
8
|
+
exports.captchaSort = captcha.captchaSort;
|
|
9
|
+
exports.compareCaptchaSolutions = captcha.compareCaptchaSolutions;
|
|
10
|
+
exports.computeCaptchaHash = captcha.computeCaptchaHash;
|
|
11
|
+
exports.computeCaptchaSolutionHash = captcha.computeCaptchaSolutionHash;
|
|
12
|
+
exports.computeItemHash = captcha.computeItemHash;
|
|
13
|
+
exports.computePendingRequestHash = captcha.computePendingRequestHash;
|
|
14
|
+
exports.getSolutionValueToHash = captcha.getSolutionValueToHash;
|
|
15
|
+
exports.matchItemsToSolutions = captcha.matchItemsToSolutions;
|
|
16
|
+
exports.parseAndSortCaptchaSolutions = captcha.parseAndSortCaptchaSolutions;
|
|
17
|
+
exports.parseCaptchaAssets = captcha.parseCaptchaAssets;
|
|
18
|
+
exports.parseCaptchaDataset = captcha.parseCaptchaDataset;
|
|
19
|
+
exports.sortAndComputeHashes = captcha.sortAndComputeHashes;
|
|
20
|
+
exports.CaptchaMerkleTree = merkle.CaptchaMerkleTree;
|
|
21
|
+
exports.verifyProof = merkle.verifyProof;
|
|
22
|
+
exports.downloadImage = util.downloadImage;
|
|
23
|
+
exports.addSolutionHashesToDataset = dataset.addSolutionHashesToDataset;
|
|
24
|
+
exports.buildCaptchaTree = dataset.buildCaptchaTree;
|
|
25
|
+
exports.buildDataset = dataset.buildDataset;
|
|
26
|
+
exports.hashDatasetItems = dataset.hashDatasetItems;
|
|
27
|
+
exports.validateDatasetContent = dataset.validateDatasetContent;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const util = require("@prosopo/util");
|
|
4
|
+
const common = require("@prosopo/common");
|
|
5
|
+
class MerkleNode {
|
|
6
|
+
constructor(hash) {
|
|
7
|
+
this.hash = hash;
|
|
8
|
+
this.parent = null;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
class CaptchaMerkleTree {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.leaves = [];
|
|
14
|
+
this.layers = [];
|
|
15
|
+
}
|
|
16
|
+
build(leaves) {
|
|
17
|
+
if (this.layers.length) {
|
|
18
|
+
this.layers = [];
|
|
19
|
+
}
|
|
20
|
+
const layerZero = [];
|
|
21
|
+
for (const leaf of leaves) {
|
|
22
|
+
const node = new MerkleNode(leaf);
|
|
23
|
+
this.leaves.push(node);
|
|
24
|
+
layerZero.push(node.hash);
|
|
25
|
+
}
|
|
26
|
+
this.layers.push(layerZero);
|
|
27
|
+
this.root = this.buildMerkleTree(this.leaves)[0];
|
|
28
|
+
}
|
|
29
|
+
buildMerkleTree(leaves) {
|
|
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 === void 0) {
|
|
40
|
+
throw new Error("leftChild undefined");
|
|
41
|
+
}
|
|
42
|
+
const rightChild = leafIndex + 1 < numLeaves ? util.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(common.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 === void 0) {
|
|
63
|
+
throw new Error("layer undefined");
|
|
64
|
+
}
|
|
65
|
+
const leafIndex = layer.indexOf(leafHash);
|
|
66
|
+
let partnerIndex = leafIndex % 2 && leafIndex > 0 ? leafIndex - 1 : leafIndex + 1;
|
|
67
|
+
if (partnerIndex > layer.length - 1) {
|
|
68
|
+
partnerIndex = leafIndex;
|
|
69
|
+
}
|
|
70
|
+
const pair = [leafHash];
|
|
71
|
+
const partner = util.at(layer, partnerIndex);
|
|
72
|
+
if (partnerIndex > leafIndex) {
|
|
73
|
+
pair.push(partner);
|
|
74
|
+
} else {
|
|
75
|
+
pair.unshift(partner);
|
|
76
|
+
}
|
|
77
|
+
proofTree.push([util.at(pair, 0), util.at(pair, 1)]);
|
|
78
|
+
layerNum += 1;
|
|
79
|
+
leafHash = common.hexHashArray(pair);
|
|
80
|
+
}
|
|
81
|
+
const last = util.at(this.layers, this.layers.length - 1);
|
|
82
|
+
return [...proofTree, [util.at(last, 0)]];
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function verifyProof(leaf, proof) {
|
|
86
|
+
try {
|
|
87
|
+
if (util.at(proof, 0).indexOf(leaf) === -1) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
for (const [layerIndex, layer] of proof.entries()) {
|
|
91
|
+
leaf = common.hexHashArray(layer);
|
|
92
|
+
if (util.at(proof, layerIndex + 1).indexOf(leaf) === -1) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
const last = util.at(proof, proof.length - 1);
|
|
96
|
+
if (leaf === util.at(last, 0)) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return false;
|
|
101
|
+
} catch (err) {
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
exports.CaptchaMerkleTree = CaptchaMerkleTree;
|
|
106
|
+
exports.verifyProof = verifyProof;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const common = require("@prosopo/common");
|
|
4
|
+
const client = require("axios");
|
|
5
|
+
async function downloadImage(url) {
|
|
6
|
+
try {
|
|
7
|
+
return new Uint8Array((await client.get(url, { url, method: "GET", responseType: "arraybuffer" })).data);
|
|
8
|
+
} catch (error) {
|
|
9
|
+
throw new common.ProsopoEnvError(error);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.downloadImage = downloadImage;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
require("./api/index.cjs");
|
|
4
|
+
require("./modules/index.cjs");
|
|
5
|
+
require("./types/index.cjs");
|
|
6
|
+
require("./utils/index.cjs");
|
|
7
|
+
const HttpClientBase = require("./api/HttpClientBase.cjs");
|
|
8
|
+
const Manager = require("./modules/Manager.cjs");
|
|
9
|
+
const ProsopoCaptchaApi = require("./modules/ProsopoCaptchaApi.cjs");
|
|
10
|
+
const manager = require("./types/manager.cjs");
|
|
11
|
+
const utils = require("./utils/utils.cjs");
|
|
12
|
+
exports.HttpClientBase = HttpClientBase.HttpClientBase;
|
|
13
|
+
exports.Manager = Manager.Manager;
|
|
14
|
+
exports.defaultState = Manager.defaultState;
|
|
15
|
+
exports.getNetwork = Manager.getNetwork;
|
|
16
|
+
exports.ProsopoCaptchaApi = ProsopoCaptchaApi.ProsopoCaptchaApi;
|
|
17
|
+
exports.ProcapchaEventNames = manager.ProcapchaEventNames;
|
|
18
|
+
exports.sleep = utils.sleep;
|