@tinycloud/node-sdk 2.1.0-beta.6 → 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/index.cjs CHANGED
@@ -17025,9 +17025,13 @@ var require_utils2 = __commonJS({
17025
17025
  // src/index.ts
17026
17026
  var index_exports = {};
17027
17027
  __export(index_exports, {
17028
+ ACCOUNT_REGISTRY_PATH: () => import_sdk_core8.ACCOUNT_REGISTRY_PATH,
17029
+ ACCOUNT_REGISTRY_SPACE: () => import_sdk_core8.ACCOUNT_REGISTRY_SPACE,
17028
17030
  AutoApproveSpaceCreationHandler: () => import_sdk_core7.AutoApproveSpaceCreationHandler,
17029
17031
  CapabilityKeyRegistry: () => import_sdk_core15.CapabilityKeyRegistry,
17030
17032
  CapabilityKeyRegistryErrorCodes: () => import_sdk_core15.CapabilityKeyRegistryErrorCodes,
17033
+ DEFAULT_MANIFEST_SPACE: () => import_sdk_core8.DEFAULT_MANIFEST_SPACE,
17034
+ DEFAULT_MANIFEST_VERSION: () => import_sdk_core8.DEFAULT_MANIFEST_VERSION,
17031
17035
  DataVaultService: () => import_sdk_core12.DataVaultService,
17032
17036
  DatabaseHandle: () => import_sdk_core10.DatabaseHandle,
17033
17037
  DelegatedAccess: () => DelegatedAccess,
@@ -17065,6 +17069,7 @@ __export(index_exports, {
17065
17069
  WasmKeyProvider: () => WasmKeyProvider,
17066
17070
  buildSpaceUri: () => import_sdk_core16.buildSpaceUri,
17067
17071
  checkNodeInfo: () => import_sdk_core17.checkNodeInfo,
17072
+ composeManifestRequest: () => import_sdk_core8.composeManifestRequest,
17068
17073
  createCapabilityKeyRegistry: () => import_sdk_core15.createCapabilityKeyRegistry,
17069
17074
  createSharingService: () => import_sdk_core14.createSharingService,
17070
17075
  createSpaceService: () => import_sdk_core16.createSpaceService,
@@ -17080,6 +17085,7 @@ __export(index_exports, {
17080
17085
  parseExpiry: () => import_sdk_core8.parseExpiry,
17081
17086
  parseSpaceUri: () => import_sdk_core16.parseSpaceUri,
17082
17087
  resolveManifest: () => import_sdk_core8.resolveManifest,
17088
+ resourceCapabilitiesToSpaceAbilitiesMap: () => import_sdk_core8.resourceCapabilitiesToSpaceAbilitiesMap,
17083
17089
  serializeDelegation: () => serializeDelegation,
17084
17090
  validateManifest: () => import_sdk_core8.validateManifest
17085
17091
  });
@@ -17327,7 +17333,9 @@ var NodeUserAuthorization = class {
17327
17333
  this.enablePublicSpace = config.enablePublicSpace ?? true;
17328
17334
  this.nonce = config.nonce;
17329
17335
  this.siweConfig = config.siweConfig;
17336
+ this.includeAccountRegistryPermissions = config.includeAccountRegistryPermissions ?? true;
17330
17337
  this._manifest = config.manifest;
17338
+ this._capabilityRequest = config.capabilityRequest;
17331
17339
  this.sessionManager = this.wasm.createSessionManager();
17332
17340
  }
17333
17341
  /**
@@ -17339,12 +17347,19 @@ var NodeUserAuthorization = class {
17339
17347
  get manifest() {
17340
17348
  return this._manifest;
17341
17349
  }
17350
+ get capabilityRequest() {
17351
+ return this.getCapabilityRequest();
17352
+ }
17342
17353
  /**
17343
17354
  * Install or replace the stored manifest. Takes effect on the next
17344
17355
  * `signIn()` call — the current session (if any) is not touched.
17345
17356
  */
17346
17357
  setManifest(manifest) {
17347
17358
  this._manifest = manifest;
17359
+ this._capabilityRequest = void 0;
17360
+ }
17361
+ setCapabilityRequest(request) {
17362
+ this._capabilityRequest = request;
17348
17363
  }
17349
17364
  /**
17350
17365
  * The current active session (web-core compatible).
@@ -17388,12 +17403,51 @@ var NodeUserAuthorization = class {
17388
17403
  *
17389
17404
  * @internal
17390
17405
  */
17391
- resolveSignInAbilities() {
17406
+ getCapabilityRequest() {
17407
+ if (this._capabilityRequest !== void 0) {
17408
+ return this._capabilityRequest;
17409
+ }
17392
17410
  if (this._manifest === void 0) {
17393
- return this.defaultActions;
17411
+ return void 0;
17412
+ }
17413
+ this._capabilityRequest = (0, import_sdk_core.composeManifestRequest)(
17414
+ Array.isArray(this._manifest) ? this._manifest : [this._manifest],
17415
+ {
17416
+ includeAccountRegistryPermissions: this.includeAccountRegistryPermissions
17417
+ }
17418
+ );
17419
+ return this._capabilityRequest;
17420
+ }
17421
+ resolveSpaceName(space, address, chainId) {
17422
+ if (space.startsWith("tinycloud:")) {
17423
+ return space;
17394
17424
  }
17395
- const resolved = (0, import_sdk_core.resolveManifest)(this._manifest);
17396
- return (0, import_sdk_core.manifestAbilitiesUnion)(resolved);
17425
+ return this.wasm.makeSpaceId(address, chainId, space);
17426
+ }
17427
+ resolveSignInCapabilities(address, chainId) {
17428
+ const request = this.getCapabilityRequest();
17429
+ if (request === void 0) {
17430
+ return {
17431
+ abilities: this.defaultActions,
17432
+ spaceId: this.wasm.makeSpaceId(address, chainId, this.spacePrefix)
17433
+ };
17434
+ }
17435
+ const primarySpaceName = request.resources.find((entry) => entry.space !== "account")?.space ?? import_sdk_core.DEFAULT_MANIFEST_SPACE;
17436
+ const primarySpaceId = this.resolveSpaceName(
17437
+ primarySpaceName,
17438
+ address,
17439
+ chainId
17440
+ );
17441
+ const bySpace = (0, import_sdk_core.resourceCapabilitiesToSpaceAbilitiesMap)(request.resources);
17442
+ const spaceAbilities = {};
17443
+ for (const [space, abilities] of Object.entries(bySpace)) {
17444
+ spaceAbilities[this.resolveSpaceName(space, address, chainId)] = abilities;
17445
+ }
17446
+ return {
17447
+ abilities: spaceAbilities[primarySpaceId] ?? (0, import_sdk_core.resourceCapabilitiesToAbilitiesMap)([]),
17448
+ spaceId: primarySpaceId,
17449
+ spaceAbilities
17450
+ };
17397
17451
  }
17398
17452
  /**
17399
17453
  * Build SIWE overrides from the top-level nonce and siweConfig.
@@ -17474,6 +17528,13 @@ var NodeUserAuthorization = class {
17474
17528
  async hostPublicSpace(spaceId) {
17475
17529
  return this.hostSpace(spaceId);
17476
17530
  }
17531
+ /**
17532
+ * Create a specific owned space on the server via host delegation.
17533
+ * Used by manifest registry setup for the account space.
17534
+ */
17535
+ async hostOwnedSpace(spaceId) {
17536
+ return this.hostSpace(spaceId);
17537
+ }
17477
17538
  /**
17478
17539
  * Ensure the user's space exists on the TinyCloud server.
17479
17540
  * Creates the space if it doesn't exist and autoCreateSpace is enabled.
@@ -17602,11 +17663,13 @@ var NodeUserAuthorization = class {
17602
17663
  throw new Error("Failed to create session key");
17603
17664
  }
17604
17665
  const jwk = JSON.parse(jwkString);
17605
- const spaceId = this.wasm.makeSpaceId(address, chainId, this.spacePrefix);
17666
+ const capabilityPlan = this.resolveSignInCapabilities(address, chainId);
17667
+ const spaceId = capabilityPlan.spaceId;
17606
17668
  const now = /* @__PURE__ */ new Date();
17607
17669
  const expirationTime = new Date(now.getTime() + this.sessionExpirationMs);
17608
17670
  const prepared = this.wasm.prepareSession({
17609
- abilities: this.resolveSignInAbilities(),
17671
+ abilities: capabilityPlan.abilities,
17672
+ ...capabilityPlan.spaceAbilities !== void 0 ? { spaceAbilities: capabilityPlan.spaceAbilities } : {},
17610
17673
  address,
17611
17674
  chainId,
17612
17675
  domain: this.domain,
@@ -17745,11 +17808,13 @@ var NodeUserAuthorization = class {
17745
17808
  throw new Error("Failed to create session key");
17746
17809
  }
17747
17810
  const jwk = JSON.parse(jwkString);
17748
- const spaceId = this.wasm.makeSpaceId(address, chainId, this.spacePrefix);
17811
+ const capabilityPlan = this.resolveSignInCapabilities(address, chainId);
17812
+ const spaceId = capabilityPlan.spaceId;
17749
17813
  const now = /* @__PURE__ */ new Date();
17750
17814
  const expirationTime = new Date(now.getTime() + this.sessionExpirationMs);
17751
17815
  const prepared = this.wasm.prepareSession({
17752
- abilities: this.resolveSignInAbilities(),
17816
+ abilities: capabilityPlan.abilities,
17817
+ ...capabilityPlan.spaceAbilities !== void 0 ? { spaceAbilities: capabilityPlan.spaceAbilities } : {},
17753
17818
  address,
17754
17819
  chainId,
17755
17820
  domain: this.domain,
@@ -18256,7 +18321,9 @@ var _TinyCloudNode = class _TinyCloudNode {
18256
18321
  spaceCreationHandler: config.spaceCreationHandler,
18257
18322
  nonce: config.nonce,
18258
18323
  siweConfig: config.siweConfig,
18259
- manifest: config.manifest
18324
+ manifest: config.manifest,
18325
+ capabilityRequest: config.capabilityRequest,
18326
+ includeAccountRegistryPermissions: config.includeAccountRegistryPermissions
18260
18327
  });
18261
18328
  this.tc = new import_sdk_core4.TinyCloud(this.auth, {
18262
18329
  invokeAny: this.wasmBindings.invokeAny
@@ -18275,8 +18342,20 @@ var _TinyCloudNode = class _TinyCloudNode {
18275
18342
  "setManifest requires wallet mode. Provide a signer or privateKey in the TinyCloudNode config."
18276
18343
  );
18277
18344
  }
18345
+ this.config.manifest = manifest;
18346
+ this.config.capabilityRequest = void 0;
18278
18347
  this.auth.setManifest(manifest);
18279
18348
  }
18349
+ setCapabilityRequest(request) {
18350
+ if (!this.auth) {
18351
+ throw new Error(
18352
+ "setCapabilityRequest requires wallet mode. Provide a signer or privateKey in the TinyCloudNode config."
18353
+ );
18354
+ }
18355
+ this.config.capabilityRequest = request;
18356
+ this.config.manifest = request?.manifests;
18357
+ this.auth.setCapabilityRequest(request);
18358
+ }
18280
18359
  /**
18281
18360
  * Return the manifest currently installed on the auth handler,
18282
18361
  * or `undefined` if none is set.
@@ -18284,6 +18363,9 @@ var _TinyCloudNode = class _TinyCloudNode {
18284
18363
  get manifest() {
18285
18364
  return this.auth?.manifest;
18286
18365
  }
18366
+ get capabilityRequest() {
18367
+ return this.auth?.capabilityRequest;
18368
+ }
18287
18369
  /**
18288
18370
  * Get the primary identity DID for this user.
18289
18371
  * - If wallet connected and signed in: returns PKH DID (did:pkh:eip155:{chainId}:{address})
@@ -18357,8 +18439,39 @@ var _TinyCloudNode = class _TinyCloudNode {
18357
18439
  this._serviceContext = void 0;
18358
18440
  await this.tc.signIn(options);
18359
18441
  this.initializeServices();
18442
+ await this.writeManifestRegistryRecords();
18360
18443
  this.notificationHandler.success("Successfully signed in");
18361
18444
  }
18445
+ ownedSpaceId(name) {
18446
+ if (!this._address) {
18447
+ throw new Error("Cannot resolve owned space before sign-in");
18448
+ }
18449
+ return this.wasmBindings.makeSpaceId(this._address, this._chainId, name);
18450
+ }
18451
+ async writeManifestRegistryRecords() {
18452
+ const request = this.capabilityRequest;
18453
+ if (!request || request.registryRecords.length === 0) {
18454
+ return;
18455
+ }
18456
+ if (!this.auth || !this.signer) {
18457
+ throw new Error("Manifest registry write requires wallet mode");
18458
+ }
18459
+ const accountSpaceId = this.ownedSpaceId(import_sdk_core4.ACCOUNT_REGISTRY_SPACE);
18460
+ await this.auth.hostOwnedSpace(accountSpaceId);
18461
+ const accountKV = this.spaces.get(accountSpaceId).kv;
18462
+ for (const record of request.registryRecords) {
18463
+ const result = await accountKV.put(record.key, {
18464
+ app_id: record.app_id,
18465
+ manifests: record.manifests,
18466
+ updated_at: (/* @__PURE__ */ new Date()).toISOString()
18467
+ });
18468
+ if (!result.ok) {
18469
+ throw new Error(
18470
+ `Failed to write manifest registry record ${record.key}: ${result.error.message}`
18471
+ );
18472
+ }
18473
+ }
18474
+ }
18362
18475
  /**
18363
18476
  * Restore a previously established session from stored delegation data.
18364
18477
  *
@@ -18496,7 +18609,10 @@ var _TinyCloudNode = class _TinyCloudNode {
18496
18609
  enablePublicSpace: this.config.enablePublicSpace ?? true,
18497
18610
  spaceCreationHandler: this.config.spaceCreationHandler,
18498
18611
  nonce: this.config.nonce,
18499
- siweConfig: this.config.siweConfig
18612
+ siweConfig: this.config.siweConfig,
18613
+ manifest: this.config.manifest,
18614
+ capabilityRequest: this.config.capabilityRequest,
18615
+ includeAccountRegistryPermissions: this.config.includeAccountRegistryPermissions
18500
18616
  });
18501
18617
  this.tc = new import_sdk_core4.TinyCloud(this.auth, {
18502
18618
  invokeAny: this.wasmBindings.invokeAny
@@ -18536,7 +18652,10 @@ var _TinyCloudNode = class _TinyCloudNode {
18536
18652
  enablePublicSpace: this.config.enablePublicSpace ?? true,
18537
18653
  spaceCreationHandler: this.config.spaceCreationHandler,
18538
18654
  nonce: this.config.nonce,
18539
- siweConfig: this.config.siweConfig
18655
+ siweConfig: this.config.siweConfig,
18656
+ manifest: this.config.manifest,
18657
+ capabilityRequest: this.config.capabilityRequest,
18658
+ includeAccountRegistryPermissions: this.config.includeAccountRegistryPermissions
18540
18659
  });
18541
18660
  this.tc = new import_sdk_core4.TinyCloud(this.auth, {
18542
18661
  invokeAny: this.wasmBindings.invokeAny
@@ -19398,6 +19517,42 @@ var _TinyCloudNode = class _TinyCloudNode {
19398
19517
  );
19399
19518
  return { delegation, prompted: false };
19400
19519
  }
19520
+ /**
19521
+ * Materialize one manifest-declared delegation using the current session key.
19522
+ * Delivery is intentionally out of band; callers decide how to transmit the
19523
+ * returned UCAN to the delegate.
19524
+ */
19525
+ async materializeDelegation(did, request = this.capabilityRequest) {
19526
+ if (!request) {
19527
+ throw new Error(
19528
+ "materializeDelegation requires a composed manifest request"
19529
+ );
19530
+ }
19531
+ const target = request.delegationTargets.find((entry) => entry.did === did);
19532
+ if (!target) {
19533
+ throw new Error(`No manifest delegation target found for DID ${did}`);
19534
+ }
19535
+ const result = await this.delegateTo(target.did, target.permissions, {
19536
+ expiry: target.expiryMs
19537
+ });
19538
+ return { ...result, target };
19539
+ }
19540
+ /**
19541
+ * Materialize every delegation target declared by the composed manifest
19542
+ * request. This does not deliver the delegations anywhere.
19543
+ */
19544
+ async materializeDelegations(request = this.capabilityRequest) {
19545
+ if (!request) {
19546
+ throw new Error(
19547
+ "materializeDelegations requires a composed manifest request"
19548
+ );
19549
+ }
19550
+ const out = [];
19551
+ for (const target of request.delegationTargets) {
19552
+ out.push(await this.materializeDelegation(target.did, request));
19553
+ }
19554
+ return out;
19555
+ }
19401
19556
  /**
19402
19557
  * Issue a delegation via the session-key UCAN WASM path.
19403
19558
  *
@@ -19421,7 +19576,7 @@ var _TinyCloudNode = class _TinyCloudNode {
19421
19576
  }
19422
19577
  const resolvedSpaces = /* @__PURE__ */ new Set();
19423
19578
  for (const entry of entries) {
19424
- const spaceId2 = entry.space === "default" ? session.spaceId : entry.space;
19579
+ const spaceId2 = this.resolvePermissionSpace(entry.space, session);
19425
19580
  resolvedSpaces.add(spaceId2);
19426
19581
  }
19427
19582
  if (resolvedSpaces.size !== 1) {
@@ -19496,6 +19651,22 @@ var _TinyCloudNode = class _TinyCloudNode {
19496
19651
  host: this.config.host
19497
19652
  };
19498
19653
  }
19654
+ resolvePermissionSpace(space, session) {
19655
+ if (space === void 0) {
19656
+ return this.wasmBindings.makeSpaceId(
19657
+ session.address,
19658
+ session.chainId,
19659
+ "applications"
19660
+ );
19661
+ }
19662
+ if (space === "default") {
19663
+ return session.spaceId;
19664
+ }
19665
+ if (space.startsWith("tinycloud:")) {
19666
+ return space;
19667
+ }
19668
+ return this.wasmBindings.makeSpaceId(session.address, session.chainId, space);
19669
+ }
19499
19670
  /**
19500
19671
  * Issue a delegation via the legacy wallet-signed SIWE path for a single
19501
19672
  * {@link PermissionEntry}. Shares the implementation with the public
@@ -19506,7 +19677,8 @@ var _TinyCloudNode = class _TinyCloudNode {
19506
19677
  * @internal
19507
19678
  */
19508
19679
  async createDelegationLegacyWalletPath(delegateDID, entry, expirationTime) {
19509
- const spaceIdOverride = entry.space === "default" ? void 0 : entry.space;
19680
+ const session = this.auth?.tinyCloudSession;
19681
+ const spaceIdOverride = session === void 0 || entry.space === "default" ? void 0 : this.resolvePermissionSpace(entry.space, session);
19510
19682
  return this.createDelegationWalletPath({
19511
19683
  path: entry.path,
19512
19684
  actions: entry.actions,
@@ -20038,9 +20210,13 @@ var import_sdk_core17 = require("@tinycloud/sdk-core");
20038
20210
  var import_sdk_core18 = require("@tinycloud/sdk-core");
20039
20211
  // Annotate the CommonJS export names for ESM import in node:
20040
20212
  0 && (module.exports = {
20213
+ ACCOUNT_REGISTRY_PATH,
20214
+ ACCOUNT_REGISTRY_SPACE,
20041
20215
  AutoApproveSpaceCreationHandler,
20042
20216
  CapabilityKeyRegistry,
20043
20217
  CapabilityKeyRegistryErrorCodes,
20218
+ DEFAULT_MANIFEST_SPACE,
20219
+ DEFAULT_MANIFEST_VERSION,
20044
20220
  DataVaultService,
20045
20221
  DatabaseHandle,
20046
20222
  DelegatedAccess,
@@ -20078,6 +20254,7 @@ var import_sdk_core18 = require("@tinycloud/sdk-core");
20078
20254
  WasmKeyProvider,
20079
20255
  buildSpaceUri,
20080
20256
  checkNodeInfo,
20257
+ composeManifestRequest,
20081
20258
  createCapabilityKeyRegistry,
20082
20259
  createSharingService,
20083
20260
  createSpaceService,
@@ -20093,6 +20270,7 @@ var import_sdk_core18 = require("@tinycloud/sdk-core");
20093
20270
  parseExpiry,
20094
20271
  parseSpaceUri,
20095
20272
  resolveManifest,
20273
+ resourceCapabilitiesToSpaceAbilitiesMap,
20096
20274
  serializeDelegation,
20097
20275
  validateManifest
20098
20276
  });