pico-engine 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -0
- package/dist/RulesetRegistryLoaderFs.js +2 -0
- package/dist/auth/AuthService.js +77 -13
- package/dist/bundledKrl.js +42 -0
- package/dist/identity/IdentityService.js +340 -0
- package/dist/identity/crossPico.js +238 -0
- package/dist/identity/dbHelpers.js +67 -0
- package/dist/identity/didcommIngressRoutes.js +44 -0
- package/dist/identity/didoModule.js +252 -0
- package/dist/identity/errors.js +11 -0
- package/dist/identity/establishSubscription.js +97 -0
- package/dist/identity/index.js +15 -0
- package/dist/identity/peerDid.js +9 -0
- package/dist/identity/skyIntro.js +194 -0
- package/dist/identity/skyProtocol.js +105 -0
- package/dist/identity/store.js +233 -0
- package/dist/identity/types.js +13 -0
- package/dist/identity/veramoDidComm.js +260 -0
- package/dist/identity/webvh.js +266 -0
- package/dist/identity/webvhCrypto.js +128 -0
- package/dist/identity/webvhRoutes.js +61 -0
- package/dist/index.js +26 -2
- package/dist/oauth/OAuthService.js +32 -0
- package/dist/provisionRoot.js +56 -18
- package/dist/server.js +52 -1
- package/dist/uiChannelPolicies.js +48 -0
- package/krl/io.picolabs.did-o.krl +5 -1
- package/krl/io.picolabs.pico-engine-ui.krl +106 -18
- package/krl/io.picolabs.subscription.krl +242 -23
- package/krl/io.picolabs.wrangler.krl +221 -12
- package/package.json +29 -6
- package/public/docs/krl/_sidebar.md +1 -0
- package/public/docs/krl/discovery.md +144 -0
- package/public/docs/krl/modules/ctx.md +8 -0
- package/public/docs/krl/modules/event.md +41 -0
- package/public/pico-engine-ui.css +1 -1
- package/public/pico-engine-ui.js +21 -18
- package/src/RulesetRegistryLoaderFs.ts +2 -0
- package/src/auth/AuthService.ts +109 -14
- package/src/bundledKrl.ts +42 -0
- package/src/identity/IdentityService.ts +571 -0
- package/src/identity/crossPico.ts +371 -0
- package/src/identity/dbHelpers.ts +51 -0
- package/src/identity/didcommIngressRoutes.ts +54 -0
- package/src/identity/didoModule.ts +388 -0
- package/src/identity/errors.ts +8 -0
- package/src/identity/establishSubscription.ts +160 -0
- package/src/identity/index.ts +24 -0
- package/src/identity/peerDid.ts +22 -0
- package/src/identity/skyIntro.ts +321 -0
- package/src/identity/skyProtocol.ts +192 -0
- package/src/identity/store.ts +343 -0
- package/src/identity/types.ts +63 -0
- package/src/identity/veramoDidComm.ts +363 -0
- package/src/identity/webvh.ts +325 -0
- package/src/identity/webvhCrypto.ts +200 -0
- package/src/identity/webvhRoutes.ts +64 -0
- package/src/index.ts +31 -2
- package/src/oauth/OAuthService.ts +36 -0
- package/src/provisionRoot.ts +75 -21
- package/src/server.ts +58 -1
- package/src/uiChannelPolicies.ts +49 -0
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import { krl } from "krl-stdlib";
|
|
2
|
+
import { IdentityService } from "./IdentityService";
|
|
3
|
+
import { IdentityError } from "./errors";
|
|
4
|
+
|
|
5
|
+
const QUERY_TIMEOUT = 10000;
|
|
6
|
+
|
|
7
|
+
export default function initDidoModule(identity: IdentityService) {
|
|
8
|
+
const module: krl.Module = {
|
|
9
|
+
myDid: krl.Function([], async function () {
|
|
10
|
+
return identity.getMyDid(this.rsCtx.pico().id);
|
|
11
|
+
}),
|
|
12
|
+
|
|
13
|
+
publicIntro: krl.Function([], async function () {
|
|
14
|
+
return identity.getPublicIntro(this.rsCtx.pico().id);
|
|
15
|
+
}),
|
|
16
|
+
|
|
17
|
+
setPublicIntro: krl.Action(["enabled"], async function (enabled: boolean) {
|
|
18
|
+
return identity.setPublicIntro(this.rsCtx.pico().id, !!enabled);
|
|
19
|
+
}),
|
|
20
|
+
|
|
21
|
+
acceptsPublicIntro: krl.Function([], async function () {
|
|
22
|
+
return identity.acceptsPublicIntro(this.rsCtx.pico().id);
|
|
23
|
+
}),
|
|
24
|
+
|
|
25
|
+
addRoute: krl.Function(["type", "domain", "rule"], async function (
|
|
26
|
+
type: string,
|
|
27
|
+
domain: string,
|
|
28
|
+
rule: string
|
|
29
|
+
) {
|
|
30
|
+
return identity.addRoute(this.rsCtx.pico().id, type, domain, rule);
|
|
31
|
+
}),
|
|
32
|
+
|
|
33
|
+
generateMessage: krl.Function(["messageOptions"], async function (
|
|
34
|
+
messageOptions: Parameters<IdentityService["generateMessage"]>[0]
|
|
35
|
+
) {
|
|
36
|
+
return identity.generateMessage(messageOptions);
|
|
37
|
+
}),
|
|
38
|
+
|
|
39
|
+
pack: krl.Function(["message", "from", "to"], async function (
|
|
40
|
+
message: any,
|
|
41
|
+
from: string,
|
|
42
|
+
to: string
|
|
43
|
+
) {
|
|
44
|
+
return identity.packMessage(this.rsCtx.pico().id, message, from, to);
|
|
45
|
+
}),
|
|
46
|
+
|
|
47
|
+
unpack: krl.Function(["message"], async function (message: string) {
|
|
48
|
+
const result = await identity.unpackMessage(
|
|
49
|
+
this.rsCtx.pico().id,
|
|
50
|
+
message
|
|
51
|
+
);
|
|
52
|
+
return [result.message, result.metadata];
|
|
53
|
+
}),
|
|
54
|
+
|
|
55
|
+
route: krl.Function(["message"], async function (message: string) {
|
|
56
|
+
const picoId = this.rsCtx.pico().id;
|
|
57
|
+
const { message: unpacked, metadata: meta } =
|
|
58
|
+
await identity.unpackMessage(picoId, message);
|
|
59
|
+
|
|
60
|
+
const introType = "https://picolabs.org/sky/1.0/intro";
|
|
61
|
+
if (unpacked.type === introType) {
|
|
62
|
+
const allowed = await identity.acceptsPublicIntro(picoId);
|
|
63
|
+
if (!allowed) {
|
|
64
|
+
this.log.info("Rejected unsolicited intro — publicIntro is false", {
|
|
65
|
+
picoId,
|
|
66
|
+
from: unpacked.from,
|
|
67
|
+
});
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const routes = await identity.getRoutes(picoId);
|
|
73
|
+
const body = unpacked as Record<string, any>;
|
|
74
|
+
|
|
75
|
+
if (body.type === "https://picolabs.io/event/1.0/event") {
|
|
76
|
+
this.rsCtx.raiseEvent(
|
|
77
|
+
body.body.domain,
|
|
78
|
+
body.body.name,
|
|
79
|
+
body.body.attrs
|
|
80
|
+
);
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
if (body.type === "https://picolabs.io/query/1.0/query") {
|
|
84
|
+
try {
|
|
85
|
+
await this.useModule(body.body.rid);
|
|
86
|
+
return await this.krl.assertFunction(
|
|
87
|
+
this.module(body.body.rid)![body.body.name]
|
|
88
|
+
)(this, body.body.args);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
return "Unable to query: " + error;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const route = routes[body.type];
|
|
95
|
+
if (route) {
|
|
96
|
+
this.rsCtx.raiseEvent(route.domain, route.name, {
|
|
97
|
+
message: unpacked,
|
|
98
|
+
metadata: meta,
|
|
99
|
+
});
|
|
100
|
+
} else {
|
|
101
|
+
this.log.error("Unknown route for message", { message: unpacked });
|
|
102
|
+
}
|
|
103
|
+
return undefined;
|
|
104
|
+
}),
|
|
105
|
+
|
|
106
|
+
prepareQuery: deprecated("prepareQuery"),
|
|
107
|
+
|
|
108
|
+
sendQuery: krl.Function(["did", "message"], async function (
|
|
109
|
+
did: string,
|
|
110
|
+
message: any
|
|
111
|
+
) {
|
|
112
|
+
const picoId = this.rsCtx.pico().id;
|
|
113
|
+
const from = message.from as string;
|
|
114
|
+
if (!from) {
|
|
115
|
+
throw new IdentityError("sendQuery message missing from", 400);
|
|
116
|
+
}
|
|
117
|
+
const doc = await identity.resolveDid(picoId, did);
|
|
118
|
+
const endpoint = extractDidcommEndpoint(doc);
|
|
119
|
+
const packed = await identity.packMessage(picoId, message, from, did);
|
|
120
|
+
const response = await Promise.race([
|
|
121
|
+
this.krl.assertAction(this.module("http")!["post"])(this, {
|
|
122
|
+
url: endpoint,
|
|
123
|
+
json: packed,
|
|
124
|
+
}),
|
|
125
|
+
new Promise((_resolve, reject) =>
|
|
126
|
+
setTimeout(() => reject(new Error("timeout")), QUERY_TIMEOUT)
|
|
127
|
+
),
|
|
128
|
+
]).catch(() => "Query timed out");
|
|
129
|
+
|
|
130
|
+
if (
|
|
131
|
+
response &&
|
|
132
|
+
typeof response === "object" &&
|
|
133
|
+
(response as any).status_code === 200
|
|
134
|
+
) {
|
|
135
|
+
return JSON.parse((response as any).content).directives[0].options;
|
|
136
|
+
}
|
|
137
|
+
return response;
|
|
138
|
+
}),
|
|
139
|
+
|
|
140
|
+
send: krl.Function(["did", "message"], async function (
|
|
141
|
+
did: string,
|
|
142
|
+
message: any
|
|
143
|
+
) {
|
|
144
|
+
const picoId = this.rsCtx.pico().id;
|
|
145
|
+
const from = message.from as string;
|
|
146
|
+
const doc = await identity.resolveDid(picoId, did);
|
|
147
|
+
const endpoint = extractDidcommEndpoint(doc);
|
|
148
|
+
const packed = await identity.packMessage(picoId, message, from, did);
|
|
149
|
+
await this.krl.assertAction(this.module("http")!["post"])(this, {
|
|
150
|
+
url: endpoint,
|
|
151
|
+
json: packed,
|
|
152
|
+
autosend: {
|
|
153
|
+
eci: this.rsCtx.pico().channels[0]?.id,
|
|
154
|
+
domain: "dido",
|
|
155
|
+
type: "dido_send_response",
|
|
156
|
+
name: "dido_send_response",
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
}),
|
|
160
|
+
|
|
161
|
+
createInviteUrl: krl.Function(["base64"], function (base64: string) {
|
|
162
|
+
return identity.createInviteUrl(base64);
|
|
163
|
+
}),
|
|
164
|
+
|
|
165
|
+
establishSubscription: krl.Function(["bus"], async function (
|
|
166
|
+
bus: Record<string, unknown>
|
|
167
|
+
) {
|
|
168
|
+
return identity.establishSubscriptionFromBus(
|
|
169
|
+
this.rsCtx.pico().id,
|
|
170
|
+
bus || {}
|
|
171
|
+
);
|
|
172
|
+
}),
|
|
173
|
+
|
|
174
|
+
linkRemotePeer: krl.Function(["subscriptionId", "link"], async function (
|
|
175
|
+
subscriptionId: string,
|
|
176
|
+
link: Record<string, unknown>
|
|
177
|
+
) {
|
|
178
|
+
return identity.linkRemotePeer(this.rsCtx.pico().id, subscriptionId, {
|
|
179
|
+
remotePeerDid: String(link.remotePeerDid || link.Tx_did || ""),
|
|
180
|
+
remoteWebvhDid:
|
|
181
|
+
typeof link.remoteWebvhDid === "string"
|
|
182
|
+
? link.remoteWebvhDid
|
|
183
|
+
: undefined,
|
|
184
|
+
remoteMeshRootId:
|
|
185
|
+
typeof link.remoteMeshRootId === "string"
|
|
186
|
+
? link.remoteMeshRootId
|
|
187
|
+
: undefined,
|
|
188
|
+
remoteTxHost:
|
|
189
|
+
typeof link.remoteTxHost === "string" ? link.remoteTxHost : undefined,
|
|
190
|
+
remoteDidcommEndpoint:
|
|
191
|
+
typeof link.remoteDidcommEndpoint === "string"
|
|
192
|
+
? link.remoteDidcommEndpoint
|
|
193
|
+
: undefined,
|
|
194
|
+
});
|
|
195
|
+
}),
|
|
196
|
+
|
|
197
|
+
sendSkyIntro: krl.Function(["input"], async function (
|
|
198
|
+
input: Record<string, unknown>
|
|
199
|
+
) {
|
|
200
|
+
return identity.sendSkyIntro(this.rsCtx.pico().id, {
|
|
201
|
+
subscriptionId: String(input.subscriptionId || input.Id || ""),
|
|
202
|
+
targetDid: String(
|
|
203
|
+
input.targetDid || input.target_did || input.target_webvh || ""
|
|
204
|
+
),
|
|
205
|
+
name: String(input.name || "subscription"),
|
|
206
|
+
Tx_role: String(input.Tx_role || ""),
|
|
207
|
+
Rx_role: String(input.Rx_role || ""),
|
|
208
|
+
channel_type:
|
|
209
|
+
typeof input.channel_type === "string" ? input.channel_type : undefined,
|
|
210
|
+
Tx_host:
|
|
211
|
+
typeof input.Tx_host === "string" ? input.Tx_host : undefined,
|
|
212
|
+
});
|
|
213
|
+
}),
|
|
214
|
+
|
|
215
|
+
sendSkyIntroResponse: krl.Function(
|
|
216
|
+
["subscriptionId", "toPeerDid", "response", "options"],
|
|
217
|
+
async function (
|
|
218
|
+
subscriptionId: string,
|
|
219
|
+
toPeerDid: string,
|
|
220
|
+
response: Record<string, unknown>,
|
|
221
|
+
options: Record<string, unknown> = {}
|
|
222
|
+
) {
|
|
223
|
+
const status =
|
|
224
|
+
response.status === "rejected" ? "rejected" : "accepted";
|
|
225
|
+
return identity.sendSkyIntroResponse(
|
|
226
|
+
this.rsCtx.pico().id,
|
|
227
|
+
subscriptionId,
|
|
228
|
+
toPeerDid,
|
|
229
|
+
{
|
|
230
|
+
status,
|
|
231
|
+
subscription_id:
|
|
232
|
+
typeof response.subscription_id === "string"
|
|
233
|
+
? response.subscription_id
|
|
234
|
+
: subscriptionId,
|
|
235
|
+
peer_did_long:
|
|
236
|
+
typeof response.peer_did_long === "string"
|
|
237
|
+
? response.peer_did_long
|
|
238
|
+
: undefined,
|
|
239
|
+
Tx_host:
|
|
240
|
+
typeof response.Tx_host === "string" ? response.Tx_host : undefined,
|
|
241
|
+
reason:
|
|
242
|
+
typeof response.reason === "string" ? response.reason : undefined,
|
|
243
|
+
detail:
|
|
244
|
+
typeof response.detail === "string" ? response.detail : undefined,
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
thid:
|
|
248
|
+
typeof options.thid === "string" ? options.thid : undefined,
|
|
249
|
+
remoteDidcommEndpoint:
|
|
250
|
+
typeof options.remoteDidcommEndpoint === "string"
|
|
251
|
+
? options.remoteDidcommEndpoint
|
|
252
|
+
: undefined,
|
|
253
|
+
}
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
),
|
|
257
|
+
|
|
258
|
+
crossPicoEvent: krl.Function(["subscriptionId", "event"], async function (
|
|
259
|
+
subscriptionId: string,
|
|
260
|
+
event: Record<string, unknown>
|
|
261
|
+
) {
|
|
262
|
+
return identity.crossPicoEvent(
|
|
263
|
+
this.rsCtx.pico().id,
|
|
264
|
+
undefined,
|
|
265
|
+
subscriptionId,
|
|
266
|
+
{
|
|
267
|
+
domain: String(event.domain || ""),
|
|
268
|
+
name: String(event.name || event.type || ""),
|
|
269
|
+
attrs:
|
|
270
|
+
event.attrs && typeof event.attrs === "object"
|
|
271
|
+
? (event.attrs as Record<string, unknown>)
|
|
272
|
+
: {},
|
|
273
|
+
eid: typeof event.eid === "string" ? event.eid : undefined,
|
|
274
|
+
}
|
|
275
|
+
);
|
|
276
|
+
}),
|
|
277
|
+
|
|
278
|
+
crossPicoQuery: krl.Function(["subscriptionId", "query"], async function (
|
|
279
|
+
subscriptionId: string,
|
|
280
|
+
query: Record<string, unknown>
|
|
281
|
+
) {
|
|
282
|
+
return identity.crossPicoQuery(
|
|
283
|
+
this.rsCtx.pico().id,
|
|
284
|
+
undefined,
|
|
285
|
+
subscriptionId,
|
|
286
|
+
{
|
|
287
|
+
rid: String(query.rid || ""),
|
|
288
|
+
name: String(query.name || ""),
|
|
289
|
+
args:
|
|
290
|
+
query.args && typeof query.args === "object"
|
|
291
|
+
? (query.args as Record<string, unknown>)
|
|
292
|
+
: {},
|
|
293
|
+
}
|
|
294
|
+
);
|
|
295
|
+
}),
|
|
296
|
+
|
|
297
|
+
teardownSubscription: krl.Function(["subscriptionId"], async function (
|
|
298
|
+
subscriptionId: string
|
|
299
|
+
) {
|
|
300
|
+
return identity.teardownSubscription(
|
|
301
|
+
this.rsCtx.pico().id,
|
|
302
|
+
subscriptionId
|
|
303
|
+
);
|
|
304
|
+
}),
|
|
305
|
+
|
|
306
|
+
picoQuery: krl.Function(["did", "query"], async function (
|
|
307
|
+
did: string,
|
|
308
|
+
query: Record<string, unknown>
|
|
309
|
+
) {
|
|
310
|
+
return identity.picoQuery(this.rsCtx.pico().id, did, {
|
|
311
|
+
rid: String(query.rid || ""),
|
|
312
|
+
name: String(query.name || ""),
|
|
313
|
+
args:
|
|
314
|
+
query.args && typeof query.args === "object"
|
|
315
|
+
? (query.args as Record<string, unknown>)
|
|
316
|
+
: {},
|
|
317
|
+
});
|
|
318
|
+
}),
|
|
319
|
+
|
|
320
|
+
picoEvent: krl.Action(["did", "event"], async function (
|
|
321
|
+
did: string,
|
|
322
|
+
event: Record<string, unknown>
|
|
323
|
+
) {
|
|
324
|
+
await identity.picoEvent(this.rsCtx.pico().id, did, {
|
|
325
|
+
domain: String(event.domain || ""),
|
|
326
|
+
name: String(event.name || event.type || ""),
|
|
327
|
+
attrs:
|
|
328
|
+
event.attrs && typeof event.attrs === "object"
|
|
329
|
+
? (event.attrs as Record<string, unknown>)
|
|
330
|
+
: {},
|
|
331
|
+
eid: typeof event.eid === "string" ? event.eid : undefined,
|
|
332
|
+
});
|
|
333
|
+
}),
|
|
334
|
+
|
|
335
|
+
// Legacy did-o / did:peer:2 surface — removed in Layer 2 reboot.
|
|
336
|
+
generateDID: deprecated("generateDID"),
|
|
337
|
+
deleteDID: deprecated("deleteDID"),
|
|
338
|
+
updateDID: deprecated("updateDID"),
|
|
339
|
+
rotateDID: deprecated("rotateDID"),
|
|
340
|
+
rotateInviteDID: deprecated("rotateInviteDID"),
|
|
341
|
+
clearPendingRotations: deprecated("clearPendingRotations"),
|
|
342
|
+
mapDid: deprecated("mapDid"),
|
|
343
|
+
clearDidMap: deprecated("clearDidMap"),
|
|
344
|
+
clearDidDocs: deprecated("clearDidDocs"),
|
|
345
|
+
storeDidDoc: deprecated("storeDidDoc"),
|
|
346
|
+
addLabelsToChannel: deprecated("addLabelsToChannel"),
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
return module;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
function deprecated(name: string) {
|
|
353
|
+
return krl.Function([], async function () {
|
|
354
|
+
throw new IdentityError(
|
|
355
|
+
`dido:${name} removed in Layer 2 reboot — use engine identity primitives`,
|
|
356
|
+
410
|
|
357
|
+
);
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function extractDidcommEndpoint(doc: Record<string, unknown>): string {
|
|
362
|
+
const services = doc.service as Array<Record<string, unknown>> | undefined;
|
|
363
|
+
if (!services || services.length === 0) {
|
|
364
|
+
throw new IdentityError("DID document has no service endpoints", 404);
|
|
365
|
+
}
|
|
366
|
+
const svc = services.find(
|
|
367
|
+
(s) =>
|
|
368
|
+
s.type === "DIDCommMessaging" ||
|
|
369
|
+
s.type === "Messaging" ||
|
|
370
|
+
s.type === "did-communication"
|
|
371
|
+
);
|
|
372
|
+
const endpoint = svc?.serviceEndpoint;
|
|
373
|
+
if (typeof endpoint === "string") {
|
|
374
|
+
return endpoint;
|
|
375
|
+
}
|
|
376
|
+
if (
|
|
377
|
+
endpoint &&
|
|
378
|
+
typeof endpoint === "object" &&
|
|
379
|
+
typeof (endpoint as any).uri === "string"
|
|
380
|
+
) {
|
|
381
|
+
return (endpoint as any).uri;
|
|
382
|
+
}
|
|
383
|
+
const fallback = services[0].serviceEndpoint;
|
|
384
|
+
if (typeof fallback === "string") {
|
|
385
|
+
return fallback;
|
|
386
|
+
}
|
|
387
|
+
throw new IdentityError("Could not extract DIDComm service endpoint", 404);
|
|
388
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { ChannelConfig, Pico, PicoFramework } from "pico-framework";
|
|
2
|
+
import { IdentityStore } from "./store";
|
|
3
|
+
import type { PeerSubscriptionRecord } from "./types";
|
|
4
|
+
import { IdentityError } from "./errors";
|
|
5
|
+
import { createPeerDidWithKeys } from "./veramoDidComm";
|
|
6
|
+
|
|
7
|
+
export interface EstablishSubscriptionInput {
|
|
8
|
+
subscriptionId: string;
|
|
9
|
+
/** Remote party's peer DID (may be linked later via linkRemotePeer). */
|
|
10
|
+
remotePeerDid?: string;
|
|
11
|
+
remoteWebvhDid?: string;
|
|
12
|
+
remoteMeshRootId?: string;
|
|
13
|
+
remoteTxHost?: string;
|
|
14
|
+
remoteDidcommEndpoint?: string;
|
|
15
|
+
/** Use an existing internal Rx channel; created if omitted. */
|
|
16
|
+
rxEci?: string;
|
|
17
|
+
channelName?: string;
|
|
18
|
+
channelType?: string;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface EstablishSubscriptionResult {
|
|
22
|
+
subscriptionId: string;
|
|
23
|
+
peerDid: string;
|
|
24
|
+
rxEci: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const DEFAULT_RX_CHANNEL: ChannelConfig = {
|
|
28
|
+
tags: ["subscription", "tx_rx"],
|
|
29
|
+
eventPolicy: {
|
|
30
|
+
allow: [{ domain: "*", name: "*" }],
|
|
31
|
+
deny: [],
|
|
32
|
+
},
|
|
33
|
+
queryPolicy: {
|
|
34
|
+
allow: [{ rid: "*", name: "*" }],
|
|
35
|
+
deny: [],
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function findPico(pf: PicoFramework, picoId: string): Pico | undefined {
|
|
40
|
+
return pf.loadedPicos().find((p) => p.id === picoId);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export async function establishSubscriptionIdentity(
|
|
44
|
+
store: IdentityStore,
|
|
45
|
+
pf: PicoFramework,
|
|
46
|
+
picoId: string,
|
|
47
|
+
input: EstablishSubscriptionInput
|
|
48
|
+
): Promise<EstablishSubscriptionResult> {
|
|
49
|
+
if (!input.subscriptionId) {
|
|
50
|
+
throw new IdentityError("subscriptionId is required", 400);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const existing = await store.getPeerSubscription(picoId, input.subscriptionId);
|
|
54
|
+
if (existing?.peerDid && existing.rxEci) {
|
|
55
|
+
return {
|
|
56
|
+
subscriptionId: input.subscriptionId,
|
|
57
|
+
peerDid: existing.peerDid,
|
|
58
|
+
rxEci: existing.rxEci,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const pico = findPico(pf, picoId);
|
|
63
|
+
if (!pico) {
|
|
64
|
+
throw new IdentityError(`Pico ${picoId} not loaded`, 404);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
let rxEci = input.rxEci || existing?.rxEci;
|
|
68
|
+
if (!rxEci) {
|
|
69
|
+
const tags = [
|
|
70
|
+
input.channelName || "sub-rx",
|
|
71
|
+
input.channelType || "Tx_Rx",
|
|
72
|
+
"subscription",
|
|
73
|
+
];
|
|
74
|
+
const channel = await pico.newChannel({
|
|
75
|
+
...DEFAULT_RX_CHANNEL,
|
|
76
|
+
tags,
|
|
77
|
+
});
|
|
78
|
+
rxEci = channel.toReadOnly().id;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const { did, keys } = await createPeerDidWithKeys();
|
|
82
|
+
|
|
83
|
+
const record: PeerSubscriptionRecord = {
|
|
84
|
+
subscriptionId: input.subscriptionId,
|
|
85
|
+
peerDid: did,
|
|
86
|
+
remotePeerDid: input.remotePeerDid || existing?.remotePeerDid,
|
|
87
|
+
remoteWebvhDid: input.remoteWebvhDid || existing?.remoteWebvhDid,
|
|
88
|
+
rxEci,
|
|
89
|
+
remoteMeshRootId: input.remoteMeshRootId || existing?.remoteMeshRootId,
|
|
90
|
+
remoteTxHost: input.remoteTxHost || existing?.remoteTxHost,
|
|
91
|
+
remoteDidcommEndpoint:
|
|
92
|
+
input.remoteDidcommEndpoint || existing?.remoteDidcommEndpoint,
|
|
93
|
+
keys: keys as unknown as Record<string, unknown>,
|
|
94
|
+
createdAt: existing?.createdAt || new Date().toISOString(),
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
await store.putPeerSubscription(picoId, record);
|
|
98
|
+
|
|
99
|
+
return {
|
|
100
|
+
subscriptionId: input.subscriptionId,
|
|
101
|
+
peerDid: did,
|
|
102
|
+
rxEci,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface LinkRemotePeerInput {
|
|
107
|
+
remotePeerDid: string;
|
|
108
|
+
remoteWebvhDid?: string;
|
|
109
|
+
remoteMeshRootId?: string;
|
|
110
|
+
remoteTxHost?: string;
|
|
111
|
+
remoteDidcommEndpoint?: string;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export async function linkRemotePeer(
|
|
115
|
+
store: IdentityStore,
|
|
116
|
+
picoId: string,
|
|
117
|
+
subscriptionId: string,
|
|
118
|
+
input: LinkRemotePeerInput
|
|
119
|
+
): Promise<PeerSubscriptionRecord> {
|
|
120
|
+
const sub = await store.getPeerSubscription(picoId, subscriptionId);
|
|
121
|
+
if (!sub) {
|
|
122
|
+
throw new IdentityError(`Unknown subscription ${subscriptionId}`, 404);
|
|
123
|
+
}
|
|
124
|
+
const updated: PeerSubscriptionRecord = {
|
|
125
|
+
...sub,
|
|
126
|
+
remotePeerDid: input.remotePeerDid,
|
|
127
|
+
remoteWebvhDid: input.remoteWebvhDid ?? sub.remoteWebvhDid,
|
|
128
|
+
remoteMeshRootId: input.remoteMeshRootId ?? sub.remoteMeshRootId,
|
|
129
|
+
remoteTxHost: input.remoteTxHost ?? sub.remoteTxHost,
|
|
130
|
+
remoteDidcommEndpoint:
|
|
131
|
+
input.remoteDidcommEndpoint ?? sub.remoteDidcommEndpoint,
|
|
132
|
+
};
|
|
133
|
+
await store.putPeerSubscription(picoId, updated);
|
|
134
|
+
return updated;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** Parse wrangler/subscription bus map into establish input. */
|
|
138
|
+
export function establishInputFromBus(
|
|
139
|
+
bus: Record<string, unknown>
|
|
140
|
+
): EstablishSubscriptionInput {
|
|
141
|
+
const subscriptionId = String(bus.Id || bus.subscriptionId || "");
|
|
142
|
+
return {
|
|
143
|
+
subscriptionId,
|
|
144
|
+
remotePeerDid:
|
|
145
|
+
optionalString(bus.Tx_did) ||
|
|
146
|
+
optionalString(bus.remotePeerDid) ||
|
|
147
|
+
optionalString(bus.remoteDid),
|
|
148
|
+
remoteWebvhDid: optionalString(bus.remoteWebvhDid),
|
|
149
|
+
remoteMeshRootId: optionalString(bus.remoteMeshRootId),
|
|
150
|
+
remoteTxHost: optionalString(bus.Tx_host),
|
|
151
|
+
remoteDidcommEndpoint: optionalString(bus.remoteDidcommEndpoint),
|
|
152
|
+
rxEci: optionalString(bus.Rx),
|
|
153
|
+
channelName: optionalString(bus.channel_name),
|
|
154
|
+
channelType: optionalString(bus.channel_type),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function optionalString(value: unknown): string | undefined {
|
|
159
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
160
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export {
|
|
2
|
+
IdentityStore,
|
|
3
|
+
IDENTITY_PREFIX,
|
|
4
|
+
migrateFromDidO,
|
|
5
|
+
type IdentityStoreDeps,
|
|
6
|
+
} from "./store";
|
|
7
|
+
export { IdentityService, type IdentityServiceDeps } from "./IdentityService";
|
|
8
|
+
export { IdentityError } from "./errors";
|
|
9
|
+
export { default as initDidoModule } from "./didoModule";
|
|
10
|
+
export {
|
|
11
|
+
IDENTITY_STORAGE_KNOWN_GAP,
|
|
12
|
+
type CachedDidDoc,
|
|
13
|
+
type DidCommRoutes,
|
|
14
|
+
type PeerSubscriptionRecord,
|
|
15
|
+
type PendingRotation,
|
|
16
|
+
type WebvhKeys,
|
|
17
|
+
type WebvhLogEntry,
|
|
18
|
+
} from "./types";
|
|
19
|
+
export type {
|
|
20
|
+
EstablishSubscriptionInput,
|
|
21
|
+
EstablishSubscriptionResult,
|
|
22
|
+
LinkRemotePeerInput,
|
|
23
|
+
} from "./establishSubscription";
|
|
24
|
+
export type { StoredVeramoKey, StoredPeerDidBundle } from "./peerDid";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Stored Veramo key material for did:peer re-import after engine restart. */
|
|
2
|
+
export interface StoredVeramoKey {
|
|
3
|
+
type: string;
|
|
4
|
+
kid: string;
|
|
5
|
+
privateKeyHex: string;
|
|
6
|
+
publicKeyHex: string;
|
|
7
|
+
meta?: Record<string, unknown>;
|
|
8
|
+
kms: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface StoredPeerDidBundle {
|
|
12
|
+
veramoKeys: StoredVeramoKey[];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function peerKeysFromRecord(
|
|
16
|
+
keys: Record<string, unknown> | undefined
|
|
17
|
+
): StoredVeramoKey[] | null {
|
|
18
|
+
if (!keys || !Array.isArray(keys.veramoKeys)) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
return keys.veramoKeys as StoredVeramoKey[];
|
|
22
|
+
}
|