@tinycloud/node-sdk 2.6.4-beta.0 → 2.7.0-beta.2

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(/\/$/, "");
@@ -20391,6 +20412,7 @@ var _TinyCloudNode = class _TinyCloudNode {
20391
20412
  hosts: [this.config.host],
20392
20413
  session: serviceSession,
20393
20414
  invoke: this.invokeWithRuntimePermissions,
20415
+ invokeAny: this.invokeAnyWithRuntimePermissions,
20394
20416
  fetch: globalThis.fetch.bind(globalThis)
20395
20417
  });
20396
20418
  this._spaceService = new import_sdk_core7.SpaceService({
@@ -20498,7 +20520,10 @@ var _TinyCloudNode = class _TinyCloudNode {
20498
20520
  delegationCid: params.session.delegationCid,
20499
20521
  jwk: params.session.jwk,
20500
20522
  spaceId: params.session.spaceId,
20501
- verificationMethod: params.session.verificationMethod
20523
+ // Session storage carries the verification method as a DID URL
20524
+ // (`did:key:...#key-id`). The Rust UCAN builder expects the principal
20525
+ // DID here and rejects the fragment as an invalid audience DID.
20526
+ verificationMethod: params.session.verificationMethod.split("#", 1)[0]
20502
20527
  };
20503
20528
  const result = this.wasmBindings.createDelegation(
20504
20529
  wasmSession,
@@ -20525,56 +20550,69 @@ var _TinyCloudNode = class _TinyCloudNode {
20525
20550
  * with expiry longer than the current session.
20526
20551
  * @internal
20527
20552
  */
20528
- async createRootDelegationForSharing(params) {
20529
- if (!this.signer) {
20530
- return void 0;
20553
+ async createOwnerDelegation(params) {
20554
+ if (!params.delegateDid.startsWith("did:key:") || params.actions.length === 0 || params.path.length === 0) {
20555
+ throw new Error("Owner delegation requires an external did:key audience and bounded capabilities");
20531
20556
  }
20557
+ const now = /* @__PURE__ */ new Date();
20558
+ if (params.expiresAt.getTime() <= now.getTime() || params.expiresAt.getTime() - now.getTime() > import_sdk_core7.EXPIRY.MAX_MS) {
20559
+ throw new Error("Owner delegation expiry must be explicit, future, and within EXPIRY.MAX_MS");
20560
+ }
20561
+ if (!this.signer) throw new Error("Owner wallet signer is required");
20532
20562
  const session = this.currentTinyCloudSession();
20533
- if (!session) {
20534
- return void 0;
20563
+ if (!session) throw new Error("Owner session is required");
20564
+ const abilities = sharingActionsToAbilities(params.path, [...params.actions]);
20565
+ if (!abilities) throw new Error("Owner delegation capabilities are unsupported");
20566
+ const host = this.config.host;
20567
+ const prepared = this.wasmBindings.prepareSession({
20568
+ abilities,
20569
+ address: this.wasmBindings.ensureEip55(session.address),
20570
+ chainId: session.chainId,
20571
+ domain: this.siweDomain,
20572
+ issuedAt: now.toISOString(),
20573
+ expirationTime: params.expiresAt.toISOString(),
20574
+ spaceId: params.spaceId,
20575
+ delegateUri: params.delegateDid
20576
+ });
20577
+ const signature2 = await this.signer.signMessage(prepared.siwe);
20578
+ const delegationSession = this.wasmBindings.completeSessionSetup({ ...prepared, signature: signature2 });
20579
+ const activation = await (0, import_sdk_core7.activateSessionWithHost)(host, delegationSession.delegationHeader);
20580
+ if (!activation.success) {
20581
+ throw new Error(`Owner delegation import failed: ${activation.status} ${activation.error ?? ""}`.trim());
20535
20582
  }
20536
- try {
20537
- const host = this.config.host;
20538
- const now = /* @__PURE__ */ new Date();
20539
- const abilities = sharingActionsToAbilities(params.path, params.actions);
20540
- if (!abilities) {
20541
- return void 0;
20542
- }
20543
- const prepared = this.wasmBindings.prepareSession({
20544
- abilities,
20545
- address: this.wasmBindings.ensureEip55(session.address),
20546
- chainId: session.chainId,
20547
- domain: this.siweDomain,
20548
- issuedAt: now.toISOString(),
20549
- expirationTime: params.requestedExpiry.toISOString(),
20550
- spaceId: params.spaceId,
20551
- delegateUri: params.shareKeyDID
20552
- });
20553
- const signature2 = await this.signer.signMessage(prepared.siwe);
20554
- const delegationSession = this.wasmBindings.completeSessionSetup({
20555
- ...prepared,
20556
- signature: signature2
20557
- });
20558
- const activateResult = await (0, import_sdk_core7.activateSessionWithHost)(
20559
- host,
20560
- delegationSession.delegationHeader
20561
- );
20562
- if (!activateResult.success) {
20563
- return void 0;
20583
+ const delegation = {
20584
+ cid: delegationSession.delegationCid,
20585
+ delegateDID: params.delegateDid,
20586
+ delegatorDID: (0, import_sdk_core7.pkhDid)(session.address, session.chainId),
20587
+ spaceId: params.spaceId,
20588
+ path: params.path,
20589
+ actions: [...params.actions],
20590
+ expiry: params.expiresAt,
20591
+ isRevoked: false,
20592
+ allowSubDelegation: true,
20593
+ createdAt: now,
20594
+ authHeader: delegationSession.delegationHeader.Authorization
20595
+ };
20596
+ return {
20597
+ delegation,
20598
+ signedDagCbor: decodeAuthorizationBytes(delegationSession.delegationHeader.Authorization),
20599
+ delegationCid: delegationSession.delegationCid,
20600
+ nodeReceipt: {
20601
+ commitEventCid: activation.commitEventCid,
20602
+ activated: activation.activated ?? [],
20603
+ skipped: activation.skipped ?? []
20564
20604
  }
20565
- return {
20566
- cid: delegationSession.delegationCid,
20567
- delegateDID: params.shareKeyDID,
20568
- delegatorDID: (0, import_sdk_core7.pkhDid)(session.address, session.chainId),
20605
+ };
20606
+ }
20607
+ async createRootDelegationForSharing(params) {
20608
+ try {
20609
+ return (await this.createOwnerDelegation({
20610
+ delegateDid: params.shareKeyDID,
20569
20611
  spaceId: params.spaceId,
20570
20612
  path: params.path,
20571
20613
  actions: params.actions,
20572
- expiry: params.requestedExpiry,
20573
- isRevoked: false,
20574
- allowSubDelegation: true,
20575
- createdAt: now,
20576
- authHeader: delegationSession.delegationHeader.Authorization
20577
- };
20614
+ expiresAt: params.requestedExpiry
20615
+ })).delegation;
20578
20616
  } catch {
20579
20617
  return void 0;
20580
20618
  }
@@ -21342,6 +21380,20 @@ var _TinyCloudNode = class _TinyCloudNode {
21342
21380
  async revokeDelegation(cid) {
21343
21381
  return this.delegationManager.revoke(cid);
21344
21382
  }
21383
+ /** Read node-confirmed lifecycle state for one delegation. */
21384
+ async getDelegationStatus(cid) {
21385
+ return this.delegationManager.status(cid);
21386
+ }
21387
+ /** Compute the canonical CID of a compact delegation authorization. */
21388
+ computeDelegationCid(authorization) {
21389
+ if (!authorization || !this.wasmBindings.computeCid) {
21390
+ throw new Error("Delegation CID computation is unavailable");
21391
+ }
21392
+ return this.wasmBindings.computeCid(
21393
+ new TextEncoder().encode(authorization),
21394
+ 0x55n
21395
+ );
21396
+ }
21345
21397
  /**
21346
21398
  * List all delegations for the current session's space.
21347
21399
  *