-
Notifications
You must be signed in to change notification settings - Fork 10
/
connectionService.ts
334 lines (310 loc) · 10.5 KB
/
connectionService.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
import { v4 as uuidv4 } from "uuid";
import {
ConnectionDetails,
ConnectionHistoryItem,
ConnectionHistoryType,
ConnectionKeriEventTypes,
ConnectionKeriStateChangedEvent,
ConnectionNoteDetails,
ConnectionNoteProps,
ConnectionShortDetails,
ConnectionStatus,
KeriContact,
OOBIScan,
ScanOOBIRes,
} from "../agent.types";
import { AgentService } from "./agentService";
import { Agent } from "../agent";
import { BasicRecord } from "../records";
import { RecordType } from "../../storage/storage.types";
import { PreferencesKeys, PreferencesStorage } from "../../storage";
import { waitAndGetDoneOp } from "./utils";
class ConnectionService extends AgentService {
// static readonly NOT_FOUND_DOMAIN_CONFIG_ERROR_MSG =
// "No domain found in config";
static readonly COULD_NOT_CREATE_OOB_VIA_MEDIATOR =
"Could not create new mediator oob invitation";
static readonly INVALID_CONNECTIONLESS_MSG =
"Invalid connectionless OOBI - does not contain d_m parameter";
static readonly CONNECTION_NOTE_RECORD_NOT_FOUND =
"Connection note record not found";
static readonly CONNECTION_KERI_METADATA_RECORD_NOT_FOUND =
"Connection keri metadata record not found";
static readonly FAILED_TO_RESOLVE_OOBI =
"Failed to resolve OOBI, operation not completing...";
static resolvedOobi: { [key: string]: any } = {};
onConnectionKeriStateChanged(
callback: (event: ConnectionKeriStateChangedEvent) => void
) {
this.eventService.on(
ConnectionKeriEventTypes.ConnectionKeriStateChanged,
async (event: ConnectionKeriStateChangedEvent) => {
callback(event);
}
);
}
async receiveInvitationFromUrl(url: string): Promise<ScanOOBIRes> {
this.eventService.emit<ConnectionKeriStateChangedEvent>({
type: ConnectionKeriEventTypes.ConnectionKeriStateChanged,
payload: {
connectionId: undefined,
status: ConnectionStatus.PENDING,
},
});
const operation = await this.resolveOobi(url);
const connectionId = operation.response.i;
await this.createConnectionKeriMetadata(connectionId, {
alias: operation.alias,
oobi: url,
});
// @TODO - foconnor: This is temporary for ease of development, will be removed soon.
// This will take our first KERI identifier and get the server to resolve it, so that the connection is resolved from both sides and we can issue to this wallet using its API.
if (url.includes("dev.keria.cf-keripy.metadata.dev.cf-deployments.org")) {
// This is inefficient but it will change going forward.
const aids = await Agent.agent.identifiers.getIdentifiers();
if (aids.length > 0) {
let userName;
try {
userName = (
await PreferencesStorage.get(PreferencesKeys.APP_USER_NAME)
).userName as string;
} catch (error) {
if (
(error as Error).message !==
`${PreferencesStorage.KEY_NOT_FOUND} ${PreferencesKeys.APP_USER_NAME}`
) {
throw error;
}
}
// signifyName should always be set
const oobi = await Agent.agent.connections.getKeriOobi(
aids[0].signifyName,
userName
);
await (
await fetch(
"https://dev.credentials.cf-keripy.metadata.dev.cf-deployments.org/resolveOobi",
{
method: "POST",
body: JSON.stringify({ oobi }),
headers: {
"Content-Type": "application/json",
},
}
)
).json();
} else {
// eslint-disable-next-line no-console
console.warn(
"Please create a KERI AID first before scanning an OOBI of the deployed server, if you wish to be issued an ACDC automatically."
);
}
}
await this.eventService.emit<ConnectionKeriStateChangedEvent>({
type: ConnectionKeriEventTypes.ConnectionKeriStateChanged,
payload: {
connectionId: operation.response.i,
status: ConnectionStatus.CONFIRMED,
},
});
return new URL(url).searchParams.get("delegated")
? {
oobiType: OOBIScan.Delegated,
delegatePrefix: "TODO", // TODO: must recheck when confirmed with Fergal
}
: { oobiType: OOBIScan.Aid };
}
async getConnections(): Promise<ConnectionShortDetails[]> {
const connectionsDetails: ConnectionShortDetails[] = [];
const connectionKeriMetadatas = await this.getAllConnectionKeriMetadata();
connectionKeriMetadatas.forEach(async (connection) => {
connectionsDetails.push(this.getConnectionKeriShortDetails(connection));
});
return connectionsDetails;
}
private getConnectionKeriShortDetails(
record: BasicRecord
): ConnectionShortDetails {
return {
id: record.id,
label: record.content?.alias as string,
connectionDate: record.createdAt.toISOString(),
status: ConnectionStatus.CONFIRMED,
oobi: record.content?.oobi as string,
};
}
async getConnectionById(id: string): Promise<ConnectionDetails> {
const connection = await this.signifyClient.contacts().get(id);
return {
label: connection?.alias,
id: connection.id,
status: ConnectionStatus.CONFIRMED,
connectionDate: (
await this.getConnectionKeriMetadataById(connection.id)
).createdAt.toISOString(),
serviceEndpoints: [connection.oobi],
notes: await this.getConnectNotesByConnectionId(connection.id),
};
}
async deleteConnectionById(id: string): Promise<void> {
await this.basicStorage.deleteById(id);
// await this.signifyApi.deleteContactById(id); TODO: must open when Keria runs well
const notes = await this.getConnectNotesByConnectionId(id);
for (const note of notes) {
this.basicStorage.deleteById(note.id);
}
}
async getConnectionKeriShortDetailById(
id: string
): Promise<ConnectionShortDetails> {
const metadata = await this.getConnectionKeriMetadataById(id);
return this.getConnectionKeriShortDetails(metadata);
}
async createConnectionNote(
connectionId: string,
note: ConnectionNoteProps
): Promise<void> {
await this.basicStorage.save({
id: uuidv4(),
content: note,
tags: {
connectionId,
type: RecordType.CONNECTION_NOTE,
},
});
}
async updateConnectionNoteById(
connectionNoteId: string,
note: ConnectionNoteProps
) {
const noteRecord = await this.basicStorage.findById(connectionNoteId);
if (!noteRecord) {
throw new Error(ConnectionService.CONNECTION_NOTE_RECORD_NOT_FOUND);
}
noteRecord.content = note;
await this.basicStorage.update(noteRecord);
}
async deleteConnectionNoteById(connectionNoteId: string) {
return this.basicStorage.deleteById(connectionNoteId);
}
async getKeriOobi(identifier: string, alias?: string): Promise<string> {
const metadata = await this.identifierStorage.getIdentifierMetadata(
identifier
);
const result = await this.signifyClient.oobis().get(metadata.signifyName);
const oobi = result.oobis[0];
const encodedParams: Record<string, string> = {};
if (alias) {
encodedParams["name"] = alias;
}
if (metadata.delegated) {
encodedParams["delegated"] = "true";
}
return Object.keys(encodedParams).length > 0
? `${oobi}?${Object.keys(encodedParams)
.map(function (key) {
return (
encodeURIComponent(key) +
"=" +
encodeURIComponent(encodedParams[key])
);
})
.join("&")}`
: oobi;
}
private async createConnectionKeriMetadata(
connectionId: string,
metadata?: Record<string, unknown>
): Promise<void> {
await this.basicStorage.save({
id: connectionId,
content: metadata || {},
tags: {
type: RecordType.CONNECTION_KERI_METADATA,
},
});
}
private async getConnectionKeriMetadataById(
connectionId: string
): Promise<BasicRecord> {
const connectionKeri = await this.basicStorage.findById(connectionId);
if (!connectionKeri) {
throw new Error(
ConnectionService.CONNECTION_KERI_METADATA_RECORD_NOT_FOUND
);
}
return connectionKeri;
}
async getAllConnectionKeriMetadata(): Promise<BasicRecord[]> {
const connectionKeris = await this.basicStorage.findAllByQuery({
type: RecordType.CONNECTION_KERI_METADATA,
});
return connectionKeris;
}
async getConnectionHistoryById(
connectionId: string
): Promise<ConnectionHistoryItem[]> {
let histories: ConnectionHistoryItem[] = [];
const credentialRecords =
await this.credentialStorage.getCredentialMetadataByConnectionId(
connectionId
);
histories = histories.concat(
credentialRecords.map((record) => {
return {
type: ConnectionHistoryType.CREDENTIAL_ACCEPTED,
timestamp: record.createdAt.toISOString(),
credentialType: record.credentialType,
};
})
);
return histories;
}
async syncKeriaContacts() {
const signifyContacts = await this.signifyClient.contacts().list();
const storageContacts = await this.getAllConnectionKeriMetadata();
const unSyncedData = signifyContacts.filter(
(contact: KeriContact) =>
!storageContacts.find((item: BasicRecord) => contact.id == item.id)
);
if (unSyncedData.length) {
//sync the storage with the signify data
for (const contact of unSyncedData) {
await this.createConnectionKeriMetadata(contact.id, {
alias: contact.alias,
oobi: contact.oobi,
});
}
}
}
async resolveOobi(url: string): Promise<any> {
if (ConnectionService.resolvedOobi[url]) {
return ConnectionService.resolvedOobi[url];
}
const alias = new URL(url).searchParams.get("name") ?? uuidv4();
let operation = await this.signifyClient.oobis().resolve(url, alias);
operation = await waitAndGetDoneOp(this.signifyClient, operation);
if (!operation.done) {
throw new Error(ConnectionService.FAILED_TO_RESOLVE_OOBI);
}
const Oobi = { ...operation, alias };
ConnectionService.resolvedOobi[url] = Oobi;
return Oobi;
}
private async getConnectNotesByConnectionId(
connectionId: string
): Promise<ConnectionNoteDetails[]> {
const notes = await this.basicStorage.findAllByQuery({
connectionId,
type: RecordType.CONNECTION_NOTE,
});
return notes.map((note) => {
return {
id: note.id,
title: note.content.title as string,
message: note.content.message as string,
};
});
}
}
export { ConnectionService };