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.
Files changed (82) hide show
  1. package/README.md +188 -0
  2. package/dist/RulesetRegistryLoaderFs.js +2 -0
  3. package/dist/auth/AuthService.js +601 -0
  4. package/dist/auth/index.js +8 -0
  5. package/dist/auth/webauthn.js +97 -0
  6. package/dist/bundledKrl.js +42 -0
  7. package/dist/cli.js +11 -0
  8. package/dist/identity/IdentityService.js +340 -0
  9. package/dist/identity/crossPico.js +238 -0
  10. package/dist/identity/dbHelpers.js +67 -0
  11. package/dist/identity/didcommIngressRoutes.js +44 -0
  12. package/dist/identity/didoModule.js +252 -0
  13. package/dist/identity/errors.js +11 -0
  14. package/dist/identity/establishSubscription.js +97 -0
  15. package/dist/identity/index.js +15 -0
  16. package/dist/identity/peerDid.js +9 -0
  17. package/dist/identity/skyIntro.js +194 -0
  18. package/dist/identity/skyProtocol.js +105 -0
  19. package/dist/identity/store.js +233 -0
  20. package/dist/identity/types.js +13 -0
  21. package/dist/identity/veramoDidComm.js +260 -0
  22. package/dist/identity/webvh.js +266 -0
  23. package/dist/identity/webvhCrypto.js +128 -0
  24. package/dist/identity/webvhRoutes.js +61 -0
  25. package/dist/index.js +100 -44
  26. package/dist/oauth/OAuthService.js +388 -0
  27. package/dist/oauth/acg.js +495 -0
  28. package/dist/oauth/channelEligibility.js +26 -0
  29. package/dist/oauth/errors.js +12 -0
  30. package/dist/oauth/index.js +21 -0
  31. package/dist/oauth/krlModule.js +27 -0
  32. package/dist/oauth/meshOAuth.js +43 -0
  33. package/dist/provisionRoot.js +118 -0
  34. package/dist/server.js +609 -15
  35. package/dist/uiChannelPolicies.js +48 -0
  36. package/krl/io.picolabs.did-o.krl +5 -1
  37. package/krl/io.picolabs.oauth.krl +48 -0
  38. package/krl/io.picolabs.pico-engine-ui.krl +106 -18
  39. package/krl/io.picolabs.subscription.krl +242 -23
  40. package/krl/io.picolabs.wrangler.krl +225 -16
  41. package/package.json +38 -9
  42. package/public/docs/krl/_sidebar.md +1 -0
  43. package/public/docs/krl/discovery.md +144 -0
  44. package/public/docs/krl/modules/ctx.md +8 -0
  45. package/public/docs/krl/modules/event.md +41 -0
  46. package/public/pico-engine-ui.css +1 -1
  47. package/public/pico-engine-ui.js +24 -20
  48. package/public/pico-labs-logo.png +0 -0
  49. package/src/RulesetRegistryLoaderFs.ts +2 -0
  50. package/src/auth/AuthService.ts +850 -0
  51. package/src/auth/index.ts +17 -0
  52. package/src/auth/webauthn.ts +182 -0
  53. package/src/bundledKrl.ts +42 -0
  54. package/src/cli.ts +11 -0
  55. package/src/identity/IdentityService.ts +571 -0
  56. package/src/identity/crossPico.ts +371 -0
  57. package/src/identity/dbHelpers.ts +51 -0
  58. package/src/identity/didcommIngressRoutes.ts +54 -0
  59. package/src/identity/didoModule.ts +388 -0
  60. package/src/identity/errors.ts +8 -0
  61. package/src/identity/establishSubscription.ts +160 -0
  62. package/src/identity/index.ts +24 -0
  63. package/src/identity/peerDid.ts +22 -0
  64. package/src/identity/skyIntro.ts +321 -0
  65. package/src/identity/skyProtocol.ts +192 -0
  66. package/src/identity/store.ts +343 -0
  67. package/src/identity/types.ts +63 -0
  68. package/src/identity/veramoDidComm.ts +363 -0
  69. package/src/identity/webvh.ts +325 -0
  70. package/src/identity/webvhCrypto.ts +200 -0
  71. package/src/identity/webvhRoutes.ts +64 -0
  72. package/src/index.ts +145 -53
  73. package/src/oauth/OAuthService.ts +510 -0
  74. package/src/oauth/acg.ts +666 -0
  75. package/src/oauth/channelEligibility.ts +26 -0
  76. package/src/oauth/errors.ts +10 -0
  77. package/src/oauth/index.ts +14 -0
  78. package/src/oauth/krlModule.ts +31 -0
  79. package/src/oauth/meshOAuth.ts +50 -0
  80. package/src/provisionRoot.ts +151 -0
  81. package/src/server.ts +700 -24
  82. package/src/uiChannelPolicies.ts +49 -0
