-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
354 lines (302 loc) · 11.4 KB
/
index.js
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
require('dotenv').config();
const uuid = require('uuid');
const helmet = require('helmet');
const morgan = require('morgan');
const express = require('express');
const { Issuer, ConfirmedParticipation, LedgerEntry, SignedResource } = require('prepams-shared');
const { openDatabase } = require('./src/utils/db');
const { BadRequest } = require('./src/utils/errors');
const app = express();
app.use(helmet());
app.use(express.json());
app.use(express.raw());
app.use(morgan('dev'));
app.use((req, res, next) => {
res.sendBinary = (data, status=200) => {
res.header('Content-Type', 'application/octet-stream');
res.status(status).send(Buffer.from(data));
};
next();
})
const asyncWrapper = fn => (...args) => fn(...args).catch(e => {
console.log(e);
args[1].status(e?.status || 500).json({ error: e?.message || 'Internal Server Error' });
});
const ATTRIBUTES = JSON.parse(process.env.ATTRIBUTES);
if (process.argv[2] === '--init') {
console.log('Creating new issuer keys...');
console.log(ATTRIBUTES.length, 'attributes: ', ATTRIBUTES.map(e => e[0]).join(', '));
const issuer = new Issuer(ATTRIBUTES.length, []);
console.log('Add the following line to your .env file:');
console.log();
console.log(`ISSUER_SECRET="${Buffer.from(issuer.serialize()).toString('base64url')}"`);
console.log();
process.exit(0);
}
(async () => {
const db = await openDatabase();
let issuer;
try {
const entries = await db.all('SELECT * FROM ledger ORDER BY id ASC');
issuer = entries.reduce((issuer, entry) => {
if (entry.participation) {
const participation = ConfirmedParticipation.from(entry.participation, entry.tag, entry.study, entry.request, entry.signature, entry.value);
return issuer.appendEntry(LedgerEntry.fromTransaction(issuer.head, participation, entry.coin, entry.chain));
} else {
return issuer.appendEntry(LedgerEntry.fromPayout(issuer.head, entry.coin, entry.chain));
}
}, Issuer.deserialize(Buffer.from(process.env['ISSUER_SECRET'], 'base64url'), []));
console.log(`issuer credential loaded, ${entries.length} transaction${entries.length !== 1 ? 's' : ''} applied`);
console.log(`ISSUER_PK="${Buffer.from(issuer.publicKey).toString('base64url')}"`);
} catch (e) {
console.error('Error: Issuer credential missing, initialize issuer using --init argument');
console.error(e);
process.exit(1);
}
app.get('/api/issuer/attributes', (req, res) => res.json(ATTRIBUTES));
app.get('/api/issuer/pk', (req, res) => res.sendBinary(issuer.publicKey));
app.get('/api/issuer/vk', (req, res) => res.sendBinary(issuer.verificationKey));
app.get('/api/ledger/vk', (req, res) => res.sendBinary(issuer.ledgerVerificationKey));
app.get('/api/ledger', (req, res) => res.sendBinary(issuer.ledger));
app.post('/api/nulls', (req, res) => res.sendBinary(issuer.issueNulls(req.body)));
app.post('/api/auth/signup', asyncWrapper(async (req, res) => {
if (await db.get('SELECT * FROM users WHERE id = ?', req.query.id)) {
// throw new Error('id already registered');
}
if (req.query.role === 'participant') {
const signature = issuer.issueCredential(req.body);
await db.run('INSERT INTO users (id, role) VALUES (?, ?)', req.query.id, 'participant');
await db.run('INSERT INTO issued (signature) VALUES (?)', signature);
res.sendBinary(signature);
} else {
await db.run('INSERT INTO users (id, role, publicKey) VALUES (?, ?, ?)', req.query.id, req.query.role, req.body);
res.json({ ok: true });
}
}));
app.get('/api/auth/signin', asyncWrapper(async (req, res) => {
const response = {
issuer: {
pk: Buffer.from(issuer.publicKey).toString('base64'),
vk: Buffer.from(issuer.verificationKey).toString('base64')
},
ledger: {
vk: Buffer.from(issuer.ledgerVerificationKey).toString('base64')
}
};
if (req.query.role === 'participant') {
const rows = await db.all('SELECT signature FROM issued');
response.log = rows.map(e => e.signature.toString('base64'));
} else {
const rows = await db.all('SELECT publicKey FROM users WHERE publicKey IS NOT NULL');
response.publicKeys = rows.map(e => e.publicKey.toString('base64'));
}
res.json(response);
}));
app.get('/api/studies', asyncWrapper(async (req, res) => {
const rows = req.query.id
? await db.all('SELECT * FROM studies WHERE owner = ?', req.query.id)
: await db.all('SELECT * FROM studies');
res.json(rows.map((row) => {
row.qualifier = JSON.parse(row.qualifier);
row.disqualifier = JSON.parse(row.disqualifier);
row.constraints = JSON.parse(row.constraints);
return row;
}));
}));
app.post('/api/studies', asyncWrapper(async (req, res) => {
try {
let signedResource = SignedResource.deserialize(req.body);
const row = await db.get('SELECT publicKey FROM users WHERE id = ?', signedResource.owner);
if (!issuer.checkResourceSignature(signedResource, row.publicKey)) {
throw new BadRequest('signature not valid');
}
const resource = signedResource.resource;
await db.run(`
INSERT INTO studies (
id,
name,
owner,
abstract,
description,
duration,
reward,
qualifier,
disqualifier,
constraints,
webBased,
studyURL,
signature
) VALUES (
:id,
:name,
:owner,
:abstract,
:description,
:duration,
:reward,
:qualifier,
:disqualifier,
:constraints,
:webBased,
:studyURL,
:signature
)
`, {
':id': resource.id,
':name': resource.name,
':owner': signedResource.owner,
':abstract': resource.summary,
':description': resource.description,
':duration': resource.duration,
':reward': resource.reward,
':qualifier': JSON.stringify(resource.qualifier),
':disqualifier': JSON.stringify(resource.disqualifier),
':constraints': JSON.stringify(resource.constraints),
':webBased': resource.webBased,
':studyURL': resource.studyUrl,
':signature': signedResource.signature
});
res.status(201).json({ ok: true, id: resource.id });
} catch (e) {
console.log(e);
}
}));
app.get('/api/participations', asyncWrapper(async (req, res) => {
const rows = await db.all('SELECT * FROM participations');
res.json(rows);
}));
app.post('/api/participations', asyncWrapper(async (req, res) => {
const iv = req.body.slice(0, 12);
const data = req.body.slice(12);
const participationId = uuid.v4();
await db.run(`INSERT INTO participations (id, iv, data) VALUES (:id, :iv, :data)`, {
':id': participationId,
':iv': iv,
':data': data,
});
res.status(201).json({
ok: true,
id: participationId,
url: `${process.env['APP_URL']}/participation/${participationId}`
});
}));
app.get('/api/participations/:id', asyncWrapper(async (req, res) => {
const participation = await db.get('SELECT id, iv, data FROM participations WHERE id = ?', req.params.id);
if (!participation) {
return res.status(404).end('Not Found');
}
return res.sendBinary(Buffer.concat([participation.iv, participation.data]));
}));
app.get('/api/rewards/:id?', asyncWrapper(async (req, res) => {
const transactions = await db.all('SELECT value, study, tag, coin FROM ledger WHERE study = :id OR :id IS NULL', req.params.id);
res.json({ transactions });
}));
app.post('/api/rewards', asyncWrapper(async (req, res) => {
const participation = ConfirmedParticipation.deserialize(req.body);
const { id, tag, study, request, signature, value } = participation;
const { reward, publicKey } = await db.get(`
SELECT reward, publicKey
FROM users
JOIN studies ON studies.owner = users.id
WHERE publicKey IS NOT NULL AND studies.id = ?
`, [ study ]);
const entry = issuer.issueReward(participation, publicKey, reward);
const issued = await db.get('SELECT 1 FROM ledger WHERE tag = ?', tag);
if (issued) {
throw new BadRequest('reward for this participation already issued');
}
const row = await db.get('SELECT iv, data FROM participations WHERE id = ?', id);
if (!row) {
throw new BadRequest('participation does not exist');
}
await db.run(`
INSERT INTO ledger (participation, tag, iv, data, study, request, signature, value, coin, chain)
VALUES (:participation, :tag, :iv, :data, :study, :request, :signature, :value, :coin, :chain);
DELETE FROM participations WHERE id = :participation
`, {
':participation': id,
':tag': tag,
':iv': row.iv,
':data': row.data,
':study': study,
':request': request,
':signature': signature,
':value': value,
':coin': entry.transaction.coin,
':chain': entry.signature,
});
res.sendBinary(entry.serialize());
}));
app.post('/api/payout', asyncWrapper(async (req, res) => {
const payout = issuer.checkPayoutRequest(req.body);
const entry = payout.entry;
const receipt = entry.payout.serialize();
await db.run(`
INSERT INTO ledger (participation, tag, iv, data, study, request, signature, value, coin, chain)
VALUES (NULL, :tag, NULL, NULL, NULL, NULL, NULL, :value, :coin, :chain)
`, {
':tag': JSON.stringify({ target: payout.target, recipient: payout.recipient }),
':value': entry.payout.value,
':coin': receipt,
':chain': entry.signature
});
res.json({ receipt: Buffer.from(receipt).toString('base64') });
}));
const { populateDemoData, demoIdentities } = require('./src/utils/demo');
await populateDemoData(db, issuer, ATTRIBUTES);
app.get('/api/demo/credentials', (req, res) => res.json(demoIdentities));
app.get('/api/demo/payouts', asyncWrapper(async (req, res) => {
const rows = await db.all('SELECT * FROM ledger WHERE "participation" IS NULL');
const payouts = [];
for (const row of rows) {
try {
row.tag = JSON.parse(row.tag);
payouts.push({
id: row.id,
recipient: row.tag.recipient,
target: row.tag.target,
value: row.value,
receipt: row.coin.toString('base64')
});
} catch {
// ignore
}
}
res.json(payouts);
}));
app.get('/api/demo/resetdemo', (req, res) => res.set('content-type', 'text/html').end(`
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<form method="POST">
<input type="submit" value="Reset Demo (DELETES ALL DATA)" />
</form>
</body>
</html>
`));
app.post('/api/demo/resetdemo', asyncWrapper(async (req, res) => {
await db.run('DROP TABLE users');
await db.run('DROP TABLE issued');
await db.run('DROP TABLE studies');
await db.run('DROP TABLE ledger');
await db.run('DROP TABLE participations');
await db.run('DROP TABLE migrations');
await db.close();
res.set('refresh', '10;url=/').set('content-type', 'text/html').end(`
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
</head>
<body>
<progress />
</body>
</html>
`);
process.exit(1);
}));
app.listen(process.env['PORT'], () => console.log(`listening at http://localhost:${process.env['PORT']}`));
})();