@prosopo/procaptcha-frictionless 0.3.2 → 0.3.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.
@@ -1,3 +1,3 @@
1
- import { ProcaptchaProps } from '@prosopo/procaptcha-pow';
1
+ import { ProcaptchaProps } from '@prosopo/types';
2
2
  export declare const ProcaptchaFrictionless: ({ config, callbacks }: ProcaptchaProps) => import("react/jsx-runtime").JSX.Element;
3
3
  //# sourceMappingURL=ProcaptchaFrictionless.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"ProcaptchaFrictionless.d.ts","sourceRoot":"","sources":["../src/ProcaptchaFrictionless.tsx"],"names":[],"mappings":"AAeA,OAAO,EAAiB,eAAe,EAAE,MAAM,yBAAyB,CAAA;AAIxE,eAAO,MAAM,sBAAsB,0BAA2B,eAAe,4CAmB5E,CAAA"}
1
+ {"version":3,"file":"ProcaptchaFrictionless.d.ts","sourceRoot":"","sources":["../src/ProcaptchaFrictionless.tsx"],"names":[],"mappings":"AAgBA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AAIhD,eAAO,MAAM,sBAAsB,0BAA2B,eAAe,4CAmB5E,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"file":"ProcaptchaFrictionless.js","sourceRoot":"","sources":["../src/ProcaptchaFrictionless.tsx"],"names":[],"mappings":";AAaA,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;AACtD,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AAC/D,OAAO,EAAE,aAAa,EAAmB,MAAM,yBAAyB,CAAA;AACxE,OAAO,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAA;AAC1C,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAE3C,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,EAAE,MAAM,EAAE,SAAS,EAAmB,EAAE,EAAE;IAE7E,MAAM,CAA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C,QAAQ,EAAE,MAAM,CAAC,KAAK,GAAI,CAAC,CAAA;IAE7G,SAAS,CAAC,GAAG,EAAE;QACX,MAAM,SAAS,GAAG,KAAK,IAAI,EAAE;YACzB,MAAM,IAAI,GAAG,MAAM,IAAI,EAAE,CAAA;YACzB,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,CAAA;YAC5B,IAAI,MAAM,CAAC,GAAG,EAAE;gBACZ,oBAAoB,CAAC,KAAC,UAAU,IAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,GAAI,CAAC,CAAA;aAC7E;iBAAM;gBACH,oBAAoB,CAAC,KAAC,aAAa,IAAC,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,GAAI,CAAC,CAAA;aAChF;QACL,CAAC,CAAA;QAED,SAAS,EAAE,CAAA;IACf,CAAC,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAA;IAEvB,OAAO,iBAAiB,CAAA;AAC5B,CAAC,CAAA"}
@@ -1,29 +1,47 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const types = require("@prosopo/types");
3
4
  const Api = require("@polkadot/api/promise/Api");
4
5
  require("../../../account/dist/index.cjs");
5
6
  const keyring = require("@polkadot/keyring");
6
- const types = require("@prosopo/types");
7
7
  const contract = require("@prosopo/contract");
8
8
  const common = require("@prosopo/common");
9
9
  const api = require("@prosopo/api");
10
10
  const ws = require("@polkadot/rpc-provider/ws");
11
11
  const contractInfo = require("@prosopo/captcha-contract/contract-info");
12
+ const procaptchaCommon = require("@prosopo/procaptcha-common");
13
+ const procaptcha = require("@prosopo/procaptcha");
12
14
  const SolverService = require("./SolverService.cjs");
13
15
  const ExtensionWeb2 = require("../../../account/dist/extension/ExtensionWeb2.cjs");
14
- const Manager = async (configInput) => {
16
+ const Manager = (configInput, state, onStateUpdate, callbacks) => {
17
+ const defaultState = () => {
18
+ return {
19
+ showModal: false,
20
+ loading: false,
21
+ index: 0,
22
+ challenge: void 0,
23
+ solutions: void 0,
24
+ isHuman: false,
25
+ captchaApi: void 0,
26
+ account: void 0
27
+ };
28
+ };
29
+ const clearTimeout = () => {
30
+ window.clearTimeout(state.timeout);
31
+ updateState({ timeout: void 0 });
32
+ };
15
33
  const getConfig = () => {
16
- const config2 = {
34
+ const config = {
17
35
  userAccountAddress: "",
18
36
  ...configInput
19
37
  };
20
- return types.ProcaptchaConfigSchema.parse(config2);
38
+ return types.ProcaptchaConfigSchema.parse(config);
21
39
  };
22
- const getNetwork = (config2) => {
23
- const network = config2.networks[config2.defaultNetwork];
40
+ const getNetwork = (config) => {
41
+ const network = config.networks[config.defaultNetwork];
24
42
  if (!network) {
25
43
  throw new common.ProsopoEnvError("DEVELOPER.NETWORK_NOT_FOUND", {
26
- context: { error: `No network found for environment ${config2.defaultEnvironment}` }
44
+ context: { error: `No network found for environment ${config.defaultEnvironment}` }
27
45
  });
28
46
  }
29
47
  return network;
@@ -35,25 +53,71 @@ const Manager = async (configInput) => {
35
53
  const keyring$1 = new keyring.Keyring({ type, ss58Format: api2.registry.chainSS58 });
36
54
  return new contract.ProsopoCaptchaContract(api2, JSON.parse(contractInfo.ContractAbi), network.contract.address, "prosopo", 0, keyring$1.addFromAddress(getConfig().account.address || ""));
37
55
  };
38
- const config = getConfig();
39
- if (!config.web2 && !config.userAccountAddress) {
40
- throw new common.ProsopoEnvError("GENERAL.ACCOUNT_NOT_FOUND", {
41
- context: { error: "Account address has not been set for web3 mode" }
42
- });
43
- }
44
- const ext = new ExtensionWeb2.ExtensionWeb2();
45
- const account = await ext.getAccount(config);
46
- const contract$1 = await loadContract();
47
- const getRandomProviderResponse = await contract.wrapQuery(contract$1.query.getRandomActiveProvider, contract$1.query)(account.account.address, configInput.account.address || "");
48
- const providerUrl = common.trimProviderUrl(getRandomProviderResponse.provider.url.toString());
49
- const providerApi = new api.ProviderApi(getNetwork(getConfig()), providerUrl, configInput.account.address || "");
50
- const challenge = await providerApi.getPowCaptchaChallenge(account.account.address, configInput.account.address || "");
51
- console.log("challenge", challenge);
52
- console.log("challenge", challenge.challenge);
53
- console.log("challenge", challenge.difficulty);
54
- const solution = SolverService.solvePoW(challenge.challenge, challenge.difficulty);
55
- console.log("solution", solution);
56
- const verifiedSolution = await providerApi.submitPowCaptchaSolution(challenge, account.account.address, configInput.account.address || "", getRandomProviderResponse, solution);
57
- return verifiedSolution;
56
+ const getAccount = () => {
57
+ if (!state.account) {
58
+ throw new common.ProsopoEnvError("GENERAL.ACCOUNT_NOT_FOUND", { context: { error: "Account not loaded" } });
59
+ }
60
+ const account = state.account;
61
+ return account;
62
+ };
63
+ const getDappAccount = () => {
64
+ if (!state.dappAccount) {
65
+ throw new common.ProsopoEnvError("GENERAL.SITE_KEY_MISSING");
66
+ }
67
+ const dappAccount = state.dappAccount;
68
+ return dappAccount;
69
+ };
70
+ const getBlockNumber = () => {
71
+ if (!state.blockNumber) {
72
+ throw new common.ProsopoContractError("CAPTCHA.INVALID_BLOCK_NO", { context: { error: "Block number not found" } });
73
+ }
74
+ const blockNumber = state.blockNumber;
75
+ return blockNumber;
76
+ };
77
+ const updateState = procaptchaCommon.buildUpdateState(state, onStateUpdate);
78
+ const resetState = () => {
79
+ clearTimeout();
80
+ updateState(defaultState());
81
+ };
82
+ const start = async () => {
83
+ if (state.loading) {
84
+ return;
85
+ }
86
+ if (state.isHuman) {
87
+ return;
88
+ }
89
+ resetState();
90
+ updateState({ loading: true });
91
+ const config = getConfig();
92
+ updateState({ dappAccount: config.account.address });
93
+ await procaptcha.sleep(100);
94
+ if (!config.web2 && !config.userAccountAddress) {
95
+ throw new common.ProsopoEnvError("GENERAL.ACCOUNT_NOT_FOUND", {
96
+ context: { error: "Account address has not been set for web3 mode" }
97
+ });
98
+ }
99
+ const ext = new ExtensionWeb2.ExtensionWeb2();
100
+ const account = await ext.getAccount(config);
101
+ const contract$1 = await loadContract();
102
+ const events = procaptchaCommon.getDefaultEvents(onStateUpdate, state, callbacks);
103
+ const getRandomProviderResponse = await contract.wrapQuery(contract$1.query.getRandomActiveProvider, contract$1.query)(account.account.address, getDappAccount());
104
+ const providerUrl = common.trimProviderUrl(getRandomProviderResponse.provider.url.toString());
105
+ const providerApi = new api.ProviderApi(getNetwork(getConfig()), providerUrl, getDappAccount());
106
+ const challenge = await providerApi.getPowCaptchaChallenge(account.account.address, getDappAccount());
107
+ const solution = SolverService.solvePoW(challenge.challenge, challenge.difficulty);
108
+ await providerApi.submitPowCaptchaSolution(challenge, getAccount().account.address, getDappAccount(), getRandomProviderResponse, solution);
109
+ if (state.isHuman) {
110
+ events.onHuman({
111
+ providerUrl,
112
+ [types.ApiParams.user]: getAccount().account.address,
113
+ [types.ApiParams.dapp]: getDappAccount(),
114
+ [types.ApiParams.challengeId]: challenge.challenge,
115
+ [types.ApiParams.blockNumber]: getBlockNumber()
116
+ });
117
+ }
118
+ };
119
+ return {
120
+ start
121
+ };
58
122
  };
59
123
  exports.Manager = Manager;
@@ -2,6 +2,7 @@
2
2
  const jsxRuntime = require("@emotion/react/jsx-runtime");
3
3
  require("../../../web-components/dist/index.cjs");
4
4
  const Manager = require("../Services/Manager.cjs");
5
+ const procaptchaCommon = require("@prosopo/procaptcha-common");
5
6
  const react = require("react");
6
7
  const theme = require("../../../web-components/dist/theme.cjs");
7
8
  const Containers = require("../../../web-components/dist/Containers.cjs");
@@ -10,21 +11,13 @@ const LoadingSpinner = require("../../../web-components/dist/LoadingSpinner.cjs"
10
11
  const Checkbox = require("../../../web-components/dist/Checkbox.cjs");
11
12
  const Logo = require("../../../web-components/dist/Logo.cjs");
12
13
  const Procaptcha = (props) => {
13
- const [checked, setChecked] = react.useState(false);
14
- const [loading, setLoading] = react.useState(false);
15
- const darkMode = props.config.theme;
16
- const themeColor = darkMode ? "light" : "dark";
17
- const theme$1 = react.useMemo(() => darkMode === "light" ? theme.lightTheme : theme.darkTheme, [darkMode]);
18
- const handlePowCaptcha = async () => {
19
- setLoading(true);
20
- Manager.Manager(props.config).then((verified) => {
21
- if (verified.verified) {
22
- console.log("verified");
23
- setChecked(true);
24
- }
25
- setLoading(false);
26
- });
27
- };
14
+ const config = props.config;
15
+ const themeColor = config.theme === "light" ? "light" : "dark";
16
+ const theme$1 = props.config.theme === "light" ? theme.lightTheme : theme.darkTheme;
17
+ const callbacks = props.callbacks || {};
18
+ const [state, _updateState] = procaptchaCommon.useProcaptcha(react.useState, react.useRef);
19
+ const updateState = procaptchaCommon.buildUpdateState(state, _updateState);
20
+ const manager = Manager.Manager(config, state, updateState, callbacks);
28
21
  return jsxRuntime.jsx("div", { children: jsxRuntime.jsx("div", { style: { maxWidth: "100%", maxHeight: "100%", overflowX: "auto" }, children: jsxRuntime.jsx(Containers.ContainerDiv, { children: jsxRuntime.jsx(Containers.WidthBasedStylesDiv, { children: jsxRuntime.jsxs("div", { style: WidgetConstants.WIDGET_DIMENSIONS, "data-cy": "button-human", children: [" ", jsxRuntime.jsxs("div", { style: {
29
22
  padding: "2px",
30
23
  border: "1px solid",
@@ -50,6 +43,6 @@ const Procaptcha = (props) => {
50
43
  verticalAlign: "middle"
51
44
  }, children: jsxRuntime.jsx("div", { style: {
52
45
  display: "flex"
53
- }, children: jsxRuntime.jsx("div", { style: { flex: 1 }, children: loading ? jsxRuntime.jsx(LoadingSpinner.LoadingSpinner, { themeColor }) : jsxRuntime.jsx(Checkbox.Checkbox, { checked, onChange: handlePowCaptcha, themeColor, labelText: "I am human" }) }) }) }) }) }), jsxRuntime.jsx("div", { style: { display: "inline-flex", flexDirection: "column" }, children: jsxRuntime.jsx("a", { href: WidgetConstants.WIDGET_URL, target: "_blank", "aria-label": WidgetConstants.WIDGET_URL_TEXT, children: jsxRuntime.jsx("div", { style: { flex: 1 }, children: jsxRuntime.jsx(Logo, { themeColor }) }) }) })] })] }) }) }) }) });
46
+ }, children: jsxRuntime.jsx("div", { style: { flex: 1 }, children: state.loading ? jsxRuntime.jsx(LoadingSpinner.LoadingSpinner, { themeColor }) : jsxRuntime.jsx(Checkbox.Checkbox, { checked: state.isHuman, onChange: manager.start, themeColor, labelText: "I am human" }) }) }) }) }) }), jsxRuntime.jsx("div", { style: { display: "inline-flex", flexDirection: "column" }, children: jsxRuntime.jsx("a", { href: WidgetConstants.WIDGET_URL, target: "_blank", "aria-label": WidgetConstants.WIDGET_URL_TEXT, children: jsxRuntime.jsx("div", { style: { flex: 1 }, children: jsxRuntime.jsx(Logo, { themeColor }) }) }) })] })] }) }) }) }) });
54
47
  };
