forked from openwallet-foundation/credo-ts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mediator.ts
112 lines (91 loc) · 3.44 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
111
112
import express, { Express } from 'express';
import cors from 'cors';
import config from './config';
import testLogger from '../lib/__tests__/logger';
import { Agent, InboundTransporter, OutboundTransporter } from '../lib';
import { OutboundPackage } from '../lib/types';
import { MessageRepository } from '../lib/storage/MessageRepository';
import { InMemoryMessageRepository } from '../lib/storage/InMemoryMessageRepository';
class HttpInboundTransporter implements InboundTransporter {
private app: Express;
public constructor(app: Express) {
this.app = app;
}
public start(agent: Agent) {
this.app.post('/msg', async (req, res) => {
const message = req.body;
const packedMessage = JSON.parse(message);
const outboundMessage = await agent.receiveMessage(packedMessage);
if (outboundMessage) {
res.status(200).json(outboundMessage.payload).end();
} else {
res.status(200).end();
}
});
}
}
class StorageOutboundTransporter implements OutboundTransporter {
public messages: { [key: string]: any } = {};
private messageRepository: MessageRepository;
public constructor(messageRepository: MessageRepository) {
this.messageRepository = messageRepository;
}
public async sendMessage(outboundPackage: OutboundPackage) {
const { connection, payload } = outboundPackage;
if (!connection) {
throw new Error(`Missing connection. I don't know how and where to send the message.`);
}
if (!connection.theirKey) {
throw new Error('Trying to save message without theirKey!');
}
testLogger.debug('Storing message', { connection, payload });
this.messageRepository.save(connection.theirKey, payload);
}
}
const PORT = config.port;
const app = express();
app.use(cors());
app.use(
express.text({
type: ['application/ssi-agent-wire', 'text/plain'],
})
);
app.set('json spaces', 2);
const messageRepository = new InMemoryMessageRepository();
const messageSender = new StorageOutboundTransporter(messageRepository);
const messageReceiver = new HttpInboundTransporter(app);
const agent = new Agent(config, messageReceiver, messageSender, messageRepository);
app.get('/', async (req, res) => {
const agentDid = agent.publicDid;
res.send(agentDid);
});
// Create new invitation as inviter to invitee
app.get('/invitation', async (req, res) => {
const { invitation } = await agent.connections.createConnection();
res.send(invitation.toUrl());
});
app.get('/api/connections/:verkey', async (req, res) => {
// TODO This endpoint is for testing purpose only. Return mediator connection by their verkey.
const verkey = req.params.verkey;
const connection = await agent.connections.findConnectionByTheirKey(verkey);
res.send(connection);
});
app.get('/api/connections', async (req, res) => {
// TODO This endpoint is for testing purpose only. Return mediator connection by their verkey.
const connections = await agent.connections.getAll();
res.json(connections);
});
app.get('/api/routes', async (req, res) => {
// TODO This endpoint is for testing purpose only. Return mediator connection by their verkey.
const routes = agent.routing.getRoutingTable();
res.send(routes);
});
app.get('/api/messages', async (req, res) => {
// TODO This endpoint is for testing purpose only.
res.send(messageSender.messages);
});
app.listen(PORT, async () => {
await agent.init();
messageReceiver.start(agent);
testLogger.info(`Application started on port ${PORT}`);
});