pico-engine 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/README.md +41 -0
  2. package/dist/RulesetRegistryLoaderFs.js +2 -0
  3. package/dist/auth/AuthService.js +77 -13
  4. package/dist/bundledKrl.js +42 -0
  5. package/dist/identity/IdentityService.js +340 -0
  6. package/dist/identity/crossPico.js +238 -0
  7. package/dist/identity/dbHelpers.js +67 -0
  8. package/dist/identity/didcommIngressRoutes.js +44 -0
  9. package/dist/identity/didoModule.js +252 -0
  10. package/dist/identity/errors.js +11 -0
  11. package/dist/identity/establishSubscription.js +97 -0
  12. package/dist/identity/index.js +15 -0
  13. package/dist/identity/peerDid.js +9 -0
  14. package/dist/identity/skyIntro.js +194 -0
  15. package/dist/identity/skyProtocol.js +105 -0
  16. package/dist/identity/store.js +233 -0
  17. package/dist/identity/types.js +13 -0
  18. package/dist/identity/veramoDidComm.js +260 -0
  19. package/dist/identity/webvh.js +266 -0
  20. package/dist/identity/webvhCrypto.js +128 -0
  21. package/dist/identity/webvhRoutes.js +61 -0
  22. package/dist/index.js +26 -2
  23. package/dist/oauth/OAuthService.js +32 -0
  24. package/dist/provisionRoot.js +56 -18
  25. package/dist/server.js +52 -1
  26. package/dist/uiChannelPolicies.js +48 -0
  27. package/krl/io.picolabs.did-o.krl +5 -1
  28. package/krl/io.picolabs.pico-engine-ui.krl +106 -18
  29. package/krl/io.picolabs.subscription.krl +242 -23
  30. package/krl/io.picolabs.wrangler.krl +221 -12
  31. package/package.json +29 -6
  32. package/public/docs/krl/_sidebar.md +1 -0
  33. package/public/docs/krl/discovery.md +144 -0
  34. package/public/docs/krl/modules/ctx.md +8 -0
  35. package/public/docs/krl/modules/event.md +41 -0
  36. package/public/pico-engine-ui.css +1 -1
  37. package/public/pico-engine-ui.js +21 -18
  38. package/src/RulesetRegistryLoaderFs.ts +2 -0
  39. package/src/auth/AuthService.ts +109 -14
  40. package/src/bundledKrl.ts +42 -0
  41. package/src/identity/IdentityService.ts +571 -0
  42. package/src/identity/crossPico.ts +371 -0
  43. package/src/identity/dbHelpers.ts +51 -0
  44. package/src/identity/didcommIngressRoutes.ts +54 -0
  45. package/src/identity/didoModule.ts +388 -0
  46. package/src/identity/errors.ts +8 -0
  47. package/src/identity/establishSubscription.ts +160 -0
  48. package/src/identity/index.ts +24 -0
  49. package/src/identity/peerDid.ts +22 -0
  50. package/src/identity/skyIntro.ts +321 -0
  51. package/src/identity/skyProtocol.ts +192 -0
  52. package/src/identity/store.ts +343 -0
  53. package/src/identity/types.ts +63 -0
  54. package/src/identity/veramoDidComm.ts +363 -0
  55. package/src/identity/webvh.ts +325 -0
  56. package/src/identity/webvhCrypto.ts +200 -0
  57. package/src/identity/webvhRoutes.ts +64 -0
  58. package/src/index.ts +31 -2
  59. package/src/oauth/OAuthService.ts +36 -0
  60. package/src/provisionRoot.ts +75 -21
  61. package/src/server.ts +58 -1
  62. package/src/uiChannelPolicies.ts +49 -0
@@ -6,6 +6,7 @@ import * as path from "path";
6
6
  import { RulesetRegistryLoader } from "pico-engine-core";
7
7
  import { PicoDbKey, Ruleset } from "pico-framework";
8
8
  import * as urlLib from "url";
9
+ import { resolveBundledKrlUrl } from "./bundledKrl";
9
10
  const krlCompiler = require("krl-compiler");
10
11
  const krlCompilerVersion = require("krl-compiler/package.json").version;
11
12
  const charwise = require("charwise");
@@ -127,6 +128,7 @@ export function RulesetRegistryLoaderFs(
127
128
  }
128
129
 
