@prosopo/procaptcha 0.1.7 → 0.1.9
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/.eslintrc.js +20 -4
- package/README.md +3 -3
- package/dist/api/AccountCreator.d.ts +19 -0
- package/dist/api/AccountCreator.d.ts.map +1 -0
- package/dist/api/AccountCreator.js +88 -0
- package/dist/api/AccountCreator.js.map +1 -0
- package/dist/api/AsyncFactory.d.ts +7 -0
- package/dist/api/AsyncFactory.d.ts.map +1 -0
- package/dist/api/AsyncFactory.js +30 -0
- package/dist/api/AsyncFactory.js.map +1 -0
- package/dist/api/ExtensionWeb2.d.ts +27 -0
- package/dist/api/ExtensionWeb2.d.ts.map +1 -0
- package/dist/api/ExtensionWeb2.js +106 -0
- package/dist/api/ExtensionWeb2.js.map +1 -0
- package/dist/api/ExtensionWeb3.d.ts +56 -0
- package/dist/api/ExtensionWeb3.d.ts.map +1 -0
- package/dist/api/ExtensionWeb3.js +151 -0
- package/dist/api/ExtensionWeb3.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/dist/api/ProsopoContract.js +38 -0
- package/dist/api/ProsopoContract.js.map +1 -0
- package/dist/api/ProsopoContractBase.d.ts +32 -0
- package/dist/api/ProsopoContractBase.d.ts.map +1 -0
- package/dist/api/ProsopoContractBase.js +132 -0
- package/dist/api/ProsopoContractBase.js.map +1 -0
- package/dist/api/ProviderApi.d.ts +14 -0
- package/dist/api/ProviderApi.d.ts.map +1 -0
- package/dist/api/ProviderApi.js +53 -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 +21 -0
- package/dist/api/handlers.js.map +1 -0
- package/dist/api/index.d.ts +8 -0
- package/dist/api/index.d.ts.map +1 -0
- package/dist/api/index.js +26 -0
- package/dist/api/index.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/modules/ProsopoCaptchaApi.d.ts +21 -0
- package/dist/modules/ProsopoCaptchaApi.d.ts.map +1 -0
- package/dist/modules/ProsopoCaptchaApi.js +120 -0
- package/dist/modules/ProsopoCaptchaApi.js.map +1 -0
- package/dist/modules/ProsopoCaptchaClient.d.ts +26 -0
- package/dist/modules/ProsopoCaptchaClient.d.ts.map +1 -0
- package/dist/modules/ProsopoCaptchaClient.js +112 -0
- package/dist/modules/ProsopoCaptchaClient.js.map +1 -0
- package/dist/modules/ProsopoCaptchaStateClient.d.ts +18 -0
- package/dist/modules/ProsopoCaptchaStateClient.d.ts.map +1 -0
- package/dist/modules/ProsopoCaptchaStateClient.js +111 -0
- package/dist/modules/ProsopoCaptchaStateClient.js.map +1 -0
- package/dist/modules/canvas.d.ts +2 -0
- package/dist/modules/canvas.d.ts.map +1 -0
- package/dist/modules/canvas.js +286 -0
- package/dist/modules/canvas.js.map +1 -0
- package/dist/modules/client.d.ts +9 -0
- package/dist/modules/client.d.ts.map +1 -0
- package/dist/modules/client.js +40 -0
- 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/dist/modules/contract.js +42 -0
- package/dist/modules/contract.js.map +1 -0
- package/dist/modules/extension.d.ts +4 -0
- package/dist/modules/extension.d.ts.map +1 -0
- package/dist/modules/extension.js +31 -0
- 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/dist/modules/index.js +26 -0
- 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/dist/modules/storage.js +35 -0
- package/dist/modules/storage.js.map +1 -0
- package/dist/types/api.d.ts +53 -0
- package/dist/types/api.d.ts.map +1 -0
- package/dist/types/api.js +22 -0
- package/dist/types/api.js.map +1 -0
- package/dist/types/client.d.ts +61 -0
- 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/dist/types/index.js +22 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +9 -6
package/.eslintrc.js
CHANGED
|
@@ -12,10 +12,26 @@ module.exports = {
|
|
|
12
12
|
"ecmaVersion": "latest",
|
|
13
13
|
"sourceType": "module"
|
|
14
14
|
},
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
],
|
|
15
|
+
|
|
16
|
+
"plugins": ["unused-imports", "@typescript-eslint"],
|
|
18
17
|
"rules": {
|
|
18
|
+
"no-unused-vars": "off",
|
|
19
|
+
"unused-imports/no-unused-imports": "error",
|
|
20
|
+
"unused-imports/no-unused-vars": [
|
|
21
|
+
"warn",
|
|
22
|
+
{
|
|
23
|
+
"vars": "all",
|
|
24
|
+
"varsIgnorePattern": "^_",
|
|
25
|
+
"args": "after-used",
|
|
26
|
+
"argsIgnorePattern": "^_"
|
|
27
|
+
}
|
|
28
|
+
],
|
|
19
29
|
"indent": ["error", 4],
|
|
20
|
-
|
|
30
|
+
"sort-imports": [
|
|
31
|
+
"error",
|
|
32
|
+
{
|
|
33
|
+
"ignoreDeclarationSort": true
|
|
34
|
+
}
|
|
35
|
+
]
|
|
36
|
+
},
|
|
21
37
|
}
|
package/README.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
# Procaptcha
|
|
2
2
|
|
|
3
|
-
Contains interfaces for accessing the Prosopo [protocol](https://github.com/prosopo
|
|
3
|
+
Contains interfaces for accessing the Prosopo [protocol](https://github.com/prosopo/protocol/) contract, Prosopo [provider](https://github.com/prosopo/provider) API and the captcha client state via the `ProsopoCaptchaClient`, `ProsopoCaptchaApi` and `ProsopoCaptchaStateClient` classes.
|
|
4
4
|
|
|
5
5
|
## Prerequisites
|
|
6
6
|
|
|
7
|
-
The Dapp smart contract account must be [registered](https://github.com/prosopo
|
|
7
|
+
The Dapp smart contract account must be [registered](https://github.com/prosopo/protocol/blob/5cb282a911522bfbf29d34d1830badee3eecec83/contracts/lib.rs#L698-L737) in the Prosopo [protocol](https://github.com/prosopo/protocol/) contract in order to interact with the Prosopo protocol. You can complete Dapp registration by navigating to [polkadot-js apps](https://polkadot.js.org/apps/) and using the contracts page.
|
|
8
8
|
|
|
9
9
|

|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
-
This repository can be imported into any JavaScript framework and used to create a CAPTCHA User Interface. See, for example, the [procaptcha-react](https://github.com/prosopo
|
|
13
|
+
This repository can be imported into any JavaScript framework and used to create a CAPTCHA User Interface. See, for example, the [procaptcha-react](https://github.com/prosopo/procaptcha-react) repository.
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ApiPromise } from "@polkadot/api";
|
|
2
|
+
import { InjectedAccountWithMeta } from "@polkadot/extension-inject/types";
|
|
3
|
+
import AsyncFactory from "./AsyncFactory";
|
|
4
|
+
import { Keyring } from "@polkadot/keyring";
|
|
5
|
+
import { ProviderInterface } from "@polkadot/rpc-provider/types";
|
|
6
|
+
import { AccountCreatorConfig } from "../types/index";
|
|
7
|
+
export declare class AccountCreator extends AsyncFactory {
|
|
8
|
+
protected api: ApiPromise;
|
|
9
|
+
protected config: AccountCreatorConfig;
|
|
10
|
+
protected source: string;
|
|
11
|
+
/**
|
|
12
|
+
* @param providerInterface
|
|
13
|
+
* @param config
|
|
14
|
+
*/
|
|
15
|
+
init(providerInterface: ProviderInterface, config: AccountCreatorConfig, source: string): Promise<this>;
|
|
16
|
+
createAccount(keyring?: Keyring, address?: string): Promise<InjectedAccountWithMeta>;
|
|
17
|
+
getFingerprint(): Promise<string>;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=AccountCreator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AccountCreator.d.ts","sourceRoot":"","sources":["../../src/api/AccountCreator.ts"],"names":[],"mappings":"AAeA,OAAO,EAAC,UAAU,EAAC,MAAM,eAAe,CAAC;AACzC,OAAO,EAAC,uBAAuB,EAAC,MAAM,kCAAkC,CAAC;AACzE,OAAO,YAAY,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAC,OAAO,EAA+B,MAAM,mBAAmB,CAAC;AAExE,OAAO,EAAC,iBAAiB,EAAC,MAAM,8BAA8B,CAAC;AAK/D,OAAO,EAAC,oBAAoB,EAAC,MAAM,gBAAgB,CAAC;AAGpD,qBAAa,cAAe,SAAQ,YAAY;IAE5C,SAAS,CAAC,GAAG,EAAE,UAAU,CAAC;IAC1B,SAAS,CAAC,MAAM,EAAE,oBAAoB,CAAC;IACvC,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC;IAEzB;;;OAGG;IACU,IAAI,CAAC,iBAAiB,EAAE,iBAAiB,EAAE,MAAM,EAAE,oBAAoB,EAAE,MAAM,EAAE,MAAM;IAOvF,aAAa,CAAC,OAAO,CAAC,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,uBAAuB,CAAC;IAsCpF,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC;CAUjD"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AccountCreator = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
// Copyright (C) 2021-2022 Prosopo (UK) Ltd.
|
|
6
|
+
// This file is part of procaptcha <https://github.com/prosopo/procaptcha>.
|
|
7
|
+
//
|
|
8
|
+
// procaptcha is free software: you can redistribute it and/or modify
|
|
9
|
+
// it under the terms of the GNU General Public License as published by
|
|
10
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+
// (at your option) any later version.
|
|
12
|
+
//
|
|
13
|
+
// procaptcha is distributed in the hope that it will be useful,
|
|
14
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+
// GNU General Public License for more details.
|
|
17
|
+
//
|
|
18
|
+
// You should have received a copy of the GNU General Public License
|
|
19
|
+
// along with procaptcha. If not, see <http://www.gnu.org/licenses/>.
|
|
20
|
+
const api_1 = require("@polkadot/api");
|
|
21
|
+
const AsyncFactory_1 = tslib_1.__importDefault(require("./AsyncFactory"));
|
|
22
|
+
const keyring_1 = require("@polkadot/keyring");
|
|
23
|
+
const util_crypto_1 = require("@polkadot/util-crypto");
|
|
24
|
+
const fingerprintjs_1 = tslib_1.__importStar(require("@fingerprintjs/fingerprintjs"));
|
|
25
|
+
const bip39_1 = require("@polkadot/util-crypto/mnemonic/bip39");
|
|
26
|
+
const util_1 = require("@polkadot/util");
|
|
27
|
+
const canvas_1 = require("../modules/canvas");
|
|
28
|
+
const datasets_1 = require("@prosopo/datasets");
|
|
29
|
+
class AccountCreator extends AsyncFactory_1.default {
|
|
30
|
+
api;
|
|
31
|
+
config;
|
|
32
|
+
source;
|
|
33
|
+
/**
|
|
34
|
+
* @param providerInterface
|
|
35
|
+
* @param config
|
|
36
|
+
*/
|
|
37
|
+
async init(providerInterface, config, source) {
|
|
38
|
+
this.api = await api_1.ApiPromise.create({ provider: providerInterface });
|
|
39
|
+
this.source = source;
|
|
40
|
+
this.config = config;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
async createAccount(keyring, address) {
|
|
44
|
+
const params = {
|
|
45
|
+
area: this.config.area,
|
|
46
|
+
offsetParameter: this.config.offsetParameter,
|
|
47
|
+
multiplier: this.config.multiplier,
|
|
48
|
+
fontSizeFactor: this.config.fontSizeFactor,
|
|
49
|
+
maxShadowBlur: this.config.maxShadowBlur
|
|
50
|
+
};
|
|
51
|
+
const browserEntropy = await this.getFingerprint();
|
|
52
|
+
const canvasEntropy = (0, canvas_1.picassoCanvas)(this.config.numberOfRounds, this.config.seed, params);
|
|
53
|
+
const entropy = (0, datasets_1.hexHash)([canvasEntropy, browserEntropy].join(""), 128).slice(2);
|
|
54
|
+
console.log("canvas entropy", canvasEntropy);
|
|
55
|
+
console.log("browserEntropy", browserEntropy);
|
|
56
|
+
console.log("entropy", entropy);
|
|
57
|
+
const u8Entropy = (0, util_1.stringToU8a)(entropy);
|
|
58
|
+
const mnemonic = (0, bip39_1.entropyToMnemonic)(u8Entropy);
|
|
59
|
+
if (!keyring) {
|
|
60
|
+
keyring = new keyring_1.Keyring({ type: 'sr25519', ss58Format: this.api.registry.chainSS58 });
|
|
61
|
+
}
|
|
62
|
+
await (0, util_crypto_1.cryptoWaitReady)();
|
|
63
|
+
if (address) {
|
|
64
|
+
return { address: address, meta: { source: this.source, name: address } };
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
const account = keyring?.addFromMnemonic(mnemonic);
|
|
68
|
+
return {
|
|
69
|
+
address: account.address.length === 42
|
|
70
|
+
? account.address
|
|
71
|
+
: (0, keyring_1.encodeAddress)((0, keyring_1.decodeAddress)(account.address), this.api.registry.chainSS58),
|
|
72
|
+
meta: { source: this.source, name: account.address },
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
async getFingerprint() {
|
|
77
|
+
// Initialize an agent at application startup.
|
|
78
|
+
const fpPromise = fingerprintjs_1.default.load();
|
|
79
|
+
// Get the visitor identifier when you need it.
|
|
80
|
+
const fp = await fpPromise;
|
|
81
|
+
const result = await fp.get();
|
|
82
|
+
// strip out the components that change in incognito mode
|
|
83
|
+
const { screenFrame, ...componentsReduced } = result.components;
|
|
84
|
+
return (0, fingerprintjs_1.hashComponents)(componentsReduced);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
exports.AccountCreator = AccountCreator;
|
|
88
|
+
//# sourceMappingURL=AccountCreator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AccountCreator.js","sourceRoot":"","sources":["../../src/api/AccountCreator.ts"],"names":[],"mappings":";;;;AAAA,4CAA4C;AAC5C,2EAA2E;AAC3E,EAAE;AACF,qEAAqE;AACrE,uEAAuE;AACvE,oEAAoE;AACpE,sCAAsC;AACtC,EAAE;AACF,gEAAgE;AAChE,iEAAiE;AACjE,gEAAgE;AAChE,+CAA+C;AAC/C,EAAE;AACF,oEAAoE;AACpE,sEAAsE;AACtE,uCAAyC;AAEzC,0EAA0C;AAC1C,+CAAwE;AACxE,uDAAsD;AAEtD,sFAA0E;AAC1E,gEAAuE;AACvE,yCAA2C;AAC3C,8CAAgD;AAEhD,gDAA0C;AAE1C,MAAa,cAAe,SAAQ,sBAAY;IAElC,GAAG,CAAa;IAChB,MAAM,CAAuB;IAC7B,MAAM,CAAS;IAEzB;;;OAGG;IACI,KAAK,CAAC,IAAI,CAAC,iBAAoC,EAAE,MAA4B,EAAE,MAAc;QAChG,IAAI,CAAC,GAAG,GAAG,MAAM,gBAAU,CAAC,MAAM,CAAC,EAAC,QAAQ,EAAE,iBAAiB,EAAC,CAAC,CAAC;QAClE,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,OAAO,IAAI,CAAC;IAChB,CAAC;IAEM,KAAK,CAAC,aAAa,CAAC,OAAiB,EAAE,OAAgB;QAC1D,MAAM,MAAM,GAAG;YACX,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,IAAI;YACtB,eAAe,EAAE,IAAI,CAAC,MAAM,CAAC,eAAe;YAC5C,UAAU,EAAE,IAAI,CAAC,MAAM,CAAC,UAAU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AO;gBACH,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,MAAM,KAAK,EAAE;oBAClC,CAAC,CAAC,OAAO,CAAC,OAAO;oBACjB,CAAC,CAAC,IAAA,uBAAa,EAAC,IAAA,uBAAa,EAAC,OAAO,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,SAAS,CAAC;gBAChF,IAAI,EAAE,EAAC,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,OAAO,CAAC,OAAO,EAAC;aACrD,CAAA;SACJ;IAGL,CAAC;IAEM,KAAK,CAAC,cAAc;QACvB,8CAA8C;QAC9C,MAAM,SAAS,GAAG,uBAAa,CAAC,IAAI,EAAE,CAAA;QACtC,+CAA+C;QAC/C,MAAM,EAAE,GAAG,MAAM,SAAS,CAAA;QAC1B,MAAM,MAAM,GAAG,MAAM,EAAE,CAAC,GAAG,EAAE,CAAA;QAC7B,yDAAyD;QACzD,MAAM,EAAC,WAAW,EAAE,GAAG,iBAAiB,EAAC,GAAG,MAAM,CAAC,UAAU,CAAA;QAC7D,OAAO,IAAA,8BAAc,EAAC,iBAAiB,CAAC,CAAA;IAC5C,CAAC;CACJ;AAjED,wCAiEC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AsyncFactory.d.ts","sourceRoot":"","sources":["../../src/api/AsyncFactory.ts"],"names":[],"mappings":"AAiBA,8BAAsB,YAAY;;WAMV,MAAM,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;aAIzB,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAEtD;AAED,eAAe,YAAY,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AsyncFactory = void 0;
|
|
4
|
+
// Copyright (C) 2021-2022 Prosopo (UK) Ltd.
|
|
5
|
+
// This file is part of procaptcha <https://github.com/prosopo/procaptcha>.
|
|
6
|
+
//
|
|
7
|
+
// procaptcha is free software: you can redistribute it and/or modify
|
|
8
|
+
// it under the terms of the GNU General Public License as published by
|
|
9
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+
// (at your option) any later version.
|
|
11
|
+
//
|
|
12
|
+
// procaptcha is distributed in the hope that it will be useful,
|
|
13
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15
|
+
// GNU General Public License for more details.
|
|
16
|
+
//
|
|
17
|
+
// You should have received a copy of the GNU General Public License
|
|
18
|
+
// along with procaptcha. If not, see <http://www.gnu.org/licenses/>.
|
|
19
|
+
const contract_1 = require("@prosopo/contract");
|
|
20
|
+
class AsyncFactory {
|
|
21
|
+
constructor() {
|
|
22
|
+
throw new contract_1.ProsopoEnvError("GENERAL.ASYNC_FACTORY_CREATE");
|
|
23
|
+
}
|
|
24
|
+
static async create(...args) {
|
|
25
|
+
return await Object.create(this.prototype).init(...args);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.AsyncFactory = AsyncFactory;
|
|
29
|
+
exports.default = AsyncFactory;
|
|
30
|
+
//# sourceMappingURL=AsyncFactory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AsyncFactory.js","sourceRoot":"","sources":["../../src/api/AsyncFactory.ts"],"names":[],"mappings":";;;AAAA,4CAA4C;AAC5C,2EAA2E;AAC3E,EAAE;AACF,qEAAqE;AACrE,uEAAuE;AACvE,oEAAoE;AACpE,sCAAsC;AACtC,EAAE;AACF,gEAAgE;AAChE,iEAAiE;AACjE,gEAAgE;AAChE,+CAA+C;AAC/C,EAAE;AACF,oEAAoE;AACpE,sEAAsE;AACtE,gDAAkD;AAElD,MAAsB,YAAY;IAE9B;QACI,MAAM,IAAI,0BAAe,CAAC,8BAA8B,CAAC,CAAC;IAC9D,CAAC;IAEM,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,IAAW;QACrC,OAAO,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;IAC7D,CAAC;CAIJ;AAZD,oCAYC;AAED,kBAAe,YAAY,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { InjectedAccountWithMeta, InjectedExtension } from "@polkadot/extension-inject/types";
|
|
2
|
+
import { IExtensionInterface } from "../types/client";
|
|
3
|
+
import AsyncFactory from "./AsyncFactory";
|
|
4
|
+
import { WsProvider } from "@polkadot/rpc-provider";
|
|
5
|
+
import { AccountCreatorConfig } from "../types/index";
|
|
6
|
+
export declare class ExtensionWeb2 extends AsyncFactory implements IExtensionInterface {
|
|
7
|
+
private extension?;
|
|
8
|
+
private account;
|
|
9
|
+
private accounts;
|
|
10
|
+
protected source: string;
|
|
11
|
+
protected accountCreatorConfig: AccountCreatorConfig;
|
|
12
|
+
protected wsProvider: WsProvider;
|
|
13
|
+
init(wsProvider: WsProvider, accountCreatorConfig: AccountCreatorConfig, source: string): Promise<this>;
|
|
14
|
+
checkExtension(): Promise<void>;
|
|
15
|
+
getExtension(): InjectedExtension | undefined;
|
|
16
|
+
private setExtension;
|
|
17
|
+
getAccounts(): InjectedAccountWithMeta[];
|
|
18
|
+
private setAccounts;
|
|
19
|
+
getAccount(): InjectedAccountWithMeta | undefined;
|
|
20
|
+
setAccount(address: string): void;
|
|
21
|
+
unsetAccount(): void;
|
|
22
|
+
getDefaultAccount(): InjectedAccountWithMeta | undefined;
|
|
23
|
+
setDefaultAccount(): void;
|
|
24
|
+
createAccount(): Promise<any>;
|
|
25
|
+
}
|
|
26
|
+
export default ExtensionWeb2;
|
|
27
|
+
//# sourceMappingURL=ExtensionWeb2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExtensionWeb2.d.ts","sourceRoot":"","sources":["../../src/api/ExtensionWeb2.ts"],"names":[],"mappings":"AAeA,OAAO,EACH,uBAAuB,EACvB,iBAAiB,EACpB,MAAM,kCAAkC,CAAA;AAEzC,OAAO,EAAC,mBAAmB,EAAC,MAAM,iBAAiB,CAAC;AACpD,OAAO,YAAY,MAAM,gBAAgB,CAAC;AAG1C,OAAO,EAAC,UAAU,EAAC,MAAM,wBAAwB,CAAC;AAKlD,OAAO,EAAC,oBAAoB,EAAC,MAAM,gBAAgB,CAAC;AAEpD,qBAAa,aAAc,SAAQ,YAAa,YAAW,mBAAmB;IAE1E,OAAO,CAAC,SAAS,CAAC,CAAoB;IACtC,OAAO,CAAC,OAAO,CAAsC;IACrD,OAAO,CAAC,QAAQ,CAA4B;IAE5C,SAAS,CAAC,MAAM,EAAE,MAAM,CAAA;IACxB,SAAS,CAAC,oBAAoB,EAAE,oBAAoB,CAAA;IACpD,SAAS,CAAC,UAAU,EAAE,UAAU,CAAA;IAEnB,IAAI,CAAC,UAAU,EAAE,UAAU,EAAE,oBAAoB,EAAE,oBAAoB,EAAE,MAAM,EAAE,MAAM;IAQvF,cAAc;IAIpB,YAAY;YAIL,YAAY;IA8BnB,WAAW;YAIJ,WAAW;IASlB,UAAU;IAIV,UAAU,CAAC,OAAO,EAAE,MAAM;IAc1B,YAAY;IAKZ,iBAAiB;IAKjB,iBAAiB;IAOX,aAAa;CAS7B;AAED,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExtensionWeb2 = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const storage_1 = tslib_1.__importDefault(require("../modules/storage"));
|
|
6
|
+
const AsyncFactory_1 = tslib_1.__importDefault(require("./AsyncFactory"));
|
|
7
|
+
const contract_1 = require("@prosopo/contract");
|
|
8
|
+
const AccountCreator_1 = require("./AccountCreator");
|
|
9
|
+
const Signer_1 = tslib_1.__importDefault(require("@polkadot/extension-base/page/Signer"));
|
|
10
|
+
class ExtensionWeb2 extends AsyncFactory_1.default {
|
|
11
|
+
extension;
|
|
12
|
+
account;
|
|
13
|
+
accounts;
|
|
14
|
+
source;
|
|
15
|
+
accountCreatorConfig;
|
|
16
|
+
wsProvider;
|
|
17
|
+
async init(wsProvider, accountCreatorConfig, source) {
|
|
18
|
+
this.source = source;
|
|
19
|
+
await this.setExtension();
|
|
20
|
+
this.wsProvider = wsProvider;
|
|
21
|
+
this.accountCreatorConfig = accountCreatorConfig;
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
async checkExtension() {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
getExtension() {
|
|
28
|
+
return this.extension;
|
|
29
|
+
}
|
|
30
|
+
async setExtension() {
|
|
31
|
+
/*
|
|
32
|
+
InjectedAccounts
|
|
33
|
+
get: (anyType?: boolean) => Promise<InjectedAccount[]>;
|
|
34
|
+
subscribe: (cb: (accounts: InjectedAccount[]) => void | Promise<void>) => Unsubcall;
|
|
35
|
+
*/
|
|
36
|
+
const injectedAccounts = {
|
|
37
|
+
get: () => Promise.resolve([]), subscribe: () => () => {
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
async function sendMessage(message) {
|
|
41
|
+
return new Promise((resolve, reject) => {
|
|
42
|
+
resolve();
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
await sendMessage('pub(authorize.tab)');
|
|
46
|
+
const signer = new Signer_1.default(sendMessage);
|
|
47
|
+
this.extension = {
|
|
48
|
+
name: "polkadot-js-web2",
|
|
49
|
+
version: "procaptcha",
|
|
50
|
+
accounts: injectedAccounts,
|
|
51
|
+
signer: signer,
|
|
52
|
+
};
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
getAccounts() {
|
|
56
|
+
return this.accounts;
|
|
57
|
+
}
|
|
58
|
+
async setAccounts(accounts) {
|
|
59
|
+
try {
|
|
60
|
+
this.accounts = accounts;
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
throw new contract_1.ProsopoEnvError(err);
|
|
64
|
+
}
|
|
65
|
+
this.setDefaultAccount();
|
|
66
|
+
}
|
|
67
|
+
getAccount() {
|
|
68
|
+
return this.account;
|
|
69
|
+
}
|
|
70
|
+
setAccount(address) {
|
|
71
|
+
if (!this.accounts.length) {
|
|
72
|
+
throw new contract_1.ProsopoEnvError("WIDGET.NO_ACCOUNTS_FOUND");
|
|
73
|
+
}
|
|
74
|
+
const account = this.accounts.find(acc => acc.address === address);
|
|
75
|
+
if (!account) {
|
|
76
|
+
throw new contract_1.ProsopoEnvError("WIDGET.ACCOUNT_NOT_FOUND");
|
|
77
|
+
}
|
|
78
|
+
this.account = account;
|
|
79
|
+
storage_1.default.setAccount(account.address);
|
|
80
|
+
}
|
|
81
|
+
unsetAccount() {
|
|
82
|
+
this.account = undefined;
|
|
83
|
+
storage_1.default.setAccount("");
|
|
84
|
+
}
|
|
85
|
+
getDefaultAccount() {
|
|
86
|
+
const defaultAccount = storage_1.default.getAccount();
|
|
87
|
+
return this.accounts.find(acc => acc.address === defaultAccount);
|
|
88
|
+
}
|
|
89
|
+
setDefaultAccount() {
|
|
90
|
+
const defaultAccount = storage_1.default.getAccount();
|
|
91
|
+
if (defaultAccount) {
|
|
92
|
+
this.setAccount(defaultAccount);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async createAccount() {
|
|
96
|
+
const accountCreator = await AccountCreator_1.AccountCreator.create(this.wsProvider, this.accountCreatorConfig, this.source);
|
|
97
|
+
const storedAccount = storage_1.default.getAccount();
|
|
98
|
+
const account = await accountCreator.createAccount(undefined, storedAccount);
|
|
99
|
+
await this.setAccounts([account]);
|
|
100
|
+
this.setAccount(account.address);
|
|
101
|
+
return account;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
exports.ExtensionWeb2 = ExtensionWeb2;
|
|
105
|
+
exports.default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Q,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,OAAO,KAAK,cAAc,CAAC,CAAC;IACrE,CAAC;IAEM,iBAAiB;QACpB,MAAM,cAAc,GAAG,iBAAO,CAAC,UAAU,EAAE,CAAC;QAC5C,IAAI,cAAc,EAAE;YAChB,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,CAAC;SACnC;IACL,CAAC;IAEM,KAAK,CAAC,aAAa;QACtB,MAAM,cAAc,GAAG,MAAM,+BAAc,CAAC,MAAM,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,oBAAoB,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QAC5G,MAAM,aAAa,GAAG,iBAAO,CAAC,UAAU,EAAE,CAAC;QAC3C,MAAM,OAAO,GAAG,MAAM,cAAc,CAAC,aAAa,CAAC,SAAS,EAAE,aAAa,CAAC,CAAA;QAC5E,MAAM,IAAI,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAAC,CAAA;QACjC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,OAAO,CAAC,CAAA;QAChC,OAAO,OAAO,CAAA;IAClB,CAAC;CAEJ;AAjHD,sCAiHC;AAED,kBAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { InjectedAccountWithMeta, InjectedExtension } from "@polkadot/extension-inject/types";
|
|
2
|
+
import { IExtensionInterface } from "../types/client";
|
|
3
|
+
import AsyncFactory from "./AsyncFactory";
|
|
4
|
+
/**
|
|
5
|
+
* Class to discover web3 accounts from browser extensions
|
|
6
|
+
*/
|
|
7
|
+
export declare class ExtensionWeb3 extends AsyncFactory implements IExtensionInterface {
|
|
8
|
+
private extension?;
|
|
9
|
+
private account;
|
|
10
|
+
private accounts;
|
|
11
|
+
private injectedExtensions;
|
|
12
|
+
private source;
|
|
13
|
+
init(source: string): Promise<this>;
|
|
14
|
+
checkExtension(): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Get the extension for the selected account.
|
|
17
|
+
* @returns the extension holding the selected account. Undefined if no account selected.
|
|
18
|
+
*/
|
|
19
|
+
getExtension(): InjectedExtension | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Set the extension for the selected account.
|
|
22
|
+
*/
|
|
23
|
+
private setExtension;
|
|
24
|
+
/**
|
|
25
|
+
* Get all accounts across all extensions.
|
|
26
|
+
* @returns all accounts across all extensions.
|
|
27
|
+
*/
|
|
28
|
+
getAccounts(): InjectedAccountWithMeta[];
|
|
29
|
+
/**
|
|
30
|
+
* Discover accounts across all extensions.
|
|
31
|
+
*/
|
|
32
|
+
private setAccounts;
|
|
33
|
+
/**
|
|
34
|
+
* Get the selected account.
|
|
35
|
+
* @returns the selected account.
|
|
36
|
+
*/
|
|
37
|
+
getAccount(): InjectedAccountWithMeta | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Set the selected account.
|
|
40
|
+
* @param address the address of the account to be selected.
|
|
41
|
+
*/
|
|
42
|
+
setAccount(address: string): void;
|
|
43
|
+
unsetAccount(): void;
|
|
44
|
+
/**
|
|
45
|
+
* Get the last used account via the address held in local storage.
|
|
46
|
+
* @returns the account associated with the address held in storage.
|
|
47
|
+
*/
|
|
48
|
+
getDefaultAccount(): InjectedAccountWithMeta | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Select the account using the last used account address held in local storage.
|
|
51
|
+
*/
|
|
52
|
+
setDefaultAccount(): void;
|
|
53
|
+
createAccount(): Promise<undefined>;
|
|
54
|
+
}
|
|
55
|
+
export default ExtensionWeb3;
|
|
56
|
+
//# sourceMappingURL=ExtensionWeb3.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExtensionWeb3.d.ts","sourceRoot":"","sources":["../../src/api/ExtensionWeb3.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,uBAAuB,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAA;AAE7F,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,YAAY,MAAM,gBAAgB,CAAC;AAG1C;;GAEG;AACH,qBAAa,aAAc,SAAQ,YAAa,YAAW,mBAAmB;IAG1E,OAAO,CAAC,SAAS,CAAC,CAAoB;IAEtC,OAAO,CAAC,OAAO,CAAsC;IAErD,OAAO,CAAC,QAAQ,CAA4B;IAE5C,OAAO,CAAC,kBAAkB,CAAsB;IAChD,OAAO,CAAC,MAAM,CAAQ;IAET,IAAI,CAAC,MAAM,EAAE,MAAM;IAQnB,cAAc;IAW3B;;;OAGG;IACI,YAAY;IAInB;;OAEG;YACW,YAAY;IAY1B;;;OAGG;IACI,WAAW;IAIlB;;OAEG;YACW,WAAW;IAczB;;;OAGG;IACI,UAAU;IAIjB;;;OAGG;IACI,UAAU,CAAC,OAAO,EAAE,MAAM;IAY1B,YAAY;IAMnB;;;OAGG;IACI,iBAAiB;IAKxB;;OAEG;IACI,iBAAiB;IAOX,aAAa;CAY7B;AAED,eAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ExtensionWeb3 = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
// Copyright (C) 2021-2022 Prosopo (UK) Ltd.
|
|
6
|
+
// This file is part of procaptcha <https://github.com/prosopo/procaptcha>.
|
|
7
|
+
//
|
|
8
|
+
// procaptcha is free software: you can redistribute it and/or modify
|
|
9
|
+
// it under the terms of the GNU General Public License as published by
|
|
10
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+
// (at your option) any later version.
|
|
12
|
+
//
|
|
13
|
+
// procaptcha is distributed in the hope that it will be useful,
|
|
14
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+
// GNU General Public License for more details.
|
|
17
|
+
//
|
|
18
|
+
// You should have received a copy of the GNU General Public License
|
|
19
|
+
// along with procaptcha. If not, see <http://www.gnu.org/licenses/>.
|
|
20
|
+
const extension_dapp_1 = require("@polkadot/extension-dapp");
|
|
21
|
+
const storage_1 = tslib_1.__importDefault(require("../modules/storage"));
|
|
22
|
+
const AsyncFactory_1 = tslib_1.__importDefault(require("./AsyncFactory"));
|
|
23
|
+
const contract_1 = require("@prosopo/contract");
|
|
24
|
+
/**
|
|
25
|
+
* Class to discover web3 accounts from browser extensions
|
|
26
|
+
*/
|
|
27
|
+
class ExtensionWeb3 extends AsyncFactory_1.default {
|
|
28
|
+
// the extension for the currently selected account, if any
|
|
29
|
+
extension;
|
|
30
|
+
// the currently selected account, if any
|
|
31
|
+
account;
|
|
32
|
+
// the discovered accounts
|
|
33
|
+
accounts;
|
|
34
|
+
// the discovered extensions
|
|
35
|
+
injectedExtensions;
|
|
36
|
+
source;
|
|
37
|
+
async init(source) {
|
|
38
|
+
this.source = source;
|
|
39
|
+
await this.checkExtension();
|
|
40
|
+
await this.setAccounts();
|
|
41
|
+
await this.setExtension();
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
async checkExtension() {
|
|
45
|
+
try {
|
|
46
|
+
this.injectedExtensions = await (0, extension_dapp_1.web3Enable)(this.source);
|
|
47
|
+
}
|
|
48
|
+
catch (err) {
|
|
49
|
+
throw new contract_1.ProsopoEnvError(err);
|
|
50
|
+
}
|
|
51
|
+
if (!this.injectedExtensions.length) {
|
|
52
|
+
throw new contract_1.ProsopoEnvError("WIDGET.NO_EXTENSION_FOUND");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Get the extension for the selected account.
|
|
57
|
+
* @returns the extension holding the selected account. Undefined if no account selected.
|
|
58
|
+
*/
|
|
59
|
+
getExtension() {
|
|
60
|
+
return this.extension;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Set the extension for the selected account.
|
|
64
|
+
*/
|
|
65
|
+
async setExtension() {
|
|
66
|
+
try {
|
|
67
|
+
// https://polkadot.js.org/docs/extension/cookbook/
|
|
68
|
+
this.extension = await (0, extension_dapp_1.web3FromSource)(this.accounts[0].meta.source);
|
|
69
|
+
}
|
|
70
|
+
catch (err) {
|
|
71
|
+
throw new contract_1.ProsopoEnvError(err);
|
|
72
|
+
}
|
|
73
|
+
if (!this.extension) {
|
|
74
|
+
throw new contract_1.ProsopoEnvError("WIDGET.EXTENSION_NOT_FOUND");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Get all accounts across all extensions.
|
|
79
|
+
* @returns all accounts across all extensions.
|
|
80
|
+
*/
|
|
81
|
+
getAccounts() {
|
|
82
|
+
return this.accounts;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Discover accounts across all extensions.
|
|
86
|
+
*/
|
|
87
|
+
async setAccounts() {
|
|
88
|
+
this.accounts = await (0, extension_dapp_1.web3Accounts)();
|
|
89
|
+
// get the last used account address from storage
|
|
90
|
+
const lastUsedAccountAddress = storage_1.default.getAccount();
|
|
91
|
+
// lookup this account in the discovered accounts
|
|
92
|
+
const account = this.accounts.find(acc => acc.address === lastUsedAccountAddress);
|
|
93
|
+
// if found then set it
|
|
94
|
+
if (account) {
|
|
95
|
+
this.account = account;
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
this.account = undefined;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Get the selected account.
|
|
103
|
+
* @returns the selected account.
|
|
104
|
+
*/
|
|
105
|
+
getAccount() {
|
|
106
|
+
return this.account;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Set the selected account.
|
|
110
|
+
* @param address the address of the account to be selected.
|
|
111
|
+
*/
|
|
112
|
+
setAccount(address) {
|
|
113
|
+
if (!this.accounts.length) {
|
|
114
|
+
throw new contract_1.ProsopoEnvError("WIDGET.NO_ACCOUNTS_FOUND");
|
|
115
|
+
}
|
|
116
|
+
const account = this.accounts.find(acc => acc.address === address);
|
|
117
|
+
if (!account) {
|
|
118
|
+
throw new contract_1.ProsopoEnvError("WIDGET.ACCOUNT_NOT_FOUND");
|
|
119
|
+
}
|
|
120
|
+
this.account = account;
|
|
121
|
+
storage_1.default.setAccount(account.address);
|
|
122
|
+
}
|
|
123
|
+
unsetAccount() {
|
|
124
|
+
this.account = undefined;
|
|
125
|
+
// update the last used account to empty
|
|
126
|
+
storage_1.default.setAccount("");
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Get the last used account via the address held in local storage.
|
|
130
|
+
* @returns the account associated with the address held in storage.
|
|
131
|
+
*/
|
|
132
|
+
getDefaultAccount() {
|
|
133
|
+
const defaultAccount = storage_1.default.getAccount();
|
|
134
|
+
return this.accounts.find(acc => acc.address === defaultAccount);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Select the account using the last used account address held in local storage.
|
|
138
|
+
*/
|
|
139
|
+
setDefaultAccount() {
|
|
140
|
+
const defaultAccount = storage_1.default.getAccount();
|
|
141
|
+
if (defaultAccount) {
|
|
142
|
+
this.setAccount(defaultAccount);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
async createAccount() {
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
exports.ExtensionWeb3 = ExtensionWeb3;
|
|
150
|
+
exports.default = ExtensionWeb3;
|
|
151
|
+
//# sourceMappingURL=ExtensionWeb3.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExtensionWeb3.js","sourceRoot":"","sources":["../../src/api/ExtensionWeb3.ts"],"names":[],"mappings":";;;;AAAA,4CAA4C;AAC5C,2EAA2E;AAC3E,EAAE;AACF,qEAAqE;AACrE,uEAAuE;AACvE,oEAAoE;AACpE,sCAAsC;AACtC,EAAE;AACF,gEAAgE;AAChE,iEAAiE;AACjE,gEAAgE;AAChE,+CAA+C;AAC/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AAS,CAAC;QACzB,wCAAwC;QACxC,iBAAO,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IAC3B,CAAC;IAED;;;OAGG;IACI,iBAAiB;QACpB,MAAM,cAAc,GAAG,iBAAO,CAAC,UAAU,EAAE,CAAC;QAC5C,OAAO,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,OAAO,KAAK,cAAc,CAAC,CAAC;IACrE,CAAC;IAED;;OAEG;IACI,iBAAiB;QACpB,MAAM,cAAc,GAAG,iBAAO,CAAC,UAAU,EAAE,CAAC;QAC5C,IAAI,cAAc,EAAE;YAChB,IAAI,CAAC,UAAU,CAAC,cAAc,CAAC,CAAC;SACnC;IACL,CAAC;IAEM,KAAK,CAAC,aAAa;QACtB,OAAO,SAAS,CAAA;IACpB,CAAC;CAUJ;AA5ID,sCA4IC;AAED,kBAAe,aAAa,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AxiosInstance, AxiosResponse } from "axios";
|
|
2
|
+
export declare class HttpClientBase {
|
|
3
|
+
protected readonly axios: AxiosInstance;
|
|
4
|
+
constructor(baseURL: string, prefix?: string);
|
|
5
|
+
protected responseHandler: (response: AxiosResponse) => any;
|
|
6
|
+
protected errorHandler: (error: any) => Promise<never>;
|
|
7
|
+
}
|
|
8
|
+
export default HttpClientBase;
|
|
9
|
+
//# sourceMappingURL=HttpClientBase.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HttpClientBase.d.ts","sourceRoot":"","sources":["../../src/api/HttpClientBase.ts"],"names":[],"mappings":"AAeA,OAAc,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,OAAO,CAAC;AAE5D,qBAAa,cAAc;IAEvB,SAAS,CAAC,QAAQ,CAAC,KAAK,EAAE,aAAa,CAAC;gBAE5B,OAAO,EAAE,MAAM,EAAE,MAAM,SAAK;IAMxC,SAAS,CAAC,eAAe,aAAc,aAAa,SAGnD;IAED,SAAS,CAAC,YAAY,UAAW,GAAG,oBAAoC;CAE3E;AAED,eAAe,cAAc,CAAC"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HttpClientBase = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
// Copyright (C) 2021-2022 Prosopo (UK) Ltd.
|
|
6
|
+
// This file is part of procaptcha <https://github.com/prosopo/procaptcha>.
|
|
7
|
+
//
|
|
8
|
+
// procaptcha is free software: you can redistribute it and/or modify
|
|
9
|
+
// it under the terms of the GNU General Public License as published by
|
|
10
|
+
// the Free Software Foundation, either version 3 of the License, or
|
|
11
|
+
// (at your option) any later version.
|
|
12
|
+
//
|
|
13
|
+
// procaptcha is distributed in the hope that it will be useful,
|
|
14
|
+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
+
// GNU General Public License for more details.
|
|
17
|
+
//
|
|
18
|
+
// You should have received a copy of the GNU General Public License
|
|
19
|
+
// along with procaptcha. If not, see <http://www.gnu.org/licenses/>.
|
|
20
|
+
const axios_1 = tslib_1.__importDefault(require("axios"));
|
|
21
|
+
class HttpClientBase {
|
|
22
|
+
axios;
|
|
23
|
+
constructor(baseURL, prefix = "") {
|
|
24
|
+
baseURL = baseURL + prefix;
|
|
25
|
+
this.axios = axios_1.default.create({ baseURL });
|
|
26
|
+
this.axios.interceptors.response.use(this.responseHandler, this.errorHandler);
|
|
27
|
+
}
|
|
28
|
+
responseHandler = (response) => {
|
|
29
|
+
console.log("API REQUEST", response.request);
|
|
30
|
+
return response.data;
|
|
31
|
+
};
|
|
32
|
+
errorHandler = (error) => Promise.reject(error.response);
|
|
33
|
+
}
|
|
34
|
+
exports.HttpClientBase = HttpClientBase;
|
|
35
|
+
exports.default = HttpClientBase;
|
|
36
|
+
//# sourceMappingURL=HttpClientBase.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HttpClientBase.js","sourceRoot":"","sources":["../../src/api/HttpClientBase.ts"],"names":[],"mappings":";;;;AAAA,4CAA4C;AAC5C,2EAA2E;AAC3E,EAAE;AACF,qEAAqE;AACrE,uEAAuE;AACvE,oEAAoE;AACpE,sCAAsC;AACtC,EAAE;AACF,gEAAgE;AAChE,iEAAiE;AACjE,gEAAgE;AAChE,+CAA+C;AAC/C,EAAE;AACF,oEAAoE;AACpE,sEAAsE;AACtE,0DAA4D;AAE5D,MAAa,cAAc;IAEJ,KAAK,CAAgB;IAExC,YAAY,OAAe,EAAE,MAAM,GAAG,EAAE;QACpC,OAAO,GAAG,OAAO,GAAG,MAAM,CAAC;QAC3B,IAAI,CAAC,KAAK,GAAG,eAAK,CAAC,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC;QACvC,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,eAAe,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC;IAClF,CAAC;IAES,eAAe,GAAG,CAAC,QAAuB,EAAE,EAAE;QACpD,OAAO,CAAC,GAAG,CAAC,aAAa,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC;QAC7C,OAAO,QAAQ,CAAC,IAAI,CAAC;IACzB,CAAC,CAAA;IAES,YAAY,GAAG,CAAC,KAAU,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;CAE3E;AAjBD,wCAiBC;AAED,kBAAe,cAAc,CAAC"}
|