@prosopo/server 0.2.0 → 0.2.1

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.
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const api$1 = require("@polkadot/api");
4
+ const types = require("@prosopo/types");
5
+ const keyring = require("@polkadot/keyring");
6
+ const common = require("@prosopo/common");
7
+ const contract = require("@prosopo/contract");
8
+ const api = require("@prosopo/api");
9
+ const rpcProvider = require("@polkadot/rpc-provider");
10
+ const util = require("@prosopo/util");
11
+ class ProsopoServer {
12
+ constructor(pair, config) {
13
+ this.config = config;
14
+ this.pair = pair;
15
+ if (this.config.defaultEnvironment && Object.prototype.hasOwnProperty.call(this.config.networks, this.config.defaultEnvironment)) {
16
+ this.defaultEnvironment = this.config.defaultEnvironment;
17
+ const networkName = types.NetworkNamesSchema.parse(this.defaultEnvironment);
18
+ this.network = util.get(this.config.networks, networkName);
19
+ this.wsProvider = new rpcProvider.WsProvider(this.network.endpoint);
20
+ this.prosopoContractAddress = this.network.contract.address;
21
+ this.dappContractAddress = this.config.account.address;
22
+ this.contractName = this.network.contract.name;
23
+ this.logger = common.getLogger(this.config.logLevel, "@prosopo/server");
24
+ this.keyring = new keyring.Keyring({
25
+ type: "sr25519"
26
+ // TODO get this from the chain
27
+ });
28
+ this.abi = contract.abiJson;
29
+ } else {
30
+ throw new common.ProsopoEnvError(
31
+ "CONFIG.UNKNOWN_ENVIRONMENT",
32
+ this.constructor.name,
33
+ {},
34
+ this.config.defaultEnvironment
35
+ );
36
+ }
37
+ }
38
+ async getProviderApi(providerUrl) {
39
+ return new api.ProviderApi(this.network, providerUrl, this.config.account.address);
40
+ }
41
+ async isReady() {
42
+ try {
43
+ this.api = await api$1.ApiPromise.create({ provider: this.wsProvider });
44
+ await this.getSigner();
45
+ await this.getContractApi();
46
+ } catch (err) {
47
+ throw new common.ProsopoEnvError(err, "GENERAL.ENVIRONMENT_NOT_READY");
48
+ }
49
+ }
50
+ async getSigner() {
51
+ if (!this.api) {
52
+ this.api = await api$1.ApiPromise.create({ provider: this.wsProvider });
53
+ }
54
+ await this.api.isReadyOrError;
55
+ try {
56
+ this.pair = this.keyring.addPair(this.pair);
57
+ } catch (err) {
58
+ throw new common.ProsopoEnvError("CONTRACT.SIGNER_UNDEFINED", this.getSigner.name, {}, err);
59
+ }
60
+ }
61
+ getApi() {
62
+ if (this.api === void 0) {
63
+ throw new common.ProsopoEnvError(new Error("api undefined"));
64
+ }
65
+ return this.api;
66
+ }
67
+ getContract() {
68
+ if (this.contract === void 0) {
69
+ throw new common.ProsopoEnvError(new Error("contract undefined"));
70
+ }
71
+ return this.contract;
72
+ }
73
+ async isVerified(payload) {
74
+ const { user, dapp, providerUrl, commitmentId, blockNumber } = payload;
75
+ const contractApi = await this.getContractApi();
76
+ const block = await this.getApi().rpc.chain.getBlockHash(blockNumber);
77
+ const getRandomProviderResponse = await this.getContract().queryAtBlock(
78
+ block,
79
+ "getRandomActiveProvider",
80
+ [user, dapp]
81
+ );
82
+ const providerUrlTrimmed = common.trimProviderUrl(getRandomProviderResponse.provider.url.toString());
83
+ if (providerUrlTrimmed !== providerUrl) {
84
+ return false;
85
+ }
86
+ console.log("providerUrlTrimmed", providerUrlTrimmed, "commitmentId", commitmentId);
87
+ if (providerUrlTrimmed && commitmentId) {
88
+ const providerApi = await this.getProviderApi(providerUrl);
89
+ const result = await providerApi.verifyDappUser(user, commitmentId);
90
+ console.log(result);
91
+ return result.solutionApproved;
92
+ } else {
93
+ return (await contractApi.query.dappOperatorIsHumanUser(user, this.config.solutionThreshold)).value.unwrap().unwrap();
94
+ }
95
+ }
96
+ async getContractApi() {
97
+ this.contract = new contract.ProsopoCaptchaContract(
98
+ this.getApi(),
99
+ this.abi,
100
+ this.prosopoContractAddress,
101
+ this.pair,
102
+ this.contractName,
103
+ 0
104
+ );
105
+ return this.contract;
106
+ }
107
+ }
108
+ exports.ProsopoServer = ProsopoServer;
package/package.json CHANGED
@@ -1,12 +1,19 @@
1
1
  {
2
2
  "name": "@prosopo/server",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "NodeJS package for server side communication with the prosopo captcha client",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "require": "./dist/cjs/index.cjs"
11
+ }
12
+ },
7
13
  "scripts": {
8
14
  "clean": "tsc --build --clean",
9
- "build": "tsc --build --verbose",
15
+ "build": "tsc --build --verbose tsconfig.json",
16
+ "build:cjs": "npx vite --config vite.cjs.config.ts build",
10
17
  "lint": "npx eslint .",
11
18
  "lint:fix": "npx eslint . --fix --config ../../.eslintrc.js"
12
19
  },
@@ -26,10 +33,10 @@
26
33
  "@polkadot/keyring": "12.3.2",
27
34
  "@polkadot/rpc-provider": "10.9.1",
28
35
  "@polkadot/types": "10.9.1",
29
- "@prosopo/api": "0.2.0",
30
- "@prosopo/contract": "0.2.0",
31
- "@prosopo/procaptcha": "0.2.0",
32
- "@prosopo/types": "0.2.0"
36
+ "@prosopo/api": "0.2.1",
37
+ "@prosopo/contract": "0.2.1",
38
+ "@prosopo/procaptcha": "0.2.1",
39
+ "@prosopo/types": "0.2.1"
33
40
  },
34
41
  "devDependencies": {
35
42
  "tslib": "^2.6.2",
@@ -0,0 +1,6 @@
1
+ import { ViteCommonJSConfig } from '@prosopo/config'
2
+ import path from 'path'
3
+
4
+ export default function () {
5
+ return ViteCommonJSConfig('server', path.resolve('./tsconfig.cjs.json'))
6
+ }