@prosopo/procaptcha 0.1.0
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/LICENSE +674 -0
- package/package.json +45 -0
- package/src/api/AsyncFactory.ts +30 -0
- package/src/api/Extension.ts +120 -0
- package/src/api/HttpClientBase.ts +37 -0
- package/src/api/ProsopoContract.ts +42 -0
- package/src/api/ProsopoContractBase.ts +169 -0
- package/src/api/ProviderApi.ts +59 -0
- package/src/api/handlers.ts +29 -0
- package/src/api/index.ts +21 -0
- package/src/index.ts +18 -0
- package/src/modules/ProsopoCaptchaApi.ts +105 -0
- package/src/modules/ProsopoCaptchaClient.ts +131 -0
- package/src/modules/ProsopoCaptchaStateClient.ts +146 -0
- package/src/modules/client.ts +39 -0
- package/src/modules/contract.ts +23 -0
- package/src/modules/extension.ts +20 -0
- package/src/modules/index.ts +22 -0
- package/src/modules/storage.ts +35 -0
- package/src/types/api.ts +79 -0
- package/src/types/client.ts +88 -0
- package/src/types/contract.ts +21 -0
- package/src/types/index.ts +18 -0
- package/tests/mocks/browser.ts +25 -0
- package/tests/modules/client.test.ts +56 -0
- package/tsconfig.json +24 -0
|
@@ -0,0 +1,88 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
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';
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
// import { LocalStorageMock } from "../mocks/browser";
|
|
2
|
+
import { ProsopoCaptchaConfig } from "../../src/types/api";
|
|
3
|
+
|
|
4
|
+
import { captchaContextReducer, captchaStateReducer, captchaStatusReducer } from "../../src/modules/client";
|
|
5
|
+
import { ICaptchaContextState, ICaptchaState } from "../../src/types/client";
|
|
6
|
+
|
|
7
|
+
import chai from "chai";
|
|
8
|
+
|
|
9
|
+
const expect = chai.expect;
|
|
10
|
+
|
|
11
|
+
describe("CLIENT UNIT TESTS", () => {
|
|
12
|
+
|
|
13
|
+
// var localStorage: LocalStorageMock;
|
|
14
|
+
|
|
15
|
+
const testConfig: ProsopoCaptchaConfig = {
|
|
16
|
+
"providerApi.baseURL": "http://localhost:3000",
|
|
17
|
+
"providerApi.prefix": "/v1/prosopo",
|
|
18
|
+
"dappAccount": "",
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
before(async () => {
|
|
22
|
+
// localStorage = new LocalStorageMock();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("client context reducer", async () => {
|
|
26
|
+
|
|
27
|
+
const testState: ICaptchaContextState = {
|
|
28
|
+
config: testConfig,
|
|
29
|
+
contractAddress: "test",
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
testConfig.dappAccount = "dapp";
|
|
33
|
+
const contextReducer = captchaContextReducer(testState, { config: testConfig });
|
|
34
|
+
|
|
35
|
+
expect(contextReducer.config.dappAccount).to.equal(testConfig.dappAccount);
|
|
36
|
+
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("client state reducer", async () => {
|
|
40
|
+
|
|
41
|
+
const testState: ICaptchaState = {
|
|
42
|
+
captchaIndex: 0,
|
|
43
|
+
captchaSolution: [[0, 1, 2, 3]],
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const captchaSolution = testState.captchaSolution;
|
|
47
|
+
captchaSolution.push([4, 5]);
|
|
48
|
+
|
|
49
|
+
const stateReducer = captchaStateReducer(testState, { captchaSolution });
|
|
50
|
+
|
|
51
|
+
expect(stateReducer.captchaSolution.length).to.equal(2);
|
|
52
|
+
expect(stateReducer.captchaSolution[1]).to.include.members([4, 5]);
|
|
53
|
+
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.build.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"rootDir": "./src",
|
|
5
|
+
"outDir": "./dist",
|
|
6
|
+
"module": "commonjs",
|
|
7
|
+
"target": "esnext",
|
|
8
|
+
"lib": [
|
|
9
|
+
"esnext",
|
|
10
|
+
"dom"
|
|
11
|
+
]
|
|
12
|
+
},
|
|
13
|
+
"include": [
|
|
14
|
+
"./src/**/*.ts"
|
|
15
|
+
],
|
|
16
|
+
"references": [
|
|
17
|
+
{
|
|
18
|
+
"path": "../provider"
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
"path": "../contract"
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
}
|