-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathPluto.ts
460 lines (378 loc) · 13.1 KB
/
Pluto.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
import type { MangoQuery } from "rxdb";
import * as Domain from "../domain";
import * as Models from "./models";
import { PeerDID } from "../peer-did/PeerDID";
import { BackupManager } from "./backup/BackupManager";
import { PlutoRepositories, repositoryFactory } from "./repositories";
import { Arrayable, asArray, notNil } from "../utils";
import { Startable } from "../domain/protocols/Startable";
import { Version } from "../domain/backup";
/**
* Pluto implementation
*
* Structure:
* - Pluto class is an orchestration layer
* - Repositories handle mapping Domain <-> Storable Models
* - Models suggest db structure
* - Store abstracts db implementation
*
* Pluto:
* - always handles Domain classes
* - manage relationships
* - handle logic and concepts
* - throw known Errors
* - return null
* - naming convention
* - (get/store) (Domain name Pluralized) ie getCredentials
*
* Models:
* - naming convention
* - alias for optional names
* - name for required identifiers
* - dataJson for JSON.stringified objects
*
* Store:
* - simplified interface
* - crud interactions
* - only use Models
*
*
* Future:
* - versioning
* - migrations
*/
export namespace Pluto {
export interface Store {
/**
* Handle any necessary startup.
* Will be called first before any usage, if provided.
*/
start?(): Promise<void>;
/**
* Handle any necessary teardown.
*/
stop?(): Promise<void>;
/**
* Run a query to fetch data from the Store
*
* @param table table name
* @param query a MangoQuery object, a set of values and operators defining the query
*
* properties within an object will be AND'ed
* different objects will be OR'd
*
* @example
* search for a model in TableOne with uuid and name
* ```ts
* store.query("TableOne", { selector: { uuid: "1", name: "eg" }})
* ```
* @example
* search for models in TableOne with uuid of 1 or 2
* ```ts
* store.query("TableOne", { selector: { $or: [{ uuid: "1" }, { uuid: "2" }] }})
* ```
* @example
* search for all models in TableOne
* ```ts
* store.query("TableOne")
* ```
*
* @returns relevant Models
*/
query<T extends Models.Model>(table: string, query?: MangoQuery<T>): Promise<T[]>;
/**
* Persist new data in the Store.
*
* @param table table name
* @param model object to save
*/
insert<T extends Models.Model>(table: string, model: T): Promise<void>;
/**
* Updating a new row in the Store
* @param table
* @param model
*/
update<T extends Models.Model>(table: string, model: T): Promise<void>;
/**
* Deleting a row in the Store
* @param table
* @param model
*/
delete(table: string, uuid: string): Promise<void>;
}
}
export class Pluto extends Startable.Controller implements Domain.Pluto {
public BackupMgr: BackupManager;
private Repositories: PlutoRepositories;
constructor(
private readonly store: Pluto.Store,
private readonly keyRestoration: Domain.KeyRestoration
) {
super();
this.Repositories = repositoryFactory(store, keyRestoration);
this.BackupMgr = new BackupManager(this, this.Repositories);
}
// TODO breaking change workarounds
override async start(): Promise<any> {
return await super.start();
}
override async stop(): Promise<any> {
return await super.stop();
}
protected async _start() {
if (notNil(this.store.start)) {
await this.store.start();
}
}
protected async _stop() {
if (notNil(this.store.stop)) {
await this.store.stop();
}
}
/** Backups **/
backup(version?: Version) {
return this.BackupMgr.backup(version);
}
restore(backup: Domain.Backup.Schema) {
return this.BackupMgr.restore(backup);
}
async deleteMessage(id: string): Promise<void> {
const message = await this.Repositories.Messages.findOne({ id });
//TODO: Improve error handling
if (message) {
await this.Repositories.Messages.delete(message.uuid);
}
}
/** Credentials **/
async storeCredential(credential: Domain.Credential): Promise<void> {
await this.Repositories.Credentials.save(credential);
}
async getAllCredentials(): Promise<Domain.Credential[]> {
return this.Repositories.Credentials.get();
}
async revokeCredential(credential: Domain.Credential): Promise<void> {
if (!credential || !credential.isStorable()) {
throw new Error("Credential not found or invalid");
}
credential.properties.set("revoked", true);
const credentialModel = await this.Repositories.Credentials.toModel(credential);
await this.Repositories.Credentials.update(credentialModel);
}
/** Credential Metadata **/
async storeCredentialMetadata(metadata: Domain.CredentialMetadata): Promise<void> {
await this.Repositories.CredentialMetadata.save(metadata);
}
async getCredentialMetadata(name: string): Promise<Domain.CredentialMetadata | null> {
return await this.Repositories.CredentialMetadata.findOne({ name });
}
/** LinkSecret **/
async storeLinkSecret(linkSecret: Domain.LinkSecret): Promise<void> {
return await this.Repositories.LinkSecrets.save(linkSecret);
}
async getLinkSecret(name: string = Domain.LinkSecret.defaultName): Promise<Domain.LinkSecret | null> {
return await this.Repositories.LinkSecrets.findOne({ alias: name });
}
/** PrivateKeys **/
async storePrivateKey(privateKey: Domain.PrivateKey): Promise<void> {
await this.Repositories.Keys.save(privateKey);
}
async getDIDPrivateKeysByDID(did: Domain.DID): Promise<Domain.PrivateKey[]> {
const links = await this.Repositories.DIDKeyLinks.getModels({ selector: { didId: did.uuid } });
const $or = links.map(x => ({ uuid: x.keyId }));
const keys = await this.Repositories.Keys.get({ selector: { $or } });
return keys;
}
/** DIDs **/
async storeDID(did: Domain.DID, keys?: Arrayable<Domain.PrivateKey>, alias?: string): Promise<void> {
await this.Repositories.DIDs.save(did, alias);
for (const key of asArray(keys)) {
await this.Repositories.Keys.save(key);
await this.Repositories.DIDKeyLinks.insert({
alias,
didId: did.uuid,
keyId: key.uuid
});
}
}
/** Prism DIDs **/
async storePrismDID(did: Domain.DID, privateKey: Domain.PrivateKey, alias?: string): Promise<void> {
await this.Repositories.DIDs.save(did, alias);
await this.Repositories.Keys.save(privateKey);
await this.Repositories.DIDKeyLinks.insert({
alias,
didId: did.uuid,
keyId: privateKey.uuid
});
}
async getAllPrismDIDs(): Promise<Domain.PrismDID[]> {
const dids = await this.Repositories.DIDs.find({ method: "prism" });
const prismDIDS: Domain.PrismDID[] = [];
for (const did of dids) {
const dbDids = await this.getPrismDIDS(did.uuid);
for (const prismDID of dbDids) {
prismDIDS.push(prismDID);
}
}
return prismDIDS;
}
private async getPrismDIDS(didId: string): Promise<Domain.PrismDID[]> {
const links = await this.Repositories.DIDKeyLinks.getModels({ selector: { didId } });
return Promise.all(
links.map(async (link) => {
const did = await this.Repositories.DIDs.byUUID(link.didId);
const key = await this.Repositories.Keys.byUUID(link.keyId);
if (!did || !key) {
throw new Error("PrismDID not found");
}
const prismDID = new Domain.PrismDID(did, key, link.alias);
return prismDID;
})
);
}
/** Peer DIDs **/
async storePeerDID(did: Domain.DID, privateKeys: Domain.PrivateKey[]): Promise<void> {
await this.Repositories.DIDs.save(did);
for (const key of privateKeys) {
await this.Repositories.Keys.save(key);
await this.Repositories.DIDKeyLinks.insert({ didId: did.uuid, keyId: key.uuid });
}
}
async getAllPeerDIDs(): Promise<PeerDID[]> {
const allDids = await this.Repositories.DIDs.find({ method: "peer" });
const allLinks = await this.Repositories.DIDKeyLinks.getModels({
selector: { $or: allDids.map(x => ({ didId: x.uuid })) }
});
const allKeys = await this.Repositories.Keys.get({
selector: { $or: allLinks.map(x => ({ uuid: x.keyId })) }
});
const peerDids = allDids.map(did => {
const keyIds = allLinks.filter(x => x.didId === did.uuid).map(x => x.keyId);
const keys = allKeys.filter(x => keyIds.includes(x.uuid));
const peerDid = new PeerDID(
did,
// TODO: remove this when PeerDIDs are updated to use PrivateKey
keys.map(x => ({
keyCurve: Domain.getKeyCurveByNameAndIndex(x.curve, x.index),
value: x.getEncoded()
}))
);
return peerDid;
});
return peerDids;
}
/** Messages **/
async storeMessage(message: Domain.Message): Promise<void> {
await this.Repositories.Messages.save(message);
}
async storeMessages(messages: Domain.Message[]): Promise<void> {
for (const msg of messages) {
await this.Repositories.Messages.save(msg);
}
}
async getMessage(id: string): Promise<Domain.Message | null> {
return await this.Repositories.Messages.findOne({ id });
}
async getAllMessages(): Promise<Domain.Message[]> {
return this.Repositories.Messages.get();
}
/** DID Pairs **/
async storeDIDPair(host: Domain.DID, receiver: Domain.DID, alias: string): Promise<void> {
await this.Repositories.DIDs.save(host);
await this.Repositories.DIDs.save(receiver);
await this.Repositories.DIDLinks.insert({
alias,
role: Models.DIDLink.role.pair,
hostId: host.uuid,
targetId: receiver.uuid
});
}
async getAllDidPairs(): Promise<Domain.DIDPair[]> {
const links = await this.Repositories.DIDLinks.getModels({ selector: { role: Models.DIDLink.role.pair } });
const didPairs = await Promise.all(links.map(x => this.mapDIDPairToDomain(x)));
const filtered = didPairs.filter((x): x is Domain.DIDPair => x != null);
return filtered;
}
async getPairByDID(did: Domain.DID): Promise<Domain.DIDPair | null> {
const links = await this.Repositories.DIDLinks.getModels({
selector: {
$or: [
{ role: Models.DIDLink.role.pair, hostId: did.uuid },
{ role: Models.DIDLink.role.pair, targetId: did.uuid }
]
}
});
// ?? this seems presumptuous? couldnt hostDID be re-used?
const link = this.onlyOne(links);
const didPair = this.mapDIDPairToDomain(link);
return didPair;
}
async getPairByName(alias: string): Promise<Domain.DIDPair | null> {
const links = await this.Repositories.DIDLinks.getModels(
{
selector: { alias, role: Models.DIDLink.role.pair }
});
const link = this.onlyOne(links);
const didPair = this.mapDIDPairToDomain(link);
return didPair;
}
private async mapDIDPairToDomain(link: Models.DIDLink): Promise<Domain.DIDPair | null> {
const hostDID = await this.Repositories.DIDs.byUUID(link.hostId);
const targetDID = await this.Repositories.DIDs.byUUID(link.targetId);
const alias = link.alias ?? "";
if (!hostDID || !targetDID) {
return null;
}
const didPair = new Domain.DIDPair(hostDID, targetDID, alias);
return didPair;
}
/** Mediators **/
async getAllMediators(): Promise<Domain.Mediator[]> {
const links = await this.Repositories.DIDLinks.getModels({
selector: {
$or: [
{ role: Models.DIDLink.role.mediator },
{ role: Models.DIDLink.role.routing },
]
}
});
const hostIds = links.map(x => x.hostId).filter((x, i, s) => s.indexOf(x) === i);
const result = await Promise.all(
hostIds.map(async hostId => {
const mediatorLink = links.find(x => x.hostId === hostId && x.role === Models.DIDLink.role.mediator);
const routingLink = links.find(x => x.hostId === hostId && x.role === Models.DIDLink.role.routing);
if (!mediatorLink || !routingLink) {
throw new Error();
}
const hostDID = await this.Repositories.DIDs.byUUID(hostId);
const mediatorDID = await this.Repositories.DIDs.byUUID(mediatorLink.targetId);
const routingDID = await this.Repositories.DIDs.byUUID(routingLink.targetId);
if (!hostDID || !mediatorDID || !routingDID) {
throw new Error();
}
const domain: Domain.Mediator = { hostDID, mediatorDID, routingDID };
return domain;
})
);
return result;
}
async storeMediator(mediator: Domain.Mediator): Promise<void> {
await this.Repositories.DIDs.save(mediator.hostDID);
await this.Repositories.DIDs.save(mediator.mediatorDID);
await this.Repositories.DIDs.save(mediator.routingDID);
await this.Repositories.DIDLinks.insert({
role: Models.DIDLink.role.mediator,
hostId: mediator.hostDID.uuid,
targetId: mediator.mediatorDID.uuid
});
await this.Repositories.DIDLinks.insert({
role: Models.DIDLink.role.routing,
hostId: mediator.hostDID.uuid,
targetId: mediator.routingDID.uuid
});
}
private onlyOne<T>(arr: T[]): T {
const item = arr.at(0);
if (!item || arr.length !== 1) throw new Error("something wrong");
return item;
}
}