@prosopo/env 0.1.19 → 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,131 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const api = require("@polkadot/api");
4
+ const database = require("@prosopo/database");
5
+ const keyring = require("@polkadot/keyring");
6
+ const common = require("@prosopo/common");
7
+ const contract = require("@prosopo/contract");
8
+ const rpcProvider = require("@polkadot/rpc-provider");
9
+ const util = require("@prosopo/util");
10
+ class Environment {
11
+ constructor(pair, config) {
12
+ var _a, _b, _c, _d;
13
+ this.config = config;
14
+ this.defaultEnvironment = this.config.defaultEnvironment;
15
+ this.pair = pair;
16
+ this.logger = common.getLogger(this.config.logLevel, `ProsopoEnvironment`);
17
+ if (this.config.defaultEnvironment && Object.prototype.hasOwnProperty.call(this.config.networks, this.config.defaultEnvironment) && this.config.networks && this.config.networks[this.defaultEnvironment]) {
18
+ this.logger.info(`Endpoint: ${(_a = this.config.networks[this.defaultEnvironment]) == null ? void 0 : _a.endpoint}`);
19
+ this.wsProvider = new rpcProvider.WsProvider((_b = this.config.networks[this.defaultEnvironment]) == null ? void 0 : _b.endpoint);
20
+ this.contractAddress = ((_c = this.config.networks[this.defaultEnvironment]) == null ? void 0 : _c.contract.address) || "";
21
+ this.contractName = ((_d = this.config.networks[this.defaultEnvironment]) == null ? void 0 : _d.contract.name) || "";
22
+ this.keyring = new keyring.Keyring({
23
+ type: "sr25519"
24
+ // TODO get this from the chain
25
+ });
26
+ this.keyring.addPair(this.pair);
27
+ this.abi = contract.abiJson;
28
+ this.importDatabase().catch((err) => {
29
+ this.logger.error(err);
30
+ });
31
+ } else {
32
+ throw new common.ProsopoEnvError(
33
+ "CONFIG.UNKNOWN_ENVIRONMENT",
34
+ this.constructor.name,
35
+ {},
36
+ this.config.defaultEnvironment
37
+ );
38
+ }
39
+ }
40
+ async getSigner() {
41
+ await this.getApi().isReadyOrError;
42
+ try {
43
+ this.pair = this.keyring.addPair(this.pair);
44
+ } catch (err) {
45
+ throw new common.ProsopoEnvError("CONTRACT.SIGNER_UNDEFINED", this.getSigner.name, {}, err);
46
+ }
47
+ }
48
+ getContractInterface() {
49
+ if (this.contractInterface === void 0) {
50
+ throw new common.ProsopoEnvError(new Error("contractInterface not setup! Please call isReady() first"));
51
+ }
52
+ return this.contractInterface;
53
+ }
54
+ getApi() {
55
+ if (this.api === void 0) {
56
+ throw new common.ProsopoEnvError(new Error("api not setup! Please call isReady() first"));
57
+ }
58
+ return this.api;
59
+ }
60
+ async changeSigner(pair) {
61
+ await this.getApi().isReadyOrError;
62
+ this.pair = pair;
63
+ await this.getSigner();
64
+ this.contractInterface = await this.getContractApi();
65
+ }
66
+ async getContractApi() {
67
+ const nonce = await this.getApi().rpc.system.accountNextIndex(this.pair.address);
68
+ this.contractInterface = new contract.ProsopoCaptchaContract(
69
+ this.getApi(),
70
+ this.abi,
71
+ this.contractAddress,
72
+ this.pair,
73
+ this.contractName,
74
+ // TODO can't find .toNumber() on Index type?
75
+ parseInt(nonce.toString()),
76
+ this.config.logLevel
77
+ );
78
+ return this.contractInterface;
79
+ }
80
+ async isReady() {
81
+ var _a;
82
+ try {
83
+ if (this.config.account.password) {
84
+ this.pair.unlock(this.config.account.password);
85
+ }
86
+ if (!this.api) {
87
+ this.api = await api.ApiPromise.create({ provider: this.wsProvider });
88
+ }
89
+ await this.getSigner();
90
+ this.contractInterface = await this.getContractApi();
91
+ if (!this.db) {
92
+ await this.importDatabase().catch((err) => {
93
+ this.logger.error(err);
94
+ });
95
+ }
96
+ if (this.db && ((_a = this.db.connection) == null ? void 0 : _a.readyState) !== 1) {
97
+ this.logger.warn(`Database connection is not ready, reconnecting...`);
98
+ await this.db.connect();
99
+ this.logger.info(`Connected to db`);
100
+ }
101
+ } catch (err) {
102
+ this.logger.error(err);
103
+ throw new common.ProsopoEnvError(err, "GENERAL.ENVIRONMENT_NOT_READY");
104
+ }
105
+ }
106
+ async importDatabase() {
107
+ var _a;
108
+ try {
109
+ if (this.config.database) {
110
+ const dbConfig = this.config.database[this.defaultEnvironment];
111
+ if (dbConfig) {
112
+ const ProsopoDatabase = util.get(database.Databases, dbConfig.type);
113
+ this.db = await ProsopoDatabase.create(
114
+ dbConfig.endpoint,
115
+ dbConfig.dbname,
116
+ this.logger,
117
+ dbConfig.authSource
118
+ );
119
+ }
120
+ }
121
+ } catch (err) {
122
+ throw new common.ProsopoEnvError(
123
+ err,
124
+ "DATABASE.DATABASE_IMPORT_FAILED",
125
+ {},
126
+ this.config.database ? this.config.database[this.defaultEnvironment] ? (_a = this.config.database[this.defaultEnvironment]) == null ? void 0 : _a.type : void 0 : void 0
127
+ );
128
+ }
129
+ }
130
+ }
131
+ exports.Environment = Environment;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const env = require("./env.cjs");
4
+ const provider = require("./provider.cjs");
5
+ const mockenv = require("./mockenv.cjs");
6
+ exports.Environment = env.Environment;
7
+ exports.ProviderEnvironment = provider.ProviderEnvironment;
8
+ exports.MockEnvironment = mockenv.MockEnvironment;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const provider = require("./provider.cjs");
4
+ const utilCrypto = require("@polkadot/util-crypto");
5
+ class MockEnvironment extends provider.ProviderEnvironment {
6
+ createAccountAndAddToKeyring() {
7
+ const mnemonic = utilCrypto.mnemonicGenerate();
8
+ const account = this.keyring.addFromMnemonic(mnemonic);
9
+ const { address } = account;
10
+ return [mnemonic, address];
11
+ }
12
+ }
13
+ exports.MockEnvironment = MockEnvironment;
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const env = require("./env.cjs");
4
+ class ProviderEnvironment extends env.Environment {
5
+ }
6
+ exports.ProviderEnvironment = ProviderEnvironment;
package/package.json CHANGED
@@ -1,24 +1,31 @@
1
1
  {
2
2
  "name": "@prosopo/env",
3
- "version": "0.1.19",
3
+ "version": "0.2.1",
4
4
  "description": "Path env prosopo environment",
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
  },
13
20
  "dependencies": {
14
21
  "@polkadot/util-crypto": "12.3.2",
15
- "@prosopo/common": "^0.1.19",
16
- "@prosopo/contract": "^0.1.19",
17
- "@prosopo/database": "^0.1.19",
18
- "@prosopo/types": "^0.1.19",
19
- "@prosopo/types-database": "^0.1.19",
20
- "@prosopo/types-env": "^0.1.19",
21
- "@prosopo/util": "^0.1.19",
22
+ "@prosopo/common": "0.2.1",
23
+ "@prosopo/contract": "0.2.1",
24
+ "@prosopo/database": "0.2.1",
25
+ "@prosopo/types": "0.2.1",
26
+ "@prosopo/types-database": "0.2.1",
27
+ "@prosopo/types-env": "0.2.1",
28
+ "@prosopo/util": "0.2.1",
22
29
  "dotenv": "^16.0.1"
23
30
  },
24
31
  "devDependencies": {
@@ -0,0 +1,6 @@
1
+ import { ViteCommonJSConfig } from '@prosopo/config'
2
+ import path from 'path'
3
+
4
+ export default function () {
5
+ return ViteCommonJSConfig('env', path.resolve('./tsconfig.cjs.json'))
6
+ }