Skip to content

Commit 2c8f63b

Browse files
authored
feat: externalize env (credebl#153)
1 parent 1a15bf9 commit 2c8f63b

File tree

8 files changed

+65
-32
lines changed

8 files changed

+65
-32
lines changed

src/utils/util.ts renamed to .env.sample

Lines changed: 36 additions & 13 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"axios": "^1.4.0",
5757
"body-parser": "^1.20.0",
5858
"cors": "^2.8.5",
59+
"dotenv": "^16.4.5",
5960
"express": "^4.18.1",
6061
"express-rate-limit": "^7.1.5",
6162
"joi": "^17.12.3",

src/cli.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ export async function runCliServer() {
166166
type: parsed['wallet-type'],
167167
config: {
168168
host: parsed['wallet-url'],
169-
connectTimeout: 10,
170-
maxConnections: 1000,
171-
idleTimeout: 30000,
169+
connectTimeout: Number(process.env.CONNECT_TIMEOUT),
170+
maxConnections: Number(process.env.MAX_CONNECTIONS),
171+
idleTimeout: Number(process.env.IDLE_TIMEOUT),
172172
},
173173
credentials: {
174174
account: parsed['wallet-account'],

src/cliAgent.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import jwt from 'jsonwebtoken'
5454

5555
import { setupServer } from './server'
5656
import { TsLogger } from './utils/logger'
57-
import { BCOVRIN_TEST_GENESIS } from './utils/util'
5857

5958
export type Transports = 'ws' | 'http'
6059
export type InboundTransport = {
@@ -114,6 +113,12 @@ export type RestMultiTenantAgentModules = Awaited<ReturnType<typeof getWithTenan
114113
export type RestAgentModules = Awaited<ReturnType<typeof getModules>>
115114

116115
const getModules = (networkConfig: [IndyVdrPoolConfig, ...IndyVdrPoolConfig[]]) => {
116+
const didContractAddress = process.env.DID_CONTRACT_ADDRESS as string
117+
const schemaManagerContractAddress = process.env.SCHEMA_MANAGER_CONTRACT_ADDRESS as string
118+
const fileServerToken = process.env.FILE_SERVER_TOKEN
119+
const rpcUrl = process.env.RPC_URL
120+
const serverUrl = process.env.SERVER_URL
121+
117122
const legacyIndyCredentialFormat = new LegacyIndyCredentialFormatService()
118123
const legacyIndyProofFormat = new LegacyIndyProofFormatService()
119124
const jsonLdCredentialFormatService = new JsonLdCredentialFormatService()
@@ -172,17 +177,16 @@ const getModules = (networkConfig: [IndyVdrPoolConfig, ...IndyVdrPoolConfig[]])
172177
}),
173178
w3cCredentials: new W3cCredentialsModule(),
174179
cache: new CacheModule({
175-
cache: new InMemoryLruCache({ limit: Infinity }),
180+
cache: new InMemoryLruCache({ limit: Number(process.env.INMEMORY_LRU_CACHE_LIMIT) }),
176181
}),
177182

178183
questionAnswer: new QuestionAnswerModule(),
179184
polygon: new PolygonModule({
180-
didContractAddress: '0x1adeA199dCf07E17232415Cb232442BE52517Add',
181-
schemaManagerContractAddress: '0x289c7Bd4C7d38cC54bff370d6f9f01b74Df51b11',
182-
fileServerToken:
183-
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJBeWFuV29ya3MiLCJpZCI6ImNhZDI3ZjhjLTMyNWYtNDRmZC04ZmZkLWExNGNhZTY3NTMyMSJ9.I3IR7abjWbfStnxzn1BhxhV0OEzt1x3mULjDdUcgWHk',
184-
rpcUrl: 'https://rpc-amoy.polygon.technology',
185-
serverUrl: 'https://schema.credebl.id',
185+
didContractAddress: didContractAddress,
186+
schemaManagerContractAddress: schemaManagerContractAddress,
187+
fileServerToken: fileServerToken,
188+
rpcUrl: rpcUrl,
189+
serverUrl: serverUrl,
186190
}),
187191
}
188192
}
@@ -191,8 +195,8 @@ const getWithTenantModules = (networkConfig: [IndyVdrPoolConfig, ...IndyVdrPoolC
191195
const modules = getModules(networkConfig)
192196
return {
193197
tenants: new TenantsModule<typeof modules>({
194-
sessionAcquireTimeout: Infinity,
195-
sessionLimit: Infinity,
198+
sessionAcquireTimeout: Number(process.env.SESSION_ACQUIRE_TIMEOUT),
199+
sessionLimit: Number(process.env.SESSION_LIMIT),
196200
}),
197201
...modules,
198202
}
@@ -291,7 +295,7 @@ export async function runRestAgent(restConfig: AriesRestConfig) {
291295
} else {
292296
networkConfig = [
293297
{
294-
genesisTransactions: BCOVRIN_TEST_GENESIS,
298+
genesisTransactions: process.env.BCOVRIN_TEST_GENESIS as string,
295299
indyNamespace: 'bcovrin:testnet',
296300
isProduction: false,
297301
connectOnStartup: true,

src/controllers/did/DidController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import axios from 'axios'
1414
import { injectable } from 'tsyringe'
1515

1616
import { DidMethod, Network, Role } from '../../enums/enum'
17-
import { BCOVRIN_REGISTER_URL, INDICIO_NYM_URL } from '../../utils/util'
1817
import { Did, DidRecordExample } from '../examples'
1918
import { DidCreate } from '../types'
2019

@@ -163,6 +162,7 @@ export class DidController extends Controller {
163162
didDocument: didDocument,
164163
}
165164
} else {
165+
const BCOVRIN_REGISTER_URL = process.env.BCOVRIN_REGISTER_URL as string
166166
const res = await axios.post(BCOVRIN_REGISTER_URL, {
167167
role: 'ENDORSER',
168168
alias: 'Alias',
@@ -216,6 +216,7 @@ export class DidController extends Controller {
216216
}
217217
} else {
218218
const key = await this.createIndicioKey(createDidOptions)
219+
const INDICIO_NYM_URL = process.env.INDICIO_NYM_URL as string
219220
const res = await axios.post(INDICIO_NYM_URL, key)
220221
if (res.data.statusCode === 200) {
221222
await this.importDid(didMethod, key.did, createDidOptions.seed)

src/controllers/multi-tenancy/MultiTenancyController.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import axios from 'axios'
5252
import * as fs from 'fs'
5353

5454
import { CredentialEnum, DidMethod, Network, Role } from '../../enums/enum'
55-
import { BCOVRIN_REGISTER_URL, INDICIO_NYM_URL } from '../../utils/util'
5655
import { SchemaId, CredentialDefinitionId, RecordId, ProofRecordExample, ConnectionRecordExample } from '../examples'
5756
import {
5857
RequestProofOptions,
@@ -254,6 +253,7 @@ export class MultiTenancyController extends Controller {
254253
seed: createDidOptions.seed,
255254
}
256255

256+
const BCOVRIN_REGISTER_URL = process.env.BCOVRIN_REGISTER_URL as string
257257
const res = await axios.post(BCOVRIN_REGISTER_URL, body)
258258
if (res) {
259259
const { did } = res?.data || {}
@@ -353,6 +353,7 @@ export class MultiTenancyController extends Controller {
353353
verkey: TypedArrayEncoder.toBase58(buffer),
354354
}
355355
}
356+
const INDICIO_NYM_URL = process.env.INDICIO_NYM_URL as string
356357
const res = await axios.post(INDICIO_NYM_URL, body)
357358
if (res.data.statusCode === 200) {
358359
await this.importDid(didMethod, did, createDidOptions.seed, tenantAgent)

src/server.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { Response as ExResponse, Request as ExRequest, NextFunction } from
55
import { Agent } from '@credo-ts/core'
66
import bodyParser from 'body-parser'
77
import cors from 'cors'
8+
import dotenv from 'dotenv'
89
import express from 'express'
910
import { rateLimit } from 'express-rate-limit'
1011
import * as fs from 'fs'
@@ -19,10 +20,11 @@ import { proofEvents } from './events/ProofEvents'
1920
import { questionAnswerEvents } from './events/QuestionAnswerEvents'
2021
import { RegisterRoutes } from './routes/routes'
2122
import { SecurityMiddleware } from './securityMiddleware'
22-
import { maxRateLimit, windowMs } from './utils/util'
2323

2424
import { ValidateError, type Exception } from 'tsoa'
2525

26+
dotenv.config()
27+
2628
export const setupServer = async (agent: Agent, config: ServerConfig, apiKey?: string) => {
2729
container.registerInstance(Agent, agent)
2830
fs.writeFileSync('config.json', JSON.stringify(config, null, 2))
@@ -51,6 +53,8 @@ export const setupServer = async (agent: Agent, config: ServerConfig, apiKey?: s
5153
return res.send(generateHTML(await import('./routes/swagger.json')))
5254
})
5355

56+
const windowMs = Number(process.env.windowMs)
57+
const maxRateLimit = Number(process.env.maxRateLimit)
5458
const limiter = rateLimit({
5559
windowMs, // 1 second
5660
max: maxRateLimit, // max 800 requests per second

src/utils/agent.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import { ariesAskar } from '@hyperledger/aries-askar-nodejs'
3636
import { indyVdr } from '@hyperledger/indy-vdr-nodejs'
3737

3838
import { TsLogger } from './logger'
39-
import { BCOVRIN_TEST_GENESIS } from './util'
4039

4140
export const setupAgent = async ({ name, endpoints, port }: { name: string; endpoints: string[]; port: number }) => {
4241
const logger = new TsLogger(LogLevel.debug)
@@ -62,7 +61,7 @@ export const setupAgent = async ({ name, endpoints, port }: { name: string; endp
6261
{
6362
isProduction: false,
6463
indyNamespace: 'bcovrin:test',
65-
genesisTransactions: BCOVRIN_TEST_GENESIS,
64+
genesisTransactions: process.env.BCOVRIN_TEST_GENESIS as string,
6665
connectOnStartup: true,
6766
},
6867
],

0 commit comments

Comments
 (0)