This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
forked from TBD54566975/example-pfi-aud-usd-tbdex
-
Notifications
You must be signed in to change notification settings - Fork 12
/
config.ts
86 lines (70 loc) · 2.46 KB
/
config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import type { LogLevelDesc } from 'loglevel'
import fs from 'fs/promises'
import 'dotenv/config'
import { BearerDid, DidDht } from '@web5/dids'
export type Environment = 'local' | 'staging' | 'production'
const pfi_path = 'src/pfis/'
export type Config = {
env: Environment;
logLevel: LogLevelDesc;
host: string[];
port: string[];
pfiDid: BearerDid[];
allowlist: string[];
pinPaymentsKey: string;
}
export const config: Config = {
env: (process.env['ENV'] as Environment) || 'local',
logLevel: (process.env['LOG_LEVEL'] as LogLevelDesc) || 'info',
host: [ 'http://localhost:4000', 'http://localhost:5000', 'http://localhost:8000', 'http://localhost:8080', 'http://localhost:9000'],
port: ['4000', '5000', '8000', '8080', '9000'],
pfiDid: await createOrLoadDid(['pfi_1.json', 'pfi_2.json', 'pfi_3.json', 'pfi_4.json', 'pfi_issuer.json']),
pinPaymentsKey: process.env['SEC_PIN_PAYMENTS_SECRET_KEY'],
allowlist: JSON.parse(process.env['SEC_ALLOWLISTED_DIDS'] || '[]'),
}
async function loadDID(filename) {
try {
const data = await fs.readFile(pfi_path + filename, 'utf-8')
return JSON.parse(data)
} catch (error) {
console.error('Error reading from file:', error)
return false
}
}
async function createADid(serviceEndpoint: string, filename: string) {
const existingDid = await loadDID(filename)
if (existingDid) {
const bearerDid = await DidDht.import({portableDid: existingDid})
return bearerDid
}
// else create a new one
const bearerDid = await DidDht.create({
options: {
services: [
{
id: 'pfi',
type: 'PFI',
serviceEndpoint,
},
],
},
})
const portableDid = await bearerDid.export()
await fs.writeFile(pfi_path + filename, JSON.stringify(portableDid, null, 2))
return bearerDid
}
async function createOrLoadDid(filenames: string[]): Promise<BearerDid[]> {
console.log('Setting up dids for server PFIs...')
const pfis: BearerDid[] = []
const bearerDid1 = await createADid('http://localhost:4000', filenames[0])
const bearerDid2 = await createADid('http://localhost:5000', filenames[1])
const bearerDid3 = await createADid('http://localhost:8000', filenames[2])
const bearerDid4 = await createADid('http://localhost:8080', filenames[3])
const bearerDid5 = await createADid('http://localhost:9000', filenames[4])
pfis.push(bearerDid1)
pfis.push(bearerDid2)
pfis.push(bearerDid3)
pfis.push(bearerDid4)
pfis.push(bearerDid5)
return pfis
}