-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathmediator.ts
110 lines (93 loc) · 3.56 KB
/
mediator.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
/**
* This file contains a sample mediator. The mediator supports both
* HTTP and WebSockets for communication and will automatically accept
* incoming mediation requests.
*
* You can get an invitation by going to '/invitation', which by default is
* http://localhost:3001/invitation
*
* To connect to the mediator from another agent, you can set the
* 'mediatorConnectionsInvite' parameter in the agent config to the
* url that is returned by the '/invitation/ endpoint. This will connect
* to the mediator, request mediation and set the mediator as default.
*/
import type { InitConfig } from '@credo-ts/core'
import type { Socket } from 'net'
import { ariesAskar } from '@hyperledger/aries-askar-nodejs'
import express from 'express'
import { Server } from 'ws'
import { TestLogger } from '../packages/core/tests/logger'
import { AskarModule } from '@credo-ts/askar'
import {
ConnectionsModule,
MediatorModule,
HttpOutboundTransport,
Agent,
ConnectionInvitationMessage,
LogLevel,
WsOutboundTransport,
} from '@credo-ts/core'
import { HttpInboundTransport, agentDependencies, WsInboundTransport } from '@credo-ts/node'
const port = process.env.AGENT_PORT ? Number(process.env.AGENT_PORT) : 3001
// We create our own instance of express here. This is not required
// but allows use to use the same server (and port) for both WebSockets and HTTP
const app = express()
const socketServer = new Server({ noServer: true })
const endpoints = process.env.AGENT_ENDPOINTS?.split(',') ?? [`http://localhost:${port}`, `ws://localhost:${port}`]
const logger = new TestLogger(LogLevel.info)
const agentConfig: InitConfig = {
endpoints,
label: process.env.AGENT_LABEL || 'Credo Mediator',
walletConfig: {
id: process.env.WALLET_NAME || 'Credo',
key: process.env.WALLET_KEY || 'Credo',
},
logger,
}
// Set up agent
const agent = new Agent({
config: agentConfig,
dependencies: agentDependencies,
modules: {
askar: new AskarModule({ ariesAskar }),
mediator: new MediatorModule({
autoAcceptMediationRequests: true,
}),
connections: new ConnectionsModule({
autoAcceptConnections: true,
}),
},
})
const config = agent.config
// Create all transports
const httpInboundTransport = new HttpInboundTransport({ app, port })
const httpOutboundTransport = new HttpOutboundTransport()
const wsInboundTransport = new WsInboundTransport({ server: socketServer })
const wsOutboundTransport = new WsOutboundTransport()
// Register all Transports
agent.registerInboundTransport(httpInboundTransport)
agent.registerOutboundTransport(httpOutboundTransport)
agent.registerInboundTransport(wsInboundTransport)
agent.registerOutboundTransport(wsOutboundTransport)
// Allow to create invitation, no other way to ask for invitation yet
httpInboundTransport.app.get('/invitation', async (req, res) => {
if (typeof req.query.c_i === 'string') {
const invitation = ConnectionInvitationMessage.fromUrl(req.url)
res.send(invitation.toJSON())
} else {
const { outOfBandInvitation } = await agent.oob.createInvitation()
const httpEndpoint = config.endpoints.find((e) => e.startsWith('http'))
res.send(outOfBandInvitation.toUrl({ domain: httpEndpoint + '/invitation' }))
}
})
const run = async () => {
await agent.initialize()
// When an 'upgrade' to WS is made on our http server, we forward the
// request to the WS server
httpInboundTransport.server?.on('upgrade', (request, socket, head) => {
socketServer.handleUpgrade(request, socket as Socket, head, (socket) => {
socketServer.emit('connection', socket, request)
})
})
}
void run()