@tinycloud/node-sdk 2.6.4-beta.1 → 2.7.0-beta.3

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
@@ -18697,6 +18697,22 @@ var ROOT_DELEGATION_ACTIONS = [
18697
18697
  import_sdk_core7.DUCKDB.ALL
18698
18698
  ];
18699
18699
  var DEFAULT_SESSION_EXPIRATION_MS = import_sdk_core7.EXPIRY.SESSION_MS;
18700
+ function decodeAuthorizationBytes(authorization) {
18701
+ const encoded = authorization.replace(/^Bearer /i, "");
18702
+ const match = /^([A-Za-z0-9_-]+)(={1,2})?$/.exec(encoded);
18703
+ const unpadded = match?.[1];
18704
+ const paddingLength = match?.[2]?.length ?? 0;
18705
+ const remainder = unpadded === void 0 ? 1 : unpadded.length % 4;
18706
+ const expectedPaddingLength = remainder === 2 ? 2 : remainder === 3 ? 1 : 0;
18707
+ if (unpadded === void 0 || remainder === 1 || paddingLength !== 0 && paddingLength !== expectedPaddingLength) {
18708
+ throw new Error("Delegation Authorization is not canonical base64url DAG-CBOR");
18709
+ }
18710
+ const decoded = Uint8Array.from(Buffer.from(unpadded, "base64url"));
18711
+ if (Buffer.from(decoded).toString("base64url") !== unpadded) {
18712
+ throw new Error("Delegation Authorization is not canonical base64url DAG-CBOR");
18713
+ }
18714
+ return decoded;
18715
+ }
18700
18716
  function isOpenKeyAutoSignStrategy(strategy) {
18701
18717
  return strategy?.openKeyAutoSign === true;
18702
18718
  }
