@tinycloud/node-sdk 3.1.0-beta.7 → 3.1.0-beta.9

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
@@ -4404,19 +4404,19 @@ var init_lib4 = __esm({
4404
4404
  resolve(value);
4405
4405
  });
4406
4406
  }
4407
- return new (P || (P = Promise))(function(resolve, reject) {
4407
+ return new (P || (P = Promise))(function(resolve, reject2) {
4408
4408
  function fulfilled(value) {
4409
4409
  try {
4410
4410
  step(generator.next(value));
4411
4411
  } catch (e) {
4412
- reject(e);
4412
+ reject2(e);
4413
4413
  }
4414
4414
  }
4415
4415
  function rejected(value) {
4416
4416
  try {
4417
4417
  step(generator["throw"](value));
4418
4418
  } catch (e) {
4419
- reject(e);
4419
+ reject2(e);
4420
4420
  }
4421
4421
  }
4422
4422
  function step(result) {
@@ -8040,19 +8040,19 @@ var init_typed_data = __esm({
8040
8040
  resolve(value);
8041
8041
  });
8042
8042
  }
8043
- return new (P || (P = Promise))(function(resolve, reject) {
8043
+ return new (P || (P = Promise))(function(resolve, reject2) {
8044
8044
  function fulfilled(value) {
8045
8045
  try {
8046
8046
  step(generator.next(value));
8047
8047
  } catch (e) {
8048
- reject(e);
8048
+ reject2(e);
8049
8049
  }
8050
8050
  }
8051
8051
  function rejected(value) {
8052
8052
  try {
8053
8053
  step(generator["throw"](value));
8054
8054
  } catch (e) {
8055
- reject(e);
8055
+ reject2(e);
8056
8056
  }
8057
8057
  }
8058
8058
  function step(result) {
@@ -14898,14 +14898,14 @@ var require_scrypt = __commonJS({
14898
14898
  }
14899
14899
  const lib = {
14900
14900
  scrypt: function(password, salt, N2, r2, p, dkLen, progressCallback) {
14901
- return new Promise(function(resolve, reject) {
14901
+ return new Promise(function(resolve, reject2) {
14902
14902
  let lastProgress = 0;
14903
14903
  if (progressCallback) {
14904
14904
  progressCallback(0);
14905
14905
  }
14906
14906
  _scrypt(password, salt, N2, r2, p, dkLen, function(error, progress, key2) {
14907
14907
  if (error) {
14908
- reject(error);
14908
+ reject2(error);
14909
14909
  } else if (key2) {
14910
14910
  if (progressCallback && lastProgress !== 1) {
14911
14911
  progressCallback(1);
@@ -15294,19 +15294,19 @@ var init_keystore = __esm({
15294
15294
  resolve(value);
15295
15295
  });
15296
15296
  }
15297
- return new (P || (P = Promise))(function(resolve, reject) {
15297
+ return new (P || (P = Promise))(function(resolve, reject2) {
15298
15298
  function fulfilled(value) {
15299
15299
  try {
15300
15300
  step(generator.next(value));
15301
15301
  } catch (e) {
15302
- reject(e);
15302
+ reject2(e);
15303
15303
  }
15304
15304
  }
15305
15305
  function rejected(value) {
15306
15306
  try {
15307
15307
  step(generator["throw"](value));
15308
15308
  } catch (e) {
15309
- reject(e);
15309
+ reject2(e);
15310
15310
  }
15311
15311
  }
15312
15312
  function step(result) {
@@ -15607,19 +15607,19 @@ var init_lib25 = __esm({
15607
15607
  resolve(value);
15608
15608
  });
15609
15609
  }
15610
- return new (P || (P = Promise))(function(resolve, reject) {
15610
+ return new (P || (P = Promise))(function(resolve, reject2) {
15611
15611
  function fulfilled(value) {
15612
15612
  try {
15613
15613
  step(generator.next(value));
15614
15614
  } catch (e) {
15615
- reject(e);
15615
+ reject2(e);
15616
15616
  }
15617
15617
  }
15618
15618
  function rejected(value) {
15619
15619
  try {
15620
15620
  step(generator["throw"](value));
15621
15621
  } catch (e) {
15622
- reject(e);
15622
+ reject2(e);
15623
15623
  }
15624
15624
  }
15625
15625
  function step(result) {
@@ -15689,19 +15689,19 @@ var init_lib26 = __esm({
15689
15689
  resolve(value);
15690
15690
  });
15691
15691
  }
15692
- return new (P || (P = Promise))(function(resolve, reject) {
15692
+ return new (P || (P = Promise))(function(resolve, reject2) {
15693
15693
  function fulfilled(value) {
15694
15694
  try {
15695
15695
  step(generator.next(value));
15696
15696
  } catch (e) {
15697
- reject(e);
15697
+ reject2(e);
15698
15698
  }
15699
15699
  }
15700
15700
  function rejected(value) {
15701
15701
  try {
15702
15702
  step(generator["throw"](value));
15703
15703
  } catch (e) {
15704
- reject(e);
15704
+ reject2(e);
15705
15705
  }
15706
15706
  }
15707
15707
  function step(result) {
@@ -16011,19 +16011,19 @@ var init_lib27 = __esm({
16011
16011
  resolve(value);
16012
16012
  });
16013
16013
  }
16014
- return new (P || (P = Promise))(function(resolve, reject) {
16014
+ return new (P || (P = Promise))(function(resolve, reject2) {
16015
16015
  function fulfilled(value) {
16016
16016
  try {
16017
16017
  step(generator.next(value));
16018
16018
  } catch (e) {
16019
- reject(e);
16019
+ reject2(e);
16020
16020
  }
16021
16021
  }
16022
16022
  function rejected(value) {
16023
16023
  try {
16024
16024
  step(generator["throw"](value));
16025
16025
  } catch (e) {
16026
- reject(e);
16026
+ reject2(e);
16027
16027
  }
16028
16028
  }
16029
16029
  function step(result) {
@@ -16249,19 +16249,19 @@ var init_geturl = __esm({
16249
16249
  resolve(value);
16250
16250
  });
16251
16251
  }
16252
- return new (P || (P = Promise))(function(resolve, reject) {
16252
+ return new (P || (P = Promise))(function(resolve, reject2) {
16253
16253
  function fulfilled(value) {
16254
16254
  try {
16255
16255
  step(generator.next(value));
16256
16256
  } catch (e) {
16257
- reject(e);
16257
+ reject2(e);
16258
16258
  }
16259
16259
  }
16260
16260
  function rejected(value) {
16261
16261
  try {
16262
16262
  step(generator["throw"](value));
16263
16263
  } catch (e) {
16264
- reject(e);
16264
+ reject2(e);
16265
16265
  }
16266
16266
  }
16267
16267
  function step(result) {
@@ -16402,14 +16402,14 @@ function _fetchData(connection, body, processFunc) {
16402
16402
  options.headers = flatHeaders;
16403
16403
  const runningTimeout = (function() {
16404
16404
  let timer = null;
16405
- const promise = new Promise(function(resolve, reject) {
16405
+ const promise = new Promise(function(resolve, reject2) {
16406
16406
  if (timeout) {
16407
16407
  timer = setTimeout(() => {
16408
16408
  if (timer == null) {
16409
16409
  return;
16410
16410
  }
16411
16411
  timer = null;
16412
- reject(logger28.makeError("timeout", Logger.errors.TIMEOUT, {
16412
+ reject2(logger28.makeError("timeout", Logger.errors.TIMEOUT, {
16413
16413
  requestBody: bodyify(options.body, flatHeaders["content-type"]),
16414
16414
  requestMethod: options.method,
16415
16415
  timeout,
@@ -16572,7 +16572,7 @@ function poll(func, options) {
16572
16572
  if (options.interval == null) {
16573
16573
  options.interval = 250;
16574
16574
  }
16575
- return new Promise(function(resolve, reject) {
16575
+ return new Promise(function(resolve, reject2) {
16576
16576
  let timer = null;
16577
16577
  let done = false;
16578
16578
  const cancel = () => {
@@ -16588,7 +16588,7 @@ function poll(func, options) {
16588
16588
  if (options.timeout) {
16589
16589
  timer = setTimeout(() => {
16590
16590
  if (cancel()) {
16591
- reject(new Error("timeout"));
16591
+ reject2(new Error("timeout"));
16592
16592
  }
16593
16593
  }, options.timeout);
16594
16594
  }
@@ -16608,7 +16608,7 @@ function poll(func, options) {
16608
16608
  attempt++;
16609
16609
  if (attempt > retryLimit) {
16610
16610
  if (cancel()) {
16611
- reject(new Error("retry limit reached"));
16611
+ reject2(new Error("retry limit reached"));
16612
16612
  }
16613
16613
  return;
16614
16614
  }
@@ -16624,7 +16624,7 @@ function poll(func, options) {
16624
16624
  return null;
16625
16625
  }, function(error) {
16626
16626
  if (cancel()) {
16627
- reject(error);
16627
+ reject2(error);
16628
16628
  }
16629
16629
  });
16630
16630
  }
@@ -16648,19 +16648,19 @@ var init_lib28 = __esm({
16648
16648
  resolve(value);
16649
16649
  });
16650
16650
  }
16651
- return new (P || (P = Promise))(function(resolve, reject) {
16651
+ return new (P || (P = Promise))(function(resolve, reject2) {
16652
16652
  function fulfilled(value) {
16653
16653
  try {
16654
16654
  step(generator.next(value));
16655
16655
  } catch (e) {
16656
- reject(e);
16656
+ reject2(e);
16657
16657
  }
16658
16658
  }
16659
16659
  function rejected(value) {
16660
16660
  try {
16661
16661
  step(generator["throw"](value));
16662
16662
  } catch (e) {
16663
- reject(e);
16663
+ reject2(e);
16664
16664
  }
16665
16665
  }
16666
16666
  function step(result) {
@@ -17072,6 +17072,8 @@ __export(index_exports, {
17072
17072
  PrefixedKVService: () => import_sdk_core13.PrefixedKVService,
17073
17073
  PrivateKeySigner: () => PrivateKeySigner,
17074
17074
  ProtocolMismatchError: () => import_sdk_core22.ProtocolMismatchError,
17075
+ RUNTIME_DELEGATION_REJECTIONS: () => RUNTIME_DELEGATION_REJECTIONS,
17076
+ RuntimeDelegationRejectedError: () => RuntimeDelegationRejectedError,
17075
17077
  SECRET_NAME_RE: () => import_sdk_core16.SECRET_NAME_RE,
17076
17078
  SQLAction: () => import_sdk_core14.SQLAction,
17077
17079
  SQLService: () => import_sdk_core14.SQLService,
@@ -17213,6 +17215,9 @@ var _NodeWasmBindings = class _NodeWasmBindings {
17213
17215
  validatePersistedSession(proof) {
17214
17216
  return (0, import_node_sdk_wasm.validatePersistedSession)(proof);
17215
17217
  }
17218
+ validateSessionGrant(proof) {
17219
+ return (0, import_node_sdk_wasm.validateSessionGrant)(proof);
17220
+ }
17216
17221
  createSessionManager() {
17217
17222
  return new import_node_sdk_wasm.TCWSessionManager();
17218
17223
  }
@@ -18767,14 +18772,14 @@ var NodeUserAuthorization = class {
18767
18772
  * Request signature via event emitter with timeout.
18768
18773
  */
18769
18774
  requestSignatureViaEmitter(request, emitter, timeout) {
18770
- return new Promise((resolve, reject) => {
18775
+ return new Promise((resolve, reject2) => {
18771
18776
  const timeoutId = setTimeout(() => {
18772
- reject(new Error("Sign request timed out"));
18777
+ reject2(new Error("Sign request timed out"));
18773
18778
  }, timeout);
18774
18779
  const respond = async (response) => {
18775
18780
  clearTimeout(timeoutId);
18776
18781
  if (!response.approved) {
18777
- reject(
18782
+ reject2(
18778
18783
  new Error(response.reason ?? "Sign request rejected via emitter")
18779
18784
  );
18780
18785
  } else {
@@ -19353,6 +19358,25 @@ function jsonFromVerifier(value) {
19353
19358
  function verifierRecapEntries(entries) {
19354
19359
  return entries.map((entry) => Array.isArray(entry.caveats) ? { ...entry, caveats: entry.caveats.map((caveat) => jsonFromVerifier(caveat)) } : entry);
19355
19360
  }
19361
+ function exactVerifiedRecap(verified) {
19362
+ const exactRecap = Array.isArray(verified.verifiedRecap) ? verifierRecapEntries(verified.verifiedRecap) : verified.verifiedRecap;
19363
+ if (!Array.isArray(exactRecap) || !exactRecap.every(
19364
+ (entry) => entry !== null && typeof entry === "object" && typeof entry.service === "string" && typeof entry.space === "string" && typeof entry.path === "string" && Array.isArray(entry.actions) && entry.actions.every((action) => typeof action === "string") && Array.isArray(entry.caveats) && entry.caveats.every(
19365
+ (caveat) => caveat !== null && typeof caveat === "object" && !Array.isArray(caveat)
19366
+ )
19367
+ )) {
19368
+ throw new UnsupportedSessionRestoreError(
19369
+ "it cannot reconstruct caveat-preserving persisted ReCap authority"
19370
+ );
19371
+ }
19372
+ return exactRecap.map((entry) => ({
19373
+ service: entry.service,
19374
+ space: entry.space,
19375
+ path: entry.path,
19376
+ actions: [...entry.actions],
19377
+ caveats: cloneRecapCaveats(entry.caveats)
19378
+ }));
19379
+ }
19356
19380
  var ServiceGraphLifetime = class {
19357
19381
  constructor(invokeFn, invokeAnyFn, fetchFn) {
19358
19382
  this.invokeFn = invokeFn;
@@ -21017,23 +21041,7 @@ var _TinyCloudNode = class _TinyCloudNode {
21017
21041
  throw new InvalidRestoredSessionError(error instanceof Error ? error.message : String(error));
21018
21042
  }
21019
21043
  })();
21020
- const exactRecap = Array.isArray(verified.verifiedRecap) ? verifierRecapEntries(verified.verifiedRecap) : verified.verifiedRecap;
21021
- if (!Array.isArray(exactRecap) || !exactRecap.every(
21022
- (entry) => entry !== null && typeof entry === "object" && typeof entry.service === "string" && typeof entry.space === "string" && typeof entry.path === "string" && Array.isArray(entry.actions) && entry.actions.every((action) => typeof action === "string") && Array.isArray(entry.caveats) && entry.caveats.every(
21023
- (caveat) => caveat !== null && typeof caveat === "object" && !Array.isArray(caveat)
21024
- )
21025
- )) {
21026
- throw new UnsupportedSessionRestoreError(
21027
- "it cannot reconstruct caveat-preserving persisted ReCap authority"
21028
- );
21029
- }
21030
- stagedRecap = exactRecap.map((entry) => ({
21031
- service: entry.service,
21032
- space: entry.space,
21033
- path: entry.path,
21034
- actions: [...entry.actions],
21035
- caveats: cloneRecapCaveats(entry.caveats)
21036
- }));
21044
+ stagedRecap = exactVerifiedRecap(verified);
21037
21045
  const signedExpiry = verified.expiresAt === void 0 ? void 0 : persistedExpiry(verified.expiresAt);
21038
21046
  const persistedPolicyExpiry = sessionData.expiresAt === void 0 ? void 0 : persistedExpiry(sessionData.expiresAt);
21039
21047
  if (signedExpiry && persistedPolicyExpiry && !sameInstant(signedExpiry, persistedPolicyExpiry)) {
@@ -22894,6 +22902,42 @@ var _TinyCloudNode = class _TinyCloudNode {
22894
22902
  (left, right) => left.service.localeCompare(right.service) || (left.space ?? "").localeCompare(right.space ?? "") || left.path.localeCompare(right.path) || left.actions.join("\0").localeCompare(right.actions.join("\0"))
22895
22903
  );
22896
22904
  }
22905
+ /**
22906
+ * Verify a wallet-signed session grant (a CACAO built from a SIWE and its
22907
+ * EIP-191 signature) issued to this instance's live session key. The
22908
+ * verifier rebuilds the CACAO from `siwe` and `signature`, so the grant is
22909
+ * accepted only when its authorization bytes and CID are exactly that CACAO,
22910
+ * its signer is `address`, its audience is this session key, it is valid
22911
+ * now, and its ReCap covers `spaceId` or names only raw encryption networks
22912
+ * (a decrypt-only grant has no space). Returns the signed ReCap and expiry.
22913
+ * Bindings without `validateSessionGrant` fall back to the session verifier,
22914
+ * which also refuses decrypt-only grants.
22915
+ */
22916
+ verifySessionGrant(grant) {
22917
+ const bindings = this.wasmBindings;
22918
+ const validate5 = typeof bindings.validateSessionGrant === "function" ? bindings.validateSessionGrant.bind(bindings) : bindings.validatePersistedSession?.bind(bindings);
22919
+ if (validate5 === void 0) {
22920
+ throw new UnsupportedSessionRestoreError("it cannot verify persisted SIWE authority");
22921
+ }
22922
+ const verified = validate5({
22923
+ delegationHeader: { Authorization: grant.delegationHeader.Authorization },
22924
+ delegationCid: grant.delegationCid,
22925
+ spaceId: grant.spaceId,
22926
+ jwk: this.sessionKeyJwk,
22927
+ address: (0, import_sdk_core7.canonicalizeAddress)(grant.address),
22928
+ chainId: grant.chainId,
22929
+ siwe: grant.siwe,
22930
+ signature: grant.signature
22931
+ });
22932
+ if (typeof verified.expiresAt !== "string") {
22933
+ throw new Error("Session grant has no signed expiry.");
22934
+ }
22935
+ const expiresAt = new Date(verified.expiresAt);
22936
+ if (!Number.isFinite(expiresAt.getTime())) {
22937
+ throw new Error("Session grant has an invalid signed expiry.");
22938
+ }
22939
+ return { recap: exactVerifiedRecap(verified), expiresAt };
22940
+ }
22897
22941
  /**
22898
22942
  * Install a portable runtime permission delegation into this SDK instance so
22899
22943
  * matching service calls and downstream `delegateTo()` calls can use it.
@@ -23026,7 +23070,8 @@ var _TinyCloudNode = class _TinyCloudNode {
23026
23070
  delegatedEntries,
23027
23071
  spaceId,
23028
23072
  session,
23029
- expiresAt
23073
+ expiresAt,
23074
+ { siwe: prepared.siwe, signature: signature2 }
23030
23075
  );
23031
23076
  this.runtimePermissionGrants.push({
23032
23077
  session: {
@@ -23812,7 +23857,7 @@ var _TinyCloudNode = class _TinyCloudNode {
23812
23857
  }
23813
23858
  return grants;
23814
23859
  }
23815
- runtimeDelegationFromSession(delegatedSession, entries, spaceId, session, expiresAt) {
23860
+ runtimeDelegationFromSession(delegatedSession, entries, spaceId, session, expiresAt, siweProof) {
23816
23861
  const resources = this.delegatedResourcesForEntries(entries, spaceId);
23817
23862
  const primary = resources[0];
23818
23863
  return {
@@ -23827,7 +23872,8 @@ var _TinyCloudNode = class _TinyCloudNode {
23827
23872
  delegateDID: session.verificationMethod,
23828
23873
  ownerAddress: session.address,
23829
23874
  chainId: session.chainId,
23830
- host: this.config.host
23875
+ host: this.config.host,
23876
+ siweProof
23831
23877
  };
23832
23878
  }
23833
23879
  runtimeGrantFromDelegation(delegation, session) {
@@ -24020,6 +24066,9 @@ var _TinyCloudNode = class _TinyCloudNode {
24020
24066
  if (grantedAction === requestedAction) {
24021
24067
  return true;
24022
24068
  }
24069
+ if ((0, import_sdk_core7.isExplicitOnlyAction)(requestedAction)) {
24070
+ return false;
24071
+ }
24023
24072
  if (grantedAction.endsWith("/*")) {
24024
24073
  const prefix = grantedAction.slice(0, -2);
24025
24074
  return requestedAction.startsWith(`${prefix}/`);
@@ -24697,6 +24746,29 @@ var import_sdk_core12 = require("@tinycloud/sdk-core");
24697
24746
 
24698
24747
  // src/delegation.ts
24699
24748
  var import_sdk_core9 = require("@tinycloud/sdk-core");
24749
+ var RUNTIME_DELEGATION_REJECTIONS = [
24750
+ "host_mismatch",
24751
+ "expired",
24752
+ "cid_mismatch",
24753
+ "authority_invalid",
24754
+ "proof_missing",
24755
+ "proof_invalid",
24756
+ "owner_mismatch",
24757
+ "audience_mismatch",
24758
+ "resources_mismatch",
24759
+ "unsupported",
24760
+ "activation_rejected"
24761
+ ];
24762
+ var RuntimeDelegationRejectedError = class extends Error {
24763
+ constructor(reason, message) {
24764
+ super(message);
24765
+ this.name = "RuntimeDelegationRejectedError";
24766
+ this.reason = reason;
24767
+ }
24768
+ };
24769
+ function reject(reason, message) {
24770
+ throw new RuntimeDelegationRejectedError(reason, message);
24771
+ }
24700
24772
  var SERVICE_BY_SHORT_NAME = {
24701
24773
  kv: "tinycloud.kv",
24702
24774
  sql: "tinycloud.sql",
@@ -24843,6 +24915,15 @@ function signedAuthorityFromCompactUcan(authorization) {
24843
24915
  abilities
24844
24916
  ));
24845
24917
  }
24918
+ return canonicalSignedAuthority({
24919
+ issuer: payload.iss,
24920
+ audience: payload.aud,
24921
+ expiry,
24922
+ proofs: [...payload.prf],
24923
+ ...remainingRedelegationDepth === void 0 ? {} : { remainingRedelegationDepth }
24924
+ }, permissions);
24925
+ }
24926
+ function canonicalSignedAuthority(signed2, permissions) {
24846
24927
  if (permissions.length === 0) {
24847
24928
  throw new Error("Validated runtime delegation has no signed capabilities.");
24848
24929
  }
@@ -24854,15 +24935,84 @@ function signedAuthorityFromCompactUcan(authorization) {
24854
24935
  actions: [...permission.actions],
24855
24936
  ...permission.caveats === void 0 ? {} : { caveats: permission.caveats }
24856
24937
  }));
24857
- return {
24858
- issuer: payload.iss,
24859
- audience: payload.aud,
24860
- expiry,
24861
- proofs: [...payload.prf],
24862
- ...remainingRedelegationDepth === void 0 ? {} : { remainingRedelegationDepth },
24863
- permissions: canonicalPermissions,
24864
- resources: canonicalResources
24865
- };
24938
+ return { ...signed2, permissions: canonicalPermissions, resources: canonicalResources };
24939
+ }
24940
+ var RAW_ENCRYPTION_PREFIX = "urn:tinycloud:encryption:";
24941
+ var PKH_SPACE = /^tinycloud:pkh:eip155:(\d+):(0x[0-9a-fA-F]{40}):(.+)$/;
24942
+ function comparableSpace(space) {
24943
+ const match = PKH_SPACE.exec(space);
24944
+ return match ? `tinycloud:pkh:eip155:${match[1]}:${match[2].toLowerCase()}:${match[3]}` : space;
24945
+ }
24946
+ function signedAuthorityFromSessionGrant(node, delegation, authorization, cid) {
24947
+ const proof = delegation.siweProof;
24948
+ if (proof === void 0 || proof === null || typeof proof !== "object" || typeof proof.siwe !== "string" || typeof proof.signature !== "string") {
24949
+ reject(
24950
+ "proof_missing",
24951
+ "Runtime delegation is a wallet-signed session grant stored without its signed SIWE proof, so its authority cannot be verified."
24952
+ );
24953
+ }
24954
+ if (typeof node.verifySessionGrant !== "function") {
24955
+ reject("unsupported", "This runtime cannot verify wallet-signed session grants.");
24956
+ }
24957
+ const owner = `did:pkh:eip155:${delegation.chainId}:${delegation.ownerAddress}`;
24958
+ if (typeof node.did !== "string" || !didPrincipalsMatch(owner, node.did)) {
24959
+ reject("owner_mismatch", "Runtime delegation is not signed by this session's owner.");
24960
+ }
24961
+ let verified;
24962
+ try {
24963
+ verified = node.verifySessionGrant({
24964
+ delegationHeader: { Authorization: authorization },
24965
+ delegationCid: cid,
24966
+ spaceId: delegation.spaceId,
24967
+ address: delegation.ownerAddress,
24968
+ chainId: delegation.chainId,
24969
+ siwe: proof.siwe,
24970
+ signature: proof.signature
24971
+ });
24972
+ } catch (error) {
24973
+ reject("proof_invalid", error instanceof Error ? error.message : String(error));
24974
+ }
24975
+ const space = comparableSpace(delegation.spaceId);
24976
+ const spaceOwner = PKH_SPACE.exec(delegation.spaceId);
24977
+ if (!spaceOwner || !didPrincipalsMatch(`did:pkh:eip155:${spaceOwner[1]}:${spaceOwner[2]}`, owner)) {
24978
+ reject("authority_invalid", "Runtime delegation space is not owned by its signer.");
24979
+ }
24980
+ const groups = /* @__PURE__ */ new Map();
24981
+ for (const entry of verified.recap) {
24982
+ const service = entry.service.startsWith("tinycloud.") ? entry.service : `tinycloud.${entry.service}`;
24983
+ const raw = entry.space === "encryption";
24984
+ if (raw) {
24985
+ if (service !== "tinycloud.encryption" || !entry.path.startsWith(RAW_ENCRYPTION_PREFIX)) {
24986
+ reject("authority_invalid", "Runtime delegation has an unsupported signed raw resource.");
24987
+ }
24988
+ const networkOwner = entry.path.slice(RAW_ENCRYPTION_PREFIX.length, entry.path.lastIndexOf(":"));
24989
+ if (!didPrincipalsMatch(networkOwner, owner)) {
24990
+ reject("authority_invalid", "Runtime delegation names an encryption network its signer does not own.");
24991
+ }
24992
+ } else if (service === "tinycloud.encryption" || comparableSpace(entry.space) !== space) {
24993
+ reject("authority_invalid", "Runtime delegation has signed authority outside its space.");
24994
+ }
24995
+ const caveats = signedCaveats(entry.caveats ?? []);
24996
+ const key2 = JSON.stringify([service, raw ? "" : entry.space, entry.path, (0, import_sdk_core9.canonicalizeRecapCaveats)(caveats)]);
24997
+ const group = groups.get(key2) ?? {
24998
+ service,
24999
+ ...raw ? {} : { space: entry.space },
25000
+ path: entry.path,
25001
+ actions: [],
25002
+ ...caveats === void 0 ? {} : { caveats }
25003
+ };
25004
+ for (const action of entry.actions) {
25005
+ if (!group.actions.includes(action)) group.actions.push(action);
25006
+ }
25007
+ groups.set(key2, group);
25008
+ }
25009
+ return canonicalSignedAuthority({
25010
+ issuer: owner,
25011
+ // The verifier bound the SIWE audience to this runtime's own session key.
25012
+ audience: node.sessionDid,
25013
+ expiry: verified.expiresAt,
25014
+ proofs: []
25015
+ }, [...groups.values()]);
24866
25016
  }
24867
25017
  function canonicalResourcesFromPortableDelegation(delegation) {
24868
25018
  if (delegation.resources !== void 0 && delegation.resources.length > 0) {
@@ -24898,49 +25048,58 @@ function resourcesMatch(actual, expected) {
24898
25048
  async function activateValidatedRuntimeDelegation(node, delegation, options) {
24899
25049
  const host = normalizedHost(options.host);
24900
25050
  if (delegation.host !== void 0 && normalizedHost(delegation.host) !== host) {
24901
- throw new Error(
25051
+ reject(
25052
+ "host_mismatch",
24902
25053
  `Runtime delegation host '${delegation.host}' does not match expected host '${options.host}'.`
24903
25054
  );
24904
25055
  }
24905
25056
  if (!(delegation.expiry instanceof Date) || Number.isNaN(delegation.expiry.getTime())) {
24906
- throw new Error("Runtime delegation has an invalid expiry.");
25057
+ reject("authority_invalid", "Runtime delegation has an invalid expiry.");
24907
25058
  }
24908
25059
  if (delegation.expiry.getTime() <= Date.now()) {
24909
- throw new Error("Runtime delegation is expired.");
25060
+ reject("expired", "Runtime delegation is expired.");
24910
25061
  }
24911
25062
  const authorization = delegation.delegationHeader?.Authorization;
24912
25063
  if (typeof authorization !== "string" || authorization.length === 0) {
24913
- throw new Error("Runtime delegation is missing authorization bytes.");
25064
+ reject("authority_invalid", "Runtime delegation is missing authorization bytes.");
24914
25065
  }
24915
25066
  const cid = node.computeDelegationCid(authorization);
24916
25067
  if (cid !== delegation.cid) {
24917
- throw new Error("Runtime delegation CID does not match authorization bytes.");
25068
+ reject("cid_mismatch", "Runtime delegation CID does not match authorization bytes.");
25069
+ }
25070
+ const compact = authorizationWithoutBearer(authorization).split(".").length === 3;
25071
+ let signed2;
25072
+ try {
25073
+ signed2 = compact ? signedAuthorityFromCompactUcan(authorization) : signedAuthorityFromSessionGrant(node, delegation, authorization, cid);
25074
+ } catch (error) {
25075
+ if (error instanceof RuntimeDelegationRejectedError) throw error;
25076
+ reject("authority_invalid", error instanceof Error ? error.message : String(error));
24918
25077
  }
24919
- const signed2 = signedAuthorityFromCompactUcan(authorization);
24920
25078
  if (signed2.expiry.getTime() <= Date.now()) {
24921
- throw new Error("Runtime delegation is expired.");
25079
+ reject("expired", "Runtime delegation is expired.");
24922
25080
  }
24923
25081
  if (delegation.expiry.getTime() !== signed2.expiry.getTime()) {
24924
- throw new Error("Runtime delegation expiry does not match signed authority.");
25082
+ reject("authority_invalid", "Runtime delegation expiry does not match signed authority.");
24925
25083
  }
24926
25084
  if (!didPrincipalsMatch(signed2.audience, node.sessionDid)) {
24927
- throw new Error(
25085
+ reject(
25086
+ "audience_mismatch",
24928
25087
  `Runtime delegation targets ${signed2.audience} but this session is ${node.sessionDid}.`
24929
25088
  );
24930
25089
  }
24931
25090
  if (!didPrincipalsMatch(delegation.delegateDID, signed2.audience)) {
24932
- throw new Error("Runtime delegation audience does not match signed authority.");
25091
+ reject("audience_mismatch", "Runtime delegation audience does not match signed authority.");
24933
25092
  }
24934
- const declaredResources = canonicalResourcesFromPortableDelegation(delegation);
24935
- if (!resourcesMatch(declaredResources, signed2.resources)) {
24936
- throw new Error("Runtime delegation resources do not match signed authority.");
25093
+ if (compact && !resourcesMatch(canonicalResourcesFromPortableDelegation(delegation), signed2.resources)) {
25094
+ reject("resources_mismatch", "Runtime delegation resources do not match signed authority.");
24937
25095
  }
24938
25096
  if (options.authorize !== void 0 && !options.authorize(signed2.permissions)) {
24939
25097
  throw new Error("Runtime delegation is not authorized for this runtime.");
24940
25098
  }
24941
25099
  const previouslyInstalled = node.getRuntimePermissionDelegations().find((candidate) => candidate.cid === cid);
24942
25100
  if (previouslyInstalled !== void 0 && previouslyInstalled.delegationHeader.Authorization !== authorization) {
24943
- throw new Error(
25101
+ reject(
25102
+ "cid_mismatch",
24944
25103
  "A different authorization is already installed for this runtime delegation CID."
24945
25104
  );
24946
25105
  }
@@ -24970,22 +25129,28 @@ async function activateValidatedRuntimeDelegation(node, delegation, options) {
24970
25129
  })),
24971
25130
  expiry: new Date(signed2.expiry),
24972
25131
  delegateDID: signed2.audience,
24973
- host
25132
+ host,
25133
+ ...compact ? {} : { siweProof: delegation.siweProof }
24974
25134
  };
24975
- if (node.isSessionOnly === true && typeof node.did === "string" && (0, import_sdk_core9.principalDidEquals)(node.did, node.sessionDid) && typeof node.useDelegation === "function") {
24976
- await node.useDelegation(installedCandidate);
24977
- } else {
24978
- await node.useRuntimeDelegation(installedCandidate);
25135
+ try {
25136
+ if (node.isSessionOnly === true && typeof node.did === "string" && (0, import_sdk_core9.principalDidEquals)(node.did, node.sessionDid) && typeof node.useDelegation === "function") {
25137
+ await node.useDelegation(installedCandidate);
25138
+ } else {
25139
+ await node.useRuntimeDelegation(installedCandidate);
25140
+ }
25141
+ } catch (error) {
25142
+ reject("activation_rejected", error instanceof Error ? error.message : String(error));
24979
25143
  }
24980
25144
  const installed = node.getRuntimePermissionDelegations().find((candidate) => candidate.cid === cid);
24981
25145
  if (!installed) {
24982
- throw new Error("Runtime delegation activation did not install the validated authority.");
25146
+ reject("activation_rejected", "Runtime delegation activation did not install the validated authority.");
24983
25147
  }
24984
25148
  if (installed.delegationHeader.Authorization !== authorization || !didPrincipalsMatch(installed.delegateDID, signed2.audience) || installed.expiry.getTime() !== signed2.expiry.getTime() || !resourcesMatch(
24985
25149
  canonicalResourcesFromPortableDelegation(installed),
24986
25150
  signed2.resources
24987
25151
  ) || normalizedHost(installed.host ?? "") !== host) {
24988
- throw new Error(
25152
+ reject(
25153
+ "activation_rejected",
24989
25154
  "Runtime delegation activation installed authority that differs from the validated authority."
24990
25155
  );
24991
25156
  }
@@ -25134,6 +25299,8 @@ var import_sdk_core23 = require("@tinycloud/sdk-core");
25134
25299
  PrefixedKVService,
25135
25300
  PrivateKeySigner,
25136
25301
  ProtocolMismatchError,
25302
+ RUNTIME_DELEGATION_REJECTIONS,
25303
+ RuntimeDelegationRejectedError,
25137
25304
  SECRET_NAME_RE,
25138
25305
  SQLAction,
25139
25306
  SQLService,