@prosopo/procaptcha 0.2.11 → 0.2.13
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/handlers.d.ts +2 -3
- package/dist/api/handlers.d.ts.map +1 -1
- package/dist/api/handlers.js +23 -1
- package/dist/api/handlers.js.map +1 -1
- package/dist/api/index.d.ts +0 -1
- package/dist/api/index.d.ts.map +1 -1
- package/dist/api/index.js +0 -1
- package/dist/api/index.js.map +1 -1
- package/dist/cjs/api/handlers.cjs +10 -1
- package/dist/cjs/api/index.cjs +0 -3
- package/dist/cjs/contracts/captcha/dist/contract-info/captcha.cjs +2 -2
- package/dist/cjs/index.cjs +0 -2
- package/dist/cjs/modules/Manager.cjs +209 -210
- package/dist/cjs/packages/datasets/dist/captcha/util.cjs +6 -2
- package/dist/modules/Manager.d.ts.map +1 -1
- package/dist/modules/Manager.js +265 -249
- package/dist/modules/Manager.js.map +1 -1
- package/dist/modules/ProsopoCaptchaApi.d.ts.map +1 -1
- package/dist/modules/ProsopoCaptchaApi.js +13 -0
- package/dist/modules/ProsopoCaptchaApi.js.map +1 -1
- package/package.json +23 -15
- package/dist/api/HttpClientBase.d.ts +0 -9
- package/dist/api/HttpClientBase.d.ts.map +0 -1
- package/dist/api/HttpClientBase.js +0 -28
- package/dist/api/HttpClientBase.js.map +0 -1
- package/dist/cjs/api/HttpClientBase.cjs +0 -17
package/dist/cjs/index.cjs
CHANGED
|
@@ -4,12 +4,10 @@ require("./api/index.cjs");
|
|
|
4
4
|
require("./modules/index.cjs");
|
|
5
5
|
require("./types/index.cjs");
|
|
6
6
|
require("./utils/index.cjs");
|
|
7
|
-
const HttpClientBase = require("./api/HttpClientBase.cjs");
|
|
8
7
|
const Manager = require("./modules/Manager.cjs");
|
|
9
8
|
const ProsopoCaptchaApi = require("./modules/ProsopoCaptchaApi.cjs");
|
|
10
9
|
const manager = require("./types/manager.cjs");
|
|
11
10
|
const utils = require("./utils/utils.cjs");
|
|
12
|
-
exports.HttpClientBase = HttpClientBase.HttpClientBase;
|
|
13
11
|
exports.Manager = Manager.Manager;
|
|
14
12
|
exports.defaultState = Manager.defaultState;
|
|
15
13
|
exports.getNetwork = Manager.getNetwork;
|
|
@@ -1,43 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const errors = require("../api/errors.cjs");
|
|
4
|
-
const api
|
|
4
|
+
const api = require("@polkadot/api");
|
|
5
5
|
const types = require("@prosopo/types");
|
|
6
|
-
const
|
|
6
|
+
const rxjs = require("rxjs");
|
|
7
7
|
const contract = require("@prosopo/contract");
|
|
8
8
|
const common = require("@prosopo/common");
|
|
9
|
+
const api$1 = require("@prosopo/api");
|
|
9
10
|
require("../contracts/captcha/dist/index.cjs");
|
|
10
|
-
const
|
|
11
|
-
const util$1 = require("@prosopo/util");
|
|
11
|
+
const util = require("@prosopo/util");
|
|
12
12
|
const utilCrypto = require("@polkadot/util-crypto");
|
|
13
|
-
const
|
|
14
|
-
const util = require("@polkadot/util");
|
|
13
|
+
const operators = require("rxjs/operators");
|
|
15
14
|
const ExtensionWeb2 = require("../api/ExtensionWeb2.cjs");
|
|
16
15
|
const ExtensionWeb3 = require("../api/ExtensionWeb3.cjs");
|
|
17
16
|
const ProsopoCaptchaApi = require("./ProsopoCaptchaApi.cjs");
|
|
18
17
|
const storage = require("./storage.cjs");
|
|
19
18
|
const captcha = require("../contracts/captcha/dist/contract-info/captcha.cjs");
|
|
20
|
-
const defaultState = () => {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
};
|
|
34
|
-
const buildUpdateState = (state, onStateUpdate) => {
|
|
35
|
-
const updateCurrentState = (nextState) => {
|
|
36
|
-
Object.assign(state, nextState);
|
|
37
|
-
onStateUpdate(nextState);
|
|
38
|
-
console.log("Procaptcha state update:", nextState, "\nResult:", state);
|
|
39
|
-
};
|
|
40
|
-
return updateCurrentState;
|
|
19
|
+
const defaultState = () => ({
|
|
20
|
+
showModal: false,
|
|
21
|
+
loading: false,
|
|
22
|
+
index: 0,
|
|
23
|
+
challenge: void 0,
|
|
24
|
+
solutions: void 0,
|
|
25
|
+
isHuman: false,
|
|
26
|
+
captchaApi: void 0,
|
|
27
|
+
account: void 0
|
|
28
|
+
});
|
|
29
|
+
const buildUpdateState = (state, onStateUpdate) => (nextState) => {
|
|
30
|
+
Object.assign(state, nextState);
|
|
31
|
+
onStateUpdate(nextState);
|
|
41
32
|
};
|
|
42
33
|
const getNetwork = (config) => {
|
|
43
34
|
const network = config.networks[config.defaultNetwork];
|
|
@@ -48,7 +39,7 @@ const getNetwork = (config) => {
|
|
|
48
39
|
};
|
|
49
40
|
function Manager(configOptional, state, onStateUpdate, callbacks) {
|
|
50
41
|
const alertError = (error) => {
|
|
51
|
-
console.
|
|
42
|
+
console.error(error);
|
|
52
43
|
alert(error.message);
|
|
53
44
|
};
|
|
54
45
|
const events = Object.assign(
|
|
@@ -56,7 +47,7 @@ function Manager(configOptional, state, onStateUpdate, callbacks) {
|
|
|
56
47
|
onAccountNotFound: alertError,
|
|
57
48
|
onError: alertError,
|
|
58
49
|
onHuman: (output) => {
|
|
59
|
-
console.
|
|
50
|
+
console.info("onHuman event triggered", output);
|
|
60
51
|
},
|
|
61
52
|
onExtensionNotFound: () => {
|
|
62
53
|
alert("No extension found");
|
|
@@ -71,10 +62,10 @@ function Manager(configOptional, state, onStateUpdate, callbacks) {
|
|
|
71
62
|
alert("Uncompleted challenge has expired, please try again");
|
|
72
63
|
},
|
|
73
64
|
onOpen: () => {
|
|
74
|
-
console.
|
|
65
|
+
console.info("onOpen event triggered");
|
|
75
66
|
},
|
|
76
67
|
onClose: () => {
|
|
77
|
-
console.
|
|
68
|
+
console.info("onClose event triggered");
|
|
78
69
|
}
|
|
79
70
|
},
|
|
80
71
|
callbacks
|
|
@@ -88,16 +79,10 @@ function Manager(configOptional, state, onStateUpdate, callbacks) {
|
|
|
88
79
|
}
|
|
89
80
|
};
|
|
90
81
|
const updateState = buildUpdateState(state, onStateUpdate);
|
|
91
|
-
const getConfig = () => {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
};
|
|
96
|
-
if (state.account) {
|
|
97
|
-
config.userAccountAddress = state.account.account.address;
|
|
98
|
-
}
|
|
99
|
-
return types.ProcaptchaConfigSchema.parse(config);
|
|
100
|
-
};
|
|
82
|
+
const getConfig = () => types.ProcaptchaConfigSchema.parse({
|
|
83
|
+
...configOptional,
|
|
84
|
+
userAccountAddress: state.account ? state.account.account.address : configOptional.userAccountAddress || ""
|
|
85
|
+
});
|
|
101
86
|
const fallable = async (fn) => {
|
|
102
87
|
try {
|
|
103
88
|
await fn();
|
|
@@ -108,131 +93,59 @@ function Manager(configOptional, state, onStateUpdate, callbacks) {
|
|
|
108
93
|
}
|
|
109
94
|
};
|
|
110
95
|
const start = async () => {
|
|
111
|
-
console.log("Starting procaptcha");
|
|
112
96
|
events.onOpen();
|
|
113
97
|
await fallable(async () => {
|
|
114
|
-
if (state.loading)
|
|
115
|
-
console.log("Procaptcha already loading");
|
|
116
|
-
return;
|
|
117
|
-
}
|
|
118
|
-
if (state.isHuman) {
|
|
119
|
-
console.log("already human");
|
|
98
|
+
if (state.loading || state.isHuman)
|
|
120
99
|
return;
|
|
121
|
-
}
|
|
122
100
|
resetState();
|
|
123
|
-
updateState({ loading: true });
|
|
124
|
-
const config = getConfig();
|
|
125
|
-
updateState({ dappAccount: config.account.address });
|
|
126
|
-
await utils.sleep(100);
|
|
101
|
+
updateState({ dappAccount: getConfig().account.address, loading: true });
|
|
127
102
|
const account = await loadAccount();
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
contractIsHuman = (await contract$1.query.dappOperatorIsHumanUser(account.account.address, config.solutionThreshold)).value.unwrap().unwrap();
|
|
132
|
-
} catch (error) {
|
|
133
|
-
console.warn(error);
|
|
134
|
-
}
|
|
135
|
-
if (contractIsHuman) {
|
|
136
|
-
updateState({ isHuman: true, loading: false });
|
|
137
|
-
events.onHuman({
|
|
138
|
-
user: account.account.address,
|
|
139
|
-
dapp: getDappAccount()
|
|
140
|
-
});
|
|
141
|
-
setValidChallengeTimeout();
|
|
103
|
+
const contract2 = await loadContract();
|
|
104
|
+
if (await checkHumanInContract(contract2, account)) {
|
|
105
|
+
handleHumanInContract(account);
|
|
142
106
|
return;
|
|
143
107
|
}
|
|
144
108
|
const providerUrlFromStorage = storage.getProviderUrl();
|
|
145
|
-
let providerApi;
|
|
146
109
|
if (providerUrlFromStorage) {
|
|
147
|
-
providerApi = await loadProviderApi(providerUrlFromStorage);
|
|
148
110
|
try {
|
|
149
|
-
const verifyDappUserResponse = await
|
|
111
|
+
const verifyDappUserResponse = await getVerifyDappUserFunction(providerUrlFromStorage, account);
|
|
150
112
|
if (verifyDappUserResponse.solutionApproved) {
|
|
151
|
-
|
|
152
|
-
events.onHuman({
|
|
153
|
-
providerUrl: providerUrlFromStorage,
|
|
154
|
-
user: account.account.address,
|
|
155
|
-
dapp: getDappAccount(),
|
|
156
|
-
commitmentId: verifyDappUserResponse.commitmentId
|
|
157
|
-
});
|
|
158
|
-
setValidChallengeTimeout();
|
|
113
|
+
handleHumanInCachedProvider(providerUrlFromStorage, account, verifyDappUserResponse);
|
|
159
114
|
return;
|
|
160
115
|
}
|
|
161
116
|
} catch (err) {
|
|
162
117
|
console.error("Error contacting provider from storage", providerUrlFromStorage);
|
|
163
118
|
}
|
|
164
119
|
}
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
data: util.stringToU8a("message"),
|
|
168
|
-
type: "bytes"
|
|
169
|
-
};
|
|
170
|
-
const signed = await account.extension.signer.signRaw(payload);
|
|
171
|
-
console.log("Signature:", signed);
|
|
172
|
-
const getRandomProviderResponse = await contract.wrapQuery(
|
|
173
|
-
contract$1.query.getRandomActiveProvider,
|
|
174
|
-
contract$1.query
|
|
175
|
-
)(account.account.address, getDappAccount());
|
|
176
|
-
const blockNumber = parseInt(getRandomProviderResponse.blockNumber.toString());
|
|
177
|
-
console.log("provider", getRandomProviderResponse);
|
|
178
|
-
const providerUrl = common.trimProviderUrl(getRandomProviderResponse.provider.url.toString());
|
|
179
|
-
providerApi = await loadProviderApi(providerUrl);
|
|
180
|
-
console.log("providerApi", providerApi);
|
|
181
|
-
const captchaApi = await loadCaptchaApi(contract$1, getRandomProviderResponse, providerApi);
|
|
182
|
-
console.log("captchaApi", captchaApi);
|
|
183
|
-
const challenge = await captchaApi.getCaptchaChallenge();
|
|
184
|
-
console.log("challenge", challenge);
|
|
120
|
+
const randomProviderResponse = await getRandomProviderResponse(contract2, account);
|
|
121
|
+
const challenge = await getChallenge(randomProviderResponse, contract2);
|
|
185
122
|
if (challenge.captchas.length <= 0) {
|
|
186
123
|
throw new Error("No captchas returned from provider");
|
|
187
124
|
}
|
|
188
|
-
const timeMillis = challenge.captchas.map((captcha2) => captcha2.captcha.timeLimitMs || 30 * 1e3).reduce((a, b) => a + b);
|
|
189
|
-
const timeout = setTimeout(() => {
|
|
190
|
-
console.log("challenge expired after " + timeMillis + "ms");
|
|
191
|
-
events.onChallengeExpired();
|
|
192
|
-
updateState({ isHuman: false, showModal: false, loading: false });
|
|
193
|
-
}, timeMillis);
|
|
194
125
|
updateState({
|
|
126
|
+
challenge,
|
|
195
127
|
index: 0,
|
|
196
128
|
solutions: challenge.captchas.map(() => []),
|
|
197
|
-
challenge,
|
|
198
129
|
showModal: true,
|
|
199
|
-
timeout,
|
|
200
|
-
blockNumber
|
|
130
|
+
timeout: setTimeToComplete(challenge),
|
|
131
|
+
blockNumber: getBlockNumberFromProvider(randomProviderResponse)
|
|
201
132
|
});
|
|
202
133
|
});
|
|
203
134
|
};
|
|
204
135
|
const submit = async () => {
|
|
205
136
|
await fallable(async () => {
|
|
206
|
-
console.log("submitting solutions");
|
|
207
137
|
clearTimeout();
|
|
138
|
+
updateState({ showModal: false });
|
|
208
139
|
if (!state.challenge) {
|
|
209
140
|
throw new Error("cannot submit, no challenge found");
|
|
210
141
|
}
|
|
211
|
-
|
|
212
|
-
const challenge = state.challenge;
|
|
142
|
+
const datasetId = getDatasetId();
|
|
213
143
|
const salt = utilCrypto.randomAsHex();
|
|
214
|
-
const
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
salt,
|
|
220
|
-
solution
|
|
221
|
-
};
|
|
222
|
-
});
|
|
223
|
-
const account = getAccount();
|
|
224
|
-
const blockNumber = getBlockNumber();
|
|
225
|
-
const signer = account.extension.signer;
|
|
226
|
-
const first = util$1.at(challenge.captchas, 0);
|
|
227
|
-
if (!first.captcha.datasetId) {
|
|
228
|
-
throw new Error("No datasetId set for challenge");
|
|
229
|
-
}
|
|
230
|
-
const captchaApi = getCaptchaApi();
|
|
231
|
-
const submission = await captchaApi.submitCaptchaSolution(
|
|
232
|
-
signer,
|
|
233
|
-
challenge.requestHash,
|
|
234
|
-
first.captcha.datasetId,
|
|
235
|
-
captchaSolution,
|
|
144
|
+
const submission = await getCaptchaApi().submitCaptchaSolution(
|
|
145
|
+
getAccount().extension.signer,
|
|
146
|
+
state.challenge.requestHash,
|
|
147
|
+
datasetId,
|
|
148
|
+
getSolutionsFromState(salt),
|
|
236
149
|
salt
|
|
237
150
|
);
|
|
238
151
|
const isHuman = submission[0].solutionApproved;
|
|
@@ -244,26 +157,20 @@ function Manager(configOptional, state, onStateUpdate, callbacks) {
|
|
|
244
157
|
isHuman,
|
|
245
158
|
loading: false
|
|
246
159
|
});
|
|
247
|
-
if (
|
|
248
|
-
const trimmedUrl = common.trimProviderUrl(
|
|
160
|
+
if (isHuman) {
|
|
161
|
+
const trimmedUrl = common.trimProviderUrl(getCaptchaApi().provider.provider.url.toString());
|
|
249
162
|
storage.setProviderUrl(trimmedUrl);
|
|
250
163
|
events.onHuman({
|
|
251
164
|
providerUrl: trimmedUrl,
|
|
252
|
-
user:
|
|
165
|
+
user: getAccount().account.address,
|
|
253
166
|
dapp: getDappAccount(),
|
|
254
167
|
commitmentId: submission[1],
|
|
255
|
-
blockNumber
|
|
168
|
+
blockNumber: getBlockNumberFromState()
|
|
256
169
|
});
|
|
257
170
|
setValidChallengeTimeout();
|
|
258
171
|
}
|
|
259
172
|
});
|
|
260
173
|
};
|
|
261
|
-
const cancel = async () => {
|
|
262
|
-
console.log("cancel");
|
|
263
|
-
clearTimeout();
|
|
264
|
-
resetState();
|
|
265
|
-
events.onClose();
|
|
266
|
-
};
|
|
267
174
|
const select = (hash) => {
|
|
268
175
|
if (!state.challenge) {
|
|
269
176
|
throw new Error("cannot select, no challenge found");
|
|
@@ -271,17 +178,9 @@ function Manager(configOptional, state, onStateUpdate, callbacks) {
|
|
|
271
178
|
if (state.index >= state.challenge.captchas.length || state.index < 0) {
|
|
272
179
|
throw new Error("cannot select, round index out of range");
|
|
273
180
|
}
|
|
274
|
-
const
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
if (solution.includes(hash)) {
|
|
278
|
-
console.log("deselecting", hash);
|
|
279
|
-
solution.splice(solution.indexOf(hash), 1);
|
|
280
|
-
} else {
|
|
281
|
-
console.log("selecting", hash);
|
|
282
|
-
solution.push(hash);
|
|
283
|
-
}
|
|
284
|
-
updateState({ solutions });
|
|
181
|
+
const solution = util.at(state.solutions, state.index);
|
|
182
|
+
handleIsSelected(solution, hash);
|
|
183
|
+
updateState({ solutions: state.solutions });
|
|
285
184
|
};
|
|
286
185
|
const nextRound = () => {
|
|
287
186
|
if (!state.challenge) {
|
|
@@ -290,44 +189,161 @@ function Manager(configOptional, state, onStateUpdate, callbacks) {
|
|
|
290
189
|
if (state.index + 1 >= state.challenge.captchas.length) {
|
|
291
190
|
throw new Error("cannot proceed to next round, already at last round");
|
|
292
191
|
}
|
|
293
|
-
console.log("proceeding to next round");
|
|
294
192
|
updateState({ index: state.index + 1 });
|
|
295
193
|
};
|
|
296
194
|
const loadCaptchaApi = async (contract2, provider, providerApi) => {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
195
|
+
updateState({
|
|
196
|
+
captchaApi: new ProsopoCaptchaApi.ProsopoCaptchaApi(
|
|
197
|
+
getAccount().account.address,
|
|
198
|
+
contract2,
|
|
199
|
+
provider,
|
|
200
|
+
providerApi,
|
|
201
|
+
getConfig().web2,
|
|
202
|
+
getDappAccount()
|
|
203
|
+
)
|
|
204
|
+
});
|
|
307
205
|
return getCaptchaApi();
|
|
308
206
|
};
|
|
207
|
+
const createBlockObservable = () => new rxjs.Observable(
|
|
208
|
+
(subscriber) => () => api.ApiPromise.create({ provider: new api.WsProvider(getNetwork(getConfig()).endpoint) }).then((api2) => {
|
|
209
|
+
api2.rpc.chain.subscribeNewHeads((header) => {
|
|
210
|
+
subscriber.next(header);
|
|
211
|
+
});
|
|
212
|
+
}).catch((error) => {
|
|
213
|
+
subscriber.error(error);
|
|
214
|
+
})
|
|
215
|
+
);
|
|
216
|
+
const loadAccount = async () => {
|
|
217
|
+
const config = getConfig();
|
|
218
|
+
if (!config.web2 && !config.userAccountAddress) {
|
|
219
|
+
throw new Error("Account address has not been set for web3 mode");
|
|
220
|
+
}
|
|
221
|
+
const ext = config.web2 ? new ExtensionWeb2() : new ExtensionWeb3();
|
|
222
|
+
const account = await ext.getAccount(config);
|
|
223
|
+
storage.setAccount(account.account.address);
|
|
224
|
+
updateState({ account });
|
|
225
|
+
return getAccount();
|
|
226
|
+
};
|
|
309
227
|
const loadProviderApi = async (providerUrl) => {
|
|
310
228
|
const config = getConfig();
|
|
311
|
-
const network = getNetwork(config);
|
|
312
229
|
if (!config.account.address) {
|
|
313
230
|
throw new common.ProsopoEnvError("GENERAL.SITE_KEY_MISSING");
|
|
314
231
|
}
|
|
315
|
-
return new api.ProviderApi(
|
|
232
|
+
return new api$1.ProviderApi(getNetwork(config), providerUrl, config.account.address);
|
|
233
|
+
};
|
|
234
|
+
const loadContract = async () => {
|
|
235
|
+
const network = getNetwork(getConfig());
|
|
236
|
+
const api$12 = await api.ApiPromise.create({ provider: new api.WsProvider(network.endpoint) });
|
|
237
|
+
const type = "sr25519";
|
|
238
|
+
return new contract.ProsopoCaptchaContract(
|
|
239
|
+
api$12,
|
|
240
|
+
JSON.parse(captcha.ContractAbi),
|
|
241
|
+
network.contract.address,
|
|
242
|
+
"prosopo",
|
|
243
|
+
0,
|
|
244
|
+
new api.Keyring({ type, ss58Format: api$12.registry.chainSS58 }).addFromAddress(getAccount().account.address)
|
|
245
|
+
);
|
|
246
|
+
};
|
|
247
|
+
function handleIsSelected(solution, hash) {
|
|
248
|
+
if (solution.includes(hash)) {
|
|
249
|
+
solution.splice(solution.indexOf(hash), 1);
|
|
250
|
+
} else {
|
|
251
|
+
solution.push(hash);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
function getSolutionsFromState(salt) {
|
|
255
|
+
if (!state.challenge) {
|
|
256
|
+
throw new Error("cannot get solutions, no challenge found");
|
|
257
|
+
}
|
|
258
|
+
return state.challenge.captchas.map((captcha2, index) => ({
|
|
259
|
+
captchaId: captcha2.captcha.captchaId,
|
|
260
|
+
captchaContentId: captcha2.captcha.captchaContentId,
|
|
261
|
+
salt,
|
|
262
|
+
solution: util.at(state.solutions, index)
|
|
263
|
+
}));
|
|
264
|
+
}
|
|
265
|
+
function handleHumanInCachedProvider(providerUrlFromStorage, account, verifyDappUserResponse) {
|
|
266
|
+
updateState({ isHuman: true, loading: false });
|
|
267
|
+
events.onHuman({
|
|
268
|
+
providerUrl: providerUrlFromStorage,
|
|
269
|
+
user: account.account.address,
|
|
270
|
+
dapp: getDappAccount(),
|
|
271
|
+
commitmentId: verifyDappUserResponse.commitmentId
|
|
272
|
+
});
|
|
273
|
+
setValidChallengeTimeout();
|
|
274
|
+
}
|
|
275
|
+
function getVerifyDappUserFunction(providerUrlFromStorage, account) {
|
|
276
|
+
return loadProviderApi(providerUrlFromStorage).then(
|
|
277
|
+
(providerApi) => providerApi.verifyDappUser(account.account.address)
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
function handleHumanInContract(account) {
|
|
281
|
+
updateState({ isHuman: true, loading: false });
|
|
282
|
+
events.onHuman({
|
|
283
|
+
user: account.account.address,
|
|
284
|
+
dapp: getDappAccount()
|
|
285
|
+
});
|
|
286
|
+
setValidChallengeTimeout();
|
|
287
|
+
}
|
|
288
|
+
async function checkHumanInContract(contract2, account) {
|
|
289
|
+
try {
|
|
290
|
+
return await contract2.query.dappOperatorIsHumanUser(account.account.address, getConfig().solutionThreshold).then((res) => res.value.unwrap().unwrap());
|
|
291
|
+
} catch (err) {
|
|
292
|
+
console.error(err);
|
|
293
|
+
return false;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
function getChallenge(getRandomProviderResponse2, contract2) {
|
|
297
|
+
return loadProviderApi(common.trimProviderUrl(getRandomProviderResponse2.provider.url.toString())).then((api2) => loadCaptchaApi(contract2, getRandomProviderResponse2, api2)).then((captchaApi) => captchaApi.getCaptchaChallenge());
|
|
298
|
+
}
|
|
299
|
+
function getRandomProviderResponse(contract$1, account) {
|
|
300
|
+
return rxjs.lastValueFrom(
|
|
301
|
+
rxjs.from(
|
|
302
|
+
contract.wrapQuery(contract$1.query.getRandomActiveProvider, contract$1.query)(
|
|
303
|
+
account.account.address,
|
|
304
|
+
getDappAccount()
|
|
305
|
+
)
|
|
306
|
+
).pipe(
|
|
307
|
+
operators.retry({
|
|
308
|
+
count: 3,
|
|
309
|
+
delay: (error, retryCount) => {
|
|
310
|
+
console.error(`Attempt ${retryCount} failed. Retrying on next block. Error: ${error}`);
|
|
311
|
+
return createBlockObservable().pipe(operators.take(1));
|
|
312
|
+
},
|
|
313
|
+
resetOnSuccess: true
|
|
314
|
+
})
|
|
315
|
+
)
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
function setTimeToComplete(challenge) {
|
|
319
|
+
return setTimeout(
|
|
320
|
+
() => {
|
|
321
|
+
events.onChallengeExpired();
|
|
322
|
+
updateState({ isHuman: false, showModal: false, loading: false });
|
|
323
|
+
},
|
|
324
|
+
challenge.captchas.map((captcha2) => captcha2.captcha.timeLimitMs || 30 * 1e3).reduce((a, b) => a + b)
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
const setValidChallengeTimeout = () => {
|
|
328
|
+
updateState({
|
|
329
|
+
successfullChallengeTimeout: setTimeout(
|
|
330
|
+
() => {
|
|
331
|
+
updateState({ isHuman: false });
|
|
332
|
+
events.onExpired();
|
|
333
|
+
},
|
|
334
|
+
configOptional.challengeValidLength || 120 * 1e3
|
|
335
|
+
)
|
|
336
|
+
});
|
|
337
|
+
};
|
|
338
|
+
const cancel = async () => {
|
|
339
|
+
clearTimeout();
|
|
340
|
+
resetState();
|
|
341
|
+
events.onClose();
|
|
316
342
|
};
|
|
317
343
|
const clearTimeout = () => {
|
|
318
344
|
window.clearTimeout(state.timeout);
|
|
319
345
|
updateState({ timeout: void 0 });
|
|
320
346
|
};
|
|
321
|
-
const setValidChallengeTimeout = () => {
|
|
322
|
-
console.log("setting valid challenge timeout");
|
|
323
|
-
const timeMillis = configOptional.challengeValidLength || 120 * 1e3;
|
|
324
|
-
const successfullChallengeTimeout = setTimeout(() => {
|
|
325
|
-
console.log("valid challenge expired after " + timeMillis + "ms");
|
|
326
|
-
updateState({ isHuman: false });
|
|
327
|
-
events.onExpired();
|
|
328
|
-
}, timeMillis);
|
|
329
|
-
updateState({ successfullChallengeTimeout });
|
|
330
|
-
};
|
|
331
347
|
const resetState = () => {
|
|
332
348
|
clearTimeout();
|
|
333
349
|
updateState(defaultState());
|
|
@@ -338,54 +354,37 @@ function Manager(configOptional, state, onStateUpdate, callbacks) {
|
|
|
338
354
|
}
|
|
339
355
|
return state.captchaApi;
|
|
340
356
|
};
|
|
341
|
-
const loadAccount = async () => {
|
|
342
|
-
const config = getConfig();
|
|
343
|
-
if (!config.web2 && !config.userAccountAddress) {
|
|
344
|
-
throw new Error("Account address has not been set for web3 mode");
|
|
345
|
-
}
|
|
346
|
-
const ext = config.web2 ? new ExtensionWeb2() : new ExtensionWeb3();
|
|
347
|
-
const account = await ext.getAccount(config);
|
|
348
|
-
storage.setAccount(account.account.address);
|
|
349
|
-
console.log("Using account:", account);
|
|
350
|
-
updateState({ account });
|
|
351
|
-
return getAccount();
|
|
352
|
-
};
|
|
353
357
|
const getAccount = () => {
|
|
354
358
|
if (!state.account) {
|
|
355
359
|
throw new Error("Account not loaded");
|
|
356
360
|
}
|
|
357
|
-
|
|
358
|
-
return account;
|
|
361
|
+
return state.account;
|
|
359
362
|
};
|
|
360
363
|
const getDappAccount = () => {
|
|
361
364
|
if (!state.dappAccount) {
|
|
362
365
|
throw new common.ProsopoEnvError("GENERAL.SITE_KEY_MISSING");
|
|
363
366
|
}
|
|
364
|
-
|
|
365
|
-
return dappAccount;
|
|
367
|
+
return state.dappAccount;
|
|
366
368
|
};
|
|
367
|
-
const
|
|
369
|
+
const getBlockNumberFromState = () => {
|
|
368
370
|
if (!state.blockNumber) {
|
|
369
371
|
throw new Error("Account not loaded");
|
|
370
372
|
}
|
|
371
|
-
|
|
372
|
-
return blockNumber;
|
|
373
|
-
};
|
|
374
|
-
const loadContract = async () => {
|
|
375
|
-
const config = getConfig();
|
|
376
|
-
const network = getNetwork(config);
|
|
377
|
-
const api2 = await api$1.ApiPromise.create({ provider: new rpcProvider.WsProvider(network.endpoint) });
|
|
378
|
-
const type = "sr25519";
|
|
379
|
-
const keyring = new api$1.Keyring({ type, ss58Format: api2.registry.chainSS58 });
|
|
380
|
-
return new contract.ProsopoCaptchaContract(
|
|
381
|
-
api2,
|
|
382
|
-
JSON.parse(captcha.ContractAbi),
|
|
383
|
-
network.contract.address,
|
|
384
|
-
"prosopo",
|
|
385
|
-
0,
|
|
386
|
-
keyring.addFromAddress(getAccount().account.address)
|
|
387
|
-
);
|
|
373
|
+
return state.blockNumber;
|
|
388
374
|
};
|
|
375
|
+
function getBlockNumberFromProvider(getRandomProviderResponse2) {
|
|
376
|
+
return parseInt(getRandomProviderResponse2.blockNumber.toString());
|
|
377
|
+
}
|
|
378
|
+
function getDatasetId() {
|
|
379
|
+
if (!state.challenge) {
|
|
380
|
+
throw new Error("cannot get datasetId, no challenge found");
|
|
381
|
+
}
|
|
382
|
+
const datasetId = util.at(state.challenge.captchas, 0).captcha.datasetId;
|
|
383
|
+
if (!datasetId) {
|
|
384
|
+
throw new Error("No datasetId set for challenge");
|
|
385
|
+
}
|
|
386
|
+
return datasetId;
|
|
387
|
+
}
|
|
389
388
|
return {
|
|
390
389
|
start,
|
|
391
390
|
cancel,
|
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
3
|
const common = require("@prosopo/common");
|
|
4
|
-
const client = require("axios");
|
|
5
4
|
async function downloadImage(url) {
|
|
6
5
|
try {
|
|
7
|
-
|
|
6
|
+
const response = await fetch(url);
|
|
7
|
+
if (!response.ok) {
|
|
8
|
+
throw new Error(`Network response was not ok, status: ${response.status}`);
|
|
9
|
+
}
|
|
10
|
+
const buffer = await response.arrayBuffer();
|
|
11
|
+
return new Uint8Array(buffer);
|
|
8
12
|
} catch (error) {
|
|
9
13
|
throw new common.ProsopoEnvError(error);
|
|
10
14
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Manager.d.ts","sourceRoot":"","sources":["../../src/modules/Manager.ts"],"names":[],"mappings":"AAaA,OAAO,EAEH,mBAAmB,EACnB,wBAAwB,EAExB,eAAe,EACf,uBAAuB,EAC1B,MAAM,qBAAqB,CAAA;AAG5B,OAAO,
|
|
1
|
+
{"version":3,"file":"Manager.d.ts","sourceRoot":"","sources":["../../src/modules/Manager.ts"],"names":[],"mappings":"AAaA,OAAO,EAEH,mBAAmB,EACnB,wBAAwB,EAExB,eAAe,EACf,uBAAuB,EAC1B,MAAM,qBAAqB,CAAA;AAG5B,OAAO,EAAmB,4BAA4B,EAA0B,MAAM,gBAAgB,CAAA;AAetG,eAAO,MAAM,YAAY,QAAO,QAAQ,eAAe,CASrD,CAAA;AAQF,eAAO,MAAM,UAAU,WAAY,4BAA4B;;;;;;;;CAM9D,CAAA;AAED;;GAEG;AACH,wBAAgB,OAAO,CACnB,cAAc,EAAE,wBAAwB,EACxC,KAAK,EAAE,eAAe,EACtB,aAAa,EAAE,uBAAuB,EACtC,SAAS,EAAE,mBAAmB;;;;mBAqLR,MAAM;;EA4V/B"}
|