pico-engine 1.4.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 +188 -0
- package/dist/RulesetRegistryLoaderFs.js +2 -0
- package/dist/auth/AuthService.js +601 -0
- package/dist/auth/index.js +8 -0
- package/dist/auth/webauthn.js +97 -0
- package/dist/bundledKrl.js +42 -0
- package/dist/cli.js +11 -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 +100 -44
- package/dist/oauth/OAuthService.js +388 -0
- package/dist/oauth/acg.js +495 -0
- package/dist/oauth/channelEligibility.js +26 -0
- package/dist/oauth/errors.js +12 -0
- package/dist/oauth/index.js +21 -0
- package/dist/oauth/krlModule.js +27 -0
- package/dist/oauth/meshOAuth.js +43 -0
- package/dist/provisionRoot.js +118 -0
- package/dist/server.js +609 -15
- package/dist/uiChannelPolicies.js +48 -0
- package/krl/io.picolabs.did-o.krl +5 -1
- package/krl/io.picolabs.oauth.krl +48 -0
- package/krl/io.picolabs.pico-engine-ui.krl +106 -18
- package/krl/io.picolabs.subscription.krl +242 -23
- package/krl/io.picolabs.wrangler.krl +225 -16
- package/package.json +38 -9
- 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 +24 -20
- package/public/pico-labs-logo.png +0 -0
- package/src/RulesetRegistryLoaderFs.ts +2 -0
- package/src/auth/AuthService.ts +850 -0
- package/src/auth/index.ts +17 -0
- package/src/auth/webauthn.ts +182 -0
- package/src/bundledKrl.ts +42 -0
- package/src/cli.ts +11 -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 +145 -53
- package/src/oauth/OAuthService.ts +510 -0
- package/src/oauth/acg.ts +666 -0
- package/src/oauth/channelEligibility.ts +26 -0
- package/src/oauth/errors.ts +10 -0
- package/src/oauth/index.ts +14 -0
- package/src/oauth/krlModule.ts +31 -0
- package/src/oauth/meshOAuth.ts +50 -0
- package/src/provisionRoot.ts +151 -0
- package/src/server.ts +700 -24
- package/src/uiChannelPolicies.ts +49 -0
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import * as cuid from "cuid";
|
|
2
|
+
import fetch from "cross-fetch";
|
|
3
|
+
import { Pico, PicoFramework } from "pico-framework";
|
|
4
|
+
import { IdentityStore } from "./store";
|
|
5
|
+
import type { IdentityService, DidCommPlainMessage } from "./IdentityService";
|
|
6
|
+
import { IdentityError } from "./errors";
|
|
7
|
+
import {
|
|
8
|
+
SKY_INTRO,
|
|
9
|
+
SKY_INTRO_RESPONSE,
|
|
10
|
+
parseSkyIntroBody,
|
|
11
|
+
parseSkyIntroResponseBody,
|
|
12
|
+
skyIntroToWranglerAttrs,
|
|
13
|
+
skyIntroResponseToWranglerAttrs,
|
|
14
|
+
type SkyIntroBody,
|
|
15
|
+
type SkyIntroResponseBody,
|
|
16
|
+
} from "./skyProtocol";
|
|
17
|
+
import { extractDidcommEndpoint } from "./crossPico";
|
|
18
|
+
import { packDidCommMessage } from "./veramoDidComm";
|
|
19
|
+
import {
|
|
20
|
+
establishSubscriptionIdentity,
|
|
21
|
+
linkRemotePeer,
|
|
22
|
+
} from "./establishSubscription";
|
|
23
|
+
|
|
24
|
+
export interface SkyIntroSendInput {
|
|
25
|
+
subscriptionId: string;
|
|
26
|
+
targetDid: string;
|
|
27
|
+
name: string;
|
|
28
|
+
Tx_role: string;
|
|
29
|
+
Rx_role: string;
|
|
30
|
+
channel_type?: string;
|
|
31
|
+
Tx_host?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SkyIntroDeps {
|
|
35
|
+
store: IdentityStore;
|
|
36
|
+
pf: PicoFramework;
|
|
37
|
+
getBaseUrl: () => string;
|
|
38
|
+
identity: IdentityService;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function findPico(pf: PicoFramework, picoId: string): Pico | undefined {
|
|
42
|
+
return pf.loadedPicos().find((p) => p.id === picoId);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function deliveryEci(pf: PicoFramework, picoId: string, preferred?: string): string {
|
|
46
|
+
if (preferred) {
|
|
47
|
+
return preferred;
|
|
48
|
+
}
|
|
49
|
+
const pico = findPico(pf, picoId);
|
|
50
|
+
if (!pico) {
|
|
51
|
+
throw new IdentityError(`Pico ${picoId} not loaded`, 404);
|
|
52
|
+
}
|
|
53
|
+
for (const ch of Object.values(pico.channels)) {
|
|
54
|
+
const ro = ch.toReadOnly();
|
|
55
|
+
if (ro.tags.includes("wellKnown_Rx")) {
|
|
56
|
+
return ro.id;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
for (const ch of Object.values(pico.channels)) {
|
|
60
|
+
const ro = ch.toReadOnly();
|
|
61
|
+
if (ro.tags.includes("allow-all")) {
|
|
62
|
+
return ro.id;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const first = Object.keys(pico.channels)[0];
|
|
66
|
+
if (!first) {
|
|
67
|
+
throw new IdentityError("Pico has no channel for SKY intro delivery", 404);
|
|
68
|
+
}
|
|
69
|
+
return first;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function resolveDidcommEndpointForDid(
|
|
73
|
+
identity: IdentityService,
|
|
74
|
+
did: string,
|
|
75
|
+
remoteDidcommEndpoint?: string
|
|
76
|
+
): Promise<string> {
|
|
77
|
+
if (remoteDidcommEndpoint) {
|
|
78
|
+
return remoteDidcommEndpoint;
|
|
79
|
+
}
|
|
80
|
+
const doc = await identity.resolveDid("", did);
|
|
81
|
+
return extractDidcommEndpoint(doc);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function postJwe(endpoint: string, jwe: string): Promise<void> {
|
|
85
|
+
const res = await fetch(endpoint, {
|
|
86
|
+
method: "POST",
|
|
87
|
+
headers: { "Content-Type": "application/didcomm-encrypted+json" },
|
|
88
|
+
body: jwe,
|
|
89
|
+
});
|
|
90
|
+
if (!res.ok) {
|
|
91
|
+
const detail = await res.text();
|
|
92
|
+
throw new IdentityError(
|
|
93
|
+
`DIDComm POST failed: HTTP ${res.status} ${detail}`,
|
|
94
|
+
res.status
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export async function sendSkyIntro(
|
|
100
|
+
deps: SkyIntroDeps,
|
|
101
|
+
picoId: string,
|
|
102
|
+
input: SkyIntroSendInput
|
|
103
|
+
): Promise<{ messageId: string; peerDid: string }> {
|
|
104
|
+
const established = await establishSubscriptionIdentity(
|
|
105
|
+
deps.store,
|
|
106
|
+
deps.pf,
|
|
107
|
+
picoId,
|
|
108
|
+
{
|
|
109
|
+
subscriptionId: input.subscriptionId,
|
|
110
|
+
channelName: input.name,
|
|
111
|
+
channelType: input.channel_type || "Tx_Rx",
|
|
112
|
+
remoteWebvhDid: input.targetDid.startsWith("did:webvh:")
|
|
113
|
+
? input.targetDid
|
|
114
|
+
: undefined,
|
|
115
|
+
}
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
const body: SkyIntroBody = {
|
|
119
|
+
sky_version: "1.0",
|
|
120
|
+
name: input.name,
|
|
121
|
+
channel_type: input.channel_type || "Tx_Rx",
|
|
122
|
+
Tx_role: input.Tx_role,
|
|
123
|
+
Rx_role: input.Rx_role,
|
|
124
|
+
peer_did_long: established.peerDid,
|
|
125
|
+
Tx_host: input.Tx_host || deps.getBaseUrl(),
|
|
126
|
+
subscription_id: input.subscriptionId,
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const senderWebvh = await deps.store.getWebvhDid(picoId);
|
|
130
|
+
if (senderWebvh) {
|
|
131
|
+
body.sender_webvh = senderWebvh;
|
|
132
|
+
try {
|
|
133
|
+
const senderDoc = await deps.identity.resolveDid(picoId, senderWebvh);
|
|
134
|
+
body.didcomm_endpoint = extractDidcommEndpoint(senderDoc);
|
|
135
|
+
} catch {
|
|
136
|
+
// optional on intro body
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const message = deps.identity.generateMessage({
|
|
141
|
+
type: SKY_INTRO,
|
|
142
|
+
from: established.peerDid,
|
|
143
|
+
to: [input.targetDid],
|
|
144
|
+
body,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const targetPicoId = await deps.store.findPicoIdByWebvhDid(input.targetDid);
|
|
148
|
+
if (targetPicoId && findPico(deps.pf, targetPicoId)) {
|
|
149
|
+
await handleSkyIntroAtIngress(deps, targetPicoId, message);
|
|
150
|
+
return { messageId: message.id, peerDid: established.peerDid };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const endpoint = await resolveDidcommEndpointForDid(
|
|
154
|
+
deps.identity,
|
|
155
|
+
input.targetDid
|
|
156
|
+
);
|
|
157
|
+
const jwe = await packDidCommMessage(
|
|
158
|
+
picoId,
|
|
159
|
+
message,
|
|
160
|
+
established.peerDid,
|
|
161
|
+
input.targetDid,
|
|
162
|
+
deps.store,
|
|
163
|
+
{ resolveDid: (did) => deps.identity.resolveDid("", did) }
|
|
164
|
+
);
|
|
165
|
+
await postJwe(endpoint, jwe);
|
|
166
|
+
|
|
167
|
+
return { messageId: message.id, peerDid: established.peerDid };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export async function sendSkyIntroResponse(
|
|
171
|
+
deps: SkyIntroDeps,
|
|
172
|
+
picoId: string,
|
|
173
|
+
subscriptionId: string,
|
|
174
|
+
toPeerDid: string,
|
|
175
|
+
response: Omit<SkyIntroResponseBody, "sky_version">,
|
|
176
|
+
options: { thid?: string; remoteDidcommEndpoint?: string } = {}
|
|
177
|
+
): Promise<string> {
|
|
178
|
+
const sub = await deps.store.getPeerSubscription(picoId, subscriptionId);
|
|
179
|
+
if (!sub?.peerDid) {
|
|
180
|
+
throw new IdentityError(`Unknown subscription ${subscriptionId}`, 404);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const body: SkyIntroResponseBody = {
|
|
184
|
+
sky_version: "1.0",
|
|
185
|
+
...response,
|
|
186
|
+
subscription_id: response.subscription_id || subscriptionId,
|
|
187
|
+
peer_did_long: response.peer_did_long || sub.peerDid,
|
|
188
|
+
Tx_host: response.Tx_host ?? deps.getBaseUrl(),
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
const message = deps.identity.generateMessage({
|
|
192
|
+
type: SKY_INTRO_RESPONSE,
|
|
193
|
+
from: sub.peerDid,
|
|
194
|
+
to: [toPeerDid],
|
|
195
|
+
thid: options.thid,
|
|
196
|
+
body,
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
const targetPicoId = await deps.store.findPicoIdByLocalPeerDid(toPeerDid);
|
|
200
|
+
if (targetPicoId && findPico(deps.pf, targetPicoId)) {
|
|
201
|
+
await handleSkyIntroResponseAtIngress(deps, targetPicoId, message);
|
|
202
|
+
return message.id;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const endpoint = await resolveDidcommEndpointForDid(
|
|
206
|
+
deps.identity,
|
|
207
|
+
toPeerDid,
|
|
208
|
+
options.remoteDidcommEndpoint || sub.remoteDidcommEndpoint
|
|
209
|
+
);
|
|
210
|
+
const jwe = await packDidCommMessage(
|
|
211
|
+
picoId,
|
|
212
|
+
message,
|
|
213
|
+
sub.peerDid,
|
|
214
|
+
toPeerDid,
|
|
215
|
+
deps.store,
|
|
216
|
+
{ resolveDid: (did) => deps.identity.resolveDid("", did) }
|
|
217
|
+
);
|
|
218
|
+
await postJwe(endpoint, jwe);
|
|
219
|
+
return message.id;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export async function handleSkyIntroAtIngress(
|
|
223
|
+
deps: SkyIntroDeps,
|
|
224
|
+
picoId: string,
|
|
225
|
+
plainMessage: DidCommPlainMessage
|
|
226
|
+
): Promise<Record<string, unknown>> {
|
|
227
|
+
const allowed = await deps.identity.acceptsPublicIntro(picoId);
|
|
228
|
+
if (!allowed) {
|
|
229
|
+
throw new IdentityError("public_intro_disabled", 403);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const intro = parseSkyIntroBody(plainMessage.body);
|
|
233
|
+
const subscriptionId = intro.subscription_id || cuid();
|
|
234
|
+
const remotePicoId = await deps.store.findPicoIdByLocalPeerDid(
|
|
235
|
+
intro.peer_did_long
|
|
236
|
+
);
|
|
237
|
+
const remoteWebvh =
|
|
238
|
+
(remotePicoId && (await deps.store.getWebvhDid(remotePicoId))) ||
|
|
239
|
+
intro.sender_webvh ||
|
|
240
|
+
undefined;
|
|
241
|
+
|
|
242
|
+
const established = await establishSubscriptionIdentity(
|
|
243
|
+
deps.store,
|
|
244
|
+
deps.pf,
|
|
245
|
+
picoId,
|
|
246
|
+
{
|
|
247
|
+
subscriptionId,
|
|
248
|
+
remotePeerDid: intro.peer_did_long,
|
|
249
|
+
remoteWebvhDid: remoteWebvh,
|
|
250
|
+
remoteTxHost: intro.Tx_host || undefined,
|
|
251
|
+
remoteDidcommEndpoint: intro.didcomm_endpoint || undefined,
|
|
252
|
+
channelName: intro.name,
|
|
253
|
+
channelType: intro.channel_type,
|
|
254
|
+
}
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
const attrs = skyIntroToWranglerAttrs(intro, {
|
|
258
|
+
Id: subscriptionId,
|
|
259
|
+
Rx: established.rxEci,
|
|
260
|
+
channel_name: intro.name,
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
const eci = deliveryEci(deps.pf, picoId, established.rxEci);
|
|
264
|
+
await deps.pf.eventWait(
|
|
265
|
+
{
|
|
266
|
+
eci,
|
|
267
|
+
domain: "wrangler",
|
|
268
|
+
name: "sky_intro",
|
|
269
|
+
data: { attrs },
|
|
270
|
+
time: 0,
|
|
271
|
+
},
|
|
272
|
+
undefined
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
return { status: "intro_delivered", subscriptionId };
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
export async function handleSkyIntroResponseAtIngress(
|
|
279
|
+
deps: SkyIntroDeps,
|
|
280
|
+
picoId: string,
|
|
281
|
+
plainMessage: DidCommPlainMessage
|
|
282
|
+
): Promise<Record<string, unknown>> {
|
|
283
|
+
const response = parseSkyIntroResponseBody(plainMessage.body);
|
|
284
|
+
const subscriptionId = response.subscription_id;
|
|
285
|
+
if (!subscriptionId) {
|
|
286
|
+
throw new IdentityError("intro-response missing subscription_id", 400);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const sub = await deps.store.getPeerSubscription(picoId, subscriptionId);
|
|
290
|
+
const eci = deliveryEci(deps.pf, picoId, sub?.rxEci);
|
|
291
|
+
|
|
292
|
+
if (response.status === "accepted" && response.peer_did_long) {
|
|
293
|
+
const remotePicoId = await deps.store.findPicoIdByLocalPeerDid(
|
|
294
|
+
response.peer_did_long
|
|
295
|
+
);
|
|
296
|
+
const remoteWebvh =
|
|
297
|
+
remotePicoId && (await deps.store.getWebvhDid(remotePicoId));
|
|
298
|
+
await linkRemotePeer(deps.store, picoId, subscriptionId, {
|
|
299
|
+
remotePeerDid: response.peer_did_long,
|
|
300
|
+
remoteWebvhDid: remoteWebvh || undefined,
|
|
301
|
+
remoteTxHost: response.Tx_host || undefined,
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const attrs = skyIntroResponseToWranglerAttrs(response, {
|
|
306
|
+
Id: subscriptionId,
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
await deps.pf.eventWait(
|
|
310
|
+
{
|
|
311
|
+
eci,
|
|
312
|
+
domain: "wrangler",
|
|
313
|
+
name: "sky_intro_response",
|
|
314
|
+
data: { attrs },
|
|
315
|
+
time: 0,
|
|
316
|
+
},
|
|
317
|
+
undefined
|
|
318
|
+
);
|
|
319
|
+
|
|
320
|
+
return { status: "intro_response_delivered", subscriptionId };
|
|
321
|
+
}
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
/** SKY protocol on DIDComm v2 — type URIs and payloads (1.0). */
|
|
2
|
+
|
|
3
|
+
export const SKY_NS = "https://picolabs.org/sky/1.0";
|
|
4
|
+
|
|
5
|
+
export const SKY_INTRO = `${SKY_NS}/intro`;
|
|
6
|
+
export const SKY_INTRO_RESPONSE = `${SKY_NS}/intro-response`;
|
|
7
|
+
export const SKY_EVENT = `${SKY_NS}/event`;
|
|
8
|
+
export const SKY_QUERY = `${SKY_NS}/query`;
|
|
9
|
+
export const SKY_QUERY_RESPONSE = `${SKY_NS}/query-response`;
|
|
10
|
+
|
|
11
|
+
export interface SkyEventBody {
|
|
12
|
+
sky_version: string;
|
|
13
|
+
eid?: string;
|
|
14
|
+
domain: string;
|
|
15
|
+
name: string;
|
|
16
|
+
attrs: Record<string, unknown>;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SkyQueryBody {
|
|
20
|
+
sky_version: string;
|
|
21
|
+
rid: string;
|
|
22
|
+
name: string;
|
|
23
|
+
args: Record<string, unknown>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface SkyQueryResponseBody {
|
|
27
|
+
sky_version: string;
|
|
28
|
+
status: "ok" | "error";
|
|
29
|
+
result?: unknown;
|
|
30
|
+
error?: { code?: string; message?: string };
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface SkyIntroBody {
|
|
34
|
+
sky_version: string;
|
|
35
|
+
name: string;
|
|
36
|
+
channel_type?: string;
|
|
37
|
+
Tx_role: string;
|
|
38
|
+
Rx_role: string;
|
|
39
|
+
peer_did_long: string;
|
|
40
|
+
Tx_host?: string | null;
|
|
41
|
+
/** Sender DIDComm ingress URL (for intro-response on cross-engine paths). */
|
|
42
|
+
didcomm_endpoint?: string;
|
|
43
|
+
/** Sender's did:webvh (for cross-engine peer linking on the recipient). */
|
|
44
|
+
sender_webvh?: string;
|
|
45
|
+
subscription_id?: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface SkyIntroResponseBody {
|
|
49
|
+
sky_version: string;
|
|
50
|
+
status: "accepted" | "rejected";
|
|
51
|
+
subscription_id?: string;
|
|
52
|
+
peer_did_long?: string;
|
|
53
|
+
Tx_host?: string | null;
|
|
54
|
+
reason?: string;
|
|
55
|
+
detail?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function parseSkyIntroBody(body: unknown): SkyIntroBody {
|
|
59
|
+
if (!body || typeof body !== "object") {
|
|
60
|
+
throw new Error("Invalid SKY intro body");
|
|
61
|
+
}
|
|
62
|
+
const b = body as Record<string, unknown>;
|
|
63
|
+
if (
|
|
64
|
+
typeof b.name !== "string" ||
|
|
65
|
+
typeof b.Tx_role !== "string" ||
|
|
66
|
+
typeof b.Rx_role !== "string" ||
|
|
67
|
+
typeof b.peer_did_long !== "string"
|
|
68
|
+
) {
|
|
69
|
+
throw new Error("SKY intro body missing required fields");
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
sky_version: String(b.sky_version || "1.0"),
|
|
73
|
+
name: b.name,
|
|
74
|
+
channel_type:
|
|
75
|
+
typeof b.channel_type === "string" ? b.channel_type : "Tx_Rx",
|
|
76
|
+
Tx_role: b.Tx_role,
|
|
77
|
+
Rx_role: b.Rx_role,
|
|
78
|
+
peer_did_long: b.peer_did_long,
|
|
79
|
+
Tx_host:
|
|
80
|
+
b.Tx_host === null || typeof b.Tx_host === "string"
|
|
81
|
+
? (b.Tx_host as string | null)
|
|
82
|
+
: undefined,
|
|
83
|
+
didcomm_endpoint:
|
|
84
|
+
typeof b.didcomm_endpoint === "string" ? b.didcomm_endpoint : undefined,
|
|
85
|
+
sender_webvh:
|
|
86
|
+
typeof b.sender_webvh === "string" ? b.sender_webvh : undefined,
|
|
87
|
+
subscription_id:
|
|
88
|
+
typeof b.subscription_id === "string" ? b.subscription_id : undefined,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function parseSkyIntroResponseBody(body: unknown): SkyIntroResponseBody {
|
|
93
|
+
if (!body || typeof body !== "object") {
|
|
94
|
+
throw new Error("Invalid SKY intro-response body");
|
|
95
|
+
}
|
|
96
|
+
const b = body as Record<string, unknown>;
|
|
97
|
+
if (b.status !== "accepted" && b.status !== "rejected") {
|
|
98
|
+
throw new Error("SKY intro-response missing status");
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
sky_version: String(b.sky_version || "1.0"),
|
|
102
|
+
status: b.status,
|
|
103
|
+
subscription_id:
|
|
104
|
+
typeof b.subscription_id === "string" ? b.subscription_id : undefined,
|
|
105
|
+
peer_did_long:
|
|
106
|
+
typeof b.peer_did_long === "string" ? b.peer_did_long : undefined,
|
|
107
|
+
Tx_host:
|
|
108
|
+
b.Tx_host === null || typeof b.Tx_host === "string"
|
|
109
|
+
? (b.Tx_host as string | null)
|
|
110
|
+
: undefined,
|
|
111
|
+
reason: typeof b.reason === "string" ? b.reason : undefined,
|
|
112
|
+
detail: typeof b.detail === "string" ? b.detail : undefined,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Map SKY intro to wrangler subscription attrs (layer 2). */
|
|
117
|
+
export function skyIntroToWranglerAttrs(
|
|
118
|
+
intro: SkyIntroBody,
|
|
119
|
+
extras: Record<string, unknown> = {}
|
|
120
|
+
): Record<string, unknown> {
|
|
121
|
+
return {
|
|
122
|
+
layer2: true,
|
|
123
|
+
name: intro.name,
|
|
124
|
+
channel_type: intro.channel_type || "Tx_Rx",
|
|
125
|
+
Tx_role: intro.Tx_role,
|
|
126
|
+
Rx_role: intro.Rx_role,
|
|
127
|
+
peer_did_long: intro.peer_did_long,
|
|
128
|
+
Tx_did: intro.peer_did_long,
|
|
129
|
+
Tx_host: intro.Tx_host || null,
|
|
130
|
+
Id: intro.subscription_id,
|
|
131
|
+
...extras,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function skyIntroResponseToWranglerAttrs(
|
|
136
|
+
response: SkyIntroResponseBody,
|
|
137
|
+
extras: Record<string, unknown> = {}
|
|
138
|
+
): Record<string, unknown> {
|
|
139
|
+
return {
|
|
140
|
+
layer2: true,
|
|
141
|
+
status: response.status,
|
|
142
|
+
subscription_id: response.subscription_id,
|
|
143
|
+
peer_did_long: response.peer_did_long,
|
|
144
|
+
Tx_did: response.peer_did_long,
|
|
145
|
+
Tx_host: response.Tx_host || null,
|
|
146
|
+
reason: response.reason,
|
|
147
|
+
detail: response.detail,
|
|
148
|
+
...extras,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function parseSkyEventBody(body: unknown): SkyEventBody {
|
|
153
|
+
if (!body || typeof body !== "object") {
|
|
154
|
+
throw new Error("Invalid SKY event body");
|
|
155
|
+
}
|
|
156
|
+
const b = body as Record<string, unknown>;
|
|
157
|
+
if (typeof b.domain !== "string" || typeof b.name !== "string") {
|
|
158
|
+
throw new Error("SKY event body missing domain or name");
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
sky_version: String(b.sky_version || "1.0"),
|
|
162
|
+
eid: typeof b.eid === "string" ? b.eid : "none",
|
|
163
|
+
domain: b.domain,
|
|
164
|
+
name: b.name,
|
|
165
|
+
attrs:
|
|
166
|
+
b.attrs && typeof b.attrs === "object"
|
|
167
|
+
? (b.attrs as Record<string, unknown>)
|
|
168
|
+
: {},
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
export function parseSkyQueryBody(body: unknown): SkyQueryBody {
|
|
173
|
+
if (!body || typeof body !== "object") {
|
|
174
|
+
throw new Error("Invalid SKY query body");
|
|
175
|
+
}
|
|
176
|
+
const b = body as Record<string, unknown>;
|
|
177
|
+
if (
|
|
178
|
+
typeof b.rid !== "string" ||
|
|
179
|
+
typeof b.name !== "string"
|
|
180
|
+
) {
|
|
181
|
+
throw new Error("SKY query body missing rid or name");
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
sky_version: String(b.sky_version || "1.0"),
|
|
185
|
+
rid: b.rid,
|
|
186
|
+
name: b.name,
|
|
187
|
+
args:
|
|
188
|
+
b.args && typeof b.args === "object"
|
|
189
|
+
? (b.args as Record<string, unknown>)
|
|
190
|
+
: {},
|
|
191
|
+
};
|
|
192
|
+
}
|