pico-engine 1.6.2 → 1.8.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 +117 -14
- package/dist/RulesetRegistryLoaderFs.js +24 -8
- package/dist/auth/cookies.js +32 -0
- package/dist/auth/index.js +6 -1
- package/dist/bundledKrl.js +129 -11
- package/dist/channelIdentityPolicy.js +22 -0
- package/dist/cli.js +105 -46
- package/dist/engineConfig.js +280 -0
- package/dist/identity/IdentityService.js +90 -0
- package/dist/identity/crossPico.js +106 -30
- package/dist/identity/didoModule.js +80 -18
- package/dist/identity/establishSubscription.js +2 -2
- package/dist/identity/identityPortable.js +92 -0
- package/dist/identity/index.js +10 -1
- package/dist/identity/localPeerReconcile.js +65 -0
- package/dist/identity/peerDid4.js +175 -0
- package/dist/identity/peerDid4Create.js +90 -0
- package/dist/identity/portableExportPrepare.js +196 -0
- package/dist/identity/portableMove.js +87 -0
- package/dist/identity/skyIntro.js +10 -4
- package/dist/identity/skyProtocol.js +26 -1
- package/dist/identity/skyRotation.js +121 -0
- package/dist/identity/store.js +5 -1
- package/dist/identity/subscriptionEntSync.js +72 -0
- package/dist/identity/veramoDidComm.js +41 -2
- package/dist/identity/webvh.js +4 -2
- package/dist/identity/webvhRelocation.js +115 -0
- package/dist/index.js +127 -34
- package/dist/levelDb.js +102 -0
- package/dist/logging.js +47 -8
- package/dist/oauth/OAuthService.js +12 -5
- package/dist/oauth/channelEligibility.js +7 -1
- package/dist/oauth/index.js +3 -1
- package/dist/policyConfigFromJson.js +112 -0
- package/dist/runMcp.js +27 -0
- package/dist/server.js +95 -24
- package/dist/subtreeWrangler.js +180 -0
- package/dist/uiChannelPolicies.js +16 -0
- package/krl/io.picolabs.oauth.krl +51 -5
- package/krl/io.picolabs.pds.krl +131 -10
- package/krl/io.picolabs.pico-engine-ui.krl +219 -22
- package/krl/io.picolabs.portable_subtree.krl +97 -0
- package/krl/io.picolabs.subscription.krl +226 -72
- package/krl/io.picolabs.wrangler.krl +300 -58
- package/mcp/devResources.js +118 -0
- package/mcp/devTools.js +351 -0
- package/mcp/engineClient.js +133 -0
- package/mcp/meshHttp.js +137 -0
- package/mcp/meshTools.js +189 -0
- package/mcp/runStdio.js +34 -0
- package/mcp/schemaToZod.js +56 -0
- package/package.json +22 -8
- package/public/docs/_sidebar.md +2 -1
- package/public/docs/llms-full.txt +479 -0
- package/public/docs/llms.txt +37 -0
- package/public/docs/openapi.yaml +78 -0
- package/public/docs/pico-engine.md +235 -4
- package/public/docs/pico-overview.md +117 -0
- package/public/docs/pico.md +3 -1
- package/public/docs/rulesets/README.md +25 -0
- package/public/docs/rulesets/io.picolabs.oauth.md +71 -0
- package/public/docs/rulesets/io.picolabs.pds.md +327 -0
- package/public/docs/rulesets/io.picolabs.pico-engine-ui.md +65 -0
- package/public/docs/rulesets/io.picolabs.portable_subtree.md +94 -0
- package/public/docs/rulesets/io.picolabs.subscription.md +398 -0
- package/public/docs/rulesets/io.picolabs.wrangler.md +471 -0
- package/public/pico-engine-ui.css +1 -1
- package/public/pico-engine-ui.js +28 -26
- package/src/RulesetRegistryLoaderFs.ts +43 -11
- package/src/auth/cookies.ts +34 -0
- package/src/auth/index.ts +7 -0
- package/src/bundledKrl.ts +150 -12
- package/src/channelIdentityPolicy.ts +29 -0
- package/src/cli.ts +98 -40
- package/src/engineConfig.ts +345 -0
- package/src/identity/IdentityService.ts +189 -1
- package/src/identity/crossPico.ts +161 -41
- package/src/identity/didoModule.ts +140 -23
- package/src/identity/establishSubscription.ts +2 -2
- package/src/identity/identityPortable.ts +153 -0
- package/src/identity/index.ts +14 -0
- package/src/identity/localPeerReconcile.ts +97 -0
- package/src/identity/peerDid4.ts +197 -0
- package/src/identity/peerDid4Create.ts +121 -0
- package/src/identity/portableExportPrepare.ts +302 -0
- package/src/identity/portableMove.ts +193 -0
- package/src/identity/skyIntro.ts +16 -12
- package/src/identity/skyProtocol.ts +46 -0
- package/src/identity/skyRotation.ts +211 -0
- package/src/identity/store.ts +5 -1
- package/src/identity/subscriptionEntSync.ts +97 -0
- package/src/identity/veramoDidComm.ts +52 -2
- package/src/identity/webvh.ts +4 -4
- package/src/identity/webvhRelocation.ts +167 -0
- package/src/index.ts +179 -39
- package/src/levelDb.ts +122 -0
- package/src/logging.ts +63 -11
- package/src/oauth/OAuthService.ts +20 -6
- package/src/oauth/channelEligibility.ts +9 -0
- package/src/oauth/index.ts +7 -1
- package/src/policyConfigFromJson.ts +131 -0
- package/src/runMcp.ts +30 -0
- package/src/server.ts +133 -27
- package/src/subtreeWrangler.ts +278 -0
- package/src/uiChannelPolicies.ts +16 -0
- package/krl/io.picolabs.did-o.krl +0 -349
- package/krl/test/didtestdid.krl +0 -78
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Engine global KRL module `dido` — the ruleset-facing bridge to IdentityService.
|
|
4
|
+
*
|
|
5
|
+
* Wrangler and subscription call `dido:*` for Layer 2 identity: did:webvh lookup,
|
|
6
|
+
* SKY intro/response, cross-pico query/event, DIDComm pack/unpack/route, and
|
|
7
|
+
* portable-move helpers. State lives in the engine identity store, not in KRL ent.
|
|
8
|
+
*
|
|
9
|
+
* Not the retired `io.picolabs.did-o` ruleset (removed in 1.7). Do not rename this
|
|
10
|
+
* module; it is internal OS plumbing wired at engine start via core.modules["dido"].
|
|
11
|
+
*/
|
|
2
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
13
|
exports.default = initDidoModule;
|
|
4
14
|
const krl_stdlib_1 = require("krl-stdlib");
|
|
@@ -72,7 +82,6 @@ function initDidoModule(identity) {
|
|
|
72
82
|
}
|
|
73
83
|
return undefined;
|
|
74
84
|
}),
|
|
75
|
-
prepareQuery: deprecated("prepareQuery"),
|
|
76
85
|
sendQuery: krl_stdlib_1.krl.Function(["did", "message"], async function (did, message) {
|
|
77
86
|
const picoId = this.rsCtx.pico().id;
|
|
78
87
|
const from = message.from;
|
|
@@ -207,26 +216,79 @@ function initDidoModule(identity) {
|
|
|
207
216
|
eid: typeof event.eid === "string" ? event.eid : undefined,
|
|
208
217
|
});
|
|
209
218
|
}),
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
219
|
+
relocateWebvhAfterMove: krl_stdlib_1.krl.Function(["options"], async function (options = {}) {
|
|
220
|
+
const opts = {};
|
|
221
|
+
if (typeof options.baseUrl === "string") {
|
|
222
|
+
opts.baseUrl = options.baseUrl;
|
|
223
|
+
}
|
|
224
|
+
if (options.renewIngress === true) {
|
|
225
|
+
opts.renewIngress = true;
|
|
226
|
+
}
|
|
227
|
+
return identity.relocateWebvhAfterMove(this.rsCtx.pico().id, opts);
|
|
228
|
+
}),
|
|
229
|
+
notifyPeersOfWebvhRotation: krl_stdlib_1.krl.Function(["relocation"], async function (relocation) {
|
|
230
|
+
return identity.notifyPeersOfWebvhRotation(this.rsCtx.pico().id, relocation);
|
|
231
|
+
}),
|
|
232
|
+
completePortableMove: krl_stdlib_1.krl.Function(["options"], async function (options = {}) {
|
|
233
|
+
const opts = {};
|
|
234
|
+
if (typeof options.baseUrl === "string") {
|
|
235
|
+
opts.baseUrl = options.baseUrl;
|
|
236
|
+
}
|
|
237
|
+
if (options.renewIngress === true) {
|
|
238
|
+
opts.renewIngress = true;
|
|
239
|
+
}
|
|
240
|
+
return identity.completePortableMove(this.rsCtx.pico().id, opts);
|
|
241
|
+
}),
|
|
242
|
+
exportPortableSubtree: krl_stdlib_1.krl.Function(["subtreeRootPicoId", "secret"], async function (subtreeRootPicoId, secret) {
|
|
243
|
+
return identity.exportPortableSubtree(this.rsCtx.pico().id, subtreeRootPicoId, secret);
|
|
244
|
+
}),
|
|
245
|
+
summarizePortableSubtreeExport: krl_stdlib_1.krl.Function(["blob", "secret"], function (blob, secret) {
|
|
246
|
+
return identity.summarizePortableSubtreeExport(blob, secret);
|
|
247
|
+
}),
|
|
248
|
+
importPortableSubtree: krl_stdlib_1.krl.Function(["blob", "secret", "options"], async function (blob, secret, options = {}) {
|
|
249
|
+
const opts = {};
|
|
250
|
+
if (typeof options.baseUrl === "string") {
|
|
251
|
+
opts.baseUrl = options.baseUrl;
|
|
252
|
+
}
|
|
253
|
+
if (options.renewIngress === true) {
|
|
254
|
+
opts.renewIngress = true;
|
|
255
|
+
}
|
|
256
|
+
if (Object.keys(opts).length === 0) {
|
|
257
|
+
opts.renewIngress = true;
|
|
258
|
+
}
|
|
259
|
+
const importOpts = {};
|
|
260
|
+
if (typeof options.parentChannelEci === "string") {
|
|
261
|
+
importOpts.parentChannelEci = options.parentChannelEci;
|
|
262
|
+
}
|
|
263
|
+
if (typeof options.bootstrapDisplayName === "string") {
|
|
264
|
+
let name = options.bootstrapDisplayName.trim();
|
|
265
|
+
if (name.length >= 2 &&
|
|
266
|
+
name.startsWith('"') &&
|
|
267
|
+
name.endsWith('"')) {
|
|
268
|
+
name = name.slice(1, -1).trim();
|
|
269
|
+
}
|
|
270
|
+
if (name.length > 0) {
|
|
271
|
+
importOpts.bootstrapDisplayName = name;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return identity.importPortableSubtree(this.rsCtx.pico().id, blob, secret, opts, importOpts);
|
|
275
|
+
}),
|
|
276
|
+
finalizePortableImport: krl_stdlib_1.krl.Function(["importResult", "options"], async function (importResult, options = {}) {
|
|
277
|
+
const opts = {};
|
|
278
|
+
if (typeof options.baseUrl === "string") {
|
|
279
|
+
opts.baseUrl = options.baseUrl;
|
|
280
|
+
}
|
|
281
|
+
if (options.renewIngress === true) {
|
|
282
|
+
opts.renewIngress = true;
|
|
283
|
+
}
|
|
284
|
+
if (Object.keys(opts).length === 0) {
|
|
285
|
+
opts.renewIngress = true;
|
|
286
|
+
}
|
|
287
|
+
return identity.finalizePortableImport(importResult, opts);
|
|
288
|
+
}),
|
|
222
289
|
};
|
|
223
290
|
return module;
|
|
224
291
|
}
|
|
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
292
|
function extractDidcommEndpoint(doc) {
|
|
231
293
|
const services = doc.service;
|
|
232
294
|
if (!services || services.length === 0) {
|
|
@@ -66,12 +66,12 @@ async function establishSubscriptionIdentity(store, pf, picoId, input) {
|
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
68
|
async function linkRemotePeer(store, picoId, subscriptionId, input) {
|
|
69
|
-
var _a, _b, _c, _d;
|
|
69
|
+
var _a, _b, _c, _d, _e;
|
|
70
70
|
const sub = await store.getPeerSubscription(picoId, subscriptionId);
|
|
71
71
|
if (!sub) {
|
|
72
72
|
throw new errors_1.IdentityError(`Unknown subscription ${subscriptionId}`, 404);
|
|
73
73
|
}
|
|
74
|
-
const updated = Object.assign(Object.assign({}, sub), { remotePeerDid: input.remotePeerDid, remoteWebvhDid: (
|
|
74
|
+
const updated = Object.assign(Object.assign({}, sub), { remotePeerDid: (_a = input.remotePeerDid) !== null && _a !== void 0 ? _a : sub.remotePeerDid, remoteWebvhDid: (_b = input.remoteWebvhDid) !== null && _b !== void 0 ? _b : sub.remoteWebvhDid, remoteMeshRootId: (_c = input.remoteMeshRootId) !== null && _c !== void 0 ? _c : sub.remoteMeshRootId, remoteTxHost: (_d = input.remoteTxHost) !== null && _d !== void 0 ? _d : sub.remoteTxHost, remoteDidcommEndpoint: (_e = input.remoteDidcommEndpoint) !== null && _e !== void 0 ? _e : sub.remoteDidcommEndpoint });
|
|
75
75
|
await store.putPeerSubscription(picoId, updated);
|
|
76
76
|
return updated;
|
|
77
77
|
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Identity snapshots for portable subtree export/import (Phase D).
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.rewriteWebvhLogForPicoId = rewriteWebvhLogForPicoId;
|
|
7
|
+
exports.exportNodeIdentity = exportNodeIdentity;
|
|
8
|
+
exports.importNodeIdentity = importNodeIdentity;
|
|
9
|
+
exports.createSubtreePortableHooks = createSubtreePortableHooks;
|
|
10
|
+
const errors_1 = require("./errors");
|
|
11
|
+
const webvh_1 = require("./webvh");
|
|
12
|
+
const veramoDidComm_1 = require("./veramoDidComm");
|
|
13
|
+
function rewritePicoIdInValue(value, oldPicoId, newPicoId) {
|
|
14
|
+
if (typeof value === "string") {
|
|
15
|
+
return value
|
|
16
|
+
.split(oldPicoId)
|
|
17
|
+
.join(newPicoId);
|
|
18
|
+
}
|
|
19
|
+
if (Array.isArray(value)) {
|
|
20
|
+
return value.map((item) => rewritePicoIdInValue(item, oldPicoId, newPicoId));
|
|
21
|
+
}
|
|
22
|
+
if (value && typeof value === "object") {
|
|
23
|
+
const out = {};
|
|
24
|
+
for (const [key, child] of Object.entries(value)) {
|
|
25
|
+
out[key] = rewritePicoIdInValue(child, oldPicoId, newPicoId);
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
function rewriteWebvhLogForPicoId(log, oldPicoId, newPicoId) {
|
|
32
|
+
return rewritePicoIdInValue(log, oldPicoId, newPicoId);
|
|
33
|
+
}
|
|
34
|
+
async function exportNodeIdentity(store, picoId) {
|
|
35
|
+
const webvhDid = await store.getWebvhDid(picoId);
|
|
36
|
+
const webvhLog = await store.getWebvhLog(picoId);
|
|
37
|
+
const webvhKeys = await store.getWebvhKeys(picoId);
|
|
38
|
+
if (!webvhDid || !(webvhLog === null || webvhLog === void 0 ? void 0 : webvhLog.length) || !webvhKeys) {
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
webvhDid,
|
|
43
|
+
webvhLog,
|
|
44
|
+
webvhKeys,
|
|
45
|
+
webvhWebDoc: (await store.getWebvhWebDoc(picoId)) || undefined,
|
|
46
|
+
publicIntro: (await store.getPublicIntro(picoId)) === true,
|
|
47
|
+
peerSubscriptions: await store.listPeerSubscriptions(picoId),
|
|
48
|
+
routes: (await store.getRoutes(picoId)) || undefined,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
async function importNodeIdentity(store, pf, oldPicoId, newPicoId, raw, eciMap, picoIdMap) {
|
|
52
|
+
if (!raw || typeof raw !== "object") {
|
|
53
|
+
throw new errors_1.IdentityError("Invalid portable identity export", 400);
|
|
54
|
+
}
|
|
55
|
+
const identity = raw;
|
|
56
|
+
if (typeof identity.webvhDid !== "string" ||
|
|
57
|
+
!Array.isArray(identity.webvhLog) ||
|
|
58
|
+
!identity.webvhKeys) {
|
|
59
|
+
throw new errors_1.IdentityError("Portable identity export missing webvh fields", 400);
|
|
60
|
+
}
|
|
61
|
+
const meshRootId = (0, webvh_1.meshRootIdFor)(pf, newPicoId);
|
|
62
|
+
await store.putWebvhKeys(newPicoId, identity.webvhKeys);
|
|
63
|
+
await store.putWebvhLog(newPicoId, identity.webvhLog);
|
|
64
|
+
await store.putWebvhDid(newPicoId, identity.webvhDid);
|
|
65
|
+
await store.putMeshRootId(newPicoId, meshRootId);
|
|
66
|
+
await store.putPublicIntro(newPicoId, identity.publicIntro === true);
|
|
67
|
+
await store.putDidcommIngressEci(newPicoId, "");
|
|
68
|
+
if (identity.webvhWebDoc) {
|
|
69
|
+
await store.putWebvhWebDoc(newPicoId, identity.webvhWebDoc);
|
|
70
|
+
}
|
|
71
|
+
if (identity.routes) {
|
|
72
|
+
await store.putRoutes(newPicoId, identity.routes);
|
|
73
|
+
}
|
|
74
|
+
for (const sub of identity.peerSubscriptions || []) {
|
|
75
|
+
const rxEci = sub.rxEci ? eciMap[sub.rxEci] || sub.rxEci : sub.rxEci;
|
|
76
|
+
const remoteMeshRootId = sub.remoteMeshRootId && picoIdMap[sub.remoteMeshRootId]
|
|
77
|
+
? picoIdMap[sub.remoteMeshRootId]
|
|
78
|
+
: sub.remoteMeshRootId;
|
|
79
|
+
await store.putPeerSubscription(newPicoId, Object.assign(Object.assign({}, sub), { rxEci,
|
|
80
|
+
remoteMeshRootId }));
|
|
81
|
+
}
|
|
82
|
+
await (0, veramoDidComm_1.warmPeerDidsForPico)(store, newPicoId);
|
|
83
|
+
if ((0, webvh_1.webvhPathsForPico)(newPicoId)[1] !== newPicoId) {
|
|
84
|
+
throw new errors_1.IdentityError("Webvh path rewrite failed after import", 500);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function createSubtreePortableHooks(store, pf) {
|
|
88
|
+
return {
|
|
89
|
+
exportNodeIdentity: (picoId) => exportNodeIdentity(store, picoId),
|
|
90
|
+
importNodeIdentity: (oldPicoId, newPicoId, identity, eciMap, picoIdMap) => importNodeIdentity(store, pf, oldPicoId, newPicoId, identity, eciMap, picoIdMap),
|
|
91
|
+
};
|
|
92
|
+
}
|
package/dist/identity/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
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;
|
|
3
|
+
exports.IDENTITY_STORAGE_KNOWN_GAP = exports.finalizePortableImport = exports.importPortableSubtree = exports.exportPortableSubtree = exports.rewriteWebvhLogForPicoId = exports.importNodeIdentity = exports.exportNodeIdentity = exports.createSubtreePortableHooks = exports.initDidoModule = exports.IdentityError = exports.IdentityService = exports.migrateFromDidO = exports.IDENTITY_PREFIX = exports.IdentityStore = void 0;
|
|
4
4
|
var store_1 = require("./store");
|
|
5
5
|
Object.defineProperty(exports, "IdentityStore", { enumerable: true, get: function () { return store_1.IdentityStore; } });
|
|
6
6
|
Object.defineProperty(exports, "IDENTITY_PREFIX", { enumerable: true, get: function () { return store_1.IDENTITY_PREFIX; } });
|
|
@@ -11,5 +11,14 @@ var errors_1 = require("./errors");
|
|
|
11
11
|
Object.defineProperty(exports, "IdentityError", { enumerable: true, get: function () { return errors_1.IdentityError; } });
|
|
12
12
|
var didoModule_1 = require("./didoModule");
|
|
13
13
|
Object.defineProperty(exports, "initDidoModule", { enumerable: true, get: function () { return didoModule_1.default; } });
|
|
14
|
+
var identityPortable_1 = require("./identityPortable");
|
|
15
|
+
Object.defineProperty(exports, "createSubtreePortableHooks", { enumerable: true, get: function () { return identityPortable_1.createSubtreePortableHooks; } });
|
|
16
|
+
Object.defineProperty(exports, "exportNodeIdentity", { enumerable: true, get: function () { return identityPortable_1.exportNodeIdentity; } });
|
|
17
|
+
Object.defineProperty(exports, "importNodeIdentity", { enumerable: true, get: function () { return identityPortable_1.importNodeIdentity; } });
|
|
18
|
+
Object.defineProperty(exports, "rewriteWebvhLogForPicoId", { enumerable: true, get: function () { return identityPortable_1.rewriteWebvhLogForPicoId; } });
|
|
19
|
+
var portableMove_1 = require("./portableMove");
|
|
20
|
+
Object.defineProperty(exports, "exportPortableSubtree", { enumerable: true, get: function () { return portableMove_1.exportPortableSubtree; } });
|
|
21
|
+
Object.defineProperty(exports, "importPortableSubtree", { enumerable: true, get: function () { return portableMove_1.importPortableSubtree; } });
|
|
22
|
+
Object.defineProperty(exports, "finalizePortableImport", { enumerable: true, get: function () { return portableMove_1.finalizePortableImport; } });
|
|
14
23
|
var types_1 = require("./types");
|
|
15
24
|
Object.defineProperty(exports, "IDENTITY_STORAGE_KNOWN_GAP", { enumerable: true, get: function () { return types_1.IDENTITY_STORAGE_KNOWN_GAP; } });
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* After a portable import, update relationship buses on other picos on this engine
|
|
4
|
+
* (e.g. ride coordinator) so they still point at the moved root by DID / remapped ECI.
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.patchEstablishedBusesForMovedRoot = patchEstablishedBusesForMovedRoot;
|
|
8
|
+
exports.refreshLocalPeersOfMovedRoot = refreshLocalPeersOfMovedRoot;
|
|
9
|
+
const SUBSCRIPTION_RID = "io.picolabs.subscription";
|
|
10
|
+
/** Pure patch logic for unit tests. */
|
|
11
|
+
function patchEstablishedBusesForMovedRoot(established, result, relocation, baseUrl) {
|
|
12
|
+
const host = baseUrl.replace(/\/$/, "");
|
|
13
|
+
const oldRoot = result.oldRootPicoId;
|
|
14
|
+
const newRoot = result.newRootPicoId;
|
|
15
|
+
let changed = false;
|
|
16
|
+
const next = established.map((raw) => {
|
|
17
|
+
if (!raw || typeof raw !== "object") {
|
|
18
|
+
return raw;
|
|
19
|
+
}
|
|
20
|
+
const bus = Object.assign({}, raw);
|
|
21
|
+
const tx = bus["Tx"];
|
|
22
|
+
const txRemapped = typeof tx === "string" && result.eciMap[tx] ? result.eciMap[tx] : undefined;
|
|
23
|
+
const matches = bus["picoID"] === oldRoot ||
|
|
24
|
+
bus["Tx_did"] === relocation.priorDid ||
|
|
25
|
+
txRemapped !== undefined ||
|
|
26
|
+
(typeof tx === "string" && result.eciMap[tx] !== undefined);
|
|
27
|
+
if (!matches) {
|
|
28
|
+
return raw;
|
|
29
|
+
}
|
|
30
|
+
changed = true;
|
|
31
|
+
if (bus["picoID"] === oldRoot) {
|
|
32
|
+
bus["picoID"] = newRoot;
|
|
33
|
+
}
|
|
34
|
+
if (bus["Tx_did"] === relocation.priorDid) {
|
|
35
|
+
bus["Tx_did"] = relocation.newDid;
|
|
36
|
+
}
|
|
37
|
+
if (txRemapped) {
|
|
38
|
+
bus["Tx"] = txRemapped;
|
|
39
|
+
}
|
|
40
|
+
if (bus["layer2"] === true) {
|
|
41
|
+
bus["Tx_host"] = host;
|
|
42
|
+
}
|
|
43
|
+
return bus;
|
|
44
|
+
});
|
|
45
|
+
return { next, changed };
|
|
46
|
+
}
|
|
47
|
+
/** Refresh ent:established on every loaded pico that references the moved root. */
|
|
48
|
+
async function refreshLocalPeersOfMovedRoot(pf, result, relocation, baseUrl) {
|
|
49
|
+
let picosUpdated = 0;
|
|
50
|
+
for (const pico of pf.loadedPicos()) {
|
|
51
|
+
if (!pico.rulesets[SUBSCRIPTION_RID]) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const establishedRaw = await pico.getEnt(SUBSCRIPTION_RID, "established");
|
|
55
|
+
if (!Array.isArray(establishedRaw) || establishedRaw.length === 0) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const { next, changed } = patchEstablishedBusesForMovedRoot(establishedRaw, result, relocation, baseUrl);
|
|
59
|
+
if (changed) {
|
|
60
|
+
await pico.putEnt(SUBSCRIPTION_RID, "established", next);
|
|
61
|
+
picosUpdated += 1;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return picosUpdated;
|
|
65
|
+
}
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* did:peer numalgo 4 — TypeScript port of DIF did-peer-4 (Python reference).
|
|
4
|
+
* https://github.com/decentralized-identity/did-peer-4
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.encodePeerDid4 = encodePeerDid4;
|
|
8
|
+
exports.encodePeerDid4Short = encodePeerDid4Short;
|
|
9
|
+
exports.decodePeerDid4 = decodePeerDid4;
|
|
10
|
+
exports.longToShortPeerDid4 = longToShortPeerDid4;
|
|
11
|
+
exports.isPeerDid4 = isPeerDid4;
|
|
12
|
+
exports.resolvePeerDid4 = resolvePeerDid4;
|
|
13
|
+
exports.resolvePeerDid4Short = resolvePeerDid4Short;
|
|
14
|
+
exports.peerDid4Matches = peerDid4Matches;
|
|
15
|
+
const crypto_1 = require("crypto");
|
|
16
|
+
const bs58 = require("bs58");
|
|
17
|
+
const MULTICODEC_JSON = Buffer.from([0x80, 0x04]);
|
|
18
|
+
const MULTICODEC_SHA2_256 = Buffer.from([0x12, 0x20]);
|
|
19
|
+
const MULTIBASE_BASE58_BTC = "z";
|
|
20
|
+
const BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
21
|
+
const LONG_PATTERN = new RegExp(`^did:peer:4zQm[${BASE58_ALPHABET}]{44}:z[${BASE58_ALPHABET}]{6,}$`);
|
|
22
|
+
const SHORT_PATTERN = new RegExp(`^did:peer:4zQm[${BASE58_ALPHABET}]{44}$`);
|
|
23
|
+
function encodeDoc(document) {
|
|
24
|
+
const payload = MULTICODEC_JSON.toString("binary") + JSON.stringify(document);
|
|
25
|
+
return MULTIBASE_BASE58_BTC + bs58.encode(Buffer.from(payload, "binary"));
|
|
26
|
+
}
|
|
27
|
+
function decodeDoc(encodedDoc) {
|
|
28
|
+
const encoding = encodedDoc[0];
|
|
29
|
+
const encoded = encodedDoc.slice(1);
|
|
30
|
+
if (encoding !== MULTIBASE_BASE58_BTC) {
|
|
31
|
+
throw new Error(`Unsupported encoding: ${encoding}`);
|
|
32
|
+
}
|
|
33
|
+
const decodedBytes = bs58.decode(encoded);
|
|
34
|
+
if (decodedBytes.length < 2 ||
|
|
35
|
+
decodedBytes[0] !== MULTICODEC_JSON[0] ||
|
|
36
|
+
decodedBytes[1] !== MULTICODEC_JSON[1]) {
|
|
37
|
+
throw new Error("Unsupported multicodec for did:peer:4 document");
|
|
38
|
+
}
|
|
39
|
+
const json = Buffer.from(decodedBytes.slice(2)).toString("utf8");
|
|
40
|
+
return JSON.parse(json);
|
|
41
|
+
}
|
|
42
|
+
function hashEncodedDoc(encodedDoc) {
|
|
43
|
+
const digest = (0, crypto_1.createHash)("sha256").update(encodedDoc).digest();
|
|
44
|
+
const multihash = Buffer.concat([MULTICODEC_SHA2_256, digest]);
|
|
45
|
+
return MULTIBASE_BASE58_BTC + bs58.encode(multihash);
|
|
46
|
+
}
|
|
47
|
+
/** Encode input document → long-form did:peer:4. */
|
|
48
|
+
function encodePeerDid4(document) {
|
|
49
|
+
const encodedDoc = encodeDoc(document);
|
|
50
|
+
const hashed = hashEncodedDoc(encodedDoc);
|
|
51
|
+
return `did:peer:4${hashed}:${encodedDoc}`;
|
|
52
|
+
}
|
|
53
|
+
/** Encode input document → short-form did:peer:4. */
|
|
54
|
+
function encodePeerDid4Short(document) {
|
|
55
|
+
const encodedDoc = encodeDoc(document);
|
|
56
|
+
const hashed = hashEncodedDoc(encodedDoc);
|
|
57
|
+
return `did:peer:4${hashed}`;
|
|
58
|
+
}
|
|
59
|
+
/** Decode long-form did:peer:4 → input document (not contextualized). */
|
|
60
|
+
function decodePeerDid4(did) {
|
|
61
|
+
if (!did.startsWith("did:peer:4")) {
|
|
62
|
+
throw new Error(`Invalid did:peer:4: ${did}`);
|
|
63
|
+
}
|
|
64
|
+
if (SHORT_PATTERN.test(did)) {
|
|
65
|
+
throw new Error("Cannot decode document from short form did:peer:4");
|
|
66
|
+
}
|
|
67
|
+
if (!LONG_PATTERN.test(did)) {
|
|
68
|
+
throw new Error(`Invalid did:peer:4: ${did}`);
|
|
69
|
+
}
|
|
70
|
+
const body = did.slice("did:peer:4".length);
|
|
71
|
+
const colon = body.indexOf(":");
|
|
72
|
+
if (colon < 0) {
|
|
73
|
+
throw new Error(`Invalid did:peer:4: ${did}`);
|
|
74
|
+
}
|
|
75
|
+
const hashed = body.slice(0, colon);
|
|
76
|
+
const encodedDoc = body.slice(colon + 1);
|
|
77
|
+
if (hashEncodedDoc(encodedDoc) !== hashed) {
|
|
78
|
+
throw new Error(`Hash is invalid for did: ${did}`);
|
|
79
|
+
}
|
|
80
|
+
return decodeDoc(encodedDoc);
|
|
81
|
+
}
|
|
82
|
+
function longToShortPeerDid4(did) {
|
|
83
|
+
if (!LONG_PATTERN.test(did)) {
|
|
84
|
+
throw new Error(`DID is not a long form did:peer:4: ${did}`);
|
|
85
|
+
}
|
|
86
|
+
const idx = did.lastIndexOf(":");
|
|
87
|
+
return did.slice(0, idx);
|
|
88
|
+
}
|
|
89
|
+
function isPeerDid4(did) {
|
|
90
|
+
return did.startsWith("did:peer:4");
|
|
91
|
+
}
|
|
92
|
+
function visitVerificationMethods(document, visitor) {
|
|
93
|
+
const out = Object.assign({}, document);
|
|
94
|
+
const vms = out.verificationMethod;
|
|
95
|
+
if (Array.isArray(vms)) {
|
|
96
|
+
out.verificationMethod = vms.map((vm) => typeof vm === "object" && vm !== null
|
|
97
|
+
? visitor(vm)
|
|
98
|
+
: vm);
|
|
99
|
+
}
|
|
100
|
+
for (const rel of [
|
|
101
|
+
"authentication",
|
|
102
|
+
"assertionMethod",
|
|
103
|
+
"keyAgreement",
|
|
104
|
+
"capabilityInvocation",
|
|
105
|
+
"capabilityDelegation",
|
|
106
|
+
]) {
|
|
107
|
+
const entries = out[rel];
|
|
108
|
+
if (Array.isArray(entries)) {
|
|
109
|
+
out[rel] = entries.map((entry) => {
|
|
110
|
+
if (typeof entry === "object" && entry !== null) {
|
|
111
|
+
return visitor(entry);
|
|
112
|
+
}
|
|
113
|
+
return entry;
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
function contextualizeDocument(did, document) {
|
|
120
|
+
const out = Object.assign(Object.assign({}, document), { id: did });
|
|
121
|
+
return visitVerificationMethods(out, (vm) => {
|
|
122
|
+
if (!vm.controller) {
|
|
123
|
+
return Object.assign(Object.assign({}, vm), { controller: did });
|
|
124
|
+
}
|
|
125
|
+
return vm;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/** Resolve long-form did:peer:4 → contextualized DID document. */
|
|
129
|
+
function resolvePeerDid4(did) {
|
|
130
|
+
const decoded = decodePeerDid4(did);
|
|
131
|
+
const document = contextualizeDocument(did, decoded);
|
|
132
|
+
const aka = Array.isArray(document.alsoKnownAs)
|
|
133
|
+
? [...document.alsoKnownAs]
|
|
134
|
+
: [];
|
|
135
|
+
aka.push(longToShortPeerDid4(did));
|
|
136
|
+
document.alsoKnownAs = aka;
|
|
137
|
+
return document;
|
|
138
|
+
}
|
|
139
|
+
/** Resolve short-form variant from long-form did:peer:4. */
|
|
140
|
+
function resolvePeerDid4Short(did) {
|
|
141
|
+
const decoded = decodePeerDid4(did);
|
|
142
|
+
const shortDid = longToShortPeerDid4(did);
|
|
143
|
+
const document = contextualizeDocument(shortDid, decoded);
|
|
144
|
+
const aka = Array.isArray(document.alsoKnownAs)
|
|
145
|
+
? [...document.alsoKnownAs]
|
|
146
|
+
: [];
|
|
147
|
+
aka.push(did);
|
|
148
|
+
document.alsoKnownAs = aka;
|
|
149
|
+
return document;
|
|
150
|
+
}
|
|
151
|
+
/** Match short/long peer:4 forms. */
|
|
152
|
+
function peerDid4Matches(stored, incoming) {
|
|
153
|
+
if (stored === incoming) {
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
if (!isPeerDid4(stored) || !isPeerDid4(incoming)) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
if (LONG_PATTERN.test(stored)) {
|
|
161
|
+
if (stored === incoming || longToShortPeerDid4(stored) === incoming) {
|
|
162
|
+
return true;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (LONG_PATTERN.test(incoming)) {
|
|
166
|
+
if (stored === longToShortPeerDid4(incoming)) {
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
catch (_a) {
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.inputDocFromKeys = inputDocFromKeys;
|
|
4
|
+
exports.createPeerDid4WithKeys = createPeerDid4WithKeys;
|
|
5
|
+
const ed25519_js_1 = require("@noble/curves/ed25519.js");
|
|
6
|
+
const utils_1 = require("@veramo/utils");
|
|
7
|
+
const bs58 = require("bs58");
|
|
8
|
+
const peerDid4_1 = require("./peerDid4");
|
|
9
|
+
function multibasePublicKey(prefix, second, raw) {
|
|
10
|
+
return "z" + bs58.encode(Buffer.concat([Buffer.from([prefix, second]), Buffer.from(raw)]));
|
|
11
|
+
}
|
|
12
|
+
function inputDocFromKeys(keys, services) {
|
|
13
|
+
const inputDoc = {
|
|
14
|
+
"@context": ["https://www.w3.org/ns/did/v1"],
|
|
15
|
+
};
|
|
16
|
+
keys.forEach((key, index) => {
|
|
17
|
+
const ident = key.ident || `#key-${index}`;
|
|
18
|
+
const vm = {
|
|
19
|
+
id: ident,
|
|
20
|
+
type: "Multikey",
|
|
21
|
+
publicKeyMultibase: key.multikey,
|
|
22
|
+
};
|
|
23
|
+
const vms = inputDoc.verificationMethod || [];
|
|
24
|
+
vms.push(vm);
|
|
25
|
+
inputDoc.verificationMethod = vms;
|
|
26
|
+
const ctx = inputDoc["@context"];
|
|
27
|
+
if (!ctx.includes("https://w3id.org/security/multikey/v1")) {
|
|
28
|
+
ctx.push("https://w3id.org/security/multikey/v1");
|
|
29
|
+
}
|
|
30
|
+
for (const rel of key.relationships) {
|
|
31
|
+
const list = inputDoc[rel] || [];
|
|
32
|
+
list.push(ident);
|
|
33
|
+
inputDoc[rel] = list;
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
if (services === null || services === void 0 ? void 0 : services.length) {
|
|
37
|
+
inputDoc.service = services;
|
|
38
|
+
}
|
|
39
|
+
return inputDoc;
|
|
40
|
+
}
|
|
41
|
+
/** Create did:peer:4 (long form) with Ed25519 + X25519 keys for DIDComm. */
|
|
42
|
+
function createPeerDid4WithKeys() {
|
|
43
|
+
const edSecret = ed25519_js_1.ed25519.utils.randomSecretKey();
|
|
44
|
+
const edPublic = ed25519_js_1.ed25519.getPublicKey(edSecret);
|
|
45
|
+
const xSecret = ed25519_js_1.x25519.utils.randomSecretKey();
|
|
46
|
+
const xPublic = ed25519_js_1.x25519.getPublicKey(xSecret);
|
|
47
|
+
const edMulti = multibasePublicKey(0xed, 0x01, edPublic);
|
|
48
|
+
const xMulti = multibasePublicKey(0xec, 0x01, xPublic);
|
|
49
|
+
const inputDoc = inputDocFromKeys([
|
|
50
|
+
{
|
|
51
|
+
multikey: edMulti,
|
|
52
|
+
relationships: ["authentication", "capabilityDelegation"],
|
|
53
|
+
ident: "#key-0",
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
multikey: xMulti,
|
|
57
|
+
relationships: ["keyAgreement"],
|
|
58
|
+
ident: "#key-1",
|
|
59
|
+
},
|
|
60
|
+
]);
|
|
61
|
+
const did = (0, peerDid4_1.encodePeerDid4)(inputDoc);
|
|
62
|
+
const shortDid = (0, peerDid4_1.longToShortPeerDid4)(did);
|
|
63
|
+
const didDocument = (0, peerDid4_1.resolvePeerDid4)(did);
|
|
64
|
+
const edPublicHex = (0, utils_1.bytesToHex)(edPublic);
|
|
65
|
+
const xPublicHex = (0, utils_1.bytesToHex)(xPublic);
|
|
66
|
+
const veramoKeys = [
|
|
67
|
+
{
|
|
68
|
+
type: "Ed25519",
|
|
69
|
+
kid: edPublicHex,
|
|
70
|
+
privateKeyHex: (0, utils_1.bytesToHex)(edSecret),
|
|
71
|
+
publicKeyHex: edPublicHex,
|
|
72
|
+
meta: { algorithms: ["EdDSA", "Ed25519"] },
|
|
73
|
+
kms: "local",
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
type: "X25519",
|
|
77
|
+
kid: xPublicHex,
|
|
78
|
+
privateKeyHex: (0, utils_1.bytesToHex)(xSecret),
|
|
79
|
+
publicKeyHex: xPublicHex,
|
|
80
|
+
meta: { algorithms: ["ECDH", "ECDH-ES", "ECDH-1PU"] },
|
|
81
|
+
kms: "local",
|
|
82
|
+
},
|
|
83
|
+
];
|
|
84
|
+
return {
|
|
85
|
+
did,
|
|
86
|
+
shortDid,
|
|
87
|
+
didDocument,
|
|
88
|
+
keys: { veramoKeys },
|
|
89
|
+
};
|
|
90
|
+
}
|