129
130
  async function fetchKrl(url: string): Promise<string> {
131
+ url = resolveBundledKrlUrl(url);
130
132
  const urlParsed = urlLib.parse(url);
131
133
  if (urlParsed.protocol === "file:") {
132
134
  return new Promise((resolve, reject) => {
@@ -32,7 +32,7 @@ export interface StoredAccount {
32
32
  accountId: string;
33
33
  rootPicoId: string;
34
34
  displayName: string;
35
- /** Stable WebAuthn `user.name`; legacy accounts omit this (displayName was used). */
35
+ /** @deprecated Stable opaque id; passkeys now use displayName as WebAuthn user.name. */
36
36
  webauthnUserName?: string;
37
37
  createdAt: string;
38
38
  }
@@ -58,6 +58,8 @@ export interface CredentialInfo {
58
58
  export interface InviteInfo {
59
59
  token: string;
60
60
  label?: string;
61
+ bootstrapUrl?: string;
62
+ bootstrapRid?: string;
61
63
  createdAt: string;
62
64
  expiresAt: number;
63
65
  }
@@ -65,6 +67,8 @@ export interface InviteInfo {
65
67
  export interface InvitePeek {
66
68
  valid: boolean;
67
69
  label?: string;
70
+ bootstrapUrl?: string;
71
+ bootstrapRid?: string;
68
72
  expiresAt?: number;
69
73
  }
70
74
 
@@ -72,6 +76,8 @@ interface StoredInvite {
72
76
  token: string;
73
77
  createdByAccountId: string;
74
78
  label?: string;
79
+ bootstrapUrl?: string;
80
+ bootstrapRid?: string;
75
81
  createdAt: string;
76
82
  expiresAt: number;
77
83
  }
@@ -121,9 +127,13 @@ export interface AuthServiceDeps {
121
127
  sessionTtlMs?: number;
122
128
  inviteTtlMs?: number;
123
129
 
130
+ /** Compile a bootstrap ruleset URL (invite creation validation). */
131
+ flushBootstrapUrl: (url: string) => Promise<{ rid: string }>;
132
+
124
133
  /** Mint a brand new root pico (installs base rulesets) for a new account. */
125
134
  provisionRoot: (opts?: {
126
135
  name?: string;
136
+ bootstrapUrl?: string;
127
137
  }) => Promise<{ rootPicoId: string; uiECI: string }>;
128
138
  /** Resolve the ["engine","ui"] ECI for an existing root. */
129
139
  getUiECI: (rootPicoId: string) => Promise<string | null> | string | null;
@@ -157,13 +167,29 @@ export class AuthService {
157
167
  } catch (_e) {
158
168
  // fall through to defaults
159
169
  }
170
+ const originLabel = this.originLabelFromUrl(origin);
171
+ const rpName =
172
+ this.deps.rpName ||
173
+ (originLabel
174
+ ? `${DEFAULT_RP_NAME} (${originLabel})`
175
+ : DEFAULT_RP_NAME);
160
176
  return {
161
177
  rpID: this.deps.rpID || hostname,
162
- rpName: this.deps.rpName || DEFAULT_RP_NAME,
178
+ rpName,
163
179
  origin: this.deps.origin || origin,
164
180
  };
165
181
  }
166
182
 
183
+ /** Host[:port] label for disambiguating local multi-engine setups in WebAuthn UI. */
184
+ private originLabelFromUrl(origin: string): string {
185
+ try {
186
+ const url = new URL(origin);
187
+ return url.port ? `${url.hostname}:${url.port}` : url.hostname;
188
+ } catch (_e) {
189
+ return "";
190
+ }
191
+ }
192
+
167
193
  isSecure(): boolean {
168
194
  return this.rp().origin.startsWith("https:");
169
195
  }
@@ -194,13 +220,17 @@ export class AuthService {
194
220
  }
195
221
  const { rpID, rpName } = this.rp();
196
222
  const accountId = randomId();
197
- const displayName = (input.displayName || "").trim() || `pico-${accountId.slice(0, 6)}`;
223
+ const displayName = await this.resolveRegistrationDisplayName(
224
+ accountId,
225
+ input.displayName
226
+ );
227
+ const { userName, userDisplayName } = this.webauthnUserFields(displayName);
198
228
  const options = await this.deps.webauthn.generateRegistrationOptions({
199
229
  rpID,
200
230
  rpName,
201
231
  userID: new TextEncoder().encode(accountId),
202
- userName: accountId,
203
- userDisplayName: displayName,
232
+ userName,
233
+ userDisplayName,
204
234
  excludeCredentials: [],
205
235
  });
206
236
  const ceremonyId = this.putCeremony({
@@ -246,11 +276,10 @@ export class AuthService {
246
276
  accountId: cer.accountId,
247
277
  rootPicoId: cer.rootPicoId,
248
278
  displayName: cer.displayName,
249
- webauthnUserName: cer.accountId,
250
279
  createdAt: new Date().toISOString(),
251
280
  };
252
281
  await this.deps.db.put([ACCOUNT_PREFIX, account.accountId], account);
253
- await this.storeCredential(account.accountId, result.credential, "passkey");
282
+ await this.storeCredential(account.accountId, result.credential, cer.displayName);
254
283
 
255
284
  if (this.deps.onAccountClaimed) {
256
285
  await this.deps.onAccountClaimed(cer.rootPicoId, cer.displayName);
@@ -269,7 +298,21 @@ export class AuthService {
269
298
  async createInvite(input: {
270
299
  createdByAccountId: string;
271
300
  label?: string;
301
+ bootstrapUrl?: string;
272
302
  }): Promise<InviteInfo> {
303
+ const bootstrapUrl = (input.bootstrapUrl || "").trim() || undefined;
304
+ let bootstrapRid: string | undefined;
305
+ if (bootstrapUrl) {
306
+ try {
307
+ const flushed = await this.deps.flushBootstrapUrl(bootstrapUrl);
308
+ bootstrapRid = flushed.rid;
309
+ } catch (err) {
310
+ const detail =
311
+ err instanceof Error ? err.message : "Bootstrap ruleset URL could not be loaded";
312
+ throw new AuthError(`Invalid bootstrap ruleset URL: ${detail}`, 400);
313
+ }
314
+ }
315
+
273
316
  const token = randomToken();
274
317
  const now = Date.now();
275
318
  const ttl = this.deps.inviteTtlMs ?? DEFAULT_INVITE_TTL_MS;
@@ -277,6 +320,8 @@ export class AuthService {
277
320
  token,
278
321
  createdByAccountId: input.createdByAccountId,
279
322
  label: (input.label || "").trim() || undefined,
323
+ bootstrapUrl,
324
+ bootstrapRid,
280
325
  createdAt: new Date(now).toISOString(),
281
326
  expiresAt: now + ttl,
282
327
  };
@@ -284,6 +329,8 @@ export class AuthService {
284
329
  return {
285
330
  token: invite.token,
286
331
  label: invite.label,
332
+ bootstrapUrl: invite.bootstrapUrl,
333
+ bootstrapRid: invite.bootstrapRid,
287
334
  createdAt: invite.createdAt,
288
335
  expiresAt: invite.expiresAt,
289
336
  };
@@ -300,6 +347,8 @@ export class AuthService {
300
347
  return {
301
348
  valid: true,
302
349
  label: invite.label,
350
+ bootstrapUrl: invite.bootstrapUrl,
351
+ bootstrapRid: invite.bootstrapRid,
303
352
  expiresAt: invite.expiresAt,
304
353
  };
305
354
  }
@@ -321,13 +370,18 @@ export class AuthService {
321
370
 
322
371
  const { rpID, rpName } = this.rp();
323
372
  const accountId = randomId();
324
- const displayName = (input.displayName || "").trim() || `pico-${accountId.slice(0, 6)}`;
373
+ const displayName = await this.resolveRegistrationDisplayName(
374
+ accountId,
375
+ input.displayName,
376
+ inviteToken
377
+ );
378
+ const { userName, userDisplayName } = this.webauthnUserFields(displayName);
325
379
  const options = await this.deps.webauthn.generateRegistrationOptions({
326
380
  rpID,
327
381
  rpName,
328
382
  userID: new TextEncoder().encode(accountId),
329
- userName: accountId,
330
- userDisplayName: displayName,
383
+ userName,
384
+ userDisplayName,
331
385
  excludeCredentials: [],
332
386
  });
333
387
  const ceremonyId = this.putCeremony({
@@ -365,20 +419,26 @@ export class AuthService {
365
419
  throw new AuthError("Registration could not be verified", 400);
366
420
  }
367
421
 
422
+ let bootstrapUrl: string | undefined;
423
+ if (cer.inviteToken) {
424
+ const invite = await this.getInvite(cer.inviteToken);
425
+ bootstrapUrl = invite?.bootstrapUrl;
426
+ }
427
+
368
428
  // Provision the account's root only after the ceremony verifies.
369
429
  const { rootPicoId, uiECI } = await this.deps.provisionRoot({
370
430
  name: cer.displayName,
431
+ bootstrapUrl,
371
432
  });
372
433
 
373
434
  const account: StoredAccount = {
374
435
  accountId: cer.accountId,
375
436
  rootPicoId,
376
437
  displayName: cer.displayName,
377
- webauthnUserName: cer.accountId,
378
438
  createdAt: new Date().toISOString(),
379
439
  };
380
440
  await this.deps.db.put([ACCOUNT_PREFIX, account.accountId], account);
381
- await this.storeCredential(account.accountId, result.credential, "passkey");
441
+ await this.storeCredential(account.accountId, result.credential, cer.displayName);
382
442
 
383
443
  if (cer.inviteToken) {
384
444
  await this.consumeInvite(cer.inviteToken);
@@ -455,13 +515,13 @@ export class AuthService {
455
515
  }
456
516
  const { rpID, rpName } = this.rp();
457
517
  const existing = await this.listStoredCredentials(input.accountId);
458
- const userName = account.webauthnUserName || account.displayName;
518
+ const { userName, userDisplayName } = this.webauthnUserFields(account.displayName);
459
519
  const options = await this.deps.webauthn.generateRegistrationOptions({
460
520
  rpID,
461
521
  rpName,
462
522
  userID: new TextEncoder().encode(account.accountId),
463
523
  userName,
464
- userDisplayName: account.displayName,
524
+ userDisplayName,
465
525
  excludeCredentials: existing.map((c) => ({
466
526
  id: c.credentialID,
467
527
  transports: c.transports,
@@ -592,6 +652,41 @@ export class AuthService {
592
652
 
593
653
  // ---- stores ----
594
654
 
655
+ /** Mesh name for registration: explicit input, else invite label, else generated. */
656
+ private async resolveRegistrationDisplayName(
657
+ accountId: string,
658
+ inputDisplayName?: string,
659
+ inviteToken?: string
660
+ ): Promise<string> {
661
+ const fromInput = (inputDisplayName || "").trim();
662
+ if (fromInput) {
663
+ return fromInput;
664
+ }
665
+ const token = (inviteToken || "").trim();
666
+ if (token) {
667
+ const invite = await this.getInvite(token);
668
+ const fromInvite = (invite?.label || "").trim();
669
+ if (fromInvite) {
670
+ return fromInvite;
671
+ }
672
+ }
673
+ return `pico-${accountId.slice(0, 6)}`;
674
+ }
675
+
676
+ /** WebAuthn user.name is what password managers (e.g. 1Password) show alongside the site. */
677
+ private webauthnUserFields(displayName: string): {
678
+ userName: string;
679
+ userDisplayName: string;
680
+ } {
681
+ const name = (displayName || "").trim() || "My mesh";
682
+ const originLabel = this.originLabelFromUrl(this.rp().origin);
683
+ const unique =
684
+ originLabel && !name.includes(originLabel)
685
+ ? `${name} (${originLabel})`
686
+ : name;
687
+ return { userName: unique, userDisplayName: unique };
688
+ }
689
+
595
690
  private async assertRegistrationAllowed(inviteToken?: string): Promise<void> {
596
691
  const count = await this.accountCount();
597
692
  if (count === 0) {
@@ -0,0 +1,42 @@
1
+ import * as fs from "fs";
2
+ import * as path from "path";
3
+ import { toFileUrl } from "./utils/toFileUrl";
4
+
5
+ /**
6
+ * OS-layer rulesets shipped inside the pico-engine package. On flush, these
7
+ * always reload from the running engine's krl/ directory so dev edits apply
8
+ * even when the pico was provisioned from a different absolute file:// path.
9
+ */
10
+ export const BUNDLED_KRL_FILES = [
11
+ "io.picolabs.pico-engine-ui.krl",
12
+ "io.picolabs.wrangler.krl",
13
+ "io.picolabs.subscription.krl",
14
+ "io.picolabs.pds.krl",
15
+ ];
16
+
17
+ export function bundledKrlDir(): string {
18
+ return path.resolve(__dirname, "..", "krl");
19
+ }
20
+
21
+ export function resolveBundledKrlUrl(url: string): string {
22
+ let filename: string | null = null;
23
+ try {
24
+ const parsed = new URL(url);
25
+ if (parsed.protocol === "file:") {
26
+ filename = path.basename(parsed.pathname);
27
+ }
28
+ } catch {
29
+ return url;
30
+ }
31
+
32
+ if (!filename || !BUNDLED_KRL_FILES.includes(filename)) {
33
+ return url;
34
+ }
35
+
36
+ const localPath = path.join(bundledKrlDir(), filename);
37
+ if (fs.existsSync(localPath)) {
38
+ return toFileUrl(localPath);
39
+ }
40
+
41
+ return url;
42
+ }