@prosopo/server 2.1.8 → 2.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.
@@ -0,0 +1,39 @@
1
+ export declare const getServerConfig: () => {
2
+ timeouts: {
3
+ image: {
4
+ verifiedTimeout: number;
5
+ challengeTimeout: number;
6
+ solutionTimeout: number;
7
+ cachedTimeout: number;
8
+ };
9
+ pow: {
10
+ verifiedTimeout: number;
11
+ solutionTimeout: number;
12
+ cachedTimeout: number;
13
+ };
14
+ contract: {
15
+ maxVerifiedTime: number;
16
+ };
17
+ };
18
+ logLevel: "error" | "trace" | "debug" | "info" | "warn" | "fatal" | "log";
19
+ defaultEnvironment: "development" | "staging" | "production";
20
+ account: {
21
+ secret?: string | undefined;
22
+ address?: string | undefined;
23
+ password?: string | undefined;
24
+ };
25
+ web2: boolean;
26
+ solutionThreshold: number;
27
+ dappName: string;
28
+ database?: Partial<Record<"development" | "staging" | "production", {
29
+ type: string;
30
+ endpoint: string;
31
+ dbname: string;
32
+ authSource: string;
33
+ }>> | undefined;
34
+ devOnlyWatchEvents?: boolean | undefined;
35
+ userAccountAddress?: string | undefined;
36
+ serverUrl?: string | undefined;
37
+ };
38
+ export declare const getServerUrl: () => string;
39
+ //# sourceMappingURL=config.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAeA,eAAO,MAAM,eAAe;;;;;;;;;;;;;;;;;;;;cAqBwghB,CAAC;eAAqC,CAAC;gBAAsC,CAAC;;;;;;;;;;;;;;CAX/mhB,CAAC;AAEJ,eAAO,MAAM,YAAY,QAAO,MAQ/B,CAAC"}
package/dist/config.js ADDED
@@ -0,0 +1,21 @@
1
+ import { ProsopoServerConfigSchema } from "@prosopo/types";
2
+ export const getServerConfig = () => ProsopoServerConfigSchema.parse({
3
+ defaultEnvironment: process.env.PROSOPO_DEFAULT_ENVIRONMENT,
4
+ serverUrl: getServerUrl(),
5
+ dappName: process.env.PROSOPO_DAPP_NAME || "client-example-server",
6
+ account: {
7
+ password: "",
8
+ address: process.env.PROSOPO_SITE_KEY || "",
9
+ secret: process.env.PROSOPO_SITE_PRIVATE_KEY || "",
10
+ },
11
+ });
12
+ export const getServerUrl = () => {
13
+ if (process.env.PROSOPO_SERVER_URL) {
14
+ if (process.env.PROSOPO_SERVER_URL.match(/:\d+/)) {
15
+ return process.env.PROSOPO_SERVER_URL;
16
+ }
17
+ return `${process.env.PROSOPO_SERVER_URL}:${process.env.PROSOPO_SERVER_PORT || 9228}`;
18
+ }
19
+ return "http://localhost:9228";
20
+ };
21
+ //# sourceMappingURL=config.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"config.js","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,yBAAyB,EAAE,MAAM,gBAAgB,CAAC;AAE3D,MAAM,CAAC,MAAM,eAAe,GAAG,GAAG,EAAE,CACnC,yBAAyB,CAAC,KAAK,CAAC;IAC/B,kBAAkB,EAAE,OAAO,CAAC,GAAG,CAAC,2BAA2B;IAC3D,SAAS,EAAE,YAAY,EAAE;IACzB,QAAQ,EAAE,OAAO,CAAC,GAAG,CAAC,iBAAiB,IAAI,uBAAuB;IAClE,OAAO,EAAE;QACR,QAAQ,EAAE,EAAE;QACZ,OAAO,EAAE,OAAO,CAAC,GAAG,CAAC,gBAAgB,IAAI,EAAE;QAC3C,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,wBAAwB,IAAI,EAAE;KAClD;CACD,CAAC,CAAC;AAEJ,MAAM,CAAC,MAAM,YAAY,GAAG,GAAW,EAAE;IACxC,IAAI,OAAO,CAAC,GAAG,CAAC,kBAAkB,EAAE,CAAC;QACpC,IAAI,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;YAClD,OAAO,OAAO,CAAC,GAAG,CAAC,kBAAkB,CAAC;QACvC,CAAC;QACD,OAAO,GAAG,OAAO,CAAC,GAAG,CAAC,kBAAkB,IAAI,OAAO,CAAC,GAAG,CAAC,mBAAmB,IAAI,IAAI,EAAE,CAAC;IACvF,CAAC;IACD,OAAO,uBAAuB,CAAC;AAChC,CAAC,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { ProsopoServer } from "./server.js";
2
+ export { ProsopoServer } from "./server.js";
3
+ export { getServerConfig, getServerUrl } from "./config.js";
4
+ import type { ProsopoServerConfigOutput } from "@prosopo/types";
5
+ export declare const PublicProsopoServer: (config: ProsopoServerConfigOutput) => Promise<ProsopoServer>;
6
+ //# 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,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAE5D,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,gBAAgB,CAAC;AAChE,eAAO,MAAM,mBAAmB,WACvB,yBAAyB,2BAKjC,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,9 @@
1
+ import { ProsopoServer } from "./server.js";
2
+ export { ProsopoServer } from "./server.js";
3
+ export { getServerConfig, getServerUrl } from "./config.js";
4
+ import { getPairAsync } from "@prosopo/contract";
5
+ export const PublicProsopoServer = async (config) => {
6
+ const pair = await getPairAsync(undefined, config.account.address);
7
+ return new ProsopoServer(config, pair);
8
+ };
9
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC5D,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,EACvC,MAAiC,EAChC,EAAE;IAEH,MAAM,IAAI,GAAG,MAAM,YAAY,CAAC,SAAS,EAAE,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IACnE,OAAO,IAAI,aAAa,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;AACxC,CAAC,CAAC"}
@@ -0,0 +1,18 @@
1
+ import { Keyring } from "@polkadot/keyring";
2
+ import type { KeyringPair } from "@polkadot/keyring/types";
3
+ import { ProviderApi } from "@prosopo/api";
4
+ import { type Logger } from "@prosopo/common";
5
+ import { type CaptchaTimeoutOutput, type ProcaptchaToken, type ProsopoServerConfigOutput, type VerificationResponse } from "@prosopo/types";
6
+ export declare class ProsopoServer {
7
+ config: ProsopoServerConfigOutput;
8
+ dappAccount: string | undefined;
9
+ defaultEnvironment: string;
10
+ logger: Logger;
11
+ keyring: Keyring;
12
+ pair: KeyringPair | undefined;
13
+ constructor(config: ProsopoServerConfigOutput, pair?: KeyringPair);
14
+ getProviderApi(providerUrl: string): ProviderApi;
15
+ verifyProvider(token: string, timeouts: CaptchaTimeoutOutput, providerUrl: string, timestamp: number, challenge?: string): Promise<VerificationResponse>;
16
+ isVerified(token: ProcaptchaToken): Promise<VerificationResponse>;
17
+ }
18
+ //# sourceMappingURL=server.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"server.d.ts","sourceRoot":"","sources":["../src/server.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAE3D,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAC3C,OAAO,EAEN,KAAK,MAAM,EAIX,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACN,KAAK,oBAAoB,EAEzB,KAAK,eAAe,EACpB,KAAK,yBAAyB,EAC9B,KAAK,oBAAoB,EAEzB,MAAM,gBAAgB,CAAC;AAGxB,qBAAa,aAAa;IACzB,MAAM,EAAE,yBAAyB,CAAC;IAClC,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;IAChC,kBAAkB,EAAE,MAAM,CAAC;IAC3B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,IAAI,EAAE,WAAW,GAAG,SAAS,CAAC;gBAElB,MAAM,EAAE,yBAAyB,EAAE,IAAI,CAAC,EAAE,WAAW;IAcjE,cAAc,CAAC,WAAW,EAAE,MAAM,GAAG,WAAW;IAanC,cAAc,CAC1B,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,oBAAoB,EAC9B,WAAW,EAAE,MAAM,EACnB,SAAS,EAAE,MAAM,EACjB,SAAS,CAAC,EAAE,MAAM,GAChB,OAAO,CAAC,oBAAoB,CAAC;IAgDnB,UAAU,CACtB,KAAK,EAAE,eAAe,GACpB,OAAO,CAAC,oBAAoB,CAAC;CA4BhC"}
package/dist/server.js ADDED
@@ -0,0 +1,75 @@
1
+ import { Keyring } from "@polkadot/keyring";
2
+ import { u8aToHex } from "@polkadot/util";
3
+ import { ProviderApi } from "@prosopo/api";
4
+ import { ProsopoApiError, ProsopoContractError, getLogger, } from "@prosopo/common";
5
+ import { ProcaptchaOutputSchema, decodeProcaptchaOutput, } from "@prosopo/types";
6
+ import i18n from "i18next";
7
+ export class ProsopoServer {
8
+ constructor(config, pair) {
9
+ this.config = config;
10
+ this.pair = pair;
11
+ this.defaultEnvironment = this.config.defaultEnvironment;
12
+ this.dappAccount = this.config.account.address;
13
+ this.logger = getLogger(this.config.logLevel, "@prosopo/server");
14
+ this.keyring = new Keyring({
15
+ type: "sr25519",
16
+ });
17
+ }
18
+ getProviderApi(providerUrl) {
19
+ return new ProviderApi(providerUrl, this.dappAccount || "");
20
+ }
21
+ async verifyProvider(token, timeouts, providerUrl, timestamp, challenge) {
22
+ this.logger.info("Verifying with provider.");
23
+ const dappUserSignature = this.pair?.sign(timestamp.toString());
24
+ if (!dappUserSignature) {
25
+ throw new ProsopoContractError("CAPTCHA.INVALID_TIMESTAMP", {
26
+ context: { error: "Timestamp not found" },
27
+ });
28
+ }
29
+ const signatureHex = u8aToHex(dappUserSignature);
30
+ const providerApi = this.getProviderApi(providerUrl);
31
+ if (challenge) {
32
+ const powTimeout = this.config.timeouts.pow.cachedTimeout;
33
+ const recent = timestamp ? Date.now() - timestamp < powTimeout : false;
34
+ if (!recent) {
35
+ this.logger.error("PoW captcha is not recent");
36
+ return {
37
+ verified: false,
38
+ status: i18n.t("API.USER_NOT_VERIFIED_TIME_EXPIRED"),
39
+ };
40
+ }
41
+ return await providerApi.submitPowCaptchaVerify(token, signatureHex, timeouts.pow.cachedTimeout);
42
+ }
43
+ const imageTimeout = this.config.timeouts.image.cachedTimeout;
44
+ const recent = timestamp ? Date.now() - timestamp < imageTimeout : false;
45
+ if (!recent) {
46
+ this.logger.error("Image captcha is not recent");
47
+ return {
48
+ verified: false,
49
+ status: i18n.t("API.USER_NOT_VERIFIED_TIME_EXPIRED"),
50
+ };
51
+ }
52
+ return await providerApi.verifyDappUser(token, signatureHex, timeouts.image.cachedTimeout);
53
+ }
54
+ async isVerified(token) {
55
+ try {
56
+ const payload = decodeProcaptchaOutput(token);
57
+ const { providerUrl, challenge, timestamp } = ProcaptchaOutputSchema.parse(payload);
58
+ if (providerUrl) {
59
+ return await this.verifyProvider(token, this.config.timeouts, providerUrl, Number(timestamp), challenge);
60
+ }
61
+ this.logger.error("No provider URL found in user token");
62
+ return {
63
+ verified: false,
64
+ status: i18n.t("API.USER_NOT_VERIFIED"),
65
+ };
66
+ }
67
+ catch (err) {
68
+ this.logger.error({ err, token });
69
+ throw new ProsopoApiError("API.BAD_REQUEST", {
70
+ context: { code: 500, token },
71
+ });
72
+ }
73
+ }
74
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"description": "NodeJS package for server side communication with the prosopo captcha client",
5
5
  "main": "./dist/index.js",
6
6
  "type": "module",
@@ -34,13 +34,13 @@
34
34
  "dependencies": {
35
35
  "@polkadot/keyring": "12.6.2",
36
36
  "@polkadot/util": "12.6.2",
37
- "@prosopo/api": "2.1.8",
38
- "@prosopo/common": "2.1.8",
39
- "@prosopo/contract": "2.1.8",
40
- "@prosopo/types": "2.1.8"
37
+ "@prosopo/api": "2.1.9",
38
+ "@prosopo/common": "2.1.9",
39
+ "@prosopo/contract": "2.1.9",
40
+ "@prosopo/types": "2.1.9"
41
41
  },
42
42
  "devDependencies": {
43
- "@prosopo/config": "2.1.8",
43
+ "@prosopo/config": "2.1.9",
44
44
  "@vitest/coverage-v8": "2.1.1",
45
45
  "concurrently": "9.0.1",
46
46
  "npm-run-all": "4.1.5",