pico-engine 1.5.0 → 1.6.1
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
package/README.md
CHANGED
|
@@ -131,6 +131,8 @@ For example, say you want to have your engine running with SSL on a custom domai
|
|
|
131
131
|
|
|
132
132
|
The developer UI uses **passkeys** (WebAuthn). On first visit you register a passkey, which creates your account and root pico. Sign in again with the same passkey; add more passkeys or invite others from **Settings** (gear icon).
|
|
133
133
|
|
|
134
|
+
When self-signup is disabled, **Settings → Invite someone** can include an optional **bootstrap ruleset URL** (`file://` or `https://`). The engine validates the URL when the invite is created; when the invitee registers, that ruleset is installed on their new root after the base OS rulesets (e.g. a Manifold bootstrap KRL). The invite **label** pre-fills the invitee's mesh name (editable before registering). WebAuthn `user.name` is set to the mesh name so password managers (e.g. 1Password) show it alongside the site hostname.
|
|
135
|
+
|
|
134
136
|
- **`/c/*`** — internal/UI API; requires a passkey session cookie (localhost bypass is on by default for in-engine KRL HTTP loops; set `PICO_ENGINE_ALLOW_LOCALHOST_C=0` to disable).
|
|
135
137
|
- **`/auth/*`** — registration, login, session management.
|
|
136
138
|
|
|
@@ -272,6 +274,45 @@ npm run test:auth # passkeys
|
|
|
272
274
|
|
|
273
275
|
Tests use isolated temp homes and ephemeral ports; they do not require stopping a running engine.
|
|
274
276
|
|
|
277
|
+
## Layer 2 identity and subscriptions (1.6)
|
|
278
|
+
|
|
279
|
+
Every pico has a portable **did:webvh** identity. **Layer 2 subscriptions** use **`layer2: true`** and a **`target_did`** (did:webvh) instead of the legacy **`wellKnown_Tx`** ECI handshake. After intro, remote traffic uses **did:peer** DIDs and SKY over **DIDComm** when picos are on different meshes or engines.
|
|
280
|
+
|
|
281
|
+
- **Same engine:** intra-mesh queries/events stay local when roots share a mesh.
|
|
282
|
+
- **Cross-engine:** set a peer-reachable **`PICO_ENGINE_BASE_URL`** on each engine (for local demos, two processes on `localhost:3001` and `:3002` — see below).
|
|
283
|
+
- **Developer UI:** **Subscriptions** tab (Layer 2 create, inbound approval, established subs with `Tx_did` / `Rx_did`).
|
|
284
|
+
- **Legacy ECI subscriptions** are unchanged.
|
|
285
|
+
|
|
286
|
+
**Deprecated:** `io.picolabs.did-o` and `dido:prepareQuery` — use **`wrangler:picoQuery`** and **`event:send({ did: … })`**.
|
|
287
|
+
|
|
288
|
+
Full guide: [docs/guides/layer2-subscriptions.md](../../docs/guides/layer2-subscriptions.md) · Release notes: [docs/release/1.6.md](../../docs/release/1.6.md)
|
|
289
|
+
|
|
290
|
+
**Dependency:** pico-engine 1.6 requires **pico-framework `^0.8.1`** (npm). When developing engine + framework together, use `npm run link-framework` from the repo root.
|
|
291
|
+
|
|
292
|
+
### Layer 2 tests
|
|
293
|
+
|
|
294
|
+
```sh
|
|
295
|
+
cd packages/pico-engine
|
|
296
|
+
npm run test:epic9 # release regression matrix
|
|
297
|
+
npm run test:cross-engine # two engines, intro + query + event
|
|
298
|
+
npm run test:layer2-wrangler
|
|
299
|
+
npm run test:layer2-policy
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### Cross-engine demo (two processes on one machine)
|
|
303
|
+
|
|
304
|
+
Use separate data directories and ports so each engine is an independent mesh. `PICO_ENGINE_BASE_URL` defaults to `http://localhost:$PORT`, which works for passkeys and for peer DID fetch (same as `npm run test:cross-engine`).
|
|
305
|
+
|
|
306
|
+
```sh
|
|
307
|
+
# terminal 1
|
|
308
|
+
PICO_ENGINE_HOME=/tmp/pico-engine-a PORT=3001 npm start
|
|
309
|
+
|
|
310
|
+
# terminal 2 (from repo root)
|
|
311
|
+
PICO_ENGINE_HOME=/tmp/pico-engine-b PORT=3002 npm start
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
Open http://localhost:3001 and http://localhost:3002 — register on both, then Layer 2 subscribe A→B. See [docs/guides/layer2-subscriptions.md](../../docs/guides/layer2-subscriptions.md). Test/demo homes live under **`/tmp`** (not `~/.pico-engine`).
|
|
315
|
+
|
|
275
316
|
## Contributing
|
|
276
317
|
|
|
277
318
|
See the repository [root readme](https://github.com/Picolab/pico-engine#readme)
|
|
@@ -18,6 +18,7 @@ const classic_level_1 = require("classic-level");
|
|
|
18
18
|
const makeDir = require("make-dir");
|
|
19
19
|
const path = require("path");
|
|
20
20
|
const urlLib = require("url");
|
|
21
|
+
const bundledKrl_1 = require("./bundledKrl");
|
|
21
22
|
const krlCompiler = require("krl-compiler");
|
|
22
23
|
const krlCompilerVersion = require("krl-compiler/package.json").version;
|
|
23
24
|
const charwise = require("charwise");
|
|
@@ -92,6 +93,7 @@ function RulesetRegistryLoaderFs(engineHomeDir) {
|
|
|
92
93
|
};
|
|
93
94
|
}
|
|
94
95
|
async function fetchKrl(url) {
|
|
96
|
+
url = (0, bundledKrl_1.resolveBundledKrlUrl)(url);
|
|
95
97
|
const urlParsed = urlLib.parse(url);
|
|
96
98
|
if (urlParsed.protocol === "file:") {
|
|
97
99
|
return new Promise((resolve, reject) => {
|
package/dist/auth/AuthService.js
CHANGED
|
@@ -46,12 +46,27 @@ class AuthService {
|
|
|
46
46
|
catch (_e) {
|
|
47
47
|
// fall through to defaults
|
|
48
48
|
}
|
|
49
|
+
const originLabel = this.originLabelFromUrl(origin);
|
|
50
|
+
const rpName = this.deps.rpName ||
|
|
51
|
+
(originLabel
|
|
52
|
+
? `${DEFAULT_RP_NAME} (${originLabel})`
|
|
53
|
+
: DEFAULT_RP_NAME);
|
|
49
54
|
return {
|
|
50
55
|
rpID: this.deps.rpID || hostname,
|
|
51
|
-
rpName
|
|
56
|
+
rpName,
|
|
52
57
|
origin: this.deps.origin || origin,
|
|
53
58
|
};
|
|
54
59
|
}
|
|
60
|
+
/** Host[:port] label for disambiguating local multi-engine setups in WebAuthn UI. */
|
|
61
|
+
originLabelFromUrl(origin) {
|
|
62
|
+
try {
|
|
63
|
+
const url = new URL(origin);
|
|
64
|
+
return url.port ? `${url.hostname}:${url.port}` : url.hostname;
|
|
65
|
+
}
|
|
66
|
+
catch (_e) {
|
|
67
|
+
return "";
|
|
68
|
+
}
|
|
69
|
+
}
|
|
55
70
|
isSecure() {
|
|
56
71
|
return this.rp().origin.startsWith("https:");
|
|
57
72
|
}
|
|
@@ -74,13 +89,14 @@ class AuthService {
|
|
|
74
89
|
}
|
|
75
90
|
const { rpID, rpName } = this.rp();
|
|
76
91
|
const accountId = randomId();
|
|
77
|
-
const displayName =
|
|
92
|
+
const displayName = await this.resolveRegistrationDisplayName(accountId, input.displayName);
|
|
93
|
+
const { userName, userDisplayName } = this.webauthnUserFields(displayName);
|
|
78
94
|
const options = await this.deps.webauthn.generateRegistrationOptions({
|
|
79
95
|
rpID,
|
|
80
96
|
rpName,
|
|
81
97
|
userID: new TextEncoder().encode(accountId),
|
|
82
|
-
userName
|
|
83
|
-
userDisplayName
|
|
98
|
+
userName,
|
|
99
|
+
userDisplayName,
|
|
84
100
|
excludeCredentials: [],
|
|
85
101
|
});
|
|
86
102
|
const ceremonyId = this.putCeremony({
|
|
@@ -120,11 +136,10 @@ class AuthService {
|
|
|
120
136
|
accountId: cer.accountId,
|
|
121
137
|
rootPicoId: cer.rootPicoId,
|
|
122
138
|
displayName: cer.displayName,
|
|
123
|
-
webauthnUserName: cer.accountId,
|
|
124
139
|
createdAt: new Date().toISOString(),
|
|
125
140
|
};
|
|
126
141
|
await this.deps.db.put([ACCOUNT_PREFIX, account.accountId], account);
|
|
127
|
-
await this.storeCredential(account.accountId, result.credential,
|
|
142
|
+
await this.storeCredential(account.accountId, result.credential, cer.displayName);
|
|
128
143
|
if (this.deps.onAccountClaimed) {
|
|
129
144
|
await this.deps.onAccountClaimed(cer.rootPicoId, cer.displayName);
|
|
130
145
|
}
|
|
@@ -134,6 +149,18 @@ class AuthService {
|
|
|
134
149
|
// ---- registration invites ----
|
|
135
150
|
async createInvite(input) {
|
|
136
151
|
var _a;
|
|
152
|
+
const bootstrapUrl = (input.bootstrapUrl || "").trim() || undefined;
|
|
153
|
+
let bootstrapRid;
|
|
154
|
+
if (bootstrapUrl) {
|
|
155
|
+
try {
|
|
156
|
+
const flushed = await this.deps.flushBootstrapUrl(bootstrapUrl);
|
|
157
|
+
bootstrapRid = flushed.rid;
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
const detail = err instanceof Error ? err.message : "Bootstrap ruleset URL could not be loaded";
|
|
161
|
+
throw new AuthError(`Invalid bootstrap ruleset URL: ${detail}`, 400);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
137
164
|
const token = randomToken();
|
|
138
165
|
const now = Date.now();
|
|
139
166
|
const ttl = (_a = this.deps.inviteTtlMs) !== null && _a !== void 0 ? _a : DEFAULT_INVITE_TTL_MS;
|
|
@@ -141,6 +168,8 @@ class AuthService {
|
|
|
141
168
|
token,
|
|
142
169
|
createdByAccountId: input.createdByAccountId,
|
|
143
170
|
label: (input.label || "").trim() || undefined,
|
|
171
|
+
bootstrapUrl,
|
|
172
|
+
bootstrapRid,
|
|
144
173
|
createdAt: new Date(now).toISOString(),
|
|
145
174
|
expiresAt: now + ttl,
|
|
146
175
|
};
|
|
@@ -148,6 +177,8 @@ class AuthService {
|
|
|
148
177
|
return {
|
|
149
178
|
token: invite.token,
|
|
150
179
|
label: invite.label,
|
|
180
|
+
bootstrapUrl: invite.bootstrapUrl,
|
|
181
|
+
bootstrapRid: invite.bootstrapRid,
|
|
151
182
|
createdAt: invite.createdAt,
|
|
152
183
|
expiresAt: invite.expiresAt,
|
|
153
184
|
};
|
|
@@ -163,6 +194,8 @@ class AuthService {
|
|
|
163
194
|
return {
|
|
164
195
|
valid: true,
|
|
165
196
|
label: invite.label,
|
|
197
|
+
bootstrapUrl: invite.bootstrapUrl,
|
|
198
|
+
bootstrapRid: invite.bootstrapRid,
|
|
166
199
|
expiresAt: invite.expiresAt,
|
|
167
200
|
};
|
|
168
201
|
}
|
|
@@ -175,13 +208,14 @@ class AuthService {
|
|
|
175
208
|
await this.assertRegistrationAllowed(inviteToken);
|
|
176
209
|
const { rpID, rpName } = this.rp();
|
|
177
210
|
const accountId = randomId();
|
|
178
|
-
const displayName =
|
|
211
|
+
const displayName = await this.resolveRegistrationDisplayName(accountId, input.displayName, inviteToken);
|
|
212
|
+
const { userName, userDisplayName } = this.webauthnUserFields(displayName);
|
|
179
213
|
const options = await this.deps.webauthn.generateRegistrationOptions({
|
|
180
214
|
rpID,
|
|
181
215
|
rpName,
|
|
182
216
|
userID: new TextEncoder().encode(accountId),
|
|
183
|
-
userName
|
|
184
|
-
userDisplayName
|
|
217
|
+
userName,
|
|
218
|
+
userDisplayName,
|
|
185
219
|
excludeCredentials: [],
|
|
186
220
|
});
|
|
187
221
|
const ceremonyId = this.putCeremony({
|
|
@@ -210,19 +244,24 @@ class AuthService {
|
|
|
210
244
|
if (!result.verified || !result.credential) {
|
|
211
245
|
throw new AuthError("Registration could not be verified", 400);
|
|
212
246
|
}
|
|
247
|
+
let bootstrapUrl;
|
|
248
|
+
if (cer.inviteToken) {
|
|
249
|
+
const invite = await this.getInvite(cer.inviteToken);
|
|
250
|
+
bootstrapUrl = invite === null || invite === void 0 ? void 0 : invite.bootstrapUrl;
|
|
251
|
+
}
|
|
213
252
|
// Provision the account's root only after the ceremony verifies.
|
|
214
253
|
const { rootPicoId, uiECI } = await this.deps.provisionRoot({
|
|
215
254
|
name: cer.displayName,
|
|
255
|
+
bootstrapUrl,
|
|
216
256
|
});
|
|
217
257
|
const account = {
|
|
218
258
|
accountId: cer.accountId,
|
|
219
259
|
rootPicoId,
|
|
220
260
|
displayName: cer.displayName,
|
|
221
|
-
webauthnUserName: cer.accountId,
|
|
222
261
|
createdAt: new Date().toISOString(),
|
|
223
262
|
};
|
|
224
263
|
await this.deps.db.put([ACCOUNT_PREFIX, account.accountId], account);
|
|
225
|
-
await this.storeCredential(account.accountId, result.credential,
|
|
264
|
+
await this.storeCredential(account.accountId, result.credential, cer.displayName);
|
|
226
265
|
if (cer.inviteToken) {
|
|
227
266
|
await this.consumeInvite(cer.inviteToken);
|
|
228
267
|
}
|
|
@@ -275,13 +314,13 @@ class AuthService {
|
|
|
275
314
|
}
|
|
276
315
|
const { rpID, rpName } = this.rp();
|
|
277
316
|
const existing = await this.listStoredCredentials(input.accountId);
|
|
278
|
-
const userName =
|
|
317
|
+
const { userName, userDisplayName } = this.webauthnUserFields(account.displayName);
|
|
279
318
|
const options = await this.deps.webauthn.generateRegistrationOptions({
|
|
280
319
|
rpID,
|
|
281
320
|
rpName,
|
|
282
321
|
userID: new TextEncoder().encode(account.accountId),
|
|
283
322
|
userName,
|
|
284
|
-
userDisplayName
|
|
323
|
+
userDisplayName,
|
|
285
324
|
excludeCredentials: existing.map((c) => ({
|
|
286
325
|
id: c.credentialID,
|
|
287
326
|
transports: c.transports,
|
|
@@ -386,6 +425,31 @@ class AuthService {
|
|
|
386
425
|
};
|
|
387
426
|
}
|
|
388
427
|
// ---- stores ----
|
|
428
|
+
/** Mesh name for registration: explicit input, else invite label, else generated. */
|
|
429
|
+
async resolveRegistrationDisplayName(accountId, inputDisplayName, inviteToken) {
|
|
430
|
+
const fromInput = (inputDisplayName || "").trim();
|
|
431
|
+
if (fromInput) {
|
|
432
|
+
return fromInput;
|
|
433
|
+
}
|
|
434
|
+
const token = (inviteToken || "").trim();
|
|
435
|
+
if (token) {
|
|
436
|
+
const invite = await this.getInvite(token);
|
|
437
|
+
const fromInvite = ((invite === null || invite === void 0 ? void 0 : invite.label) || "").trim();
|
|
438
|
+
if (fromInvite) {
|
|
439
|
+
return fromInvite;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return `pico-${accountId.slice(0, 6)}`;
|
|
443
|
+
}
|
|
444
|
+
/** WebAuthn user.name is what password managers (e.g. 1Password) show alongside the site. */
|
|
445
|
+
webauthnUserFields(displayName) {
|
|
446
|
+
const name = (displayName || "").trim() || "My mesh";
|
|
447
|
+
const originLabel = this.originLabelFromUrl(this.rp().origin);
|
|
448
|
+
const unique = originLabel && !name.includes(originLabel)
|
|
449
|
+
? `${name} (${originLabel})`
|
|
450
|
+
: name;
|
|
451
|
+
return { userName: unique, userDisplayName: unique };
|
|
452
|
+
}
|
|
389
453
|
async assertRegistrationAllowed(inviteToken) {
|
|
390
454
|
const count = await this.accountCount();
|
|
391
455
|
if (count === 0) {
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BUNDLED_KRL_FILES = void 0;
|
|
4
|
+
exports.bundledKrlDir = bundledKrlDir;
|
|
5
|
+
exports.resolveBundledKrlUrl = resolveBundledKrlUrl;
|
|
6
|
+
const fs = require("fs");
|
|
7
|
+
const path = require("path");
|
|
8
|
+
const toFileUrl_1 = require("./utils/toFileUrl");
|
|
9
|
+
/**
|
|
10
|
+
* OS-layer rulesets shipped inside the pico-engine package. On flush, these
|
|
11
|
+
* always reload from the running engine's krl/ directory so dev edits apply
|
|
12
|
+
* even when the pico was provisioned from a different absolute file:// path.
|
|
13
|
+
*/
|
|
14
|
+
exports.BUNDLED_KRL_FILES = [
|
|
15
|
+
"io.picolabs.pico-engine-ui.krl",
|
|
16
|
+
"io.picolabs.wrangler.krl",
|
|
17
|
+
"io.picolabs.subscription.krl",
|
|
18
|
+
"io.picolabs.pds.krl",
|
|
19
|
+
];
|
|
20
|
+
function bundledKrlDir() {
|
|
21
|
+
return path.resolve(__dirname, "..", "krl");
|
|
22
|
+
}
|
|
23
|
+
function resolveBundledKrlUrl(url) {
|
|
24
|
+
let filename = null;
|
|
25
|
+
try {
|
|
26
|
+
const parsed = new URL(url);
|
|
27
|
+
if (parsed.protocol === "file:") {
|
|
28
|
+
filename = path.basename(parsed.pathname);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
catch (_a) {
|
|
32
|
+
return url;
|
|
33
|
+
}
|
|
34
|
+
if (!filename || !exports.BUNDLED_KRL_FILES.includes(filename)) {
|
|
35
|
+
return url;
|
|
36
|
+
}
|
|
37
|
+
const localPath = path.join(bundledKrlDir(), filename);
|
|
38
|
+
if (fs.existsSync(localPath)) {
|
|
39
|
+
return (0, toFileUrl_1.toFileUrl)(localPath);
|
|
40
|
+
}
|
|
41
|
+
return url;
|
|
42
|
+
}
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IdentityService = void 0;
|
|
4
|
+
const cuid = require("cuid");
|
|
5
|
+
const cross_fetch_1 = require("cross-fetch");
|
|
6
|
+
const errors_1 = require("./errors");
|
|
7
|
+
const webvh_1 = require("./webvh");
|
|
8
|
+
const veramoDidComm_1 = require("./veramoDidComm");
|
|
9
|
+
const establishSubscription_1 = require("./establishSubscription");
|
|
10
|
+
const crossPico_1 = require("./crossPico");
|
|
11
|
+
const skyProtocol_1 = require("./skyProtocol");
|
|
12
|
+
const skyIntro_1 = require("./skyIntro");
|
|
13
|
+
/**
|
|
14
|
+
* Engine identity primitive — store-backed DID state and DIDComm capabilities.
|
|
15
|
+
* Crypto (pack/unpack) is delegated to Veramo when installed (Epic 2+).
|
|
16
|
+
*/
|
|
17
|
+
class IdentityService {
|
|
18
|
+
constructor(deps) {
|
|
19
|
+
this.store = deps.store;
|
|
20
|
+
this.getBaseUrl = deps.getBaseUrl;
|
|
21
|
+
this.pf = deps.pf;
|
|
22
|
+
}
|
|
23
|
+
async ensureWebvhDid(picoId, options = {}) {
|
|
24
|
+
return (0, webvh_1.ensureWebvhDid)(this.store, this.pf, () => this.getBaseUrl(), picoId, options);
|
|
25
|
+
}
|
|
26
|
+
async ensureWebvhForAllLoadedPicos() {
|
|
27
|
+
await (0, webvh_1.ensureWebvhForLoadedPicos)(this.store, this.pf, () => this.getBaseUrl());
|
|
28
|
+
for (const pico of this.pf.loadedPicos()) {
|
|
29
|
+
await (0, veramoDidComm_1.warmPeerDidsForPico)(this.store, pico.id);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Layer 2 subscription establish — create did:peer + internal Rx + persist keys.
|
|
34
|
+
* Idempotent when called again with the same subscriptionId.
|
|
35
|
+
*/
|
|
36
|
+
async establishSubscription(picoId, input) {
|
|
37
|
+
return (0, establishSubscription_1.establishSubscriptionIdentity)(this.store, this.pf, picoId, input);
|
|
38
|
+
}
|
|
39
|
+
async establishSubscriptionFromBus(picoId, bus) {
|
|
40
|
+
return this.establishSubscription(picoId, (0, establishSubscription_1.establishInputFromBus)(bus));
|
|
41
|
+
}
|
|
42
|
+
async linkRemotePeer(picoId, subscriptionId, input) {
|
|
43
|
+
return (0, establishSubscription_1.linkRemotePeer)(this.store, picoId, subscriptionId, input);
|
|
44
|
+
}
|
|
45
|
+
async teardownSubscription(picoId, subscriptionId) {
|
|
46
|
+
await this.store.deletePeerSubscription(picoId, subscriptionId);
|
|
47
|
+
}
|
|
48
|
+
async findSubscriptionIdForDid(picoId, did) {
|
|
49
|
+
return this.store.findSubscriptionIdForDid(picoId, did);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Query a remote pico by DID — layer2 subscription route first, then cold did:webvh.
|
|
53
|
+
*/
|
|
54
|
+
async picoQuery(picoId, did, query) {
|
|
55
|
+
const subId = await this.findSubscriptionIdForDid(picoId, did);
|
|
56
|
+
if (subId) {
|
|
57
|
+
return this.crossPicoQuery(picoId, undefined, subId, query);
|
|
58
|
+
}
|
|
59
|
+
return this.coldDidQuery(picoId, did, query);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Send an event to a remote pico by DID via an established layer2 subscription.
|
|
63
|
+
*/
|
|
64
|
+
async picoEvent(picoId, did, event) {
|
|
65
|
+
const subId = await this.findSubscriptionIdForDid(picoId, did);
|
|
66
|
+
if (!subId) {
|
|
67
|
+
throw new errors_1.IdentityError(`No layer2 subscription route for DID ${did}`, 404);
|
|
68
|
+
}
|
|
69
|
+
return this.crossPicoEvent(picoId, undefined, subId, event);
|
|
70
|
+
}
|
|
71
|
+
async coldDidQuery(picoId, did, query) {
|
|
72
|
+
const from = await this.getMyDid(picoId);
|
|
73
|
+
if (!from) {
|
|
74
|
+
throw new errors_1.IdentityError("Pico has no did:webvh for cold DID query", 404);
|
|
75
|
+
}
|
|
76
|
+
const message = this.generateMessage({
|
|
77
|
+
type: "https://picolabs.io/query/1.0/query",
|
|
78
|
+
from,
|
|
79
|
+
to: [did],
|
|
80
|
+
body: query,
|
|
81
|
+
});
|
|
82
|
+
const doc = await this.resolveDid(picoId, did);
|
|
83
|
+
const endpoint = (0, crossPico_1.extractDidcommEndpoint)(doc);
|
|
84
|
+
const packed = await this.packMessage(picoId, message, from, did);
|
|
85
|
+
const res = await (0, cross_fetch_1.default)(endpoint, {
|
|
86
|
+
method: "POST",
|
|
87
|
+
headers: { "Content-Type": "application/didcomm-encrypted+json" },
|
|
88
|
+
body: packed,
|
|
89
|
+
});
|
|
90
|
+
if (!res.ok) {
|
|
91
|
+
throw new errors_1.IdentityError(`Cold DID query failed: HTTP ${res.status}`, res.status);
|
|
92
|
+
}
|
|
93
|
+
const data = await res.json();
|
|
94
|
+
if (data.result !== undefined) {
|
|
95
|
+
return data.result;
|
|
96
|
+
}
|
|
97
|
+
return data;
|
|
98
|
+
}
|
|
99
|
+
async getMyDid(picoId) {
|
|
100
|
+
return this.store.getWebvhDid(picoId);
|
|
101
|
+
}
|
|
102
|
+
async getPublicIntro(picoId) {
|
|
103
|
+
const value = await this.store.getPublicIntro(picoId);
|
|
104
|
+
if (value === null) {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
async setPublicIntro(picoId, enabled) {
|
|
110
|
+
await this.store.putPublicIntro(picoId, enabled);
|
|
111
|
+
return true;
|
|
112
|
+
}
|
|
113
|
+
/** Whether unsolicited DIDComm intro to this pico's did:webvh is allowed. */
|
|
114
|
+
async acceptsPublicIntro(picoId) {
|
|
115
|
+
return this.getPublicIntro(picoId);
|
|
116
|
+
}
|
|
117
|
+
async getWebvhLogJsonl(picoId) {
|
|
118
|
+
const log = await this.store.getWebvhLog(picoId);
|
|
119
|
+
if (!log || log.length === 0) {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
return (0, webvh_1.formatDidLogAsJsonl)(log);
|
|
123
|
+
}
|
|
124
|
+
async getParallelWebDoc(picoId) {
|
|
125
|
+
return (0, webvh_1.parallelWebDocForPico)(this.store, picoId);
|
|
126
|
+
}
|
|
127
|
+
async addRoute(picoId, type, domain, name) {
|
|
128
|
+
const routes = (await this.store.getRoutes(picoId)) || {};
|
|
129
|
+
routes[type] = { domain, name };
|
|
130
|
+
await this.store.putRoutes(picoId, routes);
|
|
131
|
+
return true;
|
|
132
|
+
}
|
|
133
|
+
async getRoutes(picoId) {
|
|
134
|
+
return (await this.store.getRoutes(picoId)) || {};
|
|
135
|
+
}
|
|
136
|
+
generateMessage(options) {
|
|
137
|
+
const message = {
|
|
138
|
+
id: cuid(),
|
|
139
|
+
typ: "application/didcomm-plain+json",
|
|
140
|
+
type: options.type,
|
|
141
|
+
body: options.body,
|
|
142
|
+
from: options.from,
|
|
143
|
+
to: options.to,
|
|
144
|
+
thid: options.thid,
|
|
145
|
+
pthid: options.pthid,
|
|
146
|
+
created_time: Math.floor(Date.now() / 1000),
|
|
147
|
+
};
|
|
148
|
+
if (options.expires_time) {
|
|
149
|
+
message.expires_time =
|
|
150
|
+
Math.floor(Date.now() / 1000) + options.expires_time;
|
|
151
|
+
}
|
|
152
|
+
return message;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Pack a plaintext DIDComm message (authcrypt). Requires @veramo/did-comm and
|
|
156
|
+
* supporting packages to be installed (`npm install` in packages/pico-engine).
|
|
157
|
+
*/
|
|
158
|
+
async packMessage(picoId, message, from, to) {
|
|
159
|
+
return (0, veramoDidComm_1.packDidCommMessage)(picoId, message, from, to, this.store, {
|
|
160
|
+
resolveDid: (did) => this.resolveDid("", did),
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
async unpackMessage(picoId, jwe) {
|
|
164
|
+
return (0, veramoDidComm_1.unpackDidCommMessage)(picoId, jwe, this.store);
|
|
165
|
+
}
|
|
166
|
+
/** Create a did:peer (num_algo 2). Prefer establishSubscription for subscriptions. */
|
|
167
|
+
async createPeerDid(_picoId) {
|
|
168
|
+
return (0, veramoDidComm_1.createPeerDid)();
|
|
169
|
+
}
|
|
170
|
+
async registerPeerSubscription(picoId, record) {
|
|
171
|
+
await this.store.putPeerSubscription(picoId, record);
|
|
172
|
+
}
|
|
173
|
+
async crossPicoEvent(fromPicoId, toPicoId, subscriptionId, event) {
|
|
174
|
+
return (0, crossPico_1.crossPicoEvent)({
|
|
175
|
+
store: this.store,
|
|
176
|
+
pf: this.pf,
|
|
177
|
+
getBaseUrl: this.getBaseUrl,
|
|
178
|
+
identity: this,
|
|
179
|
+
}, fromPicoId, toPicoId, subscriptionId, event);
|
|
180
|
+
}
|
|
181
|
+
async crossPicoQuery(fromPicoId, toPicoId, subscriptionId, query) {
|
|
182
|
+
return (0, crossPico_1.crossPicoQuery)({
|
|
183
|
+
store: this.store,
|
|
184
|
+
pf: this.pf,
|
|
185
|
+
getBaseUrl: this.getBaseUrl,
|
|
186
|
+
identity: this,
|
|
187
|
+
}, fromPicoId, toPicoId, subscriptionId, query);
|
|
188
|
+
}
|
|
189
|
+
skyIntroDeps() {
|
|
190
|
+
return {
|
|
191
|
+
store: this.store,
|
|
192
|
+
pf: this.pf,
|
|
193
|
+
getBaseUrl: this.getBaseUrl,
|
|
194
|
+
identity: this,
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
async sendSkyIntro(picoId, input) {
|
|
198
|
+
return (0, skyIntro_1.sendSkyIntro)(this.skyIntroDeps(), picoId, input);
|
|
199
|
+
}
|
|
200
|
+
async sendSkyIntroResponse(picoId, subscriptionId, toPeerDid, response, options = {}) {
|
|
201
|
+
return (0, skyIntro_1.sendSkyIntroResponse)(this.skyIntroDeps(), picoId, subscriptionId, toPeerDid, response, options);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Door verify: unpack JWE at ingress, route by sender peer DID, deliver SKY payload.
|
|
205
|
+
*/
|
|
206
|
+
async handleDidcommIngress(ingressEci, jwe) {
|
|
207
|
+
const picoId = await this.store.findPicoIdByIngressEci(ingressEci);
|
|
208
|
+
if (!picoId) {
|
|
209
|
+
throw new errors_1.IdentityError("Unknown DIDComm ingress channel", 404);
|
|
210
|
+
}
|
|
211
|
+
const { message } = await this.unpackMessage(picoId, jwe);
|
|
212
|
+
const senderDid = message.from;
|
|
213
|
+
if (!senderDid) {
|
|
214
|
+
throw new errors_1.IdentityError("DIDComm message missing from", 400);
|
|
215
|
+
}
|
|
216
|
+
if (message.type === skyProtocol_1.SKY_INTRO) {
|
|
217
|
+
return (0, skyIntro_1.handleSkyIntroAtIngress)(this.skyIntroDeps(), picoId, message);
|
|
218
|
+
}
|
|
219
|
+
if (message.type === skyProtocol_1.SKY_INTRO_RESPONSE) {
|
|
220
|
+
return (0, skyIntro_1.handleSkyIntroResponseAtIngress)(this.skyIntroDeps(), picoId, message);
|
|
221
|
+
}
|
|
222
|
+
// Channel policy (Epic 7): authenticated sender peer DID → subscription record →
|
|
223
|
+
// internal Rx channel. Door verify proves crypto + subscription relationship;
|
|
224
|
+
// pf.eventWait / pf.query enforce assertEventPolicy / assertQueryPolicy on rxEci.
|
|
225
|
+
const sub = await this.store.findPeerSubscriptionBySenderDid(picoId, senderDid);
|
|
226
|
+
if (!sub || !sub.rxEci) {
|
|
227
|
+
throw new errors_1.IdentityError("unknown_subscription", 404);
|
|
228
|
+
}
|
|
229
|
+
const callerDid = sub.remoteWebvhDid || senderDid;
|
|
230
|
+
if (message.type === skyProtocol_1.SKY_EVENT) {
|
|
231
|
+
const body = (0, skyProtocol_1.parseSkyEventBody)(message.body);
|
|
232
|
+
try {
|
|
233
|
+
const result = await this.pf.eventWait({
|
|
234
|
+
eci: sub.rxEci,
|
|
235
|
+
domain: body.domain,
|
|
236
|
+
name: body.name,
|
|
237
|
+
data: {
|
|
238
|
+
attrs: Object.assign(Object.assign({}, body.attrs), { callerDid }),
|
|
239
|
+
},
|
|
240
|
+
time: 0,
|
|
241
|
+
}, undefined);
|
|
242
|
+
return {
|
|
243
|
+
eid: result.eid,
|
|
244
|
+
responses: result.responses,
|
|
245
|
+
directives: result.responses,
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
catch (err) {
|
|
249
|
+
if (isPolicyError(err)) {
|
|
250
|
+
throw new errors_1.IdentityError("policy_denied", 403);
|
|
251
|
+
}
|
|
252
|
+
throw err;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
if (message.type === skyProtocol_1.SKY_QUERY) {
|
|
256
|
+
const body = (0, skyProtocol_1.parseSkyQueryBody)(message.body);
|
|
257
|
+
try {
|
|
258
|
+
const result = await this.pf.query({
|
|
259
|
+
eci: sub.rxEci,
|
|
260
|
+
rid: body.rid,
|
|
261
|
+
name: body.name,
|
|
262
|
+
args: body.args,
|
|
263
|
+
});
|
|
264
|
+
return { status: "ok", result };
|
|
265
|
+
}
|
|
266
|
+
catch (err) {
|
|
267
|
+
if (isPolicyError(err)) {
|
|
268
|
+
throw new errors_1.IdentityError("policy_denied", 403);
|
|
269
|
+
}
|
|
270
|
+
throw new errors_1.IdentityError(err.message || "query_failed", 502);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
const routes = await this.getRoutes(picoId);
|
|
274
|
+
const route = routes[message.type];
|
|
275
|
+
if (route) {
|
|
276
|
+
return { status: "routed", type: message.type };
|
|
277
|
+
}
|
|
278
|
+
throw new errors_1.IdentityError(`Unknown SKY message type: ${message.type}`, 400);
|
|
279
|
+
}
|
|
280
|
+
/** Resolve a DID from the local store, then via did:webvh remote resolution. */
|
|
281
|
+
async resolveDid(picoId, did) {
|
|
282
|
+
const normalized = did.trim();
|
|
283
|
+
const cached = await this.store.getDidDoc(picoId, normalized);
|
|
284
|
+
if (cached) {
|
|
285
|
+
return cached.doc;
|
|
286
|
+
}
|
|
287
|
+
if (normalized.startsWith("did:webvh:")) {
|
|
288
|
+
const localPicoId = await this.store.findPicoIdByWebvhDid(normalized);
|
|
289
|
+
if (localPicoId) {
|
|
290
|
+
const log = await this.store.getWebvhLog(localPicoId);
|
|
291
|
+
if (log && log.length > 0) {
|
|
292
|
+
const resolved = await (0, webvh_1.resolveLocalWebvhLog)(log);
|
|
293
|
+
await this.store.putDidDoc(picoId, {
|
|
294
|
+
did: resolved.did,
|
|
295
|
+
doc: resolved.doc,
|
|
296
|
+
cachedAt: new Date().toISOString(),
|
|
297
|
+
});
|
|
298
|
+
return resolved.doc;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
try {
|
|
302
|
+
const resolved = await (0, webvh_1.resolveWebvhDid)(this.store, normalized);
|
|
303
|
+
await this.store.putDidDoc(picoId, {
|
|
304
|
+
did: resolved.did,
|
|
305
|
+
doc: resolved.doc,
|
|
306
|
+
cachedAt: new Date().toISOString(),
|
|
307
|
+
});
|
|
308
|
+
return resolved.doc;
|
|
309
|
+
}
|
|
310
|
+
catch (err) {
|
|
311
|
+
const detail = err instanceof Error ? err.message : String(err);
|
|
312
|
+
const logUrl = (0, webvh_1.portableWebvhLogHttpUrl)(normalized);
|
|
313
|
+
throw new errors_1.IdentityError(`Remote did:webvh resolution failed${logUrl ? ` (${logUrl})` : ""}: ${detail}`, 404);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
if (normalized.startsWith("did:peer:")) {
|
|
317
|
+
const doc = await (0, veramoDidComm_1.resolvePeerDidDocument)(normalized);
|
|
318
|
+
await this.store.putDidDoc(picoId, {
|
|
319
|
+
did: normalized,
|
|
320
|
+
doc,
|
|
321
|
+
cachedAt: new Date().toISOString(),
|
|
322
|
+
});
|
|
323
|
+
return doc;
|
|
324
|
+
}
|
|
325
|
+
throw new errors_1.IdentityError(`DID not found in identity store: ${normalized}`, 404);
|
|
326
|
+
}
|
|
327
|
+
createInviteUrl(base64) {
|
|
328
|
+
const host = this.getBaseUrl();
|
|
329
|
+
if (host.indexOf("localhost") >= 0) {
|
|
330
|
+
return "http://example.com/invite?_oob=" + base64;
|
|
331
|
+
}
|
|
332
|
+
return host + "/invite?_oob=" + base64;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
exports.IdentityService = IdentityService;
|
|
336
|
+
function isPolicyError(err) {
|
|
337
|
+
return (err instanceof Error &&
|
|
338
|
+
(err.message.includes("channel policy") ||
|
|
339
|
+
err.message.includes("Denied by channel")));
|
|
340
|
+
}
|