-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
395 lines (359 loc) · 9.75 KB
/
server.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import express from "express";
import Hypercore from "hypercore";
import Hyperbee from "hyperbee";
import cors from "cors";
import { encode, decode } from "@ipld/dag-cbor";
import { env } from "node:process";
import bodyParser from "body-parser";
import { expressjwt } from "express-jwt";
import assert from "node:assert";
import { getPublicKeyAsync } from "@noble/ed25519";
import { CID } from "multiformats";
import {
dbAddRelation,
dbAppend,
dbPut,
setSigningKey,
NotArrayError,
} from "./src/dbPut.js";
import { keyFromPem } from "./src/signAttestation.js";
import {
encodeFromType,
indexFindMatches,
indexList,
indexPut,
} from "./src/dbIndex.js";
import { NeedsKeyError, dbGet, dbRawValue } from "./src/dbGet.js";
// Last import
import "dotenv/config";
import { attToVC } from "./src/vc.js";
import { DecryptError } from "./src/decryptValue.js";
const sigPrivKey = await keyFromPem(env.HYPERBEE_SIGKEY_PATH);
setSigningKey(sigPrivKey);
const sigPubKey = await getPublicKeyAsync(sigPrivKey);
// Prevent leaking error msgs
// https://expressjs.com/en/advanced/best-practice-performance.html#set-node_env-to-production
env.NODE_ENV = "production";
const app = express();
const port = env.PORT ?? 3001;
const corePath = env.HYPERCORE ?? "server.hypercore";
// Setup hyperbee
const core = new Hypercore(corePath);
await core.ready();
const db = new Hyperbee(core, {
keyEncoding: "binary",
valueEncoding: "binary",
});
// CORS
app.use(
cors({
origin(origin, callback) {
// Allow all origins because authentication is checked
// This also allows non-browser clients that don't set the Origin header
callback(null, true);
},
credentials: true,
})
);
// Allow access to raw POST body bytes
app.use(bodyParser.raw({ type: () => true }));
// JWT checking (can be disabled)
if (env.JWT_SECRET !== "DISABLE_JWT_FOR_DEBUGGING_ONLY") {
app.use(
expressjwt({
secret: env.JWT_SECRET,
algorithms: ["HS256"],
}).unless({ method: ["OPTIONS", "GET"] })
);
}
// Make 401 error visible to user
app.use((err, req, res, next) => {
if (err.name === "UnauthorizedError") {
res.status(401).send("401 Unauthorized");
} else {
next(err);
}
});
/// Routes ///
// All routes are documented in docs/http.md rather than here.
// Search index (data in query params)
// CIDs are returned
app.get("/v1/i", async (req, res) => {
if (
req.query.query !== "match" &&
req.query.query !== "intersect" &&
req.query.query !== "list"
) {
res.status(400).send("only match/intersect/list queries are supported");
return;
}
if (req.query.query === "list") {
const vals = await indexList(db, req.query.key);
res.type("application/cbor");
res.send(Buffer.from(encode(vals)));
return;
}
let encodedValue;
try {
if (req.query.type === "str-array") {
encodedValue = encodeFromType(JSON.parse(req.query.val), req.query.type);
} else {
encodedValue = encodeFromType(req.query.val, req.query.type);
}
} catch (e) {
res.status(400).send(e.message);
return;
}
let cids;
if (req.query.type === "str-array") {
if (req.query.query !== "intersect") {
res.status(400).send("query type not supported with str-array");
return;
}
// Return all matches for all parts of array
// XXX this is quadratic-time-ish
const indexPromises = [];
for (const item of encodedValue) {
indexPromises.push(indexFindMatches(db, req.query.key, item));
}
const results = await Promise.all(indexPromises); // Array of CID string arrays
cids = new Set(); // Prevent duplicate matches
for (const result of results) {
for (const cid of result) {
cids.add(cid);
}
}
cids = Array.from(cids); // Convert back to array for encoding
} else {
// Regular type with just a single value
cids = await indexFindMatches(db, req.query.key, encodedValue);
}
if (req.query.names !== "1") {
res.type("application/cbor");
res.send(Buffer.from(encode(cids)));
return;
}
const ret = [];
for (const cid of cids) {
// eslint-disable-next-line no-await-in-loop
let name = await dbRawValue(db, cid, "title");
if (!name || typeof name !== "string") {
// eslint-disable-next-line no-await-in-loop
name = await dbRawValue(db, cid, "name");
}
if (!name || typeof name !== "string") {
name = "";
}
ret.push({ name, cid });
}
res.type("application/cbor");
res.send(Buffer.from(encode(ret)));
});
// Get one attestation
app.get("/v1/c/:cid/:attr", async (req, res, next) => {
let encKey = false;
if (req.query.key) {
encKey = Buffer.from(req.query.key, "base64url");
}
let leaveEncrypted = false;
if (req.query.decrypt === "0") {
leaveEncrypted = true;
}
let att;
try {
att = await dbGet(
db,
req.params.cid,
req.params.attr,
sigPubKey,
encKey,
false,
leaveEncrypted
);
if (att === null) {
res.status(404).send("attribute not found");
return;
}
} catch (e) {
if (e instanceof NeedsKeyError) {
res.status(400).send("needs encryption key");
return;
}
if (e instanceof DecryptError) {
res.status(400).send("failed to decrypt");
return;
}
// Unexpected error, give up
next(e);
return;
}
if (!req.query.format || req.query.format === "cbor") {
res.type("application/cbor");
res.send(Buffer.from(encode(att)));
} else if (req.query.format === "vc") {
res.type("application/json");
res.send(attToVC(att));
} else {
res.status(400).send("invalid format requested");
}
});
// Get all attestations for CID
app.get("/v1/c/:cid", async (req, res) => {
const metadata = {};
for await (const { key, value } of db.createReadStream({
gte: `att/${req.params.cid}`,
lt: `att/${req.params.cid}0`, // 0 is the symbol after / in binary, so the range of keys is the keys in the format <cid>/<any>
})) {
// Key of map is database key but with prefix (att), CID, and slash separators removed
// Prefix + CID-ending slash is 5 chars
metadata[key.slice(req.params.cid.length + 5)] = decode(value);
}
res.type("application/cbor");
res.send(Buffer.from(encode(metadata)));
});
// Get all CIDs
app.get("/v1/cids", async (req, res) => {
const cids = new Set();
for await (const { key } of db.createReadStream({
gt: "att/",
lt: "att0", // 0 is the symbol after / in binary
})) {
// keys are in the form "att/<cid>/<attr>", we only want the CID
cids.add(key.toString().split("/")[1]);
}
res.type("application/cbor");
res.send(Buffer.from(encode(Array.from(cids))));
});
app.get("/v1/atts", async (req, res) => {
const atts = [];
for await (const { value } of db.createReadStream({
gt: "att/",
lt: "att0", // 0 is the symbol after / in binary
})) {
atts.push(decode(value));
}
res.type("application/cbor");
res.send(Buffer.from(encode(atts)));
});
// Set a single attestation for a CID
app.post("/v1/c/:cid/:attr", async (req, res, next) => {
let data;
try {
data = decode(new Uint8Array(req.body));
assert.ok("value" in data);
assert.ok(data.encKey === false || data.encKey instanceof Uint8Array);
} catch (e) {
console.log(e);
res.status(400).send();
return;
}
try {
if (req.query.append === "1") {
await dbAppend(
db,
req.params.cid,
req.params.attr,
data.value,
data.encKey
);
} else {
await dbPut(db, req.params.cid, req.params.attr, data.value, data.encKey);
}
} catch (e) {
if (e instanceof NotArrayError) {
res.status(400).send("non-array data stored under this attribute");
return;
}
next(e);
return;
}
res.status(200).send();
});
// Set multiple attestations for a CID
app.post("/v1/c/:cid", async (req, res, next) => {
let data;
try {
data = decode(new Uint8Array(req.body));
assert.equal(typeof data, "object");
} catch (e) {
console.log(e);
res.status(400).send();
return;
}
try {
const batch = db.batch();
const putPromises = [];
for (const entry of data) {
try {
assert.ok(entry.encKey == null || entry.encKey instanceof Uint8Array);
} catch (e) {
res.status(400).send();
return;
}
putPromises.push(
dbPut(batch, req.params.cid, entry.key, entry.value, entry.encKey)
);
if (
req.query.index === "1" &&
entry.type != null &&
entry.encKey == null
) {
let encodedValue;
try {
encodedValue = encodeFromType(entry.value, entry.type);
} catch (e) {
res.status(400).send(e.message);
return;
}
putPromises.push(
indexPut(batch, entry.key, encodedValue, req.params.cid)
);
}
}
await Promise.all(putPromises);
await batch.flush();
} catch (e) {
next(e);
return;
}
res.status(200).send();
});
// Add a relationship
app.post("/v1/rel/:cid", async (req, res, next) => {
let data;
try {
data = decode(new Uint8Array(req.body));
assert.ok("type" in data);
assert.ok("relation_type" in data);
assert.ok("cid" in data);
assert.ok(data.type === "children" || data.type === "parents");
} catch (e) {
console.log(e);
res.status(400).send();
return;
}
try {
await dbAddRelation(
db,
req.params.cid,
data.type,
data.relation_type,
data.cid
);
await dbAddRelation(
db,
data.cid.toString(),
data.type === "children" ? "parents" : "children", // invert
data.relation_type,
CID.parse(req.params.cid)
);
} catch (e) {
next(e);
return;
}
res.status(200).send();
});
/// End of routes ///
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});