pico-engine 1.6.4 → 1.9.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 +16 -9
- package/dist/RulesetRegistryLoaderFs.js +60 -6
- package/dist/auth/cookies.js +32 -0
- package/dist/auth/index.js +6 -1
- package/dist/channelIdentityPolicy.js +22 -0
- package/dist/cli.js +106 -69
- package/dist/engineConfig.js +291 -0
- package/dist/identity/IdentityService.js +90 -0
- package/dist/identity/crossPico.js +24 -8
- 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 +6 -3
- 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 +84 -27
- package/dist/logging.js +156 -16
- package/dist/oauth/OAuthService.js +6 -2
- package/dist/policyConfigFromJson.js +112 -0
- package/dist/runMcp.js +27 -0
- package/dist/server.js +261 -24
- package/dist/stageKrl.js +26 -0
- package/dist/subtreeWrangler.js +180 -0
- package/dist/uiChannelPolicies.js +15 -0
- package/dist/xmluiRuntime.js +68 -0
- package/krl/io.picolabs.oauth.krl +41 -2
- package/krl/io.picolabs.pds.krl +131 -10
- package/krl/io.picolabs.pico-engine-ui.krl +217 -94
- package/krl/io.picolabs.portable_subtree.krl +97 -0
- package/krl/io.picolabs.subscription.krl +180 -72
- package/krl/io.picolabs.wrangler.krl +313 -57
- package/krl/io.picolabs.xmlui.krl +295 -0
- 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 +24 -9
- package/public/.DS_Store +0 -0
- package/public/docs/_sidebar.md +3 -1
- package/public/docs/llms-full.txt +630 -0
- package/public/docs/llms.txt +38 -0
- package/public/docs/openapi.yaml +78 -0
- package/public/docs/pico-engine.md +239 -4
- package/public/docs/pico-overview.md +117 -0
- package/public/docs/pico-views-xmlui.md +5 -0
- package/public/docs/pico-views.md +140 -0
- package/public/docs/pico.md +3 -1
- package/public/docs/rulesets/README.md +26 -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 +27 -23
- package/public/xmlui/0.12.7.js +419 -0
- package/public/xmlui/pico-view-boot.js +153 -0
- package/src/RulesetRegistryLoaderFs.ts +66 -7
- package/src/auth/cookies.ts +34 -0
- package/src/auth/index.ts +7 -0
- package/src/channelIdentityPolicy.ts +29 -0
- package/src/cli.ts +70 -37
- package/src/engineConfig.ts +363 -0
- package/src/identity/IdentityService.ts +189 -1
- package/src/identity/crossPico.ts +57 -8
- 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 +11 -11
- 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 +123 -31
- package/src/logging.ts +173 -18
- package/src/oauth/OAuthService.ts +11 -3
- package/src/policyConfigFromJson.ts +131 -0
- package/src/runMcp.ts +30 -0
- package/src/server.ts +328 -27
- package/src/stageKrl.ts +26 -0
- package/src/subtreeWrangler.ts +278 -0
- package/src/uiChannelPolicies.ts +15 -0
- package/src/xmluiRuntime.ts +66 -0
- package/krl/io.picolabs.did-o.krl +0 -349
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* did:peer numalgo 4 — TypeScript port of DIF did-peer-4 (Python reference).
|
|
3
|
+
* https://github.com/decentralized-identity/did-peer-4
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { createHash } from "crypto";
|
|
7
|
+
import * as bs58 from "bs58";
|
|
8
|
+
|
|
9
|
+
const MULTICODEC_JSON = Buffer.from([0x80, 0x04]);
|
|
10
|
+
const MULTICODEC_SHA2_256 = Buffer.from([0x12, 0x20]);
|
|
11
|
+
const MULTIBASE_BASE58_BTC = "z";
|
|
12
|
+
|
|
13
|
+
const BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
14
|
+
const LONG_PATTERN = new RegExp(
|
|
15
|
+
`^did:peer:4zQm[${BASE58_ALPHABET}]{44}:z[${BASE58_ALPHABET}]{6,}$`
|
|
16
|
+
);
|
|
17
|
+
const SHORT_PATTERN = new RegExp(`^did:peer:4zQm[${BASE58_ALPHABET}]{44}$`);
|
|
18
|
+
|
|
19
|
+
export type PeerDid4InputDocument = Record<string, unknown>;
|
|
20
|
+
|
|
21
|
+
function encodeDoc(document: PeerDid4InputDocument): string {
|
|
22
|
+
const payload = MULTICODEC_JSON.toString("binary") + JSON.stringify(document);
|
|
23
|
+
return MULTIBASE_BASE58_BTC + bs58.encode(Buffer.from(payload, "binary"));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function decodeDoc(encodedDoc: string): PeerDid4InputDocument {
|
|
27
|
+
const encoding = encodedDoc[0];
|
|
28
|
+
const encoded = encodedDoc.slice(1);
|
|
29
|
+
if (encoding !== MULTIBASE_BASE58_BTC) {
|
|
30
|
+
throw new Error(`Unsupported encoding: ${encoding}`);
|
|
31
|
+
}
|
|
32
|
+
const decodedBytes = bs58.decode(encoded);
|
|
33
|
+
if (
|
|
34
|
+
decodedBytes.length < 2 ||
|
|
35
|
+
decodedBytes[0] !== MULTICODEC_JSON[0] ||
|
|
36
|
+
decodedBytes[1] !== MULTICODEC_JSON[1]
|
|
37
|
+
) {
|
|
38
|
+
throw new Error("Unsupported multicodec for did:peer:4 document");
|
|
39
|
+
}
|
|
40
|
+
const json = Buffer.from(decodedBytes.slice(2)).toString("utf8");
|
|
41
|
+
return JSON.parse(json) as PeerDid4InputDocument;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function hashEncodedDoc(encodedDoc: string): string {
|
|
45
|
+
const digest = createHash("sha256").update(encodedDoc).digest();
|
|
46
|
+
const multihash = Buffer.concat([MULTICODEC_SHA2_256, digest]);
|
|
47
|
+
return MULTIBASE_BASE58_BTC + bs58.encode(multihash);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Encode input document → long-form did:peer:4. */
|
|
51
|
+
export function encodePeerDid4(document: PeerDid4InputDocument): string {
|
|
52
|
+
const encodedDoc = encodeDoc(document);
|
|
53
|
+
const hashed = hashEncodedDoc(encodedDoc);
|
|
54
|
+
return `did:peer:4${hashed}:${encodedDoc}`;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Encode input document → short-form did:peer:4. */
|
|
58
|
+
export function encodePeerDid4Short(document: PeerDid4InputDocument): string {
|
|
59
|
+
const encodedDoc = encodeDoc(document);
|
|
60
|
+
const hashed = hashEncodedDoc(encodedDoc);
|
|
61
|
+
return `did:peer:4${hashed}`;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** Decode long-form did:peer:4 → input document (not contextualized). */
|
|
65
|
+
export function decodePeerDid4(did: string): PeerDid4InputDocument {
|
|
66
|
+
if (!did.startsWith("did:peer:4")) {
|
|
67
|
+
throw new Error(`Invalid did:peer:4: ${did}`);
|
|
68
|
+
}
|
|
69
|
+
if (SHORT_PATTERN.test(did)) {
|
|
70
|
+
throw new Error("Cannot decode document from short form did:peer:4");
|
|
71
|
+
}
|
|
72
|
+
if (!LONG_PATTERN.test(did)) {
|
|
73
|
+
throw new Error(`Invalid did:peer:4: ${did}`);
|
|
74
|
+
}
|
|
75
|
+
const body = did.slice("did:peer:4".length);
|
|
76
|
+
const colon = body.indexOf(":");
|
|
77
|
+
if (colon < 0) {
|
|
78
|
+
throw new Error(`Invalid did:peer:4: ${did}`);
|
|
79
|
+
}
|
|
80
|
+
const hashed = body.slice(0, colon);
|
|
81
|
+
const encodedDoc = body.slice(colon + 1);
|
|
82
|
+
if (hashEncodedDoc(encodedDoc) !== hashed) {
|
|
83
|
+
throw new Error(`Hash is invalid for did: ${did}`);
|
|
84
|
+
}
|
|
85
|
+
return decodeDoc(encodedDoc);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function longToShortPeerDid4(did: string): string {
|
|
89
|
+
if (!LONG_PATTERN.test(did)) {
|
|
90
|
+
throw new Error(`DID is not a long form did:peer:4: ${did}`);
|
|
91
|
+
}
|
|
92
|
+
const idx = did.lastIndexOf(":");
|
|
93
|
+
return did.slice(0, idx);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function isPeerDid4(did: string): boolean {
|
|
97
|
+
return did.startsWith("did:peer:4");
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function visitVerificationMethods(
|
|
101
|
+
document: PeerDid4InputDocument,
|
|
102
|
+
visitor: (vm: Record<string, unknown>) => Record<string, unknown>
|
|
103
|
+
): PeerDid4InputDocument {
|
|
104
|
+
const out = { ...document };
|
|
105
|
+
const vms = out.verificationMethod;
|
|
106
|
+
if (Array.isArray(vms)) {
|
|
107
|
+
out.verificationMethod = vms.map((vm) =>
|
|
108
|
+
typeof vm === "object" && vm !== null
|
|
109
|
+
? visitor(vm as Record<string, unknown>)
|
|
110
|
+
: vm
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
for (const rel of [
|
|
114
|
+
"authentication",
|
|
115
|
+
"assertionMethod",
|
|
116
|
+
"keyAgreement",
|
|
117
|
+
"capabilityInvocation",
|
|
118
|
+
"capabilityDelegation",
|
|
119
|
+
]) {
|
|
120
|
+
const entries = out[rel];
|
|
121
|
+
if (Array.isArray(entries)) {
|
|
122
|
+
out[rel] = entries.map((entry) => {
|
|
123
|
+
if (typeof entry === "object" && entry !== null) {
|
|
124
|
+
return visitor(entry as Record<string, unknown>);
|
|
125
|
+
}
|
|
126
|
+
return entry;
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function contextualizeDocument(
|
|
134
|
+
did: string,
|
|
135
|
+
document: PeerDid4InputDocument
|
|
136
|
+
): PeerDid4InputDocument {
|
|
137
|
+
const out: PeerDid4InputDocument = {
|
|
138
|
+
...document,
|
|
139
|
+
id: did,
|
|
140
|
+
};
|
|
141
|
+
return visitVerificationMethods(out, (vm) => {
|
|
142
|
+
if (!vm.controller) {
|
|
143
|
+
return { ...vm, controller: did };
|
|
144
|
+
}
|
|
145
|
+
return vm;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Resolve long-form did:peer:4 → contextualized DID document. */
|
|
150
|
+
export function resolvePeerDid4(did: string): PeerDid4InputDocument {
|
|
151
|
+
const decoded = decodePeerDid4(did);
|
|
152
|
+
const document = contextualizeDocument(did, decoded);
|
|
153
|
+
const aka = Array.isArray(document.alsoKnownAs)
|
|
154
|
+
? [...(document.alsoKnownAs as string[])]
|
|
155
|
+
: [];
|
|
156
|
+
aka.push(longToShortPeerDid4(did));
|
|
157
|
+
document.alsoKnownAs = aka;
|
|
158
|
+
return document;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Resolve short-form variant from long-form did:peer:4. */
|
|
162
|
+
export function resolvePeerDid4Short(did: string): PeerDid4InputDocument {
|
|
163
|
+
const decoded = decodePeerDid4(did);
|
|
164
|
+
const shortDid = longToShortPeerDid4(did);
|
|
165
|
+
const document = contextualizeDocument(shortDid, decoded);
|
|
166
|
+
const aka = Array.isArray(document.alsoKnownAs)
|
|
167
|
+
? [...(document.alsoKnownAs as string[])]
|
|
168
|
+
: [];
|
|
169
|
+
aka.push(did);
|
|
170
|
+
document.alsoKnownAs = aka;
|
|
171
|
+
return document;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Match short/long peer:4 forms. */
|
|
175
|
+
export function peerDid4Matches(stored: string, incoming: string): boolean {
|
|
176
|
+
if (stored === incoming) {
|
|
177
|
+
return true;
|
|
178
|
+
}
|
|
179
|
+
if (!isPeerDid4(stored) || !isPeerDid4(incoming)) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
try {
|
|
183
|
+
if (LONG_PATTERN.test(stored)) {
|
|
184
|
+
if (stored === incoming || longToShortPeerDid4(stored) === incoming) {
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (LONG_PATTERN.test(incoming)) {
|
|
189
|
+
if (stored === longToShortPeerDid4(incoming)) {
|
|
190
|
+
return true;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
} catch {
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { ed25519, x25519 } from "@noble/curves/ed25519.js";
|
|
2
|
+
import { bytesToHex } from "@veramo/utils";
|
|
3
|
+
import * as bs58 from "bs58";
|
|
4
|
+
import {
|
|
5
|
+
encodePeerDid4,
|
|
6
|
+
longToShortPeerDid4,
|
|
7
|
+
resolvePeerDid4,
|
|
8
|
+
type PeerDid4InputDocument,
|
|
9
|
+
} from "./peerDid4";
|
|
10
|
+
import type { StoredPeerDidBundle, StoredVeramoKey } from "./peerDid";
|
|
11
|
+
|
|
12
|
+
function multibasePublicKey(prefix: number, second: number, raw: Uint8Array): string {
|
|
13
|
+
return "z" + bs58.encode(Buffer.concat([Buffer.from([prefix, second]), Buffer.from(raw)]));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface PeerDid4KeySpec {
|
|
17
|
+
multikey: string;
|
|
18
|
+
relationships: Array<
|
|
19
|
+
| "authentication"
|
|
20
|
+
| "assertionMethod"
|
|
21
|
+
| "keyAgreement"
|
|
22
|
+
| "capabilityDelegation"
|
|
23
|
+
| "capabilityInvocation"
|
|
24
|
+
>;
|
|
25
|
+
ident?: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function inputDocFromKeys(
|
|
29
|
+
keys: PeerDid4KeySpec[],
|
|
30
|
+
services?: Record<string, unknown>[]
|
|
31
|
+
): PeerDid4InputDocument {
|
|
32
|
+
const inputDoc: PeerDid4InputDocument = {
|
|
33
|
+
"@context": ["https://www.w3.org/ns/did/v1"],
|
|
34
|
+
};
|
|
35
|
+
keys.forEach((key, index) => {
|
|
36
|
+
const ident = key.ident || `#key-${index}`;
|
|
37
|
+
const vm = {
|
|
38
|
+
id: ident,
|
|
39
|
+
type: "Multikey",
|
|
40
|
+
publicKeyMultibase: key.multikey,
|
|
41
|
+
};
|
|
42
|
+
const vms = (inputDoc.verificationMethod as unknown[]) || [];
|
|
43
|
+
vms.push(vm);
|
|
44
|
+
inputDoc.verificationMethod = vms;
|
|
45
|
+
const ctx = inputDoc["@context"] as string[];
|
|
46
|
+
if (!ctx.includes("https://w3id.org/security/multikey/v1")) {
|
|
47
|
+
ctx.push("https://w3id.org/security/multikey/v1");
|
|
48
|
+
}
|
|
49
|
+
for (const rel of key.relationships) {
|
|
50
|
+
const list = (inputDoc[rel] as unknown[]) || [];
|
|
51
|
+
list.push(ident);
|
|
52
|
+
inputDoc[rel] = list;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
if (services?.length) {
|
|
56
|
+
inputDoc.service = services;
|
|
57
|
+
}
|
|
58
|
+
return inputDoc;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** Create did:peer:4 (long form) with Ed25519 + X25519 keys for DIDComm. */
|
|
62
|
+
export function createPeerDid4WithKeys(): {
|
|
63
|
+
did: string;
|
|
64
|
+
shortDid: string;
|
|
65
|
+
didDocument: PeerDid4InputDocument;
|
|
66
|
+
keys: StoredPeerDidBundle;
|
|
67
|
+
} {
|
|
68
|
+
const edSecret = ed25519.utils.randomSecretKey();
|
|
69
|
+
const edPublic = ed25519.getPublicKey(edSecret);
|
|
70
|
+
const xSecret = x25519.utils.randomSecretKey();
|
|
71
|
+
const xPublic = x25519.getPublicKey(xSecret);
|
|
72
|
+
|
|
73
|
+
const edMulti = multibasePublicKey(0xed, 0x01, edPublic);
|
|
74
|
+
const xMulti = multibasePublicKey(0xec, 0x01, xPublic);
|
|
75
|
+
|
|
76
|
+
const inputDoc = inputDocFromKeys([
|
|
77
|
+
{
|
|
78
|
+
multikey: edMulti,
|
|
79
|
+
relationships: ["authentication", "capabilityDelegation"],
|
|
80
|
+
ident: "#key-0",
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
multikey: xMulti,
|
|
84
|
+
relationships: ["keyAgreement"],
|
|
85
|
+
ident: "#key-1",
|
|
86
|
+
},
|
|
87
|
+
]);
|
|
88
|
+
|
|
89
|
+
const did = encodePeerDid4(inputDoc);
|
|
90
|
+
const shortDid = longToShortPeerDid4(did);
|
|
91
|
+
const didDocument = resolvePeerDid4(did);
|
|
92
|
+
|
|
93
|
+
const edPublicHex = bytesToHex(edPublic);
|
|
94
|
+
const xPublicHex = bytesToHex(xPublic);
|
|
95
|
+
|
|
96
|
+
const veramoKeys: StoredVeramoKey[] = [
|
|
97
|
+
{
|
|
98
|
+
type: "Ed25519",
|
|
99
|
+
kid: edPublicHex,
|
|
100
|
+
privateKeyHex: bytesToHex(edSecret),
|
|
101
|
+
publicKeyHex: edPublicHex,
|
|
102
|
+
meta: { algorithms: ["EdDSA", "Ed25519"] },
|
|
103
|
+
kms: "local",
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
type: "X25519",
|
|
107
|
+
kid: xPublicHex,
|
|
108
|
+
privateKeyHex: bytesToHex(xSecret),
|
|
109
|
+
publicKeyHex: xPublicHex,
|
|
110
|
+
meta: { algorithms: ["ECDH", "ECDH-ES", "ECDH-1PU"] },
|
|
111
|
+
kms: "local",
|
|
112
|
+
},
|
|
113
|
+
];
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
did,
|
|
117
|
+
shortDid,
|
|
118
|
+
didDocument,
|
|
119
|
+
keys: { veramoKeys },
|
|
120
|
+
};
|
|
121
|
+
}
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Before portable export: upgrade extant Layer 1 subscription buses to Layer 2 (DID)
|
|
3
|
+
* so relationships (including community ↔ thing) survive cross-engine move + webvh rotate.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { PicoFramework } from "pico-framework";
|
|
7
|
+
import {
|
|
8
|
+
establishSubscriptionIdentity,
|
|
9
|
+
linkRemotePeer,
|
|
10
|
+
} from "./establishSubscription";
|
|
11
|
+
import type { IdentityStore } from "./store";
|
|
12
|
+
import type { PortableMoveDeps } from "./portableMove";
|
|
13
|
+
|
|
14
|
+
const SUBSCRIPTION_RID = "io.picolabs.subscription";
|
|
15
|
+
|
|
16
|
+
export interface PortableExportPrepareResult {
|
|
17
|
+
/** Subscription buses upgraded on picos inside the export subtree. */
|
|
18
|
+
upgradedOnSubtree: number;
|
|
19
|
+
/** Reciprocal buses upgraded on peers outside the subtree (same engine). */
|
|
20
|
+
upgradedOnPeers: number;
|
|
21
|
+
/** Buses skipped (already L2, no remote Tx, or remote did not resolve). */
|
|
22
|
+
skipped: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function busId(bus: Record<string, unknown>): string | undefined {
|
|
26
|
+
const id = bus["Id"];
|
|
27
|
+
return typeof id === "string" && id.length > 0 ? id : undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function collectSubtreePicoIds(pf: PicoFramework, picoRef: string): string[] {
|
|
31
|
+
const pico = pf.getPico(picoRef);
|
|
32
|
+
const ids = [pico.id];
|
|
33
|
+
for (const childEci of pico.toReadOnly().children) {
|
|
34
|
+
const child = pf.lookupChannel(childEci).pico;
|
|
35
|
+
ids.push(...collectSubtreePicoIds(pf, child.id));
|
|
36
|
+
}
|
|
37
|
+
return ids;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Read did:webvh from the identity store — never pf.query (deadlocks during export KRL query). */
|
|
41
|
+
async function webvhDidForChannelEci(
|
|
42
|
+
store: IdentityStore,
|
|
43
|
+
pf: PicoFramework,
|
|
44
|
+
remoteEci: string
|
|
45
|
+
): Promise<string | null> {
|
|
46
|
+
try {
|
|
47
|
+
const did = await store.getWebvhDid(pf.getPico(remoteEci).id);
|
|
48
|
+
return typeof did === "string" && did.length > 0 ? did : null;
|
|
49
|
+
} catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function mergeBusToLayer2(
|
|
55
|
+
bus: Record<string, unknown>,
|
|
56
|
+
remoteWebvhDid: string,
|
|
57
|
+
rxPeerDid: string
|
|
58
|
+
): Record<string, unknown> {
|
|
59
|
+
return {
|
|
60
|
+
...bus,
|
|
61
|
+
layer2: true,
|
|
62
|
+
Tx_did: remoteWebvhDid,
|
|
63
|
+
Rx_did: rxPeerDid,
|
|
64
|
+
Tx: "",
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function upgradeEstablishedList(
|
|
69
|
+
store: IdentityStore,
|
|
70
|
+
pf: PicoFramework,
|
|
71
|
+
picoId: string,
|
|
72
|
+
list: Record<string, unknown>[]
|
|
73
|
+
): Promise<{
|
|
74
|
+
next: Record<string, unknown>[];
|
|
75
|
+
upgraded: number;
|
|
76
|
+
skipped: number;
|
|
77
|
+
reciprocals: Array<{ remotePicoId: string; localWebvhDid: string; subId: string }>;
|
|
78
|
+
}> {
|
|
79
|
+
const localDid = await store.getWebvhDid(picoId);
|
|
80
|
+
const reciprocals: Array<{
|
|
81
|
+
remotePicoId: string;
|
|
82
|
+
localWebvhDid: string;
|
|
83
|
+
subId: string;
|
|
84
|
+
}> = [];
|
|
85
|
+
let upgraded = 0;
|
|
86
|
+
let skipped = 0;
|
|
87
|
+
const next: Record<string, unknown>[] = [];
|
|
88
|
+
|
|
89
|
+
for (const raw of list) {
|
|
90
|
+
if (!raw || typeof raw !== "object") {
|
|
91
|
+
next.push(raw as Record<string, unknown>);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const bus = { ...(raw as Record<string, unknown>) };
|
|
95
|
+
if (bus["layer2"] === true) {
|
|
96
|
+
next.push(bus);
|
|
97
|
+
skipped += 1;
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const subId = busId(bus);
|
|
102
|
+
const remoteEci =
|
|
103
|
+
typeof bus["Tx"] === "string" && bus["Tx"].length > 0
|
|
104
|
+
? bus["Tx"]
|
|
105
|
+
: undefined;
|
|
106
|
+
|
|
107
|
+
if (!subId || !remoteEci) {
|
|
108
|
+
next.push(bus);
|
|
109
|
+
skipped += 1;
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const remoteWebvhDid = await webvhDidForChannelEci(store, pf, remoteEci);
|
|
114
|
+
if (!remoteWebvhDid) {
|
|
115
|
+
next.push(bus);
|
|
116
|
+
skipped += 1;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const established = await establishSubscriptionIdentity(store, pf, picoId, {
|
|
121
|
+
subscriptionId: subId,
|
|
122
|
+
remoteWebvhDid,
|
|
123
|
+
remoteTxHost:
|
|
124
|
+
typeof bus["Tx_host"] === "string" ? bus["Tx_host"] : undefined,
|
|
125
|
+
rxEci: typeof bus["Rx"] === "string" ? bus["Rx"] : undefined,
|
|
126
|
+
channelName:
|
|
127
|
+
typeof bus["name"] === "string" ? bus["name"] : undefined,
|
|
128
|
+
channelType:
|
|
129
|
+
typeof bus["channel_type"] === "string"
|
|
130
|
+
? bus["channel_type"]
|
|
131
|
+
: undefined,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
next.push(mergeBusToLayer2(bus, remoteWebvhDid, established.peerDid));
|
|
135
|
+
upgraded += 1;
|
|
136
|
+
|
|
137
|
+
if (localDid) {
|
|
138
|
+
try {
|
|
139
|
+
const remotePico = pf.getPico(remoteEci);
|
|
140
|
+
reciprocals.push({
|
|
141
|
+
remotePicoId: remotePico.id,
|
|
142
|
+
localWebvhDid: localDid,
|
|
143
|
+
subId,
|
|
144
|
+
});
|
|
145
|
+
} catch {
|
|
146
|
+
// remote not on this engine
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return { next, upgraded, skipped, reciprocals };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async function upgradeReciprocalBuses(
|
|
155
|
+
store: IdentityStore,
|
|
156
|
+
pf: PicoFramework,
|
|
157
|
+
remotePicoId: string,
|
|
158
|
+
localPicoId: string,
|
|
159
|
+
localWebvhDid: string
|
|
160
|
+
): Promise<number> {
|
|
161
|
+
const remote = pf.loadedPicos().find((p) => p.id === remotePicoId);
|
|
162
|
+
if (!remote?.rulesets[SUBSCRIPTION_RID]) {
|
|
163
|
+
return 0;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const establishedRaw = await remote.getEnt(SUBSCRIPTION_RID, "established");
|
|
167
|
+
if (!Array.isArray(establishedRaw) || establishedRaw.length === 0) {
|
|
168
|
+
return 0;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
let upgraded = 0;
|
|
172
|
+
const next: Record<string, unknown>[] = [];
|
|
173
|
+
|
|
174
|
+
for (const raw of establishedRaw) {
|
|
175
|
+
if (!raw || typeof raw !== "object") {
|
|
176
|
+
next.push(raw as Record<string, unknown>);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const bus = { ...(raw as Record<string, unknown>) };
|
|
180
|
+
if (bus["layer2"] === true) {
|
|
181
|
+
next.push(bus);
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const subId = busId(bus);
|
|
186
|
+
const tx =
|
|
187
|
+
typeof bus["Tx"] === "string" && bus["Tx"].length > 0
|
|
188
|
+
? bus["Tx"]
|
|
189
|
+
: undefined;
|
|
190
|
+
|
|
191
|
+
let pointsAtLocal = bus["picoID"] === localPicoId;
|
|
192
|
+
if (!pointsAtLocal && tx) {
|
|
193
|
+
try {
|
|
194
|
+
pointsAtLocal = pf.getPico(tx).id === localPicoId;
|
|
195
|
+
} catch {
|
|
196
|
+
pointsAtLocal = false;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (!subId || !pointsAtLocal) {
|
|
201
|
+
next.push(bus);
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const established = await establishSubscriptionIdentity(
|
|
206
|
+
store,
|
|
207
|
+
pf,
|
|
208
|
+
remotePicoId,
|
|
209
|
+
{
|
|
210
|
+
subscriptionId: subId,
|
|
211
|
+
remoteWebvhDid: localWebvhDid,
|
|
212
|
+
remoteTxHost:
|
|
213
|
+
typeof bus["Tx_host"] === "string" ? bus["Tx_host"] : undefined,
|
|
214
|
+
rxEci: typeof bus["Rx"] === "string" ? bus["Rx"] : undefined,
|
|
215
|
+
}
|
|
216
|
+
);
|
|
217
|
+
|
|
218
|
+
const thingSub = await store.getPeerSubscription(localPicoId, subId);
|
|
219
|
+
const remoteWebvh = await store.getWebvhDid(remotePicoId);
|
|
220
|
+
const txHost =
|
|
221
|
+
typeof bus["Tx_host"] === "string" ? bus["Tx_host"] : undefined;
|
|
222
|
+
|
|
223
|
+
if (thingSub?.peerDid) {
|
|
224
|
+
await linkRemotePeer(store, remotePicoId, subId, {
|
|
225
|
+
remotePeerDid: thingSub.peerDid,
|
|
226
|
+
remoteWebvhDid: localWebvhDid,
|
|
227
|
+
remoteTxHost: txHost,
|
|
228
|
+
});
|
|
229
|
+
await linkRemotePeer(store, localPicoId, subId, {
|
|
230
|
+
remotePeerDid: established.peerDid,
|
|
231
|
+
remoteWebvhDid: remoteWebvh ?? undefined,
|
|
232
|
+
remoteTxHost: txHost,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
next.push(mergeBusToLayer2(bus, localWebvhDid, established.peerDid));
|
|
237
|
+
upgraded += 1;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (upgraded > 0) {
|
|
241
|
+
await remote.putEnt(SUBSCRIPTION_RID, "established", next);
|
|
242
|
+
}
|
|
243
|
+
return upgraded;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Upgrade extant L1 established buses to L2 for every pico in the export subtree. */
|
|
247
|
+
export async function prepareSubtreeRelationshipsForPortableExport(
|
|
248
|
+
deps: Pick<PortableMoveDeps, "store" | "pf" | "identity">,
|
|
249
|
+
subtreeRootRef: string
|
|
250
|
+
): Promise<PortableExportPrepareResult> {
|
|
251
|
+
const subtreeIds = new Set(collectSubtreePicoIds(deps.pf, subtreeRootRef));
|
|
252
|
+
let upgradedOnSubtree = 0;
|
|
253
|
+
let upgradedOnPeers = 0;
|
|
254
|
+
let skipped = 0;
|
|
255
|
+
const reciprocalSeen = new Set<string>();
|
|
256
|
+
|
|
257
|
+
for (const picoId of subtreeIds) {
|
|
258
|
+
const pico = deps.pf.loadedPicos().find((p) => p.id === picoId);
|
|
259
|
+
if (!pico?.rulesets[SUBSCRIPTION_RID]) {
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const establishedRaw = await pico.getEnt(SUBSCRIPTION_RID, "established");
|
|
264
|
+
if (!Array.isArray(establishedRaw) || establishedRaw.length === 0) {
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const list = establishedRaw.filter(
|
|
269
|
+
(b) => b && typeof b === "object"
|
|
270
|
+
) as Record<string, unknown>[];
|
|
271
|
+
|
|
272
|
+
const {
|
|
273
|
+
next,
|
|
274
|
+
upgraded,
|
|
275
|
+
skipped: skip,
|
|
276
|
+
reciprocals,
|
|
277
|
+
} = await upgradeEstablishedList(deps.store, deps.pf, picoId, list);
|
|
278
|
+
|
|
279
|
+
skipped += skip;
|
|
280
|
+
if (upgraded > 0) {
|
|
281
|
+
await pico.putEnt(SUBSCRIPTION_RID, "established", next);
|
|
282
|
+
upgradedOnSubtree += upgraded;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
for (const r of reciprocals) {
|
|
286
|
+
const key = `${r.remotePicoId}:${r.localWebvhDid}`;
|
|
287
|
+
if (reciprocalSeen.has(key)) {
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
reciprocalSeen.add(key);
|
|
291
|
+
upgradedOnPeers += await upgradeReciprocalBuses(
|
|
292
|
+
deps.store,
|
|
293
|
+
deps.pf,
|
|
294
|
+
r.remotePicoId,
|
|
295
|
+
picoId,
|
|
296
|
+
r.localWebvhDid
|
|
297
|
+
);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return { upgradedOnSubtree, upgradedOnPeers, skipped };
|
|
302
|
+
}
|