@prosopo/procaptcha 0.1.0 → 0.1.3
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/README.md +13 -0
- package/dist/api/AsyncFactory.d.ts +7 -0
- package/dist/api/AsyncFactory.d.ts.map +1 -0
- package/{src/api/AsyncFactory.ts → dist/api/AsyncFactory.js} +8 -9
- package/dist/api/AsyncFactory.js.map +1 -0
- package/dist/api/Extension.d.ts +22 -0
- package/dist/api/Extension.d.ts.map +1 -0
- package/{src/api/Extension.ts → dist/api/Extension.js} +39 -56
- package/dist/api/Extension.js.map +1 -0
- package/dist/api/HttpClientBase.d.ts +9 -0
- package/dist/api/HttpClientBase.d.ts.map +1 -0
- package/dist/api/HttpClientBase.js +36 -0
- package/dist/api/HttpClientBase.js.map +1 -0
- package/dist/api/ProsopoContract.d.ts +13 -0
- package/dist/api/ProsopoContract.d.ts.map +1 -0
- package/{src/api/ProsopoContract.ts → dist/api/ProsopoContract.js} +18 -20
- package/dist/api/ProsopoContract.js.map +1 -0
- package/dist/api/ProsopoContractBase.d.ts +31 -0
- package/dist/api/ProsopoContractBase.d.ts.map +1 -0
- package/dist/api/ProsopoContractBase.js +131 -0
- package/dist/api/ProsopoContractBase.js.map +1 -0
- package/dist/api/ProviderApi.d.ts +21 -0
- package/dist/api/ProviderApi.d.ts.map +1 -0
- package/dist/api/ProviderApi.js +54 -0
- package/dist/api/ProviderApi.js.map +1 -0
- package/dist/api/handlers.d.ts +5 -0
- package/dist/api/handlers.d.ts.map +1 -0
- package/dist/api/handlers.js +14 -0
- package/dist/api/handlers.js.map +1 -0
- package/dist/api/index.d.ts +7 -0
- package/dist/api/index.d.ts.map +1 -0
- package/{src/api/handlers.ts → dist/api/index.js} +10 -14
- package/dist/api/index.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/{src/modules/index.ts → dist/index.js} +7 -7
- package/dist/index.js.map +1 -0
- package/dist/modules/ProsopoCaptchaApi.d.ts +16 -0
- package/dist/modules/ProsopoCaptchaApi.d.ts.map +1 -0
- package/dist/modules/ProsopoCaptchaApi.js +86 -0
- package/dist/modules/ProsopoCaptchaApi.js.map +1 -0
- package/dist/modules/ProsopoCaptchaClient.d.ts +27 -0
- package/dist/modules/ProsopoCaptchaClient.d.ts.map +1 -0
- package/dist/modules/ProsopoCaptchaClient.js +97 -0
- package/dist/modules/ProsopoCaptchaClient.js.map +1 -0
- package/dist/modules/ProsopoCaptchaStateClient.d.ts +17 -0
- package/dist/modules/ProsopoCaptchaStateClient.d.ts.map +1 -0
- package/dist/modules/ProsopoCaptchaStateClient.js +104 -0
- package/dist/modules/ProsopoCaptchaStateClient.js.map +1 -0
- package/dist/modules/client.d.ts +9 -0
- package/dist/modules/client.d.ts.map +1 -0
- package/{src/modules/client.ts → dist/modules/client.js} +13 -12
- package/dist/modules/client.js.map +1 -0
- package/dist/modules/contract.d.ts +7 -0
- package/dist/modules/contract.d.ts.map +1 -0
- package/{src/api/HttpClientBase.ts → dist/modules/contract.js} +14 -21
- package/dist/modules/contract.js.map +1 -0
- package/dist/modules/extension.d.ts +3 -0
- package/dist/modules/extension.d.ts.map +1 -0
- package/{src/api/index.ts → dist/modules/extension.js} +10 -6
- package/dist/modules/extension.js.map +1 -0
- package/dist/modules/index.d.ts +8 -0
- package/dist/modules/index.d.ts.map +1 -0
- package/{src/modules/contract.ts → dist/modules/index.js} +11 -8
- package/dist/modules/index.js.map +1 -0
- package/dist/modules/storage.d.ts +14 -0
- package/dist/modules/storage.d.ts.map +1 -0
- package/{src/modules/storage.ts → dist/modules/storage.js} +7 -7
- package/dist/modules/storage.js.map +1 -0
- package/dist/types/api.d.ts +45 -0
- package/dist/types/api.d.ts.map +1 -0
- package/{src/types/index.ts → dist/types/api.js} +7 -3
- package/dist/types/api.js.map +1 -0
- package/{src/types/client.ts → dist/types/client.d.ts} +8 -36
- package/dist/types/client.d.ts.map +1 -0
- package/dist/types/client.js +3 -0
- package/dist/types/client.js.map +1 -0
- package/dist/types/contract.d.ts +5 -0
- package/dist/types/contract.d.ts.map +1 -0
- package/dist/types/contract.js +3 -0
- package/dist/types/contract.js.map +1 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/index.d.ts.map +1 -0
- package/{src/modules/extension.ts → dist/types/index.js} +7 -5
- package/dist/types/index.js.map +1 -0
- package/img/contracts-page.png +0 -0
- package/package.json +2 -2
- package/tsconfig.tsbuildinfo +1 -0
- package/src/api/ProsopoContractBase.ts +0 -169
- package/src/api/ProviderApi.ts +0 -59
- package/src/index.ts +0 -18
- package/src/modules/ProsopoCaptchaApi.ts +0 -105
- package/src/modules/ProsopoCaptchaClient.ts +0 -131
- package/src/modules/ProsopoCaptchaStateClient.ts +0 -146
- package/src/types/api.ts +0 -79
- package/src/types/contract.ts +0 -21
- package/tests/mocks/browser.ts +0 -25
- package/tests/modules/client.test.ts +0 -56
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
// Copyright (C) 2021-2022 Prosopo (UK) Ltd.
|
|
2
|
-
// This file is part of procaptcha <https://github.com/prosopo-io/procaptcha>.
|
|
3
|
-
//
|
|
4
|
-
// procaptcha is free software: you can redistribute it and/or modify
|
|
5
|
-
// it under the terms of the GNU General Public License as published by
|
|
6
|
-
// the Free Software Foundation, either version 3 of the License, or
|
|
7
|
-
// (at your option) any later version.
|
|
8
|
-
//
|
|
9
|
-
// procaptcha is distributed in the hope that it will be useful,
|
|
10
|
-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
-
// GNU General Public License for more details.
|
|
13
|
-
//
|
|
14
|
-
// You should have received a copy of the GNU General Public License
|
|
15
|
-
// along with procaptcha. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
-
import { ApiPromise, SubmittableResult } from "@polkadot/api";
|
|
17
|
-
import { Abi, ContractPromise } from "@polkadot/api-contract";
|
|
18
|
-
import { InjectedAccountWithMeta } from "@polkadot/extension-inject/types";
|
|
19
|
-
|
|
20
|
-
import { abiJson, unwrap, encodeStringArgs, ProsopoContractError } from "@prosopo/contract";
|
|
21
|
-
import { AnyJson } from "@polkadot/types/types/codec";
|
|
22
|
-
import { ProviderInterface } from "@polkadot/rpc-provider/types";
|
|
23
|
-
import { Signer } from "@polkadot/api/types";
|
|
24
|
-
import { TransactionResponse } from '../types';
|
|
25
|
-
import AsyncFactory from "./AsyncFactory";
|
|
26
|
-
|
|
27
|
-
export class ProsopoContractBase extends AsyncFactory {
|
|
28
|
-
|
|
29
|
-
protected api: ApiPromise;
|
|
30
|
-
protected abi: Abi;
|
|
31
|
-
protected contract: ContractPromise;
|
|
32
|
-
protected account: InjectedAccountWithMeta;
|
|
33
|
-
protected dappAddress: string;
|
|
34
|
-
|
|
35
|
-
public address: string;
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* @param address
|
|
39
|
-
* @param dappAddress
|
|
40
|
-
* @param account
|
|
41
|
-
* @param providerInterface
|
|
42
|
-
*/
|
|
43
|
-
public async init(address: string, dappAddress: string, account: InjectedAccountWithMeta, providerInterface: ProviderInterface) {
|
|
44
|
-
this.api = await ApiPromise.create({ provider: providerInterface });
|
|
45
|
-
this.abi = new Abi(abiJson, this.api.registry.getChainProperties());
|
|
46
|
-
this.contract = new ContractPromise(this.api, this.abi, address);
|
|
47
|
-
this.address = address;
|
|
48
|
-
this.dappAddress = dappAddress;
|
|
49
|
-
this.account = account;
|
|
50
|
-
return this;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
public getApi(): ApiPromise {
|
|
54
|
-
return this.api;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
public getContract(): ContractPromise {
|
|
58
|
-
return this.contract;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
public getAccount(): InjectedAccountWithMeta {
|
|
62
|
-
return this.account;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
public getDappAddress(): string {
|
|
66
|
-
return this.dappAddress;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
public async query<T>(method: string, args: any[]): Promise<T | AnyJson | null> {
|
|
70
|
-
try {
|
|
71
|
-
const abiMessage = this.abi.findMessage(method);
|
|
72
|
-
const response = await this.contract.query[method](
|
|
73
|
-
this.account.address,
|
|
74
|
-
{},
|
|
75
|
-
...encodeStringArgs(abiMessage, args)
|
|
76
|
-
);
|
|
77
|
-
console.log("QUERY RESPONSE", method, args, response);
|
|
78
|
-
if (response.result.isOk) {
|
|
79
|
-
if (response.output) {
|
|
80
|
-
return unwrap(response.output.toHuman());
|
|
81
|
-
} else {
|
|
82
|
-
return null;
|
|
83
|
-
}
|
|
84
|
-
} else {
|
|
85
|
-
throw new ProsopoContractError(response.result.asErr);
|
|
86
|
-
}
|
|
87
|
-
} catch (e) {
|
|
88
|
-
console.error("ERROR", e);
|
|
89
|
-
return null;
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// https://polkadot.js.org/docs/api/cookbook/tx/
|
|
94
|
-
// https://polkadot.js.org/docs/api/start/api.tx.subs/
|
|
95
|
-
public async transaction(signer: Signer, method: string, args: any[]): Promise<TransactionResponse> {
|
|
96
|
-
|
|
97
|
-
// TODO if DEBUG==true || env.development
|
|
98
|
-
const queryBeforeTx = await this.query(method, args);
|
|
99
|
-
|
|
100
|
-
console.log("QUERY BEFORE TX....................", queryBeforeTx);
|
|
101
|
-
|
|
102
|
-
const abiMessage = this.abi.findMessage(method);
|
|
103
|
-
|
|
104
|
-
const extrinsic = this.contract.tx[method]({}, ...encodeStringArgs(abiMessage, args));
|
|
105
|
-
|
|
106
|
-
// this.api.setSigner(signer);
|
|
107
|
-
// const response = await buildTx(this.api.registry, extrinsic, this.account.address, { signer });
|
|
108
|
-
// console.log("buildTx RESPONSE", response);
|
|
109
|
-
// return;
|
|
110
|
-
|
|
111
|
-
return new Promise((resolve, reject) => {
|
|
112
|
-
|
|
113
|
-
extrinsic.signAndSend(this.account.address, { signer }, (result: SubmittableResult) => {
|
|
114
|
-
|
|
115
|
-
const { dispatchError, dispatchInfo, events, internalError, status, txHash, txIndex } = result;
|
|
116
|
-
|
|
117
|
-
console.log("TX STATUS", status.type);
|
|
118
|
-
console.log("IS FINALIZED", status?.isFinalized);
|
|
119
|
-
console.log("IN BLOCK", status?.isInBlock);
|
|
120
|
-
console.log("EVENTS", events);
|
|
121
|
-
|
|
122
|
-
if (internalError) {
|
|
123
|
-
console.error("internalError", internalError);
|
|
124
|
-
reject(internalError);
|
|
125
|
-
|
|
126
|
-
return;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (dispatchError) {
|
|
130
|
-
console.error("dispatchError", dispatchError);
|
|
131
|
-
reject(dispatchError);
|
|
132
|
-
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// [Ready, InBlock, Finalized...]
|
|
137
|
-
|
|
138
|
-
// Instant seal ON.
|
|
139
|
-
if (status?.isInBlock) {
|
|
140
|
-
|
|
141
|
-
const blockHash = status.asInBlock.toHex();
|
|
142
|
-
|
|
143
|
-
resolve({ dispatchError, dispatchInfo, events, internalError, status, txHash, txIndex, blockHash });
|
|
144
|
-
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
// TODO
|
|
148
|
-
// Instant seal OFF.
|
|
149
|
-
// if (status?.isFinalized) {
|
|
150
|
-
|
|
151
|
-
// const blockHash = status.asFinalized.toHex();
|
|
152
|
-
|
|
153
|
-
// resolve({ dispatchError, dispatchInfo, events, internalError, status, txHash, txIndex, blockHash });
|
|
154
|
-
|
|
155
|
-
// }
|
|
156
|
-
|
|
157
|
-
})
|
|
158
|
-
.catch((e) => {
|
|
159
|
-
console.error("signAndSend ERROR", e);
|
|
160
|
-
reject(e);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
export default ProsopoContractBase;
|
package/src/api/ProviderApi.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
// Copyright (C) 2021-2022 Prosopo (UK) Ltd.
|
|
2
|
-
// This file is part of procaptcha <https://github.com/prosopo-io/procaptcha>.
|
|
3
|
-
//
|
|
4
|
-
// procaptcha is free software: you can redistribute it and/or modify
|
|
5
|
-
// it under the terms of the GNU General Public License as published by
|
|
6
|
-
// the Free Software Foundation, either version 3 of the License, or
|
|
7
|
-
// (at your option) any later version.
|
|
8
|
-
//
|
|
9
|
-
// procaptcha is distributed in the hope that it will be useful,
|
|
10
|
-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
-
// GNU General Public License for more details.
|
|
13
|
-
//
|
|
14
|
-
// You should have received a copy of the GNU General Public License
|
|
15
|
-
// along with procaptcha. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
-
import HttpClientBase from "./HttpClientBase";
|
|
17
|
-
import Storage from "../modules/storage";
|
|
18
|
-
import { ProsopoCaptchaConfig, ProsopoRandomProviderResponse, GetCaptchaResponse, CaptchaSolution, CaptchaSolutionResponse } from '../types';
|
|
19
|
-
|
|
20
|
-
export class ProviderApi extends HttpClientBase {
|
|
21
|
-
|
|
22
|
-
protected config: ProsopoCaptchaConfig;
|
|
23
|
-
|
|
24
|
-
constructor(config: ProsopoCaptchaConfig) {
|
|
25
|
-
super(config['providerApi.baseURL'], config['providerApi.prefix']);
|
|
26
|
-
this.config = config;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
*
|
|
31
|
-
* @deprecated use ProsopoContract$getRandomProvider instead.
|
|
32
|
-
*/
|
|
33
|
-
public getRandomProvider() {
|
|
34
|
-
const userAccount = Storage.getAccount();
|
|
35
|
-
return this.axios.get(`/random_provider/${userAccount}/${this.config['dappAccount']}`);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
public getProviders(): Promise<{accounts: string[]}> {
|
|
39
|
-
return this.axios.get(`/providers`);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
public getContractAddress(): Promise<{contractAddress: string}> {
|
|
43
|
-
return this.axios.get(`/contract_address`);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public getCaptchaChallenge(randomProvider: ProsopoRandomProviderResponse) : Promise<GetCaptchaResponse> {
|
|
47
|
-
let { provider, blockNumber } = randomProvider;
|
|
48
|
-
blockNumber = blockNumber.replace(/,/g, ''); // TODO: middleware schema parser/validator.
|
|
49
|
-
const userAccount = Storage.getAccount();
|
|
50
|
-
return this.axios.get(`/provider/captcha/${provider.captchaDatasetId}/${userAccount}/${this.config['dappAccount']}/${blockNumber}`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
public submitCaptchaSolution(blockHash: string, captchas: CaptchaSolution[], requestHash: string, txHash: string, userAccount: string) : Promise<CaptchaSolutionResponse> {
|
|
54
|
-
return this.axios.post(`/provider/solution`, {blockHash, captchas, requestHash, txHash, userAccount, dappAccount: this.config['dappAccount']});
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export default ProviderApi;
|
package/src/index.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
// Copyright (C) 2021-2022 Prosopo (UK) Ltd.
|
|
2
|
-
// This file is part of procaptcha <https://github.com/prosopo-io/procaptcha>.
|
|
3
|
-
//
|
|
4
|
-
// procaptcha is free software: you can redistribute it and/or modify
|
|
5
|
-
// it under the terms of the GNU General Public License as published by
|
|
6
|
-
// the Free Software Foundation, either version 3 of the License, or
|
|
7
|
-
// (at your option) any later version.
|
|
8
|
-
//
|
|
9
|
-
// procaptcha is distributed in the hope that it will be useful,
|
|
10
|
-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
-
// GNU General Public License for more details.
|
|
13
|
-
//
|
|
14
|
-
// You should have received a copy of the GNU General Public License
|
|
15
|
-
// along with procaptcha. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
-
export * from './api';
|
|
17
|
-
export * from './modules';
|
|
18
|
-
export * from './types';
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
// Copyright (C) 2021-2022 Prosopo (UK) Ltd.
|
|
2
|
-
// This file is part of procaptcha <https://github.com/prosopo-io/procaptcha>.
|
|
3
|
-
//
|
|
4
|
-
// procaptcha is free software: you can redistribute it and/or modify
|
|
5
|
-
// it under the terms of the GNU General Public License as published by
|
|
6
|
-
// the Free Software Foundation, either version 3 of the License, or
|
|
7
|
-
// (at your option) any later version.
|
|
8
|
-
//
|
|
9
|
-
// procaptcha is distributed in the hope that it will be useful,
|
|
10
|
-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
-
// GNU General Public License for more details.
|
|
13
|
-
//
|
|
14
|
-
// You should have received a copy of the GNU General Public License
|
|
15
|
-
// along with procaptcha. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
-
import { randomAsHex, blake2AsHex } from '@polkadot/util-crypto';
|
|
17
|
-
// import {computeCaptchaSolutionHash} from '@prosopo/provider'; // TODO
|
|
18
|
-
import { CaptchaSolution, CaptchaMerkleTree, CaptchaSolutionCommitment } from '@prosopo/contract';
|
|
19
|
-
import { Signer } from "@polkadot/api/types";
|
|
20
|
-
|
|
21
|
-
import { ProsopoRandomProviderResponse, GetCaptchaResponse, CaptchaSolutionResponse } from "../types/api";
|
|
22
|
-
import { TransactionResponse } from "../types/contract";
|
|
23
|
-
|
|
24
|
-
import ProviderApi from "../api/ProviderApi";
|
|
25
|
-
import ProsopoContract from "../api/ProsopoContract";
|
|
26
|
-
import { TCaptchaSubmitResult } from '../types/client';
|
|
27
|
-
import {ProsopoApiError} from "../api/handlers";
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
function hexHash(data: string | Uint8Array): string {
|
|
31
|
-
return blake2AsHex(data);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function computeCaptchaSolutionHash(captcha: CaptchaSolution) {
|
|
35
|
-
return hexHash([captcha.captchaId, captcha.solution, captcha.salt].join());
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export class ProsopoCaptchaApi {
|
|
39
|
-
|
|
40
|
-
protected contract: ProsopoContract;
|
|
41
|
-
protected provider: ProsopoRandomProviderResponse;
|
|
42
|
-
protected providerApi: ProviderApi;
|
|
43
|
-
|
|
44
|
-
constructor(contract: ProsopoContract, provider: ProsopoRandomProviderResponse, providerApi: ProviderApi) {
|
|
45
|
-
this.contract = contract;
|
|
46
|
-
this.provider = provider;
|
|
47
|
-
this.providerApi = providerApi;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
public async getCaptchaChallenge(): Promise<GetCaptchaResponse> {
|
|
51
|
-
let captchaChallenge: GetCaptchaResponse;
|
|
52
|
-
try {
|
|
53
|
-
captchaChallenge = await this.providerApi.getCaptchaChallenge(this.provider);
|
|
54
|
-
} catch (err) {
|
|
55
|
-
throw new ProsopoApiError(err)
|
|
56
|
-
}
|
|
57
|
-
return captchaChallenge;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
public async submitCaptchaSolution(signer: Signer, requestHash: string, datasetId: string, solutions: CaptchaSolution[]) : Promise<TCaptchaSubmitResult> {
|
|
61
|
-
const salt = randomAsHex();
|
|
62
|
-
const tree = new CaptchaMerkleTree();
|
|
63
|
-
const captchaSolutionsSalted: CaptchaSolution[] = solutions.map(solution => ({...solution, salt: salt}));
|
|
64
|
-
const captchasHashed = captchaSolutionsSalted.map((captcha) => computeCaptchaSolutionHash(captcha));
|
|
65
|
-
|
|
66
|
-
tree.build(captchasHashed);
|
|
67
|
-
const commitmentId = tree.root!.hash;
|
|
68
|
-
|
|
69
|
-
console.log("solveCaptchaChallenge commitmentId", commitmentId);
|
|
70
|
-
// console.log("solveCaptchaChallenge USER ACCOUNT", this.contract.getAccount().address);
|
|
71
|
-
// console.log("solveCaptchaChallenge DAPP ACCOUNT", this.contract.getDappAddress());
|
|
72
|
-
// console.log("solveCaptchaChallenge CONTRACT ADDRESS", this.contract.getContract().address.toString());
|
|
73
|
-
|
|
74
|
-
let tx: TransactionResponse;
|
|
75
|
-
|
|
76
|
-
try {
|
|
77
|
-
tx = await this.contract.dappUserCommit(signer, datasetId as string, commitmentId, this.provider.providerId);
|
|
78
|
-
} catch (err) {
|
|
79
|
-
throw new ProsopoApiError(err)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
let result: CaptchaSolutionResponse;
|
|
83
|
-
|
|
84
|
-
try {
|
|
85
|
-
result = await this.providerApi.submitCaptchaSolution(tx.blockHash!, captchaSolutionsSalted, requestHash, tx.txHash.toString(), this.contract.getAccount().address);
|
|
86
|
-
} catch (err) {
|
|
87
|
-
throw new ProsopoApiError(err)
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
let commitment: CaptchaSolutionCommitment;
|
|
91
|
-
|
|
92
|
-
// TODO concurrency?
|
|
93
|
-
try {
|
|
94
|
-
commitment = await this.contract.getCaptchaSolutionCommitment(commitmentId);
|
|
95
|
-
} catch (err) {
|
|
96
|
-
throw new ProsopoApiError(err)
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
return [result, tx, commitment];
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export default ProsopoCaptchaApi;
|
|
105
|
-
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
// Copyright (C) 2021-2022 Prosopo (UK) Ltd.
|
|
2
|
-
// This file is part of procaptcha <https://github.com/prosopo-io/procaptcha>.
|
|
3
|
-
//
|
|
4
|
-
// procaptcha is free software: you can redistribute it and/or modify
|
|
5
|
-
// it under the terms of the GNU General Public License as published by
|
|
6
|
-
// the Free Software Foundation, either version 3 of the License, or
|
|
7
|
-
// (at your option) any later version.
|
|
8
|
-
//
|
|
9
|
-
// procaptcha is distributed in the hope that it will be useful,
|
|
10
|
-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
-
// GNU General Public License for more details.
|
|
13
|
-
//
|
|
14
|
-
// You should have received a copy of the GNU General Public License
|
|
15
|
-
// along with procaptcha. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
-
import { ICaptchaContextReducer, CaptchaEventCallbacks, TExtensionAccount, ICaptchaStatusReducer, IExtensionInterface } from "../types/client";
|
|
17
|
-
import { ProsopoRandomProviderResponse } from "../types/api";
|
|
18
|
-
|
|
19
|
-
import { ProsopoContract } from "../api/ProsopoContract";
|
|
20
|
-
import { getProsopoContract } from "./contract";
|
|
21
|
-
import { getExtension } from "./extension";
|
|
22
|
-
import { ProviderApi } from "../api/ProviderApi";
|
|
23
|
-
import { ProsopoCaptchaApi } from "./ProsopoCaptchaApi";
|
|
24
|
-
// import { Extension } from "../api";
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
export class ProsopoCaptchaClient {
|
|
28
|
-
|
|
29
|
-
public manager: ICaptchaContextReducer;
|
|
30
|
-
public status: ICaptchaStatusReducer;
|
|
31
|
-
public callbacks: CaptchaEventCallbacks | undefined;
|
|
32
|
-
public providerApi: ProviderApi;
|
|
33
|
-
|
|
34
|
-
private static extension: IExtensionInterface;
|
|
35
|
-
private static contract: ProsopoContract | undefined;
|
|
36
|
-
private static provider: ProsopoRandomProviderResponse | undefined;
|
|
37
|
-
private static captchaApi: ProsopoCaptchaApi | undefined;
|
|
38
|
-
|
|
39
|
-
constructor(manager: ICaptchaContextReducer, status: ICaptchaStatusReducer, callbacks?: CaptchaEventCallbacks) {
|
|
40
|
-
this.manager = manager;
|
|
41
|
-
this.status = status;
|
|
42
|
-
this.callbacks = callbacks;
|
|
43
|
-
this.providerApi = new ProviderApi(manager.state.config);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
public getExtension() {
|
|
47
|
-
return ProsopoCaptchaClient.extension;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
public setExtension(extension: IExtensionInterface) {
|
|
51
|
-
return ProsopoCaptchaClient.extension = extension;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public getContract() {
|
|
55
|
-
return ProsopoCaptchaClient.contract;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
public getProvider() {
|
|
59
|
-
return ProsopoCaptchaClient.provider;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
public getCaptchaApi() {
|
|
63
|
-
return ProsopoCaptchaClient.captchaApi;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
public async onLoad() {
|
|
67
|
-
let contractAddress = ProsopoCaptchaClient.contract?.address;
|
|
68
|
-
|
|
69
|
-
if (!ProsopoCaptchaClient.extension || !contractAddress) {
|
|
70
|
-
try {
|
|
71
|
-
[ProsopoCaptchaClient.extension, { contractAddress }] = await Promise.all([getExtension(), this.providerApi.getContractAddress()]);
|
|
72
|
-
} catch (err) {
|
|
73
|
-
throw new Error(err);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (this.callbacks?.onLoad) {
|
|
78
|
-
this.callbacks.onLoad(ProsopoCaptchaClient.extension, contractAddress);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
this.manager.update({ contractAddress });
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
public async onAccountChange(account?: TExtensionAccount) {
|
|
85
|
-
if (!account) {
|
|
86
|
-
this.onAccountUnset();
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
try {
|
|
91
|
-
ProsopoCaptchaClient.extension.setAccount(account.address);
|
|
92
|
-
} catch (err) {
|
|
93
|
-
throw new Error(err);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
try {
|
|
97
|
-
ProsopoCaptchaClient.contract = await getProsopoContract(this.manager.state.contractAddress!, this.manager.state.config['dappAccount'], account);
|
|
98
|
-
} catch (err) {
|
|
99
|
-
throw new Error(err);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
try {
|
|
103
|
-
ProsopoCaptchaClient.provider = await ProsopoCaptchaClient.contract.getRandomProvider(); // TODO how often should provider change?
|
|
104
|
-
} catch (err) {
|
|
105
|
-
throw new Error(err);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
ProsopoCaptchaClient.captchaApi = new ProsopoCaptchaApi(ProsopoCaptchaClient.contract, ProsopoCaptchaClient.provider, this.providerApi);
|
|
109
|
-
|
|
110
|
-
if (this.callbacks?.onAccountChange) {
|
|
111
|
-
this.callbacks.onAccountChange(account);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
this.manager.update({ account });
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
public onAccountUnset() {
|
|
118
|
-
ProsopoCaptchaClient.contract = undefined;
|
|
119
|
-
ProsopoCaptchaClient.provider = undefined;
|
|
120
|
-
ProsopoCaptchaClient.captchaApi = undefined;
|
|
121
|
-
|
|
122
|
-
if (this.callbacks?.onAccountChange) {
|
|
123
|
-
this.callbacks.onAccountChange(undefined);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
this.manager.update({ account: undefined });
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
export default ProsopoCaptchaClient;
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
// Copyright (C) 2021-2022 Prosopo (UK) Ltd.
|
|
2
|
-
// This file is part of procaptcha <https://github.com/prosopo-io/procaptcha>.
|
|
3
|
-
//
|
|
4
|
-
// procaptcha is free software: you can redistribute it and/or modify
|
|
5
|
-
// it under the terms of the GNU General Public License as published by
|
|
6
|
-
// the Free Software Foundation, either version 3 of the License, or
|
|
7
|
-
// (at your option) any later version.
|
|
8
|
-
//
|
|
9
|
-
// procaptcha is distributed in the hope that it will be useful,
|
|
10
|
-
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
-
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12
|
-
// GNU General Public License for more details.
|
|
13
|
-
//
|
|
14
|
-
// You should have received a copy of the GNU General Public License
|
|
15
|
-
// along with procaptcha. If not, see <http://www.gnu.org/licenses/>.
|
|
16
|
-
import { ICaptchaStateReducer, TCaptchaSubmitResult } from "../types/client";
|
|
17
|
-
import { CaptchaSolution, CaptchaSolutionResponse, GetCaptchaResponse } from "../types/api";
|
|
18
|
-
import { TransactionResponse } from "../types/contract";
|
|
19
|
-
|
|
20
|
-
import { ProsopoCaptchaClient } from "./ProsopoCaptchaClient";
|
|
21
|
-
import { convertCaptchaToCaptchaSolution } from "@prosopo/contract";
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
export class ProsopoCaptchaStateClient {
|
|
25
|
-
|
|
26
|
-
public context: ProsopoCaptchaClient;
|
|
27
|
-
public manager: ICaptchaStateReducer;
|
|
28
|
-
|
|
29
|
-
constructor(context: ProsopoCaptchaClient, manager: ICaptchaStateReducer) {
|
|
30
|
-
this.context = context;
|
|
31
|
-
this.manager = manager;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
public async onLoadCaptcha() {
|
|
35
|
-
let captchaChallenge: GetCaptchaResponse | Error | undefined;
|
|
36
|
-
|
|
37
|
-
try {
|
|
38
|
-
captchaChallenge = await this.context.getCaptchaApi()!.getCaptchaChallenge();
|
|
39
|
-
} catch (err) {
|
|
40
|
-
captchaChallenge = err as Error;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (this.context.callbacks?.onLoadCaptcha) {
|
|
44
|
-
this.context.callbacks.onLoadCaptcha(captchaChallenge);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (captchaChallenge instanceof Error) {
|
|
48
|
-
captchaChallenge = undefined;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
this.manager.update({ captchaChallenge, captchaIndex: 0 });
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
public onCancel() {
|
|
55
|
-
if (this.context.callbacks?.onCancel) {
|
|
56
|
-
this.context.callbacks.onCancel();
|
|
57
|
-
}
|
|
58
|
-
this.dismissCaptcha();
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
public async onSubmit() {
|
|
62
|
-
const { captchaChallenge, captchaIndex, captchaSolution } = this.manager.state;
|
|
63
|
-
|
|
64
|
-
const nextCaptchaIndex = captchaIndex + 1;
|
|
65
|
-
|
|
66
|
-
if (nextCaptchaIndex < captchaChallenge!.captchas.length) {
|
|
67
|
-
captchaSolution[nextCaptchaIndex] = [];
|
|
68
|
-
this.manager.update({ captchaIndex: nextCaptchaIndex, captchaSolution });
|
|
69
|
-
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
const signer = this.context.getExtension().getExtension().signer;
|
|
74
|
-
|
|
75
|
-
const currentCaptcha = captchaChallenge!.captchas[captchaIndex];
|
|
76
|
-
const { datasetId } = currentCaptcha.captcha; // TODO arbitrary datasetId? Could datasetId be moved up next to requestHash?
|
|
77
|
-
|
|
78
|
-
const solutions = this.parseSolution(captchaChallenge!, captchaSolution);
|
|
79
|
-
|
|
80
|
-
let submitResult: TCaptchaSubmitResult | Error;
|
|
81
|
-
|
|
82
|
-
try {
|
|
83
|
-
submitResult = await this.context.getCaptchaApi()!.submitCaptchaSolution(signer, captchaChallenge!.requestHash, datasetId!, solutions);
|
|
84
|
-
} catch (err) {
|
|
85
|
-
submitResult = err as Error;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (this.context.callbacks?.onSubmit) {
|
|
89
|
-
this.context.callbacks.onSubmit(submitResult, this.manager.state);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
this.manager.update({ captchaSolution: [] });
|
|
93
|
-
|
|
94
|
-
if (submitResult instanceof Error) {
|
|
95
|
-
// TODO onFail?
|
|
96
|
-
return;
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
await this.onSolved(submitResult);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
// TODO check for solved captchas.
|
|
103
|
-
public async onSolved(submitResult: TCaptchaSubmitResult) {
|
|
104
|
-
if (this.context.callbacks?.onSolved) {
|
|
105
|
-
this.context.callbacks.onSolved(submitResult);
|
|
106
|
-
}
|
|
107
|
-
this.dismissCaptcha();
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
public onChange(index: number) {
|
|
111
|
-
const { captchaIndex, captchaSolution } = this.manager.state;
|
|
112
|
-
|
|
113
|
-
let currentSolution: number[] = captchaSolution[captchaIndex] || [];
|
|
114
|
-
currentSolution = currentSolution.includes(index) ? currentSolution.filter(item => item !== index) : [...currentSolution, index];
|
|
115
|
-
|
|
116
|
-
captchaSolution[captchaIndex] = currentSolution;
|
|
117
|
-
|
|
118
|
-
if (this.context.callbacks?.onChange) {
|
|
119
|
-
this.context.callbacks.onChange(captchaSolution, captchaIndex);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
this.manager.update({ captchaSolution });
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
public dismissCaptcha() {
|
|
126
|
-
this.manager.update({ captchaChallenge: undefined });
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// TODO move to ProsopoContract/ProviderApi/Model?
|
|
130
|
-
public parseSolution(captchaChallenge: GetCaptchaResponse, captchaSolution: number[][]): CaptchaSolution[] {
|
|
131
|
-
const parsedSolution: CaptchaSolution[] = [];
|
|
132
|
-
|
|
133
|
-
for (const [index, challenge] of captchaChallenge!.captchas.entries()) {
|
|
134
|
-
const solution = captchaSolution[index] || [];
|
|
135
|
-
challenge.captcha.solution = solution;
|
|
136
|
-
parsedSolution[index] = convertCaptchaToCaptchaSolution(challenge.captcha);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
console.log("CAPTCHA SOLUTIONS", parsedSolution);
|
|
140
|
-
|
|
141
|
-
return parsedSolution;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
export default ProsopoCaptchaStateClient;
|