@prosopo/procaptcha 0.1.2 → 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/ProsopoContract.d.ts +2 -1
- package/dist/api/ProsopoContract.d.ts.map +1 -1
- package/dist/api/ProsopoContract.js +4 -0
- package/dist/api/ProsopoContract.js.map +1 -1
- package/dist/modules/ProsopoCaptchaClient.d.ts +1 -0
- package/dist/modules/ProsopoCaptchaClient.d.ts.map +1 -1
- package/dist/modules/ProsopoCaptchaClient.js +2 -0
- package/dist/modules/ProsopoCaptchaClient.js.map +1 -1
- package/dist/modules/ProsopoCaptchaStateClient.d.ts.map +1 -1
- package/dist/modules/ProsopoCaptchaStateClient.js +9 -2
- package/dist/modules/ProsopoCaptchaStateClient.js.map +1 -1
- package/dist/types/api.d.ts +4 -0
- package/dist/types/api.d.ts.map +1 -1
- package/dist/types/client.d.ts +1 -1
- package/dist/types/client.d.ts.map +1 -1
- package/img/contracts-page.png +0 -0
- package/package.json +2 -2
- package/tsconfig.tsbuildinfo +1 -0
- package/src/api/AsyncFactory.ts +0 -30
- package/src/api/Extension.ts +0 -120
- package/src/api/HttpClientBase.ts +0 -37
- package/src/api/ProsopoContract.ts +0 -42
- package/src/api/ProsopoContractBase.ts +0 -169
- package/src/api/ProviderApi.ts +0 -59
- package/src/api/handlers.ts +0 -29
- package/src/api/index.ts +0 -21
- package/src/index.ts +0 -18
- package/src/modules/ProsopoCaptchaApi.ts +0 -105
- package/src/modules/ProsopoCaptchaClient.ts +0 -132
- package/src/modules/ProsopoCaptchaStateClient.ts +0 -146
- package/src/modules/client.ts +0 -39
- package/src/modules/contract.ts +0 -27
- package/src/modules/extension.ts +0 -20
- package/src/modules/index.ts +0 -22
- package/src/modules/storage.ts +0 -35
- package/src/types/api.ts +0 -80
- package/src/types/client.ts +0 -88
- package/src/types/contract.ts +0 -21
- package/src/types/index.ts +0 -18
- package/tests/mocks/browser.ts +0 -25
- package/tests/modules/client.test.ts +0 -56
|
@@ -1,132 +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, getWsProvider } 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
|
-
getWsProvider(this.manager.state.config['dappUrl']));
|
|
99
|
-
} catch (err) {
|
|
100
|
-
throw new Error(err);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
try {
|
|
104
|
-
ProsopoCaptchaClient.provider = await ProsopoCaptchaClient.contract.getRandomProvider(); // TODO how often should provider change?
|
|
105
|
-
} catch (err) {
|
|
106
|
-
throw new Error(err);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
ProsopoCaptchaClient.captchaApi = new ProsopoCaptchaApi(ProsopoCaptchaClient.contract, ProsopoCaptchaClient.provider, this.providerApi);
|
|
110
|
-
|
|
111
|
-
if (this.callbacks?.onAccountChange) {
|
|
112
|
-
this.callbacks.onAccountChange(account);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
this.manager.update({ account });
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
public onAccountUnset() {
|
|
119
|
-
ProsopoCaptchaClient.contract = undefined;
|
|
120
|
-
ProsopoCaptchaClient.provider = undefined;
|
|
121
|
-
ProsopoCaptchaClient.captchaApi = undefined;
|
|
122
|
-
|
|
123
|
-
if (this.callbacks?.onAccountChange) {
|
|
124
|
-
this.callbacks.onAccountChange(undefined);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
this.manager.update({ account: undefined });
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
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;
|
package/src/modules/client.ts
DELETED
|
@@ -1,39 +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
|
-
|
|
17
|
-
import { ICaptchaContextState, ICaptchaState, ICaptchaStatusState, ICaptchaStatusReducerAction } from '../types';
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
export const captchaContextReducer = (state: ICaptchaContextState, action: Partial<ICaptchaContextState>) => {
|
|
21
|
-
return { ...state, ...action };
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export const captchaStateReducer = (state: ICaptchaState, action: Partial<ICaptchaState>): ICaptchaState => {
|
|
25
|
-
return { ...state, ...action };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const captchaStatusReducer = (state: ICaptchaStatusState, action: ICaptchaStatusReducerAction): ICaptchaStatusState => {
|
|
29
|
-
const logger = { info: console.log, error: console.error };
|
|
30
|
-
for (const key in action) {
|
|
31
|
-
logger[key](action[key]);
|
|
32
|
-
let status = Array.isArray(action[key]) ? action[key][1] : action[key];
|
|
33
|
-
if (status instanceof Error) {
|
|
34
|
-
status = status.message;
|
|
35
|
-
}
|
|
36
|
-
return { [key]: String(status) };
|
|
37
|
-
}
|
|
38
|
-
return state;
|
|
39
|
-
}
|
package/src/modules/contract.ts
DELETED
|
@@ -1,27 +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 ProsopoContract from "../api/ProsopoContract";
|
|
17
|
-
import { WsProvider, HttpProvider } from "@polkadot/rpc-provider";
|
|
18
|
-
import { InjectedAccountWithMeta } from "@polkadot/extension-inject/types";
|
|
19
|
-
import { ProviderInterface } from "@polkadot/rpc-provider/types";
|
|
20
|
-
|
|
21
|
-
export function getWsProvider(url?: string): WsProvider {
|
|
22
|
-
return new WsProvider(url);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export async function getProsopoContract(address: string, dappAddress: string, account: InjectedAccountWithMeta, providerInterface?: ProviderInterface): Promise<ProsopoContract> {
|
|
26
|
-
return await ProsopoContract.create(address, dappAddress, account, providerInterface ?? getWsProvider());
|
|
27
|
-
}
|
package/src/modules/extension.ts
DELETED
|
@@ -1,20 +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 Extension from "../api/Extension";
|
|
17
|
-
|
|
18
|
-
export async function getExtension(): Promise<Extension> {
|
|
19
|
-
return await Extension.create();
|
|
20
|
-
}
|
package/src/modules/index.ts
DELETED
|
@@ -1,22 +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 './contract';
|
|
17
|
-
export * from './extension';
|
|
18
|
-
export * from './storage';
|
|
19
|
-
export * from './client';
|
|
20
|
-
export * from './ProsopoCaptchaApi';
|
|
21
|
-
export * from './ProsopoCaptchaClient';
|
|
22
|
-
export * from './ProsopoCaptchaStateClient';
|
package/src/modules/storage.ts
DELETED
|
@@ -1,35 +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
|
-
const CURRENT_ACCOUNT_KEY = "@prosopo/current_account";
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Sets default `account`
|
|
20
|
-
*/
|
|
21
|
-
function setAccount(account: string) {
|
|
22
|
-
localStorage.setItem(CURRENT_ACCOUNT_KEY, account);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Gets default `account`
|
|
27
|
-
*/
|
|
28
|
-
function getAccount(): string | null {
|
|
29
|
-
return localStorage.getItem(CURRENT_ACCOUNT_KEY);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export default {
|
|
33
|
-
setAccount,
|
|
34
|
-
getAccount
|
|
35
|
-
}
|
package/src/types/api.ts
DELETED
|
@@ -1,80 +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
|
-
// declare module "*.json" {
|
|
17
|
-
// const value: any;
|
|
18
|
-
// export default value;
|
|
19
|
-
// }
|
|
20
|
-
|
|
21
|
-
import { InjectedAccountWithMeta, InjectedExtension } from "@polkadot/extension-inject/types";
|
|
22
|
-
|
|
23
|
-
// import { SubmittableResult } from "@polkadot/api";
|
|
24
|
-
import {Captcha, CaptchaStatus} from "@prosopo/contract";
|
|
25
|
-
|
|
26
|
-
export interface ProsopoRandomProviderResponse {
|
|
27
|
-
providerId: string,
|
|
28
|
-
blockNumber: string;
|
|
29
|
-
provider: ProposoProvider;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
export interface ProposoProvider {
|
|
33
|
-
balance: string;
|
|
34
|
-
captchaDatasetId: string;
|
|
35
|
-
fee: string;
|
|
36
|
-
payee: string; // TODO: enum?
|
|
37
|
-
serviceOrigin: string;
|
|
38
|
-
status: string; // TODO: enum
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
// export interface CaptchaResponseCaptchaItem {
|
|
42
|
-
// captchaId: string;
|
|
43
|
-
// datasetId: string;
|
|
44
|
-
// items: CaptchaImageSchema[];
|
|
45
|
-
// target: string;
|
|
46
|
-
// salt?: string;
|
|
47
|
-
// solution?: number[];
|
|
48
|
-
// }
|
|
49
|
-
|
|
50
|
-
export interface CaptchaImageSchema {
|
|
51
|
-
path: string,
|
|
52
|
-
type: string
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export interface CaptchaResponseCaptcha {
|
|
56
|
-
captcha: Captcha;
|
|
57
|
-
proof: string[][];
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export interface GetCaptchaResponse {
|
|
61
|
-
captchas: CaptchaResponseCaptcha[];
|
|
62
|
-
requestHash: string;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export interface CaptchaSolution {
|
|
66
|
-
captchaId: string;
|
|
67
|
-
solution: number[];
|
|
68
|
-
salt: string;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export interface CaptchaSolutionResponse {
|
|
72
|
-
status: string;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export interface ProsopoCaptchaConfig {
|
|
76
|
-
"providerApi.baseURL": string;
|
|
77
|
-
"providerApi.prefix": string;
|
|
78
|
-
"dappAccount": string;
|
|
79
|
-
"dappUrl": string;
|
|
80
|
-
}
|
package/src/types/client.ts
DELETED
|
@@ -1,88 +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 { InjectedAccountWithMeta, InjectedExtension } from "@polkadot/extension-inject/types";
|
|
17
|
-
import { ProsopoCaptchaConfig, GetCaptchaResponse, CaptchaSolutionResponse } from "../types/api";
|
|
18
|
-
import { TransactionResponse } from "../types/contract";
|
|
19
|
-
import {CaptchaSolution, CaptchaSolutionCommitment} from "@prosopo/contract";
|
|
20
|
-
import { Extension } from "../api/Extension";
|
|
21
|
-
|
|
22
|
-
export type TExtensionAccount = InjectedAccountWithMeta;
|
|
23
|
-
|
|
24
|
-
export type TCaptchaSubmitResult = [CaptchaSolutionResponse, TransactionResponse, CaptchaSolutionCommitment];
|
|
25
|
-
|
|
26
|
-
export interface IExtensionInterface {
|
|
27
|
-
checkExtension(): void;
|
|
28
|
-
getExtension(): InjectedExtension;
|
|
29
|
-
getAccounts(): InjectedAccountWithMeta[];
|
|
30
|
-
getAccount(): InjectedAccountWithMeta | undefined;
|
|
31
|
-
setAccount(account: string): void;
|
|
32
|
-
unsetAccount(): void;
|
|
33
|
-
getDefaultAccount(): InjectedAccountWithMeta | undefined;
|
|
34
|
-
setDefaultAccount(): void;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export interface ICaptchaClientEvents {
|
|
38
|
-
onLoad?: (extension: IExtensionInterface, contractAddress: string) => void;
|
|
39
|
-
onAccountChange?: (account?: TExtensionAccount) => void;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface ICaptchaStateClientEvents {
|
|
43
|
-
onLoadCaptcha?: (captchaChallenge: GetCaptchaResponse | Error) => void;
|
|
44
|
-
onSubmit?: (result: TCaptchaSubmitResult | Error, captchaState: ICaptchaState) => void;
|
|
45
|
-
onChange?: (captchaSolution: number[][], index: number) => void;
|
|
46
|
-
onCancel?: () => void;
|
|
47
|
-
onSolved?: (result: TCaptchaSubmitResult) => void;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface CaptchaEventCallbacks extends ICaptchaClientEvents, ICaptchaStateClientEvents { }
|
|
51
|
-
|
|
52
|
-
export interface ICaptchaContextState {
|
|
53
|
-
config: ProsopoCaptchaConfig;
|
|
54
|
-
contractAddress?: string;
|
|
55
|
-
account?: InjectedAccountWithMeta;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export interface ICaptchaContextReducer {
|
|
59
|
-
state: ICaptchaContextState;
|
|
60
|
-
update: (value: Partial<ICaptchaContextState>) => void;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export interface ICaptchaState {
|
|
64
|
-
captchaChallenge?: GetCaptchaResponse;
|
|
65
|
-
captchaIndex: number;
|
|
66
|
-
captchaSolution: number[][];
|
|
67
|
-
// captchaSolutions: CaptchaSolution[];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
export interface ICaptchaStateReducer {
|
|
71
|
-
state: ICaptchaState;
|
|
72
|
-
update: (value: Partial<ICaptchaState>) => void;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export interface ICaptchaStatusState {
|
|
76
|
-
info?: string;
|
|
77
|
-
error?: string;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export interface ICaptchaStatusReducerAction {
|
|
81
|
-
info?: [string, any] | string;
|
|
82
|
-
error?: [string, any] | string | Error;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export interface ICaptchaStatusReducer {
|
|
86
|
-
state: ICaptchaStatusState;
|
|
87
|
-
update: (value: Partial<ICaptchaStatusReducerAction>) => void;
|
|
88
|
-
}
|
package/src/types/contract.ts
DELETED
|
@@ -1,21 +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 { SubmittableResult } from "@polkadot/api";
|
|
17
|
-
import type { SubmittableResultValue } from '@polkadot/api/types';
|
|
18
|
-
|
|
19
|
-
export type TransactionResponse = SubmittableResultValue & {
|
|
20
|
-
blockHash?: string,
|
|
21
|
-
};
|
package/src/types/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 './contract';
|
|
18
|
-
export * from './client';
|
package/tests/mocks/browser.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
export class LocalStorageMock {
|
|
2
|
-
|
|
3
|
-
private store: {[key: string]: string};
|
|
4
|
-
|
|
5
|
-
constructor() {
|
|
6
|
-
this.store = {};
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
clear() {
|
|
10
|
-
this.store = {};
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
getItem(key: string) {
|
|
14
|
-
return this.store[key] || null;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
setItem(key: string, value: any) {
|
|
18
|
-
this.store[key] = String(value);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
removeItem(key: string) {
|
|
22
|
-
delete this.store[key];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
}
|