forked from TBD54566975/workshop-mock-pfis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
offerings.ts
110 lines (91 loc) · 3.53 KB
/
offerings.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { OfferingsApi, Offering } from '@tbdex/http-server'
import { config } from './config.js'
// import fs from 'fs/promises'
import { PresentationExchange } from '@web5/credentials'
import { BearerDid } from '@web5/dids'
import * as offerings from './offeringData.js'
// Offerings order
/*
1. African currency exchanges PFI
2. Another African currency exchanges PFI
3. USD related exchanges PFI
4. EUR related exchanges PFI
5. High liquidity exchanges PFI
*/
const customOfferings = [
// first set of offerings per PFI
{data: {...offerings.offeringDataGHSToUSDC}}, // PFI 1
{data: {...offerings.offeringDataKESToUSD2}}, // PFI 2
{data: {...offerings.offeringDataUSDToEUR}}, // PFI 3
{data: {...offerings.offeringDataEURToUSD2}}, // PFI 4
{data: {...offerings.offeringDataUSDToAUD}}, // PFI 5
// second set of offerings per PFI
{data: {...offerings.offeringDataNGNToKES}},
{data: {...offerings.offeringDataKESToUSDC}},
{data: {...offerings.offeringDataEURToUSD}},
{data: {...offerings.offeringDataEURToUSDC}},
{data: {...offerings.offeringDataUSDToGBP2}},
// third set of offerings per PFI
{data: {...offerings.offeringDataKESToUSD}},
{data: {...offerings.offeringDataNGNToGHS}},
{data: {...offerings.offeringDataUSDToGBP}},
{data: {...offerings.offeringDataUSDToEUR2}},
{data: {...offerings.offeringDataUSDToKES2}},
// fourth set of offerings per PFI
{data: {...offerings.offeringDataUSDToKES}},
{data: {...offerings.offeringDataBTCToNGN}},
{data: {...offerings.offeringDataUSDToBTC}},
{data: {...offerings.offeringDataEURToGBP}},
{data: {...offerings.offeringDataUSDToMXN}},
]
// Function to create a randomized offering
async function createRandomOffering(index: number): Promise<Offering> {
const customPFIIndex = index % 5 // 5 is number of hardcoded PFI DIDs
const offering = Offering.create({
metadata: {
from: config.pfiDid[customPFIIndex].uri, // Alternates between 5 PFI URIs
protocol: '1.0'
},
data: customOfferings[index].data //chooseRandomOfferingData(customPFIIndex),
})
try {
await offering.sign(config.pfiDid[customPFIIndex])
// console.log('Offering signed')
}
catch (e) {
console.log('error', e)
}
// offering.sign(config.pfiDid[index % 5]) // Sign with alternating URI
try {
offering.validate()
PresentationExchange.validateDefinition({
presentationDefinition: offering.data.requiredClaims
})
} catch (error) {
console.error(`Offering ${index + 1} failed validation: ${error}`)
}
// console.log(`Offering ${index + 1} created and validated`)
return offering
}
// Initialize an array of hardcoded offerings
const hardcodedOfferings: Offering[] = await Promise.all(Array.from({ length: 20 }, (_, i) => createRandomOffering(i)))
export class HardcodedOfferingRepository implements OfferingsApi {
pfi: BearerDid
pfiHardcodedOfferings: Offering[]
constructor(pfi: BearerDid) {
this.pfi = pfi
this.pfiHardcodedOfferings = hardcodedOfferings.filter((offering) => offering.metadata.from === pfi.uri)
}
// Retrieve a single offering if found
async getOffering(opts: { id: string }): Promise<Offering | undefined> {
console.log('call for offerings')
return this.pfiHardcodedOfferings.find((offering) => offering.id === opts.id)
}
// Retrieve a list of offerings
async getOfferings(): Promise<Offering[] | undefined> {
console.log('get PFI offerings')
return this.pfiHardcodedOfferings
}
}
// Export an instance of the repository
// export const OfferingRepository = new HardcodedOfferingRepository()