55
48
  module.exports = Procaptcha;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const jsxRuntime = require("@emotion/react/jsx-runtime");
4
- require("../../../web-components/dist/index.cjs");
5
4
  const react = require("react");
5
+ require("../../../web-components/dist/index.cjs");
6
6
  const CaptchaPlaceholder = require("../../../web-components/dist/CaptchaPlaceholder.cjs");
7
7
  const _interopNamespaceDefaultOnly = (e) => Object.freeze(Object.defineProperty({ __proto__: null, default: e }, Symbol.toStringTag, { value: "Module" }));
8
8
  const ProcaptchaWidget = react.lazy(async () => Promise.resolve().then(() => /* @__PURE__ */ _interopNamespaceDefaultOnly(require("./Captcha.cjs"))));
@@ -5,12 +5,10 @@ const Containers = require("./Containers.cjs");
5
5
  const LoadingSpinner = require("./LoadingSpinner.cjs");
6
6
  const WidgetConstants = require("./WidgetConstants.cjs");
7
7
  const theme = require("./theme.cjs");
8
- const react = require("react");
9
8
  const Logo = require("./Logo.cjs");
10
9
  const ProcaptchaPlaceholder = (props) => {
11
- const darkMode = props.darkMode;
12
10
  const themeColor = props.darkMode === "light" ? "light" : "dark";
13
- const theme$1 = react.useMemo(() => darkMode === "light" ? theme.lightTheme : theme.darkTheme, [darkMode]);
11
+ const theme$1 = props.darkMode === "light" ? theme.lightTheme : theme.darkTheme;
14
12
  return jsxRuntime.jsx("div", { children: jsxRuntime.jsx("div", { style: { maxWidth: "100%", maxHeight: "100%", overflowX: "auto" }, children: jsxRuntime.jsx(Containers.ContainerDiv, { children: jsxRuntime.jsx(Containers.WidthBasedStylesDiv, { children: jsxRuntime.jsxs("div", { style: WidgetConstants.WIDGET_DIMENSIONS, "data-cy": "button-human", children: [" ", jsxRuntime.jsxs("div", { style: {
15
13
  padding: "2px",
16
14
  border: "1px solid",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prosopo/procaptcha-frictionless",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "author": "PROSOPO LIMITED <info@prosopo.io>",
5
5
  "license": "Apache-2.0",
6
6
  "main": "./dist/index.js",
@@ -34,9 +34,9 @@
34
34
  ],
35
35
  "dependencies": {
36
36
  "@fingerprintjs/botd": "^1.9.0",
37
- "@prosopo/procaptcha-pow": "0.3.2",
38
- "@prosopo/procaptcha-react": "0.3.2",
39
- "@prosopo/web-components": "0.3.2",
37
+ "@prosopo/procaptcha-pow": "0.3.3",
38
+ "@prosopo/procaptcha-react": "0.3.3",
39
+ "@prosopo/web-components": "0.3.3",
40
40
  "react": "^18.2.0"
41
41
  },
42
42
  "devDependencies": {