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,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.establishSubscriptionIdentity = establishSubscriptionIdentity;
|
|
4
|
+
exports.linkRemotePeer = linkRemotePeer;
|
|
5
|
+
exports.establishInputFromBus = establishInputFromBus;
|
|
6
|
+
const errors_1 = require("./errors");
|
|
7
|
+
const veramoDidComm_1 = require("./veramoDidComm");
|
|
8
|
+
const DEFAULT_RX_CHANNEL = {
|
|
9
|
+
tags: ["subscription", "tx_rx"],
|
|
10
|
+
eventPolicy: {
|
|
11
|
+
allow: [{ domain: "*", name: "*" }],
|
|
12
|
+
deny: [],
|
|
13
|
+
},
|
|
14
|
+
queryPolicy: {
|
|
15
|
+
allow: [{ rid: "*", name: "*" }],
|
|
16
|
+
deny: [],
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
function findPico(pf, picoId) {
|
|
20
|
+
return pf.loadedPicos().find((p) => p.id === picoId);
|
|
21
|
+
}
|
|
22
|
+
async function establishSubscriptionIdentity(store, pf, picoId, input) {
|
|
23
|
+
if (!input.subscriptionId) {
|
|
24
|
+
throw new errors_1.IdentityError("subscriptionId is required", 400);
|
|
25
|
+
}
|
|
26
|
+
const existing = await store.getPeerSubscription(picoId, input.subscriptionId);
|
|
27
|
+
if ((existing === null || existing === void 0 ? void 0 : existing.peerDid) && existing.rxEci) {
|
|
28
|
+
return {
|
|
29
|
+
subscriptionId: input.subscriptionId,
|
|
30
|
+
peerDid: existing.peerDid,
|
|
31
|
+
rxEci: existing.rxEci,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
const pico = findPico(pf, picoId);
|
|
35
|
+
if (!pico) {
|
|
36
|
+
throw new errors_1.IdentityError(`Pico ${picoId} not loaded`, 404);
|
|
37
|
+
}
|
|
38
|
+
let rxEci = input.rxEci || (existing === null || existing === void 0 ? void 0 : existing.rxEci);
|
|
39
|
+
if (!rxEci) {
|
|
40
|
+
const tags = [
|
|
41
|
+
input.channelName || "sub-rx",
|
|
42
|
+
input.channelType || "Tx_Rx",
|
|
43
|
+
"subscription",
|
|
44
|
+
];
|
|
45
|
+
const channel = await pico.newChannel(Object.assign(Object.assign({}, DEFAULT_RX_CHANNEL), { tags }));
|
|
46
|
+
rxEci = channel.toReadOnly().id;
|
|
47
|
+
}
|
|
48
|
+
const { did, keys } = await (0, veramoDidComm_1.createPeerDidWithKeys)();
|
|
49
|
+
const record = {
|
|
50
|
+
subscriptionId: input.subscriptionId,
|
|
51
|
+
peerDid: did,
|
|
52
|
+
remotePeerDid: input.remotePeerDid || (existing === null || existing === void 0 ? void 0 : existing.remotePeerDid),
|
|
53
|
+
remoteWebvhDid: input.remoteWebvhDid || (existing === null || existing === void 0 ? void 0 : existing.remoteWebvhDid),
|
|
54
|
+
rxEci,
|
|
55
|
+
remoteMeshRootId: input.remoteMeshRootId || (existing === null || existing === void 0 ? void 0 : existing.remoteMeshRootId),
|
|
56
|
+
remoteTxHost: input.remoteTxHost || (existing === null || existing === void 0 ? void 0 : existing.remoteTxHost),
|
|
57
|
+
remoteDidcommEndpoint: input.remoteDidcommEndpoint || (existing === null || existing === void 0 ? void 0 : existing.remoteDidcommEndpoint),
|
|
58
|
+
keys: keys,
|
|
59
|
+
createdAt: (existing === null || existing === void 0 ? void 0 : existing.createdAt) || new Date().toISOString(),
|
|
60
|
+
};
|
|
61
|
+
await store.putPeerSubscription(picoId, record);
|
|
62
|
+
return {
|
|
63
|
+
subscriptionId: input.subscriptionId,
|
|
64
|
+
peerDid: did,
|
|
65
|
+
rxEci,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
async function linkRemotePeer(store, picoId, subscriptionId, input) {
|
|
69
|
+
var _a, _b, _c, _d;
|
|
70
|
+
const sub = await store.getPeerSubscription(picoId, subscriptionId);
|
|
71
|
+
if (!sub) {
|
|
72
|
+
throw new errors_1.IdentityError(`Unknown subscription ${subscriptionId}`, 404);
|
|
73
|
+
}
|
|
74
|
+
const updated = Object.assign(Object.assign({}, sub), { remotePeerDid: input.remotePeerDid, remoteWebvhDid: (_a = input.remoteWebvhDid) !== null && _a !== void 0 ? _a : sub.remoteWebvhDid, remoteMeshRootId: (_b = input.remoteMeshRootId) !== null && _b !== void 0 ? _b : sub.remoteMeshRootId, remoteTxHost: (_c = input.remoteTxHost) !== null && _c !== void 0 ? _c : sub.remoteTxHost, remoteDidcommEndpoint: (_d = input.remoteDidcommEndpoint) !== null && _d !== void 0 ? _d : sub.remoteDidcommEndpoint });
|
|
75
|
+
await store.putPeerSubscription(picoId, updated);
|
|
76
|
+
return updated;
|
|
77
|
+
}
|
|
78
|
+
/** Parse wrangler/subscription bus map into establish input. */
|
|
79
|
+
function establishInputFromBus(bus) {
|
|
80
|
+
const subscriptionId = String(bus.Id || bus.subscriptionId || "");
|
|
81
|
+
return {
|
|
82
|
+
subscriptionId,
|
|
83
|
+
remotePeerDid: optionalString(bus.Tx_did) ||
|
|
84
|
+
optionalString(bus.remotePeerDid) ||
|
|
85
|
+
optionalString(bus.remoteDid),
|
|
86
|
+
remoteWebvhDid: optionalString(bus.remoteWebvhDid),
|
|
87
|
+
remoteMeshRootId: optionalString(bus.remoteMeshRootId),
|
|
88
|
+
remoteTxHost: optionalString(bus.Tx_host),
|
|
89
|
+
remoteDidcommEndpoint: optionalString(bus.remoteDidcommEndpoint),
|
|
90
|
+
rxEci: optionalString(bus.Rx),
|
|
91
|
+
channelName: optionalString(bus.channel_name),
|
|
92
|
+
channelType: optionalString(bus.channel_type),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function optionalString(value) {
|
|
96
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
97
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IDENTITY_STORAGE_KNOWN_GAP = exports.initDidoModule = exports.IdentityError = exports.IdentityService = exports.migrateFromDidO = exports.IDENTITY_PREFIX = exports.IdentityStore = void 0;
|
|
4
|
+
var store_1 = require("./store");
|
|
5
|
+
Object.defineProperty(exports, "IdentityStore", { enumerable: true, get: function () { return store_1.IdentityStore; } });
|
|
6
|
+
Object.defineProperty(exports, "IDENTITY_PREFIX", { enumerable: true, get: function () { return store_1.IDENTITY_PREFIX; } });
|
|
7
|
+
Object.defineProperty(exports, "migrateFromDidO", { enumerable: true, get: function () { return store_1.migrateFromDidO; } });
|
|
8
|
+
var IdentityService_1 = require("./IdentityService");
|
|
9
|
+
Object.defineProperty(exports, "IdentityService", { enumerable: true, get: function () { return IdentityService_1.IdentityService; } });
|
|
10
|
+
var errors_1 = require("./errors");
|
|
11
|
+
Object.defineProperty(exports, "IdentityError", { enumerable: true, get: function () { return errors_1.IdentityError; } });
|
|
12
|
+
var didoModule_1 = require("./didoModule");
|
|
13
|
+
Object.defineProperty(exports, "initDidoModule", { enumerable: true, get: function () { return didoModule_1.default; } });
|
|
14
|
+
var types_1 = require("./types");
|
|
15
|
+
Object.defineProperty(exports, "IDENTITY_STORAGE_KNOWN_GAP", { enumerable: true, get: function () { return types_1.IDENTITY_STORAGE_KNOWN_GAP; } });
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.peerKeysFromRecord = peerKeysFromRecord;
|
|
4
|
+
function peerKeysFromRecord(keys) {
|
|
5
|
+
if (!keys || !Array.isArray(keys.veramoKeys)) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
return keys.veramoKeys;
|
|
9
|
+
}
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.sendSkyIntro = sendSkyIntro;
|
|
4
|
+
exports.sendSkyIntroResponse = sendSkyIntroResponse;
|
|
5
|
+
exports.handleSkyIntroAtIngress = handleSkyIntroAtIngress;
|
|
6
|
+
exports.handleSkyIntroResponseAtIngress = handleSkyIntroResponseAtIngress;
|
|
7
|
+
const cuid = require("cuid");
|
|
8
|
+
const cross_fetch_1 = require("cross-fetch");
|
|
9
|
+
const errors_1 = require("./errors");
|
|
10
|
+
const skyProtocol_1 = require("./skyProtocol");
|
|
11
|
+
const crossPico_1 = require("./crossPico");
|
|
12
|
+
const veramoDidComm_1 = require("./veramoDidComm");
|
|
13
|
+
const establishSubscription_1 = require("./establishSubscription");
|
|
14
|
+
function findPico(pf, picoId) {
|
|
15
|
+
return pf.loadedPicos().find((p) => p.id === picoId);
|
|
16
|
+
}
|
|
17
|
+
function deliveryEci(pf, picoId, preferred) {
|
|
18
|
+
if (preferred) {
|
|
19
|
+
return preferred;
|
|
20
|
+
}
|
|
21
|
+
const pico = findPico(pf, picoId);
|
|
22
|
+
if (!pico) {
|
|
23
|
+
throw new errors_1.IdentityError(`Pico ${picoId} not loaded`, 404);
|
|
24
|
+
}
|
|
25
|
+
for (const ch of Object.values(pico.channels)) {
|
|
26
|
+
const ro = ch.toReadOnly();
|
|
27
|
+
if (ro.tags.includes("wellKnown_Rx")) {
|
|
28
|
+
return ro.id;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
for (const ch of Object.values(pico.channels)) {
|
|
32
|
+
const ro = ch.toReadOnly();
|
|
33
|
+
if (ro.tags.includes("allow-all")) {
|
|
34
|
+
return ro.id;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const first = Object.keys(pico.channels)[0];
|
|
38
|
+
if (!first) {
|
|
39
|
+
throw new errors_1.IdentityError("Pico has no channel for SKY intro delivery", 404);
|
|
40
|
+
}
|
|
41
|
+
return first;
|
|
42
|
+
}
|
|
43
|
+
async function resolveDidcommEndpointForDid(identity, did, remoteDidcommEndpoint) {
|
|
44
|
+
if (remoteDidcommEndpoint) {
|
|
45
|
+
return remoteDidcommEndpoint;
|
|
46
|
+
}
|
|
47
|
+
const doc = await identity.resolveDid("", did);
|
|
48
|
+
return (0, crossPico_1.extractDidcommEndpoint)(doc);
|
|
49
|
+
}
|
|
50
|
+
async function postJwe(endpoint, jwe) {
|
|
51
|
+
const res = await (0, cross_fetch_1.default)(endpoint, {
|
|
52
|
+
method: "POST",
|
|
53
|
+
headers: { "Content-Type": "application/didcomm-encrypted+json" },
|
|
54
|
+
body: jwe,
|
|
55
|
+
});
|
|
56
|
+
if (!res.ok) {
|
|
57
|
+
const detail = await res.text();
|
|
58
|
+
throw new errors_1.IdentityError(`DIDComm POST failed: HTTP ${res.status} ${detail}`, res.status);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
async function sendSkyIntro(deps, picoId, input) {
|
|
62
|
+
const established = await (0, establishSubscription_1.establishSubscriptionIdentity)(deps.store, deps.pf, picoId, {
|
|
63
|
+
subscriptionId: input.subscriptionId,
|
|
64
|
+
channelName: input.name,
|
|
65
|
+
channelType: input.channel_type || "Tx_Rx",
|
|
66
|
+
remoteWebvhDid: input.targetDid.startsWith("did:webvh:")
|
|
67
|
+
? input.targetDid
|
|
68
|
+
: undefined,
|
|
69
|
+
});
|
|
70
|
+
const body = {
|
|
71
|
+
sky_version: "1.0",
|
|
72
|
+
name: input.name,
|
|
73
|
+
channel_type: input.channel_type || "Tx_Rx",
|
|
74
|
+
Tx_role: input.Tx_role,
|
|
75
|
+
Rx_role: input.Rx_role,
|
|
76
|
+
peer_did_long: established.peerDid,
|
|
77
|
+
Tx_host: input.Tx_host || deps.getBaseUrl(),
|
|
78
|
+
subscription_id: input.subscriptionId,
|
|
79
|
+
};
|
|
80
|
+
const senderWebvh = await deps.store.getWebvhDid(picoId);
|
|
81
|
+
if (senderWebvh) {
|
|
82
|
+
body.sender_webvh = senderWebvh;
|
|
83
|
+
try {
|
|
84
|
+
const senderDoc = await deps.identity.resolveDid(picoId, senderWebvh);
|
|
85
|
+
body.didcomm_endpoint = (0, crossPico_1.extractDidcommEndpoint)(senderDoc);
|
|
86
|
+
}
|
|
87
|
+
catch (_a) {
|
|
88
|
+
// optional on intro body
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
const message = deps.identity.generateMessage({
|
|
92
|
+
type: skyProtocol_1.SKY_INTRO,
|
|
93
|
+
from: established.peerDid,
|
|
94
|
+
to: [input.targetDid],
|
|
95
|
+
body,
|
|
96
|
+
});
|
|
97
|
+
const targetPicoId = await deps.store.findPicoIdByWebvhDid(input.targetDid);
|
|
98
|
+
if (targetPicoId && findPico(deps.pf, targetPicoId)) {
|
|
99
|
+
await handleSkyIntroAtIngress(deps, targetPicoId, message);
|
|
100
|
+
return { messageId: message.id, peerDid: established.peerDid };
|
|
101
|
+
}
|
|
102
|
+
const endpoint = await resolveDidcommEndpointForDid(deps.identity, input.targetDid);
|
|
103
|
+
const jwe = await (0, veramoDidComm_1.packDidCommMessage)(picoId, message, established.peerDid, input.targetDid, deps.store, { resolveDid: (did) => deps.identity.resolveDid("", did) });
|
|
104
|
+
await postJwe(endpoint, jwe);
|
|
105
|
+
return { messageId: message.id, peerDid: established.peerDid };
|
|
106
|
+
}
|
|
107
|
+
async function sendSkyIntroResponse(deps, picoId, subscriptionId, toPeerDid, response, options = {}) {
|
|
108
|
+
var _a;
|
|
109
|
+
const sub = await deps.store.getPeerSubscription(picoId, subscriptionId);
|
|
110
|
+
if (!(sub === null || sub === void 0 ? void 0 : sub.peerDid)) {
|
|
111
|
+
throw new errors_1.IdentityError(`Unknown subscription ${subscriptionId}`, 404);
|
|
112
|
+
}
|
|
113
|
+
const body = Object.assign(Object.assign({ sky_version: "1.0" }, response), { subscription_id: response.subscription_id || subscriptionId, peer_did_long: response.peer_did_long || sub.peerDid, Tx_host: (_a = response.Tx_host) !== null && _a !== void 0 ? _a : deps.getBaseUrl() });
|
|
114
|
+
const message = deps.identity.generateMessage({
|
|
115
|
+
type: skyProtocol_1.SKY_INTRO_RESPONSE,
|
|
116
|
+
from: sub.peerDid,
|
|
117
|
+
to: [toPeerDid],
|
|
118
|
+
thid: options.thid,
|
|
119
|
+
body,
|
|
120
|
+
});
|
|
121
|
+
const targetPicoId = await deps.store.findPicoIdByLocalPeerDid(toPeerDid);
|
|
122
|
+
if (targetPicoId && findPico(deps.pf, targetPicoId)) {
|
|
123
|
+
await handleSkyIntroResponseAtIngress(deps, targetPicoId, message);
|
|
124
|
+
return message.id;
|
|
125
|
+
}
|
|
126
|
+
const endpoint = await resolveDidcommEndpointForDid(deps.identity, toPeerDid, options.remoteDidcommEndpoint || sub.remoteDidcommEndpoint);
|
|
127
|
+
const jwe = await (0, veramoDidComm_1.packDidCommMessage)(picoId, message, sub.peerDid, toPeerDid, deps.store, { resolveDid: (did) => deps.identity.resolveDid("", did) });
|
|
128
|
+
await postJwe(endpoint, jwe);
|
|
129
|
+
return message.id;
|
|
130
|
+
}
|
|
131
|
+
async function handleSkyIntroAtIngress(deps, picoId, plainMessage) {
|
|
132
|
+
const allowed = await deps.identity.acceptsPublicIntro(picoId);
|
|
133
|
+
if (!allowed) {
|
|
134
|
+
throw new errors_1.IdentityError("public_intro_disabled", 403);
|
|
135
|
+
}
|
|
136
|
+
const intro = (0, skyProtocol_1.parseSkyIntroBody)(plainMessage.body);
|
|
137
|
+
const subscriptionId = intro.subscription_id || cuid();
|
|
138
|
+
const remotePicoId = await deps.store.findPicoIdByLocalPeerDid(intro.peer_did_long);
|
|
139
|
+
const remoteWebvh = (remotePicoId && (await deps.store.getWebvhDid(remotePicoId))) ||
|
|
140
|
+
intro.sender_webvh ||
|
|
141
|
+
undefined;
|
|
142
|
+
const established = await (0, establishSubscription_1.establishSubscriptionIdentity)(deps.store, deps.pf, picoId, {
|
|
143
|
+
subscriptionId,
|
|
144
|
+
remotePeerDid: intro.peer_did_long,
|
|
145
|
+
remoteWebvhDid: remoteWebvh,
|
|
146
|
+
remoteTxHost: intro.Tx_host || undefined,
|
|
147
|
+
remoteDidcommEndpoint: intro.didcomm_endpoint || undefined,
|
|
148
|
+
channelName: intro.name,
|
|
149
|
+
channelType: intro.channel_type,
|
|
150
|
+
});
|
|
151
|
+
const attrs = (0, skyProtocol_1.skyIntroToWranglerAttrs)(intro, {
|
|
152
|
+
Id: subscriptionId,
|
|
153
|
+
Rx: established.rxEci,
|
|
154
|
+
channel_name: intro.name,
|
|
155
|
+
});
|
|
156
|
+
const eci = deliveryEci(deps.pf, picoId, established.rxEci);
|
|
157
|
+
await deps.pf.eventWait({
|
|
158
|
+
eci,
|
|
159
|
+
domain: "wrangler",
|
|
160
|
+
name: "sky_intro",
|
|
161
|
+
data: { attrs },
|
|
162
|
+
time: 0,
|
|
163
|
+
}, undefined);
|
|
164
|
+
return { status: "intro_delivered", subscriptionId };
|
|
165
|
+
}
|
|
166
|
+
async function handleSkyIntroResponseAtIngress(deps, picoId, plainMessage) {
|
|
167
|
+
const response = (0, skyProtocol_1.parseSkyIntroResponseBody)(plainMessage.body);
|
|
168
|
+
const subscriptionId = response.subscription_id;
|
|
169
|
+
if (!subscriptionId) {
|
|
170
|
+
throw new errors_1.IdentityError("intro-response missing subscription_id", 400);
|
|
171
|
+
}
|
|
172
|
+
const sub = await deps.store.getPeerSubscription(picoId, subscriptionId);
|
|
173
|
+
const eci = deliveryEci(deps.pf, picoId, sub === null || sub === void 0 ? void 0 : sub.rxEci);
|
|
174
|
+
if (response.status === "accepted" && response.peer_did_long) {
|
|
175
|
+
const remotePicoId = await deps.store.findPicoIdByLocalPeerDid(response.peer_did_long);
|
|
176
|
+
const remoteWebvh = remotePicoId && (await deps.store.getWebvhDid(remotePicoId));
|
|
177
|
+
await (0, establishSubscription_1.linkRemotePeer)(deps.store, picoId, subscriptionId, {
|
|
178
|
+
remotePeerDid: response.peer_did_long,
|
|
179
|
+
remoteWebvhDid: remoteWebvh || undefined,
|
|
180
|
+
remoteTxHost: response.Tx_host || undefined,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
const attrs = (0, skyProtocol_1.skyIntroResponseToWranglerAttrs)(response, {
|
|
184
|
+
Id: subscriptionId,
|
|
185
|
+
});
|
|
186
|
+
await deps.pf.eventWait({
|
|
187
|
+
eci,
|
|
188
|
+
domain: "wrangler",
|
|
189
|
+
name: "sky_intro_response",
|
|
190
|
+
data: { attrs },
|
|
191
|
+
time: 0,
|
|
192
|
+
}, undefined);
|
|
193
|
+
return { status: "intro_response_delivered", subscriptionId };
|
|
194
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/** SKY protocol on DIDComm v2 — type URIs and payloads (1.0). */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.SKY_QUERY_RESPONSE = exports.SKY_QUERY = exports.SKY_EVENT = exports.SKY_INTRO_RESPONSE = exports.SKY_INTRO = exports.SKY_NS = void 0;
|
|
5
|
+
exports.parseSkyIntroBody = parseSkyIntroBody;
|
|
6
|
+
exports.parseSkyIntroResponseBody = parseSkyIntroResponseBody;
|
|
7
|
+
exports.skyIntroToWranglerAttrs = skyIntroToWranglerAttrs;
|
|
8
|
+
exports.skyIntroResponseToWranglerAttrs = skyIntroResponseToWranglerAttrs;
|
|
9
|
+
exports.parseSkyEventBody = parseSkyEventBody;
|
|
10
|
+
exports.parseSkyQueryBody = parseSkyQueryBody;
|
|
11
|
+
exports.SKY_NS = "https://picolabs.org/sky/1.0";
|
|
12
|
+
exports.SKY_INTRO = `${exports.SKY_NS}/intro`;
|
|
13
|
+
exports.SKY_INTRO_RESPONSE = `${exports.SKY_NS}/intro-response`;
|
|
14
|
+
exports.SKY_EVENT = `${exports.SKY_NS}/event`;
|
|
15
|
+
exports.SKY_QUERY = `${exports.SKY_NS}/query`;
|
|
16
|
+
exports.SKY_QUERY_RESPONSE = `${exports.SKY_NS}/query-response`;
|
|
17
|
+
function parseSkyIntroBody(body) {
|
|
18
|
+
if (!body || typeof body !== "object") {
|
|
19
|
+
throw new Error("Invalid SKY intro body");
|
|
20
|
+
}
|
|
21
|
+
const b = body;
|
|
22
|
+
if (typeof b.name !== "string" ||
|
|
23
|
+
typeof b.Tx_role !== "string" ||
|
|
24
|
+
typeof b.Rx_role !== "string" ||
|
|
25
|
+
typeof b.peer_did_long !== "string") {
|
|
26
|
+
throw new Error("SKY intro body missing required fields");
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
sky_version: String(b.sky_version || "1.0"),
|
|
30
|
+
name: b.name,
|
|
31
|
+
channel_type: typeof b.channel_type === "string" ? b.channel_type : "Tx_Rx",
|
|
32
|
+
Tx_role: b.Tx_role,
|
|
33
|
+
Rx_role: b.Rx_role,
|
|
34
|
+
peer_did_long: b.peer_did_long,
|
|
35
|
+
Tx_host: b.Tx_host === null || typeof b.Tx_host === "string"
|
|
36
|
+
? b.Tx_host
|
|
37
|
+
: undefined,
|
|
38
|
+
didcomm_endpoint: typeof b.didcomm_endpoint === "string" ? b.didcomm_endpoint : undefined,
|
|
39
|
+
sender_webvh: typeof b.sender_webvh === "string" ? b.sender_webvh : undefined,
|
|
40
|
+
subscription_id: typeof b.subscription_id === "string" ? b.subscription_id : undefined,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
function parseSkyIntroResponseBody(body) {
|
|
44
|
+
if (!body || typeof body !== "object") {
|
|
45
|
+
throw new Error("Invalid SKY intro-response body");
|
|
46
|
+
}
|
|
47
|
+
const b = body;
|
|
48
|
+
if (b.status !== "accepted" && b.status !== "rejected") {
|
|
49
|
+
throw new Error("SKY intro-response missing status");
|
|
50
|
+
}
|
|
51
|
+
return {
|
|
52
|
+
sky_version: String(b.sky_version || "1.0"),
|
|
53
|
+
status: b.status,
|
|
54
|
+
subscription_id: typeof b.subscription_id === "string" ? b.subscription_id : undefined,
|
|
55
|
+
peer_did_long: typeof b.peer_did_long === "string" ? b.peer_did_long : undefined,
|
|
56
|
+
Tx_host: b.Tx_host === null || typeof b.Tx_host === "string"
|
|
57
|
+
? b.Tx_host
|
|
58
|
+
: undefined,
|
|
59
|
+
reason: typeof b.reason === "string" ? b.reason : undefined,
|
|
60
|
+
detail: typeof b.detail === "string" ? b.detail : undefined,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** Map SKY intro to wrangler subscription attrs (layer 2). */
|
|
64
|
+
function skyIntroToWranglerAttrs(intro, extras = {}) {
|
|
65
|
+
return Object.assign({ layer2: true, name: intro.name, channel_type: intro.channel_type || "Tx_Rx", Tx_role: intro.Tx_role, Rx_role: intro.Rx_role, peer_did_long: intro.peer_did_long, Tx_did: intro.peer_did_long, Tx_host: intro.Tx_host || null, Id: intro.subscription_id }, extras);
|
|
66
|
+
}
|
|
67
|
+
function skyIntroResponseToWranglerAttrs(response, extras = {}) {
|
|
68
|
+
return Object.assign({ layer2: true, status: response.status, subscription_id: response.subscription_id, peer_did_long: response.peer_did_long, Tx_did: response.peer_did_long, Tx_host: response.Tx_host || null, reason: response.reason, detail: response.detail }, extras);
|
|
69
|
+
}
|
|
70
|
+
function parseSkyEventBody(body) {
|
|
71
|
+
if (!body || typeof body !== "object") {
|
|
72
|
+
throw new Error("Invalid SKY event body");
|
|
73
|
+
}
|
|
74
|
+
const b = body;
|
|
75
|
+
if (typeof b.domain !== "string" || typeof b.name !== "string") {
|
|
76
|
+
throw new Error("SKY event body missing domain or name");
|
|
77
|
+
}
|
|
78
|
+
return {
|
|
79
|
+
sky_version: String(b.sky_version || "1.0"),
|
|
80
|
+
eid: typeof b.eid === "string" ? b.eid : "none",
|
|
81
|
+
domain: b.domain,
|
|
82
|
+
name: b.name,
|
|
83
|
+
attrs: b.attrs && typeof b.attrs === "object"
|
|
84
|
+
? b.attrs
|
|
85
|
+
: {},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function parseSkyQueryBody(body) {
|
|
89
|
+
if (!body || typeof body !== "object") {
|
|
90
|
+
throw new Error("Invalid SKY query body");
|
|
91
|
+
}
|
|
92
|
+
const b = body;
|
|
93
|
+
if (typeof b.rid !== "string" ||
|
|
94
|
+
typeof b.name !== "string") {
|
|
95
|
+
throw new Error("SKY query body missing rid or name");
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
sky_version: String(b.sky_version || "1.0"),
|
|
99
|
+
rid: b.rid,
|
|
100
|
+
name: b.name,
|
|
101
|
+
args: b.args && typeof b.args === "object"
|
|
102
|
+
? b.args
|
|
103
|
+
: {},
|
|
104
|
+
};
|
|
105
|
+
}
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IdentityStore = exports.IDENTITY_PREFIX = void 0;
|
|
4
|
+
exports.migrateFromDidO = migrateFromDidO;
|
|
5
|
+
const dbHelpers_1 = require("./dbHelpers");
|
|
6
|
+
exports.IDENTITY_PREFIX = "pico-identity";
|
|
7
|
+
const FIELD = {
|
|
8
|
+
webvhDid: "webvhDid",
|
|
9
|
+
webvhLog: "webvhLog",
|
|
10
|
+
webvhKeys: "webvhKeys",
|
|
11
|
+
meshRootId: "meshRootId",
|
|
12
|
+
publicIntro: "publicIntro",
|
|
13
|
+
pendingRotations: "pendingRotations",
|
|
14
|
+
peerSub: "peerSub",
|
|
15
|
+
didDoc: "didDoc",
|
|
16
|
+
routes: "routes",
|
|
17
|
+
didcommIngressEci: "didcommIngressEci",
|
|
18
|
+
webvhWebDoc: "webvhWebDoc",
|
|
19
|
+
};
|
|
20
|
+
function identityKey(picoId, ...rest) {
|
|
21
|
+
return [exports.IDENTITY_PREFIX, picoId, ...rest];
|
|
22
|
+
}
|
|
23
|
+
function picoPrefix(picoId) {
|
|
24
|
+
return [exports.IDENTITY_PREFIX, picoId];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Engine-owned per-pico DID/DIDComm state. Not exposed to KRL.
|
|
28
|
+
*
|
|
29
|
+
* 1.6 stores private keys as plaintext JSON — see IDENTITY_STORAGE_KNOWN_GAP.
|
|
30
|
+
*/
|
|
31
|
+
class IdentityStore {
|
|
32
|
+
constructor(deps) {
|
|
33
|
+
this.db = deps.db;
|
|
34
|
+
}
|
|
35
|
+
// ---- scalar metadata ----
|
|
36
|
+
async getWebvhDid(picoId) {
|
|
37
|
+
return (0, dbHelpers_1.dbGet)(this.db, identityKey(picoId, FIELD.webvhDid));
|
|
38
|
+
}
|
|
39
|
+
async putWebvhDid(picoId, did) {
|
|
40
|
+
await (0, dbHelpers_1.dbPut)(this.db, identityKey(picoId, FIELD.webvhDid), did);
|
|
41
|
+
}
|
|
42
|
+
async getMeshRootId(picoId) {
|
|
43
|
+
return (0, dbHelpers_1.dbGet)(this.db, identityKey(picoId, FIELD.meshRootId));
|
|
44
|
+
}
|
|
45
|
+
async putMeshRootId(picoId, meshRootId) {
|
|
46
|
+
await (0, dbHelpers_1.dbPut)(this.db, identityKey(picoId, FIELD.meshRootId), meshRootId);
|
|
47
|
+
}
|
|
48
|
+
async getPublicIntro(picoId) {
|
|
49
|
+
return (0, dbHelpers_1.dbGet)(this.db, identityKey(picoId, FIELD.publicIntro));
|
|
50
|
+
}
|
|
51
|
+
async putPublicIntro(picoId, publicIntro) {
|
|
52
|
+
await (0, dbHelpers_1.dbPut)(this.db, identityKey(picoId, FIELD.publicIntro), publicIntro);
|
|
53
|
+
}
|
|
54
|
+
// ---- webvh log + keys ----
|
|
55
|
+
async getWebvhLog(picoId) {
|
|
56
|
+
return (0, dbHelpers_1.dbGet)(this.db, identityKey(picoId, FIELD.webvhLog));
|
|
57
|
+
}
|
|
58
|
+
async putWebvhLog(picoId, log) {
|
|
59
|
+
await (0, dbHelpers_1.dbPut)(this.db, identityKey(picoId, FIELD.webvhLog), log);
|
|
60
|
+
}
|
|
61
|
+
async getWebvhKeys(picoId) {
|
|
62
|
+
return (0, dbHelpers_1.dbGet)(this.db, identityKey(picoId, FIELD.webvhKeys));
|
|
63
|
+
}
|
|
64
|
+
async putWebvhKeys(picoId, keys) {
|
|
65
|
+
await (0, dbHelpers_1.dbPut)(this.db, identityKey(picoId, FIELD.webvhKeys), keys);
|
|
66
|
+
}
|
|
67
|
+
async getDidcommIngressEci(picoId) {
|
|
68
|
+
return (0, dbHelpers_1.dbGet)(this.db, identityKey(picoId, FIELD.didcommIngressEci));
|
|
69
|
+
}
|
|
70
|
+
async putDidcommIngressEci(picoId, eci) {
|
|
71
|
+
await (0, dbHelpers_1.dbPut)(this.db, identityKey(picoId, FIELD.didcommIngressEci), eci);
|
|
72
|
+
}
|
|
73
|
+
/** Reverse lookup: ingress channel ECI → pico id. */
|
|
74
|
+
async findPicoIdByIngressEci(eci) {
|
|
75
|
+
const rows = await (0, dbHelpers_1.dbList)(this.db, [exports.IDENTITY_PREFIX]);
|
|
76
|
+
for (const { key, value } of rows) {
|
|
77
|
+
if (key.length === 3 &&
|
|
78
|
+
key[2] === FIELD.didcommIngressEci &&
|
|
79
|
+
typeof value === "string" &&
|
|
80
|
+
value === eci) {
|
|
81
|
+
return String(key[1]);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
/** Find subscription by inbound sender peer DID (short or long form). */
|
|
87
|
+
async findPeerSubscriptionBySenderDid(picoId, senderDid) {
|
|
88
|
+
const normalized = senderDid.trim();
|
|
89
|
+
for (const sub of await this.listPeerSubscriptions(picoId)) {
|
|
90
|
+
const remote = sub.remotePeerDid || sub.remoteDid;
|
|
91
|
+
if (!remote) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (remote === normalized || peerDidMatches(remote, normalized)) {
|
|
95
|
+
return sub;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
async getWebvhWebDoc(picoId) {
|
|
101
|
+
return (0, dbHelpers_1.dbGet)(this.db, identityKey(picoId, FIELD.webvhWebDoc));
|
|
102
|
+
}
|
|
103
|
+
async putWebvhWebDoc(picoId, doc) {
|
|
104
|
+
await (0, dbHelpers_1.dbPut)(this.db, identityKey(picoId, FIELD.webvhWebDoc), doc);
|
|
105
|
+
}
|
|
106
|
+
async findPicoIdByLocalPeerDid(peerDid) {
|
|
107
|
+
const rows = await (0, dbHelpers_1.dbList)(this.db, [exports.IDENTITY_PREFIX]);
|
|
108
|
+
const picoIds = new Set();
|
|
109
|
+
for (const { key } of rows) {
|
|
110
|
+
if (key.length >= 2 && typeof key[1] === "string") {
|
|
111
|
+
picoIds.add(key[1]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
for (const picoId of picoIds) {
|
|
115
|
+
for (const sub of await this.listPeerSubscriptions(picoId)) {
|
|
116
|
+
if (peerDidMatches(sub.peerDid, peerDid)) {
|
|
117
|
+
return picoId;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
/** Find established subscription id routed to the given DID (peer or webvh). */
|
|
124
|
+
async findSubscriptionIdForDid(picoId, did) {
|
|
125
|
+
const normalized = did.trim();
|
|
126
|
+
for (const sub of await this.listPeerSubscriptions(picoId)) {
|
|
127
|
+
if (sub.remoteWebvhDid === normalized) {
|
|
128
|
+
return sub.subscriptionId;
|
|
129
|
+
}
|
|
130
|
+
const remote = sub.remotePeerDid || sub.remoteDid;
|
|
131
|
+
if (remote && peerDidMatches(remote, normalized)) {
|
|
132
|
+
return sub.subscriptionId;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
/** Reverse lookup for locally hosted did:webvh identifiers. */
|
|
138
|
+
async findPicoIdByWebvhDid(did) {
|
|
139
|
+
const rows = await (0, dbHelpers_1.dbList)(this.db, [exports.IDENTITY_PREFIX]);
|
|
140
|
+
for (const { key, value } of rows) {
|
|
141
|
+
if (key.length === 3 &&
|
|
142
|
+
key[2] === FIELD.webvhDid &&
|
|
143
|
+
typeof value === "string" &&
|
|
144
|
+
value === did) {
|
|
145
|
+
return String(key[1]);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
// ---- pending rotations ----
|
|
151
|
+
async getPendingRotations(picoId) {
|
|
152
|
+
return (0, dbHelpers_1.dbGet)(this.db, identityKey(picoId, FIELD.pendingRotations));
|
|
153
|
+
}
|
|
154
|
+
async putPendingRotations(picoId, rotations) {
|
|
155
|
+
await (0, dbHelpers_1.dbPut)(this.db, identityKey(picoId, FIELD.pendingRotations), rotations);
|
|
156
|
+
}
|
|
157
|
+
// ---- peer subscriptions (pairwise did:peer) ----
|
|
158
|
+
async getPeerSubscription(picoId, subscriptionId) {
|
|
159
|
+
return (0, dbHelpers_1.dbGet)(this.db, identityKey(picoId, FIELD.peerSub, subscriptionId));
|
|
160
|
+
}
|
|
161
|
+
async putPeerSubscription(picoId, record) {
|
|
162
|
+
await (0, dbHelpers_1.dbPut)(this.db, identityKey(picoId, FIELD.peerSub, record.subscriptionId), record);
|
|
163
|
+
}
|
|
164
|
+
async deletePeerSubscription(picoId, subscriptionId) {
|
|
165
|
+
await (0, dbHelpers_1.dbDel)(this.db, identityKey(picoId, FIELD.peerSub, subscriptionId));
|
|
166
|
+
}
|
|
167
|
+
async listPeerSubscriptions(picoId) {
|
|
168
|
+
const rows = await (0, dbHelpers_1.dbList)(this.db, identityKey(picoId, FIELD.peerSub));
|
|
169
|
+
return rows.map((r) => r.value);
|
|
170
|
+
}
|
|
171
|
+
// ---- DID document cache ----
|
|
172
|
+
async getDidDoc(picoId, did) {
|
|
173
|
+
return (0, dbHelpers_1.dbGet)(this.db, identityKey(picoId, FIELD.didDoc, did));
|
|
174
|
+
}
|
|
175
|
+
async putDidDoc(picoId, cached) {
|
|
176
|
+
await (0, dbHelpers_1.dbPut)(this.db, identityKey(picoId, FIELD.didDoc, cached.did), cached);
|
|
177
|
+
}
|
|
178
|
+
async deleteDidDoc(picoId, did) {
|
|
179
|
+
await (0, dbHelpers_1.dbDel)(this.db, identityKey(picoId, FIELD.didDoc, did));
|
|
180
|
+
}
|
|
181
|
+
async listDidDocs(picoId) {
|
|
182
|
+
const rows = await (0, dbHelpers_1.dbList)(this.db, identityKey(picoId, FIELD.didDoc));
|
|
183
|
+
return rows.map((r) => r.value);
|
|
184
|
+
}
|
|
185
|
+
async getRoutes(picoId) {
|
|
186
|
+
return (0, dbHelpers_1.dbGet)(this.db, identityKey(picoId, FIELD.routes));
|
|
187
|
+
}
|
|
188
|
+
async putRoutes(picoId, routes) {
|
|
189
|
+
await (0, dbHelpers_1.dbPut)(this.db, identityKey(picoId, FIELD.routes), routes);
|
|
190
|
+
}
|
|
191
|
+
// ---- lifecycle ----
|
|
192
|
+
/** True when any identity subkey exists for this pico. */
|
|
193
|
+
async hasIdentity(picoId) {
|
|
194
|
+
const rows = await (0, dbHelpers_1.dbList)(this.db, picoPrefix(picoId));
|
|
195
|
+
return rows.length > 0;
|
|
196
|
+
}
|
|
197
|
+
/** Remove all identity state for a pico (e.g. on delete — Epic 4+). */
|
|
198
|
+
async deleteAll(picoId) {
|
|
199
|
+
const rows = await (0, dbHelpers_1.dbList)(this.db, picoPrefix(picoId));
|
|
200
|
+
if (rows.length === 0) {
|
|
201
|
+
return;
|
|
202
|
+
}
|
|
203
|
+
const batch = this.db.batch();
|
|
204
|
+
for (const { key } of rows) {
|
|
205
|
+
batch.del(key);
|
|
206
|
+
}
|
|
207
|
+
await batch.write();
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
exports.IdentityStore = IdentityStore;
|
|
211
|
+
/** No-op for 1.6 — legacy did-o ent: migration deferred. */
|
|
212
|
+
async function migrateFromDidO(_picoId) {
|
|
213
|
+
return;
|
|
214
|
+
}
|
|
215
|
+
/** Match did:peer short/long forms (numalgo 0/2). */
|
|
216
|
+
function peerDidMatches(stored, incoming) {
|
|
217
|
+
if (stored === incoming) {
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
const storedShort = peerDidShortForm(stored);
|
|
221
|
+
const incomingShort = peerDidShortForm(incoming);
|
|
222
|
+
return storedShort.length > 0 && storedShort === incomingShort;
|
|
223
|
+
}
|
|
224
|
+
function peerDidShortForm(did) {
|
|
225
|
+
if (!did.startsWith("did:peer:")) {
|
|
226
|
+
return did;
|
|
227
|
+
}
|
|
228
|
+
const parts = did.split(".");
|
|
229
|
+
if (parts.length >= 3) {
|
|
230
|
+
return `${parts[0]}.${parts[parts.length - 1]}`;
|
|
231
|
+
}
|
|
232
|
+
return did;
|
|
233
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Engine-owned per-pico identity state (Epic 1).
|
|
4
|
+
*
|
|
5
|
+
* Private key shapes are intentionally loose until didwebvh-ts / did-peer-4
|
|
6
|
+
* integration (Epic 2–3). KRL must never receive these types directly.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.IDENTITY_STORAGE_KNOWN_GAP = void 0;
|
|
10
|
+
/** @see docs/design/pico-identity-layer2-work.md § 1.6-known-gap */
|
|
11
|
+
exports.IDENTITY_STORAGE_KNOWN_GAP = "Identity private keys are stored unencrypted in the engine DB (1.6). " +
|
|
12
|
+
"Encryption at rest and passkey-gated unlock are required before treating " +
|
|
13
|
+
"self-hosted engines as non-custodial for agent keys.";
|