@tinycloud/node-sdk 2.1.0 → 2.2.0-beta.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/dist/core.cjs CHANGED
@@ -20,9 +20,13 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/core.ts
21
21
  var core_exports = {};
22
22
  __export(core_exports, {
23
+ ACCOUNT_REGISTRY_PATH: () => import_sdk_core8.ACCOUNT_REGISTRY_PATH,
24
+ ACCOUNT_REGISTRY_SPACE: () => import_sdk_core8.ACCOUNT_REGISTRY_SPACE,
23
25
  AutoApproveSpaceCreationHandler: () => import_sdk_core7.AutoApproveSpaceCreationHandler,
24
26
  CapabilityKeyRegistry: () => import_sdk_core14.CapabilityKeyRegistry,
25
27
  CapabilityKeyRegistryErrorCodes: () => import_sdk_core14.CapabilityKeyRegistryErrorCodes,
28
+ DEFAULT_MANIFEST_SPACE: () => import_sdk_core8.DEFAULT_MANIFEST_SPACE,
29
+ DEFAULT_MANIFEST_VERSION: () => import_sdk_core8.DEFAULT_MANIFEST_VERSION,
26
30
  DataVaultService: () => import_sdk_core12.DataVaultService,
27
31
  DatabaseHandle: () => import_sdk_core10.DatabaseHandle,
28
32
  DelegatedAccess: () => DelegatedAccess,
@@ -57,6 +61,7 @@ __export(core_exports, {
57
61
  WasmKeyProvider: () => WasmKeyProvider,
58
62
  buildSpaceUri: () => import_sdk_core15.buildSpaceUri,
59
63
  checkNodeInfo: () => import_sdk_core16.checkNodeInfo,
64
+ composeManifestRequest: () => import_sdk_core8.composeManifestRequest,
60
65
  createCapabilityKeyRegistry: () => import_sdk_core14.createCapabilityKeyRegistry,
61
66
  createSharingService: () => import_sdk_core13.createSharingService,
62
67
  createSpaceService: () => import_sdk_core15.createSpaceService,
@@ -72,6 +77,7 @@ __export(core_exports, {
72
77
  parseExpiry: () => import_sdk_core8.parseExpiry,
73
78
  parseSpaceUri: () => import_sdk_core15.parseSpaceUri,
74
79
  resolveManifest: () => import_sdk_core8.resolveManifest,
80
+ resourceCapabilitiesToSpaceAbilitiesMap: () => import_sdk_core8.resourceCapabilitiesToSpaceAbilitiesMap,
75
81
  serializeDelegation: () => serializeDelegation,
76
82
  validateManifest: () => import_sdk_core8.validateManifest
77
83
  });
@@ -339,7 +345,9 @@ var NodeUserAuthorization = class {
339
345
  this.enablePublicSpace = config.enablePublicSpace ?? true;
340
346
  this.nonce = config.nonce;
341
347
  this.siweConfig = config.siweConfig;
348
+ this.includeAccountRegistryPermissions = config.includeAccountRegistryPermissions ?? true;
342
349
  this._manifest = config.manifest;
350
+ this._capabilityRequest = config.capabilityRequest;
343
351
  this.sessionManager = this.wasm.createSessionManager();
344
352
  }
345
353
  /**
@@ -351,12 +359,19 @@ var NodeUserAuthorization = class {
351
359
  get manifest() {
352
360
  return this._manifest;
353
361
  }
362
+ get capabilityRequest() {
363
+ return this.getCapabilityRequest();
364
+ }
354
365
  /**
355
366
  * Install or replace the stored manifest. Takes effect on the next
356
367
  * `signIn()` call — the current session (if any) is not touched.
357
368
  */
358
369
  setManifest(manifest) {
359
370
  this._manifest = manifest;
371
+ this._capabilityRequest = void 0;
372
+ }
373
+ setCapabilityRequest(request) {
374
+ this._capabilityRequest = request;
360
375
  }
361
376
  /**
362
377
  * The current active session (web-core compatible).
@@ -400,12 +415,51 @@ var NodeUserAuthorization = class {
400
415
  *
401
416
  * @internal
402
417
  */
403
- resolveSignInAbilities() {
418
+ getCapabilityRequest() {
419
+ if (this._capabilityRequest !== void 0) {
420
+ return this._capabilityRequest;
421
+ }
404
422
  if (this._manifest === void 0) {
405
- return this.defaultActions;
423
+ return void 0;
424
+ }
425
+ this._capabilityRequest = (0, import_sdk_core2.composeManifestRequest)(
426
+ Array.isArray(this._manifest) ? this._manifest : [this._manifest],
427
+ {
428
+ includeAccountRegistryPermissions: this.includeAccountRegistryPermissions
429
+ }
430
+ );
431
+ return this._capabilityRequest;
432
+ }
433
+ resolveSpaceName(space, address, chainId) {
434
+ if (space.startsWith("tinycloud:")) {
435
+ return space;
406
436
  }
407
- const resolved = (0, import_sdk_core2.resolveManifest)(this._manifest);
408
- return (0, import_sdk_core2.manifestAbilitiesUnion)(resolved);
437
+ return this.wasm.makeSpaceId(address, chainId, space);
438
+ }
439
+ resolveSignInCapabilities(address, chainId) {
440
+ const request = this.getCapabilityRequest();
441
+ if (request === void 0) {
442
+ return {
443
+ abilities: this.defaultActions,
444
+ spaceId: this.wasm.makeSpaceId(address, chainId, this.spacePrefix)
445
+ };
446
+ }
447
+ const primarySpaceName = request.resources.find((entry) => entry.space !== "account")?.space ?? import_sdk_core2.DEFAULT_MANIFEST_SPACE;
448
+ const primarySpaceId = this.resolveSpaceName(
449
+ primarySpaceName,
450
+ address,
451
+ chainId
452
+ );
453
+ const bySpace = (0, import_sdk_core2.resourceCapabilitiesToSpaceAbilitiesMap)(request.resources);
454
+ const spaceAbilities = {};
455
+ for (const [space, abilities] of Object.entries(bySpace)) {
456
+ spaceAbilities[this.resolveSpaceName(space, address, chainId)] = abilities;
457
+ }
458
+ return {
459
+ abilities: spaceAbilities[primarySpaceId] ?? (0, import_sdk_core2.resourceCapabilitiesToAbilitiesMap)([]),
460
+ spaceId: primarySpaceId,
461
+ spaceAbilities
462
+ };
409
463
  }
410
464
  /**
411
465
  * Build SIWE overrides from the top-level nonce and siweConfig.
@@ -486,6 +540,13 @@ var NodeUserAuthorization = class {
486
540
  async hostPublicSpace(spaceId) {
487
541
  return this.hostSpace(spaceId);
488
542
  }
543
+ /**
544
+ * Create a specific owned space on the server via host delegation.
545
+ * Used by manifest registry setup for the account space.
546
+ */
547
+ async hostOwnedSpace(spaceId) {
548
+ return this.hostSpace(spaceId);
549
+ }
489
550
  /**
490
551
  * Ensure the user's space exists on the TinyCloud server.
491
552
  * Creates the space if it doesn't exist and autoCreateSpace is enabled.
@@ -614,11 +675,13 @@ var NodeUserAuthorization = class {
614
675
  throw new Error("Failed to create session key");
615
676
  }
616
677
  const jwk = JSON.parse(jwkString);
617
- const spaceId = this.wasm.makeSpaceId(address, chainId, this.spacePrefix);
678
+ const capabilityPlan = this.resolveSignInCapabilities(address, chainId);
679
+ const spaceId = capabilityPlan.spaceId;
618
680
  const now = /* @__PURE__ */ new Date();
619
681
  const expirationTime = new Date(now.getTime() + this.sessionExpirationMs);
620
682
  const prepared = this.wasm.prepareSession({
621
- abilities: this.resolveSignInAbilities(),
683
+ abilities: capabilityPlan.abilities,
684
+ ...capabilityPlan.spaceAbilities !== void 0 ? { spaceAbilities: capabilityPlan.spaceAbilities } : {},
622
685
  address,
623
686
  chainId,
624
687
  domain: this.domain,
@@ -757,11 +820,13 @@ var NodeUserAuthorization = class {
757
820
  throw new Error("Failed to create session key");
758
821
  }
759
822
  const jwk = JSON.parse(jwkString);
760
- const spaceId = this.wasm.makeSpaceId(address, chainId, this.spacePrefix);
823
+ const capabilityPlan = this.resolveSignInCapabilities(address, chainId);
824
+ const spaceId = capabilityPlan.spaceId;
761
825
  const now = /* @__PURE__ */ new Date();
762
826
  const expirationTime = new Date(now.getTime() + this.sessionExpirationMs);
763
827
  const prepared = this.wasm.prepareSession({
764
- abilities: this.resolveSignInAbilities(),
828
+ abilities: capabilityPlan.abilities,
829
+ ...capabilityPlan.spaceAbilities !== void 0 ? { spaceAbilities: capabilityPlan.spaceAbilities } : {},
765
830
  address,
766
831
  chainId,
767
832
  domain: this.domain,
@@ -1271,7 +1336,9 @@ var _TinyCloudNode = class _TinyCloudNode {
1271
1336
  spaceCreationHandler: config.spaceCreationHandler,
1272
1337
  nonce: config.nonce,
1273
1338
  siweConfig: config.siweConfig,
1274
- manifest: config.manifest
1339
+ manifest: config.manifest,
1340
+ capabilityRequest: config.capabilityRequest,
1341
+ includeAccountRegistryPermissions: config.includeAccountRegistryPermissions
1275
1342
  });
1276
1343
  this.tc = new import_sdk_core5.TinyCloud(this.auth, {
1277
1344
  invokeAny: this.wasmBindings.invokeAny
@@ -1290,8 +1357,20 @@ var _TinyCloudNode = class _TinyCloudNode {
1290
1357
  "setManifest requires wallet mode. Provide a signer or privateKey in the TinyCloudNode config."
1291
1358
  );
1292
1359
  }
1360
+ this.config.manifest = manifest;
1361
+ this.config.capabilityRequest = void 0;
1293
1362
  this.auth.setManifest(manifest);
1294
1363
  }
1364
+ setCapabilityRequest(request) {
1365
+ if (!this.auth) {
1366
+ throw new Error(
1367
+ "setCapabilityRequest requires wallet mode. Provide a signer or privateKey in the TinyCloudNode config."
1368
+ );
1369
+ }
1370
+ this.config.capabilityRequest = request;
1371
+ this.config.manifest = request?.manifests;
1372
+ this.auth.setCapabilityRequest(request);
1373
+ }
1295
1374
  /**
1296
1375
  * Return the manifest currently installed on the auth handler,
1297
1376
  * or `undefined` if none is set.
@@ -1299,6 +1378,9 @@ var _TinyCloudNode = class _TinyCloudNode {
1299
1378
  get manifest() {
1300
1379
  return this.auth?.manifest;
1301
1380
  }
1381
+ get capabilityRequest() {
1382
+ return this.auth?.capabilityRequest;
1383
+ }
1302
1384
  /**
1303
1385
  * Get the primary identity DID for this user.
1304
1386
  * - If wallet connected and signed in: returns PKH DID (did:pkh:eip155:{chainId}:{address})
@@ -1372,8 +1454,39 @@ var _TinyCloudNode = class _TinyCloudNode {
1372
1454
  this._serviceContext = void 0;
1373
1455
  await this.tc.signIn(options);
1374
1456
  this.initializeServices();
1457
+ await this.writeManifestRegistryRecords();
1375
1458
  this.notificationHandler.success("Successfully signed in");
1376
1459
  }
1460
+ ownedSpaceId(name) {
1461
+ if (!this._address) {
1462
+ throw new Error("Cannot resolve owned space before sign-in");
1463
+ }
1464
+ return this.wasmBindings.makeSpaceId(this._address, this._chainId, name);
1465
+ }
1466
+ async writeManifestRegistryRecords() {
1467
+ const request = this.capabilityRequest;
1468
+ if (!request || request.registryRecords.length === 0) {
1469
+ return;
1470
+ }
1471
+ if (!this.auth || !this.signer) {
1472
+ throw new Error("Manifest registry write requires wallet mode");
1473
+ }
1474
+ const accountSpaceId = this.ownedSpaceId(import_sdk_core5.ACCOUNT_REGISTRY_SPACE);
1475
+ await this.auth.hostOwnedSpace(accountSpaceId);
1476
+ const accountKV = this.spaces.get(accountSpaceId).kv;
1477
+ for (const record of request.registryRecords) {
1478
+ const result = await accountKV.put(record.key, {
1479
+ app_id: record.app_id,
1480
+ manifests: record.manifests,
1481
+ updated_at: (/* @__PURE__ */ new Date()).toISOString()
1482
+ });
1483
+ if (!result.ok) {
1484
+ throw new Error(
1485
+ `Failed to write manifest registry record ${record.key}: ${result.error.message}`
1486
+ );
1487
+ }
1488
+ }
1489
+ }
1377
1490
  /**
1378
1491
  * Restore a previously established session from stored delegation data.
1379
1492
  *
@@ -1511,7 +1624,10 @@ var _TinyCloudNode = class _TinyCloudNode {
1511
1624
  enablePublicSpace: this.config.enablePublicSpace ?? true,
1512
1625
  spaceCreationHandler: this.config.spaceCreationHandler,
1513
1626
  nonce: this.config.nonce,
1514
- siweConfig: this.config.siweConfig
1627
+ siweConfig: this.config.siweConfig,
1628
+ manifest: this.config.manifest,
1629
+ capabilityRequest: this.config.capabilityRequest,
1630
+ includeAccountRegistryPermissions: this.config.includeAccountRegistryPermissions
1515
1631
  });
1516
1632
  this.tc = new import_sdk_core5.TinyCloud(this.auth, {
1517
1633
  invokeAny: this.wasmBindings.invokeAny
@@ -1551,7 +1667,10 @@ var _TinyCloudNode = class _TinyCloudNode {
1551
1667
  enablePublicSpace: this.config.enablePublicSpace ?? true,
1552
1668
  spaceCreationHandler: this.config.spaceCreationHandler,
1553
1669
  nonce: this.config.nonce,
1554
- siweConfig: this.config.siweConfig
1670
+ siweConfig: this.config.siweConfig,
1671
+ manifest: this.config.manifest,
1672
+ capabilityRequest: this.config.capabilityRequest,
1673
+ includeAccountRegistryPermissions: this.config.includeAccountRegistryPermissions
1555
1674
  });
1556
1675
  this.tc = new import_sdk_core5.TinyCloud(this.auth, {
1557
1676
  invokeAny: this.wasmBindings.invokeAny
@@ -2413,6 +2532,42 @@ var _TinyCloudNode = class _TinyCloudNode {
2413
2532
  );
2414
2533
  return { delegation, prompted: false };
2415
2534
  }
2535
+ /**
2536
+ * Materialize one manifest-declared delegation using the current session key.
2537
+ * Delivery is intentionally out of band; callers decide how to transmit the
2538
+ * returned UCAN to the delegate.
2539
+ */
2540
+ async materializeDelegation(did, request = this.capabilityRequest) {
2541
+ if (!request) {
2542
+ throw new Error(
2543
+ "materializeDelegation requires a composed manifest request"
2544
+ );
2545
+ }
2546
+ const target = request.delegationTargets.find((entry) => entry.did === did);
2547
+ if (!target) {
2548
+ throw new Error(`No manifest delegation target found for DID ${did}`);
2549
+ }
2550
+ const result = await this.delegateTo(target.did, target.permissions, {
2551
+ expiry: target.expiryMs
2552
+ });
2553
+ return { ...result, target };
2554
+ }
2555
+ /**
2556
+ * Materialize every delegation target declared by the composed manifest
2557
+ * request. This does not deliver the delegations anywhere.
2558
+ */
2559
+ async materializeDelegations(request = this.capabilityRequest) {
2560
+ if (!request) {
2561
+ throw new Error(
2562
+ "materializeDelegations requires a composed manifest request"
2563
+ );
2564
+ }
2565
+ const out = [];
2566
+ for (const target of request.delegationTargets) {
2567
+ out.push(await this.materializeDelegation(target.did, request));
2568
+ }
2569
+ return out;
2570
+ }
2416
2571
  /**
2417
2572
  * Issue a delegation via the session-key UCAN WASM path.
2418
2573
  *
@@ -2436,7 +2591,7 @@ var _TinyCloudNode = class _TinyCloudNode {
2436
2591
  }
2437
2592
  const resolvedSpaces = /* @__PURE__ */ new Set();
2438
2593
  for (const entry of entries) {
2439
- const spaceId2 = entry.space === "default" ? session.spaceId : entry.space;
2594
+ const spaceId2 = this.resolvePermissionSpace(entry.space, session);
2440
2595
  resolvedSpaces.add(spaceId2);
2441
2596
  }
2442
2597
  if (resolvedSpaces.size !== 1) {
@@ -2511,6 +2666,22 @@ var _TinyCloudNode = class _TinyCloudNode {
2511
2666
  host: this.config.host
2512
2667
  };
2513
2668
  }
2669
+ resolvePermissionSpace(space, session) {
2670
+ if (space === void 0) {
2671
+ return this.wasmBindings.makeSpaceId(
2672
+ session.address,
2673
+ session.chainId,
2674
+ "applications"
2675
+ );
2676
+ }
2677
+ if (space === "default") {
2678
+ return session.spaceId;
2679
+ }
2680
+ if (space.startsWith("tinycloud:")) {
2681
+ return space;
2682
+ }
2683
+ return this.wasmBindings.makeSpaceId(session.address, session.chainId, space);
2684
+ }
2514
2685
  /**
2515
2686
  * Issue a delegation via the legacy wallet-signed SIWE path for a single
2516
2687
  * {@link PermissionEntry}. Shares the implementation with the public
@@ -2521,7 +2692,8 @@ var _TinyCloudNode = class _TinyCloudNode {
2521
2692
  * @internal
2522
2693
  */
2523
2694
  async createDelegationLegacyWalletPath(delegateDID, entry, expirationTime) {
2524
- const spaceIdOverride = entry.space === "default" ? void 0 : entry.space;
2695
+ const session = this.auth?.tinyCloudSession;
2696
+ const spaceIdOverride = session === void 0 || entry.space === "default" ? void 0 : this.resolvePermissionSpace(entry.space, session);
2525
2697
  return this.createDelegationWalletPath({
2526
2698
  path: entry.path,
2527
2699
  actions: entry.actions,
@@ -2922,9 +3094,13 @@ var import_sdk_core16 = require("@tinycloud/sdk-core");
2922
3094
  var import_sdk_core17 = require("@tinycloud/sdk-core");
2923
3095
  // Annotate the CommonJS export names for ESM import in node:
2924
3096
  0 && (module.exports = {
3097
+ ACCOUNT_REGISTRY_PATH,
3098
+ ACCOUNT_REGISTRY_SPACE,
2925
3099
  AutoApproveSpaceCreationHandler,
2926
3100
  CapabilityKeyRegistry,
2927
3101
  CapabilityKeyRegistryErrorCodes,
3102
+ DEFAULT_MANIFEST_SPACE,
3103
+ DEFAULT_MANIFEST_VERSION,
2928
3104
  DataVaultService,
2929
3105
  DatabaseHandle,
2930
3106
  DelegatedAccess,
@@ -2959,6 +3135,7 @@ var import_sdk_core17 = require("@tinycloud/sdk-core");
2959
3135
  WasmKeyProvider,
2960
3136
  buildSpaceUri,
2961
3137
  checkNodeInfo,
3138
+ composeManifestRequest,
2962
3139
  createCapabilityKeyRegistry,
2963
3140
  createSharingService,
2964
3141
  createSpaceService,
@@ -2974,6 +3151,7 @@ var import_sdk_core17 = require("@tinycloud/sdk-core");
2974
3151
  parseExpiry,
2975
3152
  parseSpaceUri,
2976
3153
  resolveManifest,
3154
+ resourceCapabilitiesToSpaceAbilitiesMap,
2977
3155
  serializeDelegation,
2978
3156
  validateManifest
2979
3157
  });