@@ -0,0 +1,238 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.usesLocalDispatch = usesLocalDispatch;
4
+ exports.resolveRecipientPicoId = resolveRecipientPicoId;
5
+ exports.crossPicoEvent = crossPicoEvent;
6
+ exports.crossPicoQuery = crossPicoQuery;
7
+ exports.extractDidcommEndpoint = extractDidcommEndpoint;
8
+ exports.buildQueryResponseMessage = buildQueryResponseMessage;
9
+ exports.buildQueryErrorResponseMessage = buildQueryErrorResponseMessage;
10
+ const cuid = require("cuid");
11
+ const cross_fetch_1 = require("cross-fetch");
12
+ const skyProtocol_1 = require("./skyProtocol");
13
+ const veramoDidComm_1 = require("./veramoDidComm");
14
+ const errors_1 = require("./errors");
15
+ function remotePeerDid(sub) {
16
+ const did = sub.remotePeerDid || sub.remoteDid;
17
+ if (!did) {
18
+ throw new errors_1.IdentityError("Subscription missing remote peer DID", 400);
19
+ }
20
+ return did;
21
+ }
22
+ function remoteTxHost(sub, fallback) {
23
+ const host = sub.remoteTxHost || fallback;
24
+ return host.replace(/\/$/, "");
25
+ }
26
+ async function usesLocalDispatch(store, pf, fromPicoId, toPicoId, sub) {
27
+ const fromMesh = await store.getMeshRootId(fromPicoId);
28
+ const toMesh = await store.getMeshRootId(toPicoId);
29
+ if (!fromMesh || !toMesh || fromMesh !== toMesh) {
30
+ return false;
31
+ }
32
+ if (sub.remoteMeshRootId && sub.remoteMeshRootId !== fromMesh) {
33
+ return false;
34
+ }
35
+ return pf.loadedPicos().some((p) => p.id === toPicoId);
36
+ }
37
+ async function resolveRecipientPicoId(store, pf, fromPicoId, subscriptionId) {
38
+ const sub = await store.getPeerSubscription(fromPicoId, subscriptionId);
39
+ if (!sub) {
40
+ return undefined;
41
+ }
42
+ const remotePeer = sub.remotePeerDid || sub.remoteDid;
43
+ if (remotePeer) {
44
+ const byPeer = await store.findPicoIdByLocalPeerDid(remotePeer);
45
+ if (byPeer && pf.loadedPicos().some((p) => p.id === byPeer)) {
46
+ return byPeer;
47
+ }
48
+ }
49
+ if (sub.remoteWebvhDid) {
50
+ const byWebvh = await store.findPicoIdByWebvhDid(sub.remoteWebvhDid);
51
+ if (byWebvh && pf.loadedPicos().some((p) => p.id === byWebvh)) {
52
+ return byWebvh;
53
+ }
54
+ }
55
+ return undefined;
56
+ }
57
+ async function crossPicoEvent(deps, fromPicoId, toPicoId, subscriptionId, event) {
58
+ const sub = await deps.store.getPeerSubscription(fromPicoId, subscriptionId);
59
+ if (!sub || !sub.rxEci) {
60
+ throw new errors_1.IdentityError(`Unknown subscription ${subscriptionId}`, 404);
61
+ }
62
+ const callerDid = await deps.store.getWebvhDid(fromPicoId);
63
+ const attrs = Object.assign(Object.assign({}, (event.attrs || {})), { callerDid: callerDid || undefined });
64
+ const to = toPicoId ||
65
+ (await resolveRecipientPicoId(deps.store, deps.pf, fromPicoId, subscriptionId));
66
+ if (to &&
67
+ (await usesLocalDispatch(deps.store, deps.pf, fromPicoId, to, sub))) {
68
+ const recipientSub = await findRecipientSub(deps.store, to, subscriptionId);
69
+ return deps.pf.eventWait({
70
+ eci: recipientSub.rxEci,
71
+ domain: event.domain,
72
+ name: event.name,
73
+ data: { attrs },
74
+ time: 0,
75
+ }, fromPicoId);
76
+ }
77
+ return sendSkyEvent(deps, fromPicoId, sub, event, attrs);
78
+ }
79
+ async function crossPicoQuery(deps, fromPicoId, toPicoId, subscriptionId, query) {
80
+ const sub = await deps.store.getPeerSubscription(fromPicoId, subscriptionId);
81
+ if (!sub || !sub.rxEci) {
82
+ throw new errors_1.IdentityError(`Unknown subscription ${subscriptionId}`, 404);
83
+ }
84
+ const to = toPicoId ||
85
+ (await resolveRecipientPicoId(deps.store, deps.pf, fromPicoId, subscriptionId));
86
+ if (to &&
87
+ (await usesLocalDispatch(deps.store, deps.pf, fromPicoId, to, sub))) {
88
+ const recipientSub = await findRecipientSub(deps.store, to, subscriptionId);
89
+ return deps.pf.query({
90
+ eci: recipientSub.rxEci,
91
+ rid: query.rid,
92
+ name: query.name,
93
+ args: query.args || {},
94
+ }, fromPicoId);
95
+ }
96
+ return sendSkyQuery(deps, fromPicoId, sub, query);
97
+ }
98
+ async function findRecipientSub(store, toPicoId, subscriptionId) {
99
+ const recipientSub = await store.getPeerSubscription(toPicoId, subscriptionId);
100
+ if (!(recipientSub === null || recipientSub === void 0 ? void 0 : recipientSub.rxEci)) {
101
+ throw new errors_1.IdentityError(`Recipient subscription ${subscriptionId} missing rxEci`, 404);
102
+ }
103
+ return recipientSub;
104
+ }
105
+ async function sendSkyEvent(deps, fromPicoId, sub, event, attrs) {
106
+ const message = deps.identity.generateMessage({
107
+ type: skyProtocol_1.SKY_EVENT,
108
+ from: sub.peerDid,
109
+ to: [remotePeerDid(sub)],
110
+ body: {
111
+ sky_version: "1.0",
112
+ eid: event.eid || "none",
113
+ domain: event.domain,
114
+ name: event.name,
115
+ attrs,
116
+ },
117
+ });
118
+ const jwe = await (0, veramoDidComm_1.packDidCommMessage)(fromPicoId, message, sub.peerDid, remotePeerDid(sub), deps.store, { resolveDid: (did) => deps.identity.resolveDid("", did) });
119
+ const endpoint = await resolveRemoteDidcommEndpoint(deps, sub);
120
+ const res = await (0, cross_fetch_1.default)(endpoint, {
121
+ method: "POST",
122
+ headers: {
123
+ "Content-Type": "application/didcomm-encrypted+json",
124
+ },
125
+ body: jwe,
126
+ });
127
+ if (!res.ok) {
128
+ throw new errors_1.IdentityError(`DIDComm event delivery failed: HTTP ${res.status}`, res.status);
129
+ }
130
+ const data = await res.json();
131
+ return {
132
+ eid: data.eid || "none",
133
+ responses: data.responses || data.directives || [],
134
+ };
135
+ }
136
+ async function sendSkyQuery(deps, fromPicoId, sub, query) {
137
+ var _a;
138
+ const queryId = cuid();
139
+ const message = deps.identity.generateMessage({
140
+ type: skyProtocol_1.SKY_QUERY,
141
+ from: sub.peerDid,
142
+ to: [remotePeerDid(sub)],
143
+ body: {
144
+ sky_version: "1.0",
145
+ rid: query.rid,
146
+ name: query.name,
147
+ args: query.args || {},
148
+ },
149
+ });
150
+ message.id = queryId;
151
+ const jwe = await (0, veramoDidComm_1.packDidCommMessage)(fromPicoId, message, sub.peerDid, remotePeerDid(sub), deps.store, { resolveDid: (did) => deps.identity.resolveDid("", did) });
152
+ const endpoint = await resolveRemoteDidcommEndpoint(deps, sub);
153
+ const res = await (0, cross_fetch_1.default)(endpoint, {
154
+ method: "POST",
155
+ headers: {
156
+ "Content-Type": "application/didcomm-encrypted+json",
157
+ },
158
+ body: jwe,
159
+ });
160
+ if (!res.ok) {
161
+ const detail = await res.text();
162
+ throw new errors_1.IdentityError(`DIDComm query delivery failed: HTTP ${res.status} ${detail}`, res.status);
163
+ }
164
+ const data = await res.json();
165
+ if (data.result !== undefined) {
166
+ return data.result;
167
+ }
168
+ if (data.status === "error") {
169
+ throw new errors_1.IdentityError(((_a = data.error) === null || _a === void 0 ? void 0 : _a.message) || "Remote query failed", 502);
170
+ }
171
+ return data;
172
+ }
173
+ async function resolveRemoteDidcommEndpoint(deps, sub) {
174
+ if (sub.remoteDidcommEndpoint) {
175
+ return sub.remoteDidcommEndpoint;
176
+ }
177
+ if (sub.remoteWebvhDid) {
178
+ try {
179
+ const doc = await deps.identity.resolveDid("", sub.remoteWebvhDid);
180
+ return extractDidcommEndpoint(doc);
181
+ }
182
+ catch (_a) {
183
+ // fall through
184
+ }
185
+ }
186
+ const remoteDid = remotePeerDid(sub);
187
+ const doc = await deps.identity.resolveDid("", remoteDid);
188
+ return extractDidcommEndpoint(doc);
189
+ }
190
+ function extractDidcommEndpoint(doc) {
191
+ const services = doc.service;
192
+ if (!services || services.length === 0) {
193
+ throw new errors_1.IdentityError("DID document has no service endpoints", 404);
194
+ }
195
+ const svc = services.find((s) => s.type === "DIDCommMessaging" ||
196
+ s.type === "Messaging" ||
197
+ s.type === "did-communication");
198
+ const endpoint = svc === null || svc === void 0 ? void 0 : svc.serviceEndpoint;
199
+ if (typeof endpoint === "string") {
200
+ return endpoint;
201
+ }
202
+ if (endpoint &&
203
+ typeof endpoint === "object" &&
204
+ typeof endpoint.uri === "string") {
205
+ return endpoint.uri;
206
+ }
207
+ const fallback = services[0].serviceEndpoint;
208
+ if (typeof fallback === "string") {
209
+ return fallback;
210
+ }
211
+ throw new errors_1.IdentityError("Could not extract DIDComm service endpoint", 404);
212
+ }
213
+ function buildQueryResponseMessage(identity, fromPeerDid, toPeerDid, thid, result) {
214
+ return identity.generateMessage({
215
+ type: skyProtocol_1.SKY_QUERY_RESPONSE,
216
+ from: fromPeerDid,
217
+ to: [toPeerDid],
218
+ thid,
219
+ body: {
220
+ sky_version: "1.0",
221
+ status: "ok",
222
+ result,
223
+ },
224
+ });
225
+ }
226
+ function buildQueryErrorResponseMessage(identity, fromPeerDid, toPeerDid, thid, message, code = "query_failed") {
227
+ return identity.generateMessage({
228
+ type: skyProtocol_1.SKY_QUERY_RESPONSE,
229
+ from: fromPeerDid,
230
+ to: [toPeerDid],
231
+ thid,
232
+ body: {
233
+ sky_version: "1.0",
234
+ status: "error",
235
+ error: { code, message },
236
+ },
237
+ });
238
+ }
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
3
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
4
+ var m = o[Symbol.asyncIterator], i;
5
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
6
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
7
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
8
+ };
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.dbGet = dbGet;
11
+ exports.dbPut = dbPut;
12
+ exports.dbDel = dbDel;
13
+ exports.dbList = dbList;
14
+ async function dbGet(db, key) {
15
+ try {
16
+ const value = await db.get(key);
17
+ return value === undefined ? null : value;
18
+ }
19
+ catch (err) {
20
+ if (err && (err.notFound || err.code === "LEVEL_NOT_FOUND")) {
21
+ return null;
22
+ }
23
+ throw err;
24
+ }
25
+ }
26
+ async function dbPut(db, key, value) {
27
+ if (value === undefined || value === null) {
28
+ await db.del(key);
29
+ return;
30
+ }
31
+ await db.put(key, value);
32
+ }
33
+ async function dbDel(db, key) {
34
+ try {
35
+ await db.del(key);
36
+ }
37
+ catch (err) {
38
+ if (err && (err.notFound || err.code === "LEVEL_NOT_FOUND")) {
39
+ return;
40
+ }
41
+ throw err;
42
+ }
43
+ }
44
+ async function dbList(db, prefix) {
45
+ var _a, e_1, _b, _c;
46
+ const out = [];
47
+ const iter = db.iterator({
48
+ gte: prefix,
49
+ lte: prefix.concat([undefined]),
50
+ });
51
+ try {
52
+ for (var _d = true, iter_1 = __asyncValues(iter), iter_1_1; iter_1_1 = await iter_1.next(), _a = iter_1_1.done, !_a; _d = true) {
53
+ _c = iter_1_1.value;
54
+ _d = false;
55
+ const [key, value] = _c;
56
+ out.push({ key, value: value });
57
+ }
58
+ }
59
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
60
+ finally {
61
+ try {
62
+ if (!_d && !_a && (_b = iter_1.return)) await _b.call(iter_1);
63
+ }
64
+ finally { if (e_1) throw e_1.error; }
65
+ }
66
+ return out;
67
+ }
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.registerDidcommIngressRoutes = registerDidcommIngressRoutes;
4
+ const errors_1 = require("./errors");
5
+ function registerDidcommIngressRoutes(app, identity) {
6
+ app.post("/sky/event/:eci/none/dido/didcomm_message", async (req, res) => {
7
+ try {
8
+ const jwe = extractJweBody(req);
9
+ const result = await identity.handleDidcommIngress(req.params.eci, jwe);
10
+ res.status(200).json(result);
11
+ }
12
+ catch (err) {
13
+ sendIngressError(res, err);
14
+ }
15
+ });
16
+ }
17
+ function extractJweBody(req) {
18
+ const body = req.body;
19
+ if (typeof body === "string") {
20
+ return body;
21
+ }
22
+ if (body && typeof body === "object") {
23
+ const obj = body;
24
+ if (typeof obj.protected === "string" || typeof obj.ciphertext === "string") {
25
+ return JSON.stringify(body);
26
+ }
27
+ for (const key of ["jwe", "message", "payload"]) {
28
+ if (typeof obj[key] === "string") {
29
+ return obj[key];
30
+ }
31
+ }
32
+ if (typeof obj.body === "string") {
33
+ return obj.body;
34
+ }
35
+ }
36
+ throw new errors_1.IdentityError("Request body must be a DIDComm JWE", 400);
37
+ }
38
+ function sendIngressError(res, err) {
39
+ if (err instanceof errors_1.IdentityError) {
40
+ res.status(err.statusCode).json({ error: err.message });
41
+ return;
42
+ }
43
+ res.status(500).json({ error: err.message || "Internal error" });
44
+ }
@@ -0,0 +1,252 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = initDidoModule;
4
+ const krl_stdlib_1 = require("krl-stdlib");
5
+ const errors_1 = require("./errors");
6
+ const QUERY_TIMEOUT = 10000;
7
+ function initDidoModule(identity) {
8
+ const module = {
9
+ myDid: krl_stdlib_1.krl.Function([], async function () {
10
+ return identity.getMyDid(this.rsCtx.pico().id);
11
+ }),
12
+ publicIntro: krl_stdlib_1.krl.Function([], async function () {
13
+ return identity.getPublicIntro(this.rsCtx.pico().id);
14
+ }),
15
+ setPublicIntro: krl_stdlib_1.krl.Action(["enabled"], async function (enabled) {
16
+ return identity.setPublicIntro(this.rsCtx.pico().id, !!enabled);
17
+ }),
18
+ acceptsPublicIntro: krl_stdlib_1.krl.Function([], async function () {
19
+ return identity.acceptsPublicIntro(this.rsCtx.pico().id);
20
+ }),
21
+ addRoute: krl_stdlib_1.krl.Function(["type", "domain", "rule"], async function (type, domain, rule) {
22
+ return identity.addRoute(this.rsCtx.pico().id, type, domain, rule);
23
+ }),
24
+ generateMessage: krl_stdlib_1.krl.Function(["messageOptions"], async function (messageOptions) {
25
+ return identity.generateMessage(messageOptions);
26
+ }),
27
+ pack: krl_stdlib_1.krl.Function(["message", "from", "to"], async function (message, from, to) {
28
+ return identity.packMessage(this.rsCtx.pico().id, message, from, to);
29
+ }),
30
+ unpack: krl_stdlib_1.krl.Function(["message"], async function (message) {
31
+ const result = await identity.unpackMessage(this.rsCtx.pico().id, message);
32
+ return [result.message, result.metadata];
33
+ }),
34
+ route: krl_stdlib_1.krl.Function(["message"], async function (message) {
35
+ const picoId = this.rsCtx.pico().id;
36
+ const { message: unpacked, metadata: meta } = await identity.unpackMessage(picoId, message);
37
+ const introType = "https://picolabs.org/sky/1.0/intro";
38
+ if (unpacked.type === introType) {
39
+ const allowed = await identity.acceptsPublicIntro(picoId);
40
+ if (!allowed) {
41
+ this.log.info("Rejected unsolicited intro — publicIntro is false", {
42
+ picoId,
43
+ from: unpacked.from,
44
+ });
45
+ return undefined;
46
+ }
47
+ }
48
+ const routes = await identity.getRoutes(picoId);
49
+ const body = unpacked;
50
+ if (body.type === "https://picolabs.io/event/1.0/event") {
51
+ this.rsCtx.raiseEvent(body.body.domain, body.body.name, body.body.attrs);
52
+ return undefined;
53
+ }
54
+ if (body.type === "https://picolabs.io/query/1.0/query") {
55
+ try {
56
+ await this.useModule(body.body.rid);
57
+ return await this.krl.assertFunction(this.module(body.body.rid)[body.body.name])(this, body.body.args);
58
+ }
59
+ catch (error) {
60
+ return "Unable to query: " + error;
61
+ }
62
+ }
63
+ const route = routes[body.type];
64
+ if (route) {
65
+ this.rsCtx.raiseEvent(route.domain, route.name, {
66
+ message: unpacked,
67
+ metadata: meta,
68
+ });
69
+ }
70
+ else {
71
+ this.log.error("Unknown route for message", { message: unpacked });
72
+ }
73
+ return undefined;
74
+ }),
75
+ prepareQuery: deprecated("prepareQuery"),
76
+ sendQuery: krl_stdlib_1.krl.Function(["did", "message"], async function (did, message) {
77
+ const picoId = this.rsCtx.pico().id;
78
+ const from = message.from;
79
+ if (!from) {
80
+ throw new errors_1.IdentityError("sendQuery message missing from", 400);
81
+ }
82
+ const doc = await identity.resolveDid(picoId, did);
83
+ const endpoint = extractDidcommEndpoint(doc);
84
+ const packed = await identity.packMessage(picoId, message, from, did);
85
+ const response = await Promise.race([
86
+ this.krl.assertAction(this.module("http")["post"])(this, {
87
+ url: endpoint,
88
+ json: packed,
89
+ }),
90
+ new Promise((_resolve, reject) => setTimeout(() => reject(new Error("timeout")), QUERY_TIMEOUT)),
91
+ ]).catch(() => "Query timed out");
92
+ if (response &&
93
+ typeof response === "object" &&
94
+ response.status_code === 200) {
95
+ return JSON.parse(response.content).directives[0].options;
96
+ }
97
+ return response;
98
+ }),
99
+ send: krl_stdlib_1.krl.Function(["did", "message"], async function (did, message) {
100
+ var _a;
101
+ const picoId = this.rsCtx.pico().id;
102
+ const from = message.from;
103
+ const doc = await identity.resolveDid(picoId, did);
104
+ const endpoint = extractDidcommEndpoint(doc);
105
+ const packed = await identity.packMessage(picoId, message, from, did);
106
+ await this.krl.assertAction(this.module("http")["post"])(this, {
107
+ url: endpoint,
108
+ json: packed,
109
+ autosend: {
110
+ eci: (_a = this.rsCtx.pico().channels[0]) === null || _a === void 0 ? void 0 : _a.id,
111
+ domain: "dido",
112
+ type: "dido_send_response",
113
+ name: "dido_send_response",
114
+ },
115
+ });
116
+ }),
117
+ createInviteUrl: krl_stdlib_1.krl.Function(["base64"], function (base64) {
118
+ return identity.createInviteUrl(base64);
119
+ }),
120
+ establishSubscription: krl_stdlib_1.krl.Function(["bus"], async function (bus) {
121
+ return identity.establishSubscriptionFromBus(this.rsCtx.pico().id, bus || {});
122
+ }),
123
+ linkRemotePeer: krl_stdlib_1.krl.Function(["subscriptionId", "link"], async function (subscriptionId, link) {
124
+ return identity.linkRemotePeer(this.rsCtx.pico().id, subscriptionId, {
125
+ remotePeerDid: String(link.remotePeerDid || link.Tx_did || ""),
126
+ remoteWebvhDid: typeof link.remoteWebvhDid === "string"
127
+ ? link.remoteWebvhDid
128
+ : undefined,
129
+ remoteMeshRootId: typeof link.remoteMeshRootId === "string"
130
+ ? link.remoteMeshRootId
131
+ : undefined,
132
+ remoteTxHost: typeof link.remoteTxHost === "string" ? link.remoteTxHost : undefined,
133
+ remoteDidcommEndpoint: typeof link.remoteDidcommEndpoint === "string"
134
+ ? link.remoteDidcommEndpoint
135
+ : undefined,
136
+ });
137
+ }),
138
+ sendSkyIntro: krl_stdlib_1.krl.Function(["input"], async function (input) {
139
+ return identity.sendSkyIntro(this.rsCtx.pico().id, {
140
+ subscriptionId: String(input.subscriptionId || input.Id || ""),
141
+ targetDid: String(input.targetDid || input.target_did || input.target_webvh || ""),
142
+ name: String(input.name || "subscription"),
143
+ Tx_role: String(input.Tx_role || ""),
144
+ Rx_role: String(input.Rx_role || ""),
145
+ channel_type: typeof input.channel_type === "string" ? input.channel_type : undefined,
146
+ Tx_host: typeof input.Tx_host === "string" ? input.Tx_host : undefined,
147
+ });
148
+ }),
149
+ sendSkyIntroResponse: krl_stdlib_1.krl.Function(["subscriptionId", "toPeerDid", "response", "options"], async function (subscriptionId, toPeerDid, response, options = {}) {
150
+ const status = response.status === "rejected" ? "rejected" : "accepted";
151
+ return identity.sendSkyIntroResponse(this.rsCtx.pico().id, subscriptionId, toPeerDid, {
152
+ status,
153
+ subscription_id: typeof response.subscription_id === "string"
154
+ ? response.subscription_id
155
+ : subscriptionId,
156
+ peer_did_long: typeof response.peer_did_long === "string"
157
+ ? response.peer_did_long
158
+ : undefined,
159
+ Tx_host: typeof response.Tx_host === "string" ? response.Tx_host : undefined,
160
+ reason: typeof response.reason === "string" ? response.reason : undefined,
161
+ detail: typeof response.detail === "string" ? response.detail : undefined,
162
+ }, {
163
+ thid: typeof options.thid === "string" ? options.thid : undefined,
164
+ remoteDidcommEndpoint: typeof options.remoteDidcommEndpoint === "string"
165
+ ? options.remoteDidcommEndpoint
166
+ : undefined,
167
+ });
168
+ }),
169
+ crossPicoEvent: krl_stdlib_1.krl.Function(["subscriptionId", "event"], async function (subscriptionId, event) {
170
+ return identity.crossPicoEvent(this.rsCtx.pico().id, undefined, subscriptionId, {
171
+ domain: String(event.domain || ""),
172
+ name: String(event.name || event.type || ""),
173
+ attrs: event.attrs && typeof event.attrs === "object"
174
+ ? event.attrs
175
+ : {},
176
+ eid: typeof event.eid === "string" ? event.eid : undefined,
177
+ });
178
+ }),
179
+ crossPicoQuery: krl_stdlib_1.krl.Function(["subscriptionId", "query"], async function (subscriptionId, query) {
180
+ return identity.crossPicoQuery(this.rsCtx.pico().id, undefined, subscriptionId, {
181
+ rid: String(query.rid || ""),
182
+ name: String(query.name || ""),
183
+ args: query.args && typeof query.args === "object"
184
+ ? query.args
185
+ : {},
186
+ });
187
+ }),
188
+ teardownSubscription: krl_stdlib_1.krl.Function(["subscriptionId"], async function (subscriptionId) {
189
+ return identity.teardownSubscription(this.rsCtx.pico().id, subscriptionId);
190
+ }),
191
+ picoQuery: krl_stdlib_1.krl.Function(["did", "query"], async function (did, query) {
192
+ return identity.picoQuery(this.rsCtx.pico().id, did, {
193
+ rid: String(query.rid || ""),
194
+ name: String(query.name || ""),
195
+ args: query.args && typeof query.args === "object"
196
+ ? query.args
197
+ : {},
198
+ });
199
+ }),
200
+ picoEvent: krl_stdlib_1.krl.Action(["did", "event"], async function (did, event) {
201
+ await identity.picoEvent(this.rsCtx.pico().id, did, {
202
+ domain: String(event.domain || ""),
203
+ name: String(event.name || event.type || ""),
204
+ attrs: event.attrs && typeof event.attrs === "object"
205
+ ? event.attrs
206
+ : {},
207
+ eid: typeof event.eid === "string" ? event.eid : undefined,
208
+ });
209
+ }),
210
+ // Legacy did-o / did:peer:2 surface — removed in Layer 2 reboot.
211
+ generateDID: deprecated("generateDID"),
212
+ deleteDID: deprecated("deleteDID"),
213
+ updateDID: deprecated("updateDID"),
214
+ rotateDID: deprecated("rotateDID"),
215
+ rotateInviteDID: deprecated("rotateInviteDID"),
216
+ clearPendingRotations: deprecated("clearPendingRotations"),
217
+ mapDid: deprecated("mapDid"),
218
+ clearDidMap: deprecated("clearDidMap"),
219
+ clearDidDocs: deprecated("clearDidDocs"),
220
+ storeDidDoc: deprecated("storeDidDoc"),
221
+ addLabelsToChannel: deprecated("addLabelsToChannel"),
222
+ };
223
+ return module;
224
+ }
225
+ function deprecated(name) {
226
+ return krl_stdlib_1.krl.Function([], async function () {
227
+ throw new errors_1.IdentityError(`dido:${name} removed in Layer 2 reboot — use engine identity primitives`, 410);
228
+ });
229
+ }
230
+ function extractDidcommEndpoint(doc) {
231
+ const services = doc.service;
232
+ if (!services || services.length === 0) {
233
+ throw new errors_1.IdentityError("DID document has no service endpoints", 404);
234
+ }
235
+ const svc = services.find((s) => s.type === "DIDCommMessaging" ||
236
+ s.type === "Messaging" ||
237
+ s.type === "did-communication");
238
+ const endpoint = svc === null || svc === void 0 ? void 0 : svc.serviceEndpoint;
239
+ if (typeof endpoint === "string") {
240
+ return endpoint;
241
+ }
242
+ if (endpoint &&
243
+ typeof endpoint === "object" &&
244
+ typeof endpoint.uri === "string") {
245
+ return endpoint.uri;
246
+ }
247
+ const fallback = services[0].serviceEndpoint;
248
+ if (typeof fallback === "string") {
249
+ return fallback;
250
+ }
251
+ throw new errors_1.IdentityError("Could not extract DIDComm service endpoint", 404);
252
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.IdentityError = void 0;
4
+ class IdentityError extends Error {
5
+ constructor(message, statusCode = 400) {
6
+ super(message);
7
+ this.name = "IdentityError";
8
+ this.statusCode = statusCode;
9
+ }
10
+ }
11
+ exports.IdentityError = IdentityError;