@@ -18921,6 +18937,11 @@ var _TinyCloudNode = class _TinyCloudNode {
18921
18937
  fetch: globalThis.fetch.bind(globalThis),
18922
18938
  keyProvider: this._keyProvider,
18923
18939
  registry: this._capabilityRegistry,
18940
+ createDelegationWasm: (params) => this.createDelegationWrapper(params),
18941
+ computeCid: (data, codec) => {
18942
+ if (!this.wasmBindings.computeCid) throw new Error("computeCid is unavailable");
18943
+ return this.wasmBindings.computeCid(data, codec);
18944
+ },
18924
18945
  // delegationManager: undefined - not needed for receive()
18925
18946
  createKVService: (config2) => {
18926
18947
  const prefix = config2.pathPrefix?.replace(/\/$/, "");
@@ -19115,6 +19136,7 @@ var _TinyCloudNode = class _TinyCloudNode {
19115
19136
  getPrimarySpaceId: () => this.spaceId,
19116
19137
  getAccountSpaceId: () => this.accountSpaceId,
19117
19138
  getSpaces: () => this.spaces,
19139
+ getDelegationManager: () => this.delegationManager,
19118
19140
  getAccountDb: () => this.accountSpaceId ? this.sqlForSpace(this.accountSpaceId).db("account") : void 0,
19119
19141
  ensureAccountSpaceHosted: async () => {
19120
19142
  if (this.accountSpaceId && this.auth) {
@@ -20389,8 +20411,10 @@ var _TinyCloudNode = class _TinyCloudNode {
20389
20411
  }
20390
20412
  this._delegationManager = new import_sdk_core7.DelegationManager({
20391
20413
  hosts: [this.config.host],
20414
+ accountSpaceId: this.accountSpaceId,
20392
20415
  session: serviceSession,
20393
20416
  invoke: this.invokeWithRuntimePermissions,
20417
+ invokeAny: this.invokeAnyWithRuntimePermissions,
20394
20418
  fetch: globalThis.fetch.bind(globalThis)
20395
20419
  });
20396
20420
  this._spaceService = new import_sdk_core7.SpaceService({
@@ -20528,56 +20552,69 @@ var _TinyCloudNode = class _TinyCloudNode {
20528
20552
  * with expiry longer than the current session.
20529
20553
  * @internal
20530
20554
  */
20531
- async createRootDelegationForSharing(params) {
20532
- if (!this.signer) {
20533
- return void 0;
20555
+ async createOwnerDelegation(params) {
20556
+ if (!params.delegateDid.startsWith("did:key:") || params.actions.length === 0 || params.path.length === 0) {
20557
+ throw new Error("Owner delegation requires an external did:key audience and bounded capabilities");
20534
20558
  }
20559
+ const now = /* @__PURE__ */ new Date();
20560
+ if (params.expiresAt.getTime() <= now.getTime() || params.expiresAt.getTime() - now.getTime() > import_sdk_core7.EXPIRY.MAX_MS) {
20561
+ throw new Error("Owner delegation expiry must be explicit, future, and within EXPIRY.MAX_MS");
20562
+ }
20563
+ if (!this.signer) throw new Error("Owner wallet signer is required");
20535
20564
  const session = this.currentTinyCloudSession();
20536
- if (!session) {
20537
- return void 0;
20565
+ if (!session) throw new Error("Owner session is required");
20566
+ const abilities = sharingActionsToAbilities(params.path, [...params.actions]);
20567
+ if (!abilities) throw new Error("Owner delegation capabilities are unsupported");
20568
+ const host = this.config.host;
20569
+ const prepared = this.wasmBindings.prepareSession({
20570
+ abilities,
20571
+ address: this.wasmBindings.ensureEip55(session.address),
20572
+ chainId: session.chainId,
20573
+ domain: this.siweDomain,
20574
+ issuedAt: now.toISOString(),
20575
+ expirationTime: params.expiresAt.toISOString(),
20576
+ spaceId: params.spaceId,
20577
+ delegateUri: params.delegateDid
20578
+ });
20579
+ const signature2 = await this.signer.signMessage(prepared.siwe);
20580
+ const delegationSession = this.wasmBindings.completeSessionSetup({ ...prepared, signature: signature2 });
20581
+ const activation = await (0, import_sdk_core7.activateSessionWithHost)(host, delegationSession.delegationHeader);
20582
+ if (!activation.success) {
20583
+ throw new Error(`Owner delegation import failed: ${activation.status} ${activation.error ?? ""}`.trim());
20538
20584
  }
20539
- try {
20540
- const host = this.config.host;
20541
- const now = /* @__PURE__ */ new Date();
20542
- const abilities = sharingActionsToAbilities(params.path, params.actions);
20543
- if (!abilities) {
20544
- return void 0;
20545
- }
20546
- const prepared = this.wasmBindings.prepareSession({
20547
- abilities,
20548
- address: this.wasmBindings.ensureEip55(session.address),
20549
- chainId: session.chainId,
20550
- domain: this.siweDomain,
20551
- issuedAt: now.toISOString(),
20552
- expirationTime: params.requestedExpiry.toISOString(),
20553
- spaceId: params.spaceId,
20554
- delegateUri: params.shareKeyDID
20555
- });
20556
- const signature2 = await this.signer.signMessage(prepared.siwe);
20557
- const delegationSession = this.wasmBindings.completeSessionSetup({
20558
- ...prepared,
20559
- signature: signature2
20560
- });
20561
- const activateResult = await (0, import_sdk_core7.activateSessionWithHost)(
20562
- host,
20563
- delegationSession.delegationHeader
20564
- );
20565
- if (!activateResult.success) {
20566
- return void 0;
20585
+ const delegation = {
20586
+ cid: delegationSession.delegationCid,
20587
+ delegateDID: params.delegateDid,
20588
+ delegatorDID: (0, import_sdk_core7.pkhDid)(session.address, session.chainId),
20589
+ spaceId: params.spaceId,
20590
+ path: params.path,
20591
+ actions: [...params.actions],
20592
+ expiry: params.expiresAt,
20593
+ isRevoked: false,
20594
+ allowSubDelegation: true,
20595
+ createdAt: now,
20596
+ authHeader: delegationSession.delegationHeader.Authorization
20597
+ };
20598
+ return {
20599
+ delegation,
20600
+ signedDagCbor: decodeAuthorizationBytes(delegationSession.delegationHeader.Authorization),
20601
+ delegationCid: delegationSession.delegationCid,
20602
+ nodeReceipt: {
20603
+ commitEventCid: activation.commitEventCid,
20604
+ activated: activation.activated ?? [],
20605
+ skipped: activation.skipped ?? []
20567
20606
  }
20568
- return {
20569
- cid: delegationSession.delegationCid,
20570
- delegateDID: params.shareKeyDID,
20571
- delegatorDID: (0, import_sdk_core7.pkhDid)(session.address, session.chainId),
20607
+ };
20608
+ }
20609
+ async createRootDelegationForSharing(params) {
20610
+ try {
20611
+ return (await this.createOwnerDelegation({
20612
+ delegateDid: params.shareKeyDID,
20572
20613
  spaceId: params.spaceId,
20573
20614
  path: params.path,
20574
20615
  actions: params.actions,
20575
- expiry: params.requestedExpiry,
20576
- isRevoked: false,
20577
- allowSubDelegation: true,
20578
- createdAt: now,
20579
- authHeader: delegationSession.delegationHeader.Authorization
20580
- };
20616
+ expiresAt: params.requestedExpiry
20617
+ })).delegation;
20581
20618
  } catch {
20582
20619
  return void 0;
20583
20620
  }
@@ -21345,6 +21382,20 @@ var _TinyCloudNode = class _TinyCloudNode {
21345
21382
  async revokeDelegation(cid) {
21346
21383
  return this.delegationManager.revoke(cid);
21347
21384
  }
21385
+ /** Read node-confirmed lifecycle state for one delegation. */
21386
+ async getDelegationStatus(cid) {
21387
+ return this.delegationManager.status(cid);
21388
+ }
21389
+ /** Compute the canonical CID of a compact delegation authorization. */
21390
+ computeDelegationCid(authorization) {
21391
+ if (!authorization || !this.wasmBindings.computeCid) {
21392
+ throw new Error("Delegation CID computation is unavailable");
21393
+ }
21394
+ return this.wasmBindings.computeCid(
21395
+ new TextEncoder().encode(authorization),
21396
+ 0x55n
21397
+ );
21398
+ }
21348
21399
  /**
21349
21400
  * List all delegations for the current session's space.
21350
21401
  *