@prosopo/server 0.1.18 → 0.2.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/dist/index.d.ts +33 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +111 -0
- package/dist/index.js.map +1 -0
- package/package.json +5 -5
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ApiPromise } from '@polkadot/api';
|
|
2
|
+
import { ContractAbi, NetworkConfig, ProsopoServerConfig } from '@prosopo/types';
|
|
3
|
+
import { Keyring } from '@polkadot/keyring';
|
|
4
|
+
import { KeyringPair } from '@polkadot/keyring/types';
|
|
5
|
+
import { Logger } from '@prosopo/common';
|
|
6
|
+
import { ProcaptchaOutput } from '@prosopo/types';
|
|
7
|
+
import { ProsopoCaptchaContract } from '@prosopo/contract';
|
|
8
|
+
import { ProviderApi } from '@prosopo/api';
|
|
9
|
+
import { WsProvider } from '@polkadot/rpc-provider';
|
|
10
|
+
export declare class ProsopoServer {
|
|
11
|
+
config: ProsopoServerConfig;
|
|
12
|
+
contract: ProsopoCaptchaContract | undefined;
|
|
13
|
+
prosopoContractAddress: string;
|
|
14
|
+
dappContractAddress: string;
|
|
15
|
+
defaultEnvironment: string;
|
|
16
|
+
contractName: string;
|
|
17
|
+
abi: ContractAbi;
|
|
18
|
+
logger: Logger;
|
|
19
|
+
wsProvider: WsProvider;
|
|
20
|
+
keyring: Keyring;
|
|
21
|
+
pair: KeyringPair;
|
|
22
|
+
api: ApiPromise | undefined;
|
|
23
|
+
network: NetworkConfig;
|
|
24
|
+
constructor(pair: KeyringPair, config: ProsopoServerConfig);
|
|
25
|
+
getProviderApi(providerUrl: string): Promise<ProviderApi>;
|
|
26
|
+
isReady(): Promise<void>;
|
|
27
|
+
getSigner(): Promise<void>;
|
|
28
|
+
getApi(): ApiPromise;
|
|
29
|
+
getContract(): ProsopoCaptchaContract;
|
|
30
|
+
isVerified(payload: ProcaptchaOutput): Promise<boolean>;
|
|
31
|
+
getContractApi(): Promise<ProsopoCaptchaContract>;
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAE1C,OAAO,EAAE,WAAW,EAAE,aAAa,EAAsB,mBAAmB,EAAkB,MAAM,gBAAgB,CAAA;AACpH,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAA;AAC3C,OAAO,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAA;AACrD,OAAO,EAAY,MAAM,EAAa,MAAM,iBAAiB,CAAA;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAA;AACjD,OAAO,EAAE,sBAAsB,EAAW,MAAM,mBAAmB,CAAA;AAEnE,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAA;AAC1C,OAAO,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAA;AAGnD,qBAAa,aAAa;IACtB,MAAM,EAAE,mBAAmB,CAAA;IAC3B,QAAQ,EAAE,sBAAsB,GAAG,SAAS,CAAA;IAC5C,sBAAsB,EAAE,MAAM,CAAA;IAC9B,mBAAmB,EAAE,MAAM,CAAA;IAC3B,kBAAkB,EAAE,MAAM,CAAA;IAC1B,YAAY,EAAE,MAAM,CAAA;IACpB,GAAG,EAAE,WAAW,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,UAAU,EAAE,UAAU,CAAA;IACtB,OAAO,EAAE,OAAO,CAAA;IAChB,IAAI,EAAE,WAAW,CAAA;IACjB,GAAG,EAAE,UAAU,GAAG,SAAS,CAAA;IAC3B,OAAO,EAAE,aAAa,CAAA;gBAEV,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,mBAAmB;IA6B7C,cAAc,CAAC,WAAW,EAAE,MAAM;IAIzC,OAAO;IAUP,SAAS,IAAI,OAAO,CAAC,IAAI,CAAC;IAYhC,MAAM,IAAI,UAAU;IAOpB,WAAW,IAAI,sBAAsB;IAOxB,UAAU,CAAC,OAAO,EAAE,gBAAgB,GAAG,OAAO,CAAC,OAAO,CAAC;IA2BvD,cAAc,IAAI,OAAO,CAAC,sBAAsB,CAAC;CAWjE"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// Copyright 2021-2023 Prosopo (UK) Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import { ApiPromise } from '@polkadot/api';
|
|
15
|
+
import { NetworkNamesSchema } from '@prosopo/types';
|
|
16
|
+
import { Keyring } from '@polkadot/keyring';
|
|
17
|
+
import { getLogger } from '@prosopo/common';
|
|
18
|
+
import { ProsopoCaptchaContract, abiJson } from '@prosopo/contract';
|
|
19
|
+
import { ProsopoEnvError, trimProviderUrl } from '@prosopo/common';
|
|
20
|
+
import { ProviderApi } from '@prosopo/api';
|
|
21
|
+
import { WsProvider } from '@polkadot/rpc-provider';
|
|
22
|
+
import { get } from '@prosopo/util';
|
|
23
|
+
export class ProsopoServer {
|
|
24
|
+
constructor(pair, config) {
|
|
25
|
+
this.config = config;
|
|
26
|
+
this.pair = pair;
|
|
27
|
+
if (this.config.defaultEnvironment &&
|
|
28
|
+
Object.prototype.hasOwnProperty.call(this.config.networks, this.config.defaultEnvironment)) {
|
|
29
|
+
this.defaultEnvironment = this.config.defaultEnvironment;
|
|
30
|
+
const networkName = NetworkNamesSchema.parse(this.defaultEnvironment);
|
|
31
|
+
this.network = get(this.config.networks, networkName);
|
|
32
|
+
this.wsProvider = new WsProvider(this.network.endpoint);
|
|
33
|
+
this.prosopoContractAddress = this.network.contract.address;
|
|
34
|
+
this.dappContractAddress = this.config.account.address;
|
|
35
|
+
this.contractName = this.network.contract.name;
|
|
36
|
+
this.logger = getLogger(this.config.logLevel, '@prosopo/server');
|
|
37
|
+
this.keyring = new Keyring({
|
|
38
|
+
type: 'sr25519', // TODO get this from the chain
|
|
39
|
+
});
|
|
40
|
+
this.abi = abiJson;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
throw new ProsopoEnvError('CONFIG.UNKNOWN_ENVIRONMENT', this.constructor.name, {}, this.config.defaultEnvironment);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async getProviderApi(providerUrl) {
|
|
47
|
+
return new ProviderApi(this.network, providerUrl, this.config.account.address);
|
|
48
|
+
}
|
|
49
|
+
async isReady() {
|
|
50
|
+
try {
|
|
51
|
+
this.api = await ApiPromise.create({ provider: this.wsProvider });
|
|
52
|
+
await this.getSigner();
|
|
53
|
+
await this.getContractApi();
|
|
54
|
+
}
|
|
55
|
+
catch (err) {
|
|
56
|
+
throw new ProsopoEnvError(err, 'GENERAL.ENVIRONMENT_NOT_READY');
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
async getSigner() {
|
|
60
|
+
if (!this.api) {
|
|
61
|
+
this.api = await ApiPromise.create({ provider: this.wsProvider });
|
|
62
|
+
}
|
|
63
|
+
await this.api.isReadyOrError;
|
|
64
|
+
try {
|
|
65
|
+
this.pair = this.keyring.addPair(this.pair);
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
throw new ProsopoEnvError('CONTRACT.SIGNER_UNDEFINED', this.getSigner.name, {}, err);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
getApi() {
|
|
72
|
+
if (this.api === undefined) {
|
|
73
|
+
throw new ProsopoEnvError(new Error('api undefined'));
|
|
74
|
+
}
|
|
75
|
+
return this.api;
|
|
76
|
+
}
|
|
77
|
+
getContract() {
|
|
78
|
+
if (this.contract === undefined) {
|
|
79
|
+
throw new ProsopoEnvError(new Error('contract undefined'));
|
|
80
|
+
}
|
|
81
|
+
return this.contract;
|
|
82
|
+
}
|
|
83
|
+
async isVerified(payload) {
|
|
84
|
+
const { user, dapp, providerUrl, commitmentId, blockNumber } = payload;
|
|
85
|
+
// first check if the provider was actually chosen at blockNumber
|
|
86
|
+
const contractApi = await this.getContractApi();
|
|
87
|
+
const block = (await this.getApi().rpc.chain.getBlockHash(blockNumber));
|
|
88
|
+
const getRandomProviderResponse = await this.getContract().queryAtBlock(block, 'getRandomActiveProvider', [user, dapp]);
|
|
89
|
+
const providerUrlTrimmed = trimProviderUrl(getRandomProviderResponse.provider.url.toString());
|
|
90
|
+
if (providerUrlTrimmed !== providerUrl) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
console.log('providerUrlTrimmed', providerUrlTrimmed, 'commitmentId', commitmentId);
|
|
94
|
+
if (providerUrlTrimmed && commitmentId) {
|
|
95
|
+
const providerApi = await this.getProviderApi(providerUrl);
|
|
96
|
+
const result = await providerApi.verifyDappUser(user, commitmentId);
|
|
97
|
+
console.log(result);
|
|
98
|
+
return result.solutionApproved;
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
return (await contractApi.query.dappOperatorIsHumanUser(user, this.config.solutionThreshold)).value
|
|
102
|
+
.unwrap()
|
|
103
|
+
.unwrap();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
async getContractApi() {
|
|
107
|
+
this.contract = new ProsopoCaptchaContract(this.getApi(), this.abi, this.prosopoContractAddress, this.pair, this.contractName, 0);
|
|
108
|
+
return this.contract;
|
|
109
|
+
}
|
|
110
|
+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,CACJ,CAAA;QACD,OAAO,IAAI,CAAC,QAAQ,CAAA;IACxB,CAAC;CACJ"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prosopo/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "NodeJS package for server side communication with the prosopo captcha client",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -26,10 +26,10 @@
|
|
|
26
26
|
"@polkadot/keyring": "12.3.2",
|
|
27
27
|
"@polkadot/rpc-provider": "10.9.1",
|
|
28
28
|
"@polkadot/types": "10.9.1",
|
|
29
|
-
"@prosopo/api": "
|
|
30
|
-
"@prosopo/contract": "
|
|
31
|
-
"@prosopo/procaptcha": "
|
|
32
|
-
"@prosopo/types": "
|
|
29
|
+
"@prosopo/api": "0.2.0",
|
|
30
|
+
"@prosopo/contract": "0.2.0",
|
|
31
|
+
"@prosopo/procaptcha": "0.2.0",
|
|
32
|
+
"@prosopo/types": "0.2.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"tslib": "^2.6.2",
|