@tinycloud/share-sdk 1.2.0-beta.2 → 1.2.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.
@@ -21,8 +21,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var share_root_entry_exports = {};
22
22
  __export(share_root_entry_exports, {
23
23
  AGENT_SHARE_ROOT_MAX_LIFETIME_MS: () => AGENT_SHARE_ROOT_MAX_LIFETIME_MS,
24
- POLICY_OWNER_AUTHORITY_FEATURES: () => POLICY_OWNER_AUTHORITY_FEATURES,
25
- POLICY_OWNER_AUTHORITY_PROBE_TTL_MS: () => POLICY_OWNER_AUTHORITY_PROBE_TTL_MS,
24
+ EDIT_SHARE_READER_MAX_LIFETIME_MS: () => EDIT_SHARE_READER_MAX_LIFETIME_MS,
25
+ SHARE_CONTROL_HEADER_BUDGET_BYTES: () => SHARE_CONTROL_HEADER_BUDGET_BYTES,
26
+ SHARE_CONTROL_LIFETIME_MS: () => SHARE_CONTROL_LIFETIME_MS,
27
+ SHARE_NETWORK_NAME_PREFIX: () => SHARE_NETWORK_NAME_PREFIX,
26
28
  SHARE_ROOT_CLOCK_SKEW_MS: () => SHARE_ROOT_CLOCK_SKEW_MS,
27
29
  SHARE_ROOT_LINK_MAX_LIFETIME_MS: () => SHARE_ROOT_LINK_MAX_LIFETIME_MS,
28
30
  SHARE_ROOT_MAX_LIFETIME_MS: () => SHARE_ROOT_MAX_LIFETIME_MS,
@@ -31,15 +33,17 @@ __export(share_root_entry_exports, {
31
33
  ShareGrant: () => ShareGrant,
32
34
  ShareRoot: () => ShareRoot,
33
35
  ShareRootError: () => ShareRootError,
36
+ assertNetworkSafeShareId: () => assertNetworkSafeShareId,
34
37
  assertNonExtractableShareKey: () => assertNonExtractableShareKey,
35
- createPolicyOwnerModeProbe: () => createPolicyOwnerModeProbe,
38
+ assertShareGrantPath: () => assertShareGrantPath,
36
39
  decodeOwnerRootCacao: () => decodeOwnerRootCacao,
37
- defaultPolicyOwnerModeProbe: () => defaultPolicyOwnerModeProbe,
38
- ensureShareNetwork: () => ensureShareNetwork,
39
40
  ensureShareRoot: () => ensureShareRoot,
41
+ openEditShare: () => openEditShare,
40
42
  ownerRootCacaoCid: () => ownerRootCacaoCid,
41
- readPolicyOwnerAuthorityMode: () => readPolicyOwnerAuthorityMode,
42
- shareNetworkIdFor: () => shareNetworkIdFor,
43
+ perShareNetworkIdFor: () => perShareNetworkIdFor,
44
+ planShareControlBatches: () => planShareControlBatches,
45
+ revokeShareGrant: () => revokeShareGrant,
46
+ runShareControlBatch: () => runShareControlBatch,
43
47
  signShareKeyDelegation: () => signShareKeyDelegation
44
48
  });
45
49
  module.exports = __toCommonJS(share_root_entry_exports);
@@ -6617,27 +6621,20 @@ var AGENT_SHARE_ROOT_MAX_LIFETIME_MS = 30 * DAY_MS;
6617
6621
  var SHARE_ROOT_LINK_MAX_LIFETIME_MS = 30 * DAY_MS;
6618
6622
  var SHARE_ROOT_CLOCK_SKEW_MS = 5 * 6e4;
6619
6623
  var SHARE_ROOT_REQUEST_MARGIN_MS = 10 * 6e4;
6620
- var POLICY_OWNER_AUTHORITY_FEATURES = {
6621
- marker: "policy-owner-authority-v1",
6622
- record: "policy-owner-authority-record",
6623
- enforce: "policy-owner-authority-enforce"
6624
- };
6625
- var POLICY_OWNER_AUTHORITY_PROBE_TTL_MS = 5 * 6e4;
6626
- var MAX_INFO_BYTES = 64 * 1024;
6624
+ var MAX_NODE_ANSWER_BYTES = 64 * 1024;
6627
6625
  var MAX_POLICY_ROOT_MS = 31 * DAY_MS;
6628
6626
  var PROFILES = {
6629
6627
  share: { spaceName: "applications", maxLifetimeMs: SHARE_ROOT_MAX_LIFETIME_MS },
6630
6628
  agent: { spaceName: "default", maxLifetimeMs: AGENT_SHARE_ROOT_MAX_LIFETIME_MS }
6631
6629
  };
6632
6630
  var KV_ABILITIES = ["tinycloud.kv/get", "tinycloud.kv/metadata"];
6633
- var DECRYPT_ABILITY = "tinycloud.encryption/decrypt";
6634
6631
  var ShareRootError = class extends Error {
6635
6632
  code;
6636
6633
  rootCid;
6637
6634
  signedCid;
6638
6635
  mustRevoke;
6639
6636
  constructor(code3, message, details = {}) {
6640
- super(details.mustRevoke === true && details.rootCid !== void 0 ? `${message}; the Node imported root ${details.rootCid}, which may grant more than requested: the owner must revoke it` : message);
6637
+ super(details.mustRevoke === true && details.rootCid !== void 0 ? `${message}; the Node imported ${details.rootCid}, which may grant more than requested: the owner must revoke it` : message);
6641
6638
  this.name = "ShareRootError";
6642
6639
  this.code = code3;
6643
6640
  this.mustRevoke = details.mustRevoke === true;
@@ -6647,7 +6644,7 @@ var ShareRootError = class extends Error {
6647
6644
  };
6648
6645
  function parseOwnerDid(ownerDid) {
6649
6646
  const match = /^did:pkh:eip155:([1-9]\d*):(0x[0-9a-fA-F]{40})$/.exec(ownerDid);
6650
- if (match === null) throw new ShareRootError("invalid-request", "share root owner must be did:pkh:eip155");
6647
+ if (match === null) throw new ShareRootError("invalid-request", "share owner must be did:pkh:eip155");
6651
6648
  return { chainId: match[1], address: match[2].toLowerCase() };
6652
6649
  }
6653
6650
  function sameOwner(left, right) {
@@ -6656,16 +6653,9 @@ function sameOwner(left, right) {
6656
6653
  function assertSpace(spaceId, owner, kind) {
6657
6654
  const match = /^tinycloud:pkh:eip155:([1-9]\d*):(0x[0-9a-fA-F]{40}):([A-Za-z0-9_-]+)$/.exec(spaceId);
6658
6655
  if (match === null || !sameOwner({ chainId: match[1], address: match[2].toLowerCase() }, owner) || match[3] !== PROFILES[kind].spaceName) {
6659
- throw new ShareRootError("invalid-request", `${kind} root must target the owner's ${PROFILES[kind].spaceName} space`);
6656
+ throw new ShareRootError("invalid-request", `${kind} delegations must target the owner's ${PROFILES[kind].spaceName} space`);
6660
6657
  }
6661
6658
  }
6662
- function shareNetworkId(owner) {
6663
- return `urn:tinycloud:encryption:did:pkh:eip155:${owner.chainId}:${owner.address}:share`;
6664
- }
6665
- function isShareNetworkOf(resource, owner) {
6666
- const match = /^urn:tinycloud:encryption:did:pkh:eip155:([1-9]\d*):(0x[0-9a-fA-F]{40}):share$/.exec(resource);
6667
- return match !== null && sameOwner({ chainId: match[1], address: match[2].toLowerCase() }, owner);
6668
- }
6669
6659
  function nodeOriginOf(value) {
6670
6660
  let url;
6671
6661
  try {
@@ -6686,7 +6676,7 @@ function seconds(ms) {
6686
6676
  }
6687
6677
  function parseTime(value, label, rootCid) {
6688
6678
  const time = value === void 0 ? Number.NaN : Date.parse(value);
6689
- if (!Number.isFinite(time)) throw new ShareRootError("malformed-root", `share root ${label} is missing or invalid`, { rootCid, mustRevoke: true });
6679
+ if (!Number.isFinite(time)) throw new ShareRootError("malformed-root", `delegation ${label} is missing or invalid`, { rootCid, mustRevoke: true });
6690
6680
  return time;
6691
6681
  }
6692
6682
  function validateSignedOwnerDelegation(decoded, expected) {
@@ -6703,19 +6693,22 @@ function validateSignedOwnerDelegation(decoded, expected) {
6703
6693
  if (decoded.resources.length !== 1 || !decoded.resources[0].startsWith("urn:recap:")) throw new ShareRootError("resource-mismatch", `${label} must carry exactly one ReCap resource`, wider);
6704
6694
  if (decoded.reCapProofs.length !== 0) throw new ShareRootError("resource-mismatch", `${label} must not have parent capabilities`, wider);
6705
6695
  const { wanted } = expected;
6706
- for (const resource of Object.keys(decoded.attenuation)) if (wanted[resource] === void 0) throw new ShareRootError("resource-mismatch", `${label} grants a resource outside its scope`, wider);
6707
- for (const [resource, abilities] of Object.entries(wanted)) {
6708
- const granted = decoded.attenuation[resource];
6709
- if (granted === void 0) throw new ShareRootError("resource-mismatch", `${label} is missing one of its resources`, wider);
6710
- const names = Object.keys(granted);
6711
- if (names.length !== abilities.length || abilities.some((ability) => !names.includes(ability))) throw new ShareRootError("ability-mismatch", `${label} abilities differ from the request`, wider);
6712
- for (const ability of abilities) {
6713
- const caveats = granted[ability];
6696
+ for (const [resource, granted] of Object.entries(decoded.attenuation)) {
6697
+ const abilities = wanted[resource];
6698
+ if (abilities === void 0) throw new ShareRootError("resource-mismatch", `${label} grants a resource outside its scope`, wider);
6699
+ for (const [ability, caveats] of Object.entries(granted)) {
6700
+ if (!abilities.includes(ability)) throw new ShareRootError("ability-mismatch", `${label} grants an ability it was not asked for`, wider);
6714
6701
  if (caveats.length !== 1 || typeof caveats[0] !== "object" || caveats[0] === null || Array.isArray(caveats[0]) || Object.keys(caveats[0]).length !== 0) {
6715
6702
  throw new ShareRootError("ability-mismatch", `${label} abilities carry unexpected caveats`, wider);
6716
6703
  }
6717
6704
  }
6718
6705
  }
6706
+ const narrower = { rootCid: decoded.cid };
6707
+ for (const [resource, abilities] of Object.entries(wanted)) {
6708
+ const granted = decoded.attenuation[resource];
6709
+ if (granted === void 0) throw new ShareRootError("resource-mismatch", `${label} is narrower than requested: a resource is missing`, narrower);
6710
+ if (abilities.some((ability) => granted[ability] === void 0)) throw new ShareRootError("ability-mismatch", `${label} is narrower than requested: an ability is missing`, narrower);
6711
+ }
6719
6712
  const issuedAt = parseTime(decoded.issuedAt, "issued-at time", decoded.cid);
6720
6713
  const expiresAt = parseTime(decoded.expiresAt, "expiration time", decoded.cid);
6721
6714
  if (decoded.notBefore !== void 0 && parseTime(decoded.notBefore, "not-before time", decoded.cid) > expected.now) throw new ShareRootError("malformed-root", `${label} is not valid yet`, { rootCid: decoded.cid });
@@ -6728,20 +6721,21 @@ function validateSignedOwnerDelegation(decoded, expected) {
6728
6721
  return { issuedAt: new Date(issuedAt), expiresAt: new Date(expiresAt) };
6729
6722
  }
6730
6723
  function validateSignedRoot(decoded, expected) {
6731
- const wanted = { [`${expected.spaceId}/kv/${SHARE_ROOT_PREFIX}`]: KV_ABILITIES };
6732
- if (expected.scope === "kv+share-decrypt") wanted[shareNetworkId(expected.owner)] = [DECRYPT_ABILITY];
6733
6724
  return validateSignedOwnerDelegation(decoded, {
6734
6725
  label: `${expected.kind} root`,
6735
6726
  owner: expected.owner,
6736
6727
  keyDid: expected.keyDid,
6737
- wanted,
6728
+ wanted: { [`${expected.spaceId}/kv/${SHARE_ROOT_PREFIX}`]: KV_ABILITIES },
6738
6729
  maxLifetimeMs: PROFILES[expected.kind].maxLifetimeMs,
6739
6730
  ...expected.requestedExpiresAt === void 0 ? {} : { requestedExpiresAt: expected.requestedExpiresAt },
6740
6731
  now: expected.now
6741
6732
  });
6742
6733
  }
6734
+ function isExactKeyUnder(path, prefix) {
6735
+ return typeof path === "string" && path.startsWith(prefix) && path.length > prefix.length && !path.endsWith("/") && !path.includes("//") && !path.split("/").some((segment) => segment === "." || segment === "..") && !/[\u0000-\u001f\u007f\\*?#]/.test(path);
6736
+ }
6743
6737
  function assertLinkPath(path) {
6744
- if (typeof path !== "string" || !path.startsWith(SHARE_ROOT_PREFIX) || path.length === SHARE_ROOT_PREFIX.length || path.endsWith("/") || path.includes("//") || path.split("/").some((segment) => segment === "." || segment === "..") || /[\u0000-\u001f\u007f\\*?#]/.test(path)) throw new ShareRootError("link-out-of-scope", "link path must be one exact key under the share prefix");
6738
+ if (!isExactKeyUnder(path, SHARE_ROOT_PREFIX)) throw new ShareRootError("link-out-of-scope", "link path must be one exact key under the share prefix");
6745
6739
  }
6746
6740
  function errorMessage(error) {
6747
6741
  return error instanceof Error ? error.message : String(error);
@@ -6753,6 +6747,43 @@ async function assertUsableKey(key) {
6753
6747
  throw new ShareRootError("invalid-key", errorMessage(error));
6754
6748
  }
6755
6749
  }
6750
+ async function readJsonCapped(response) {
6751
+ if (!/^application\/json\s*(;|$)/i.test(response.headers.get("content-type") ?? "")) throw new Error("node answer is not JSON");
6752
+ const reader = response.body?.getReader();
6753
+ if (reader === void 0) throw new Error("node answer has no body");
6754
+ const chunks = [];
6755
+ let total = 0;
6756
+ for (; ; ) {
6757
+ const { done, value } = await reader.read();
6758
+ if (done) break;
6759
+ total += value.byteLength;
6760
+ if (total > MAX_NODE_ANSWER_BYTES) {
6761
+ await reader.cancel();
6762
+ throw new Error("node answer is too large");
6763
+ }
6764
+ chunks.push(value);
6765
+ }
6766
+ const bytes2 = new Uint8Array(total);
6767
+ let offset = 0;
6768
+ for (const chunk of chunks) {
6769
+ bytes2.set(chunk, offset);
6770
+ offset += chunk.byteLength;
6771
+ }
6772
+ return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes2));
6773
+ }
6774
+ async function isRevokeReceiptFor(response, cid) {
6775
+ if (!response.ok) {
6776
+ await response.body?.cancel();
6777
+ return false;
6778
+ }
6779
+ let receipt;
6780
+ try {
6781
+ receipt = await readJsonCapped(response);
6782
+ } catch {
6783
+ return false;
6784
+ }
6785
+ return typeof receipt === "object" && receipt !== null && "revoked" in receipt && receipt.revoked === true && "cid" in receipt && receipt.cid === cid;
6786
+ }
6756
6787
  function policyRootContents(input) {
6757
6788
  const facts = {
6758
6789
  role: input.role,
@@ -6811,69 +6842,8 @@ function ownerPolicySigner(input) {
6811
6842
  }
6812
6843
  };
6813
6844
  }
6814
- function readPolicyOwnerAuthorityMode(features) {
6815
- if (!Array.isArray(features) || !features.includes(POLICY_OWNER_AUTHORITY_FEATURES.marker)) return void 0;
6816
- const enforce = features.includes(POLICY_OWNER_AUTHORITY_FEATURES.enforce);
6817
- const record = features.includes(POLICY_OWNER_AUTHORITY_FEATURES.record);
6818
- if (enforce === record) return void 0;
6819
- return enforce ? "enforce" : "record";
6820
- }
6821
- async function readJsonCapped(response) {
6822
- if (!/^application\/json\s*(;|$)/i.test(response.headers.get("content-type") ?? "")) throw new Error("node info is not JSON");
6823
- const reader = response.body?.getReader();
6824
- if (reader === void 0) throw new Error("node info has no body");
6825
- const chunks = [];
6826
- let total = 0;
6827
- for (; ; ) {
6828
- const { done, value } = await reader.read();
6829
- if (done) break;
6830
- total += value.byteLength;
6831
- if (total > MAX_INFO_BYTES) {
6832
- await reader.cancel();
6833
- throw new Error("node info is too large");
6834
- }
6835
- chunks.push(value);
6836
- }
6837
- const bytes2 = new Uint8Array(total);
6838
- let offset = 0;
6839
- for (const chunk of chunks) {
6840
- bytes2.set(chunk, offset);
6841
- offset += chunk.byteLength;
6842
- }
6843
- return JSON.parse(new TextDecoder("utf-8", { fatal: true }).decode(bytes2));
6844
- }
6845
- function createPolicyOwnerModeProbe(options = {}) {
6846
- const fetchFn = options.fetch ?? ((input, init) => globalThis.fetch(input, init));
6847
- const ttlMs = Math.min(options.ttlMs ?? POLICY_OWNER_AUTHORITY_PROBE_TTL_MS, POLICY_OWNER_AUTHORITY_PROBE_TTL_MS);
6848
- const now = options.now ?? Date.now;
6849
- const cache3 = /* @__PURE__ */ new Map();
6850
- return {
6851
- async isEnforced(nodeOrigin) {
6852
- const origin = nodeOriginOf(nodeOrigin);
6853
- if (origin === void 0) return false;
6854
- const cached = cache3.get(origin);
6855
- if (cached !== void 0 && now() - cached.checkedAt < ttlMs) return cached.enforced;
6856
- let enforced = false;
6857
- try {
6858
- const response = await fetchFn(new URL("/info", origin), { redirect: "error", signal: AbortSignal.timeout(5e3) });
6859
- const info = response.ok ? await readJsonCapped(response) : void 0;
6860
- enforced = typeof info === "object" && info !== null && "features" in info && readPolicyOwnerAuthorityMode(info.features) === "enforce";
6861
- } catch {
6862
- enforced = false;
6863
- }
6864
- cache3.set(origin, { enforced, checkedAt: now() });
6865
- return enforced;
6866
- },
6867
- observe(nodeOrigin, features) {
6868
- const origin = nodeOriginOf(nodeOrigin);
6869
- if (origin !== void 0) cache3.set(origin, { enforced: readPolicyOwnerAuthorityMode(features) === "enforce", checkedAt: now() });
6870
- }
6871
- };
6872
- }
6873
- var defaultPolicyOwnerModeProbe = createPolicyOwnerModeProbe();
6874
6845
  var ShareRoot = class _ShareRoot {
6875
6846
  kind;
6876
- scope;
6877
6847
  nodeOrigin;
6878
6848
  cid;
6879
6849
  ownerDid;
@@ -6886,7 +6856,6 @@ var ShareRoot = class _ShareRoot {
6886
6856
  #options;
6887
6857
  constructor(input) {
6888
6858
  this.kind = input.kind;
6889
- this.scope = input.scope;
6890
6859
  this.nodeOrigin = input.nodeOrigin;
6891
6860
  this.cid = input.cid;
6892
6861
  this.ownerDid = input.ownerDid;
@@ -6899,12 +6868,10 @@ var ShareRoot = class _ShareRoot {
6899
6868
  this.#options = input.options;
6900
6869
  }
6901
6870
  /**
6902
- * Ask the owner for a new root (one approval) for a fresh share key. The
6903
- * scope is chosen now: `kv+share-decrypt` only for a `share` root with the
6904
- * flag on and `nodeOrigin` advertising `enforce`, otherwise `kv`. All request
6905
- * checks run before anything is signed. If the signed result is rejected,
6906
- * the key is cleared and the error carries the imported `rootCid` (and
6907
- * `mustRevoke` when the root may be wider than requested).
6871
+ * Ask the owner for a new root (one approval) for a fresh share key. All
6872
+ * request checks run before anything is signed. If the signed result is
6873
+ * rejected, the key is cleared and the error carries the imported `rootCid`
6874
+ * (and `mustRevoke` when the root may be wider than requested).
6908
6875
  *
6909
6876
  * `keyFactory` defaults to a fresh non-extractable WebCrypto key that only
6910
6877
  * this root holds. A custom factory must not persist the key before
@@ -6920,7 +6887,6 @@ var ShareRoot = class _ShareRoot {
6920
6887
  const expiry = input.expiresAt instanceof Date ? input.expiresAt.getTime() : Number.NaN;
6921
6888
  if (!Number.isFinite(expiry) || expiry <= now) throw new ShareRootError("invalid-request", "share root expiry must be in the future");
6922
6889
  if (expiry - now > PROFILES[kind].maxLifetimeMs) throw new ShareRootError("lifetime-exceeds-cap", `${kind} root lifetime exceeds ${PROFILES[kind].maxLifetimeMs / DAY_MS} days`);
6923
- const scope = kind === "share" && options.shareRootPolicyRoots === true && await (options.policyOwnerModeProbe ?? defaultPolicyOwnerModeProbe).isEnforced(nodeOrigin) ? "kv+share-decrypt" : "kv";
6924
6890
  const key = await (input.keyFactory ?? (() => createDelegatedShareKey({ extractable: false })))();
6925
6891
  try {
6926
6892
  await assertUsableKey(key);
@@ -6929,7 +6895,6 @@ var ShareRoot = class _ShareRoot {
6929
6895
  throw error;
6930
6896
  }
6931
6897
  const permissions = [{ service: "tinycloud.kv", path: SHARE_ROOT_PREFIX, actions: [...KV_ABILITIES] }];
6932
- if (scope === "kv+share-decrypt") permissions.push({ service: "tinycloud.encryption", path: shareNetworkId(owner), actions: [DECRYPT_ABILITY] });
6933
6898
  let receipt;
6934
6899
  try {
6935
6900
  receipt = await input.owner.createOwnerDelegation({ delegateDid: key.did, spaceId: input.spaceId, expiresAt: input.expiresAt, permissions });
@@ -6947,8 +6912,8 @@ var ShareRoot = class _ShareRoot {
6947
6912
  } catch (error) {
6948
6913
  throw new ShareRootError("malformed-root", errorMessage(error), { rootCid: receipt.delegationCid, mustRevoke: true });
6949
6914
  }
6950
- const times = validateSignedRoot(decoded, { kind, scope, owner, spaceId: input.spaceId, keyDid: key.did, requestedExpiresAt: input.expiresAt, now: (options.now ?? Date.now)() });
6951
- return new _ShareRoot({ kind, scope, nodeOrigin, cid: decoded.cid, ownerDid: input.owner.ownerDid, spaceId: input.spaceId, ...times, authorization, key, options });
6915
+ const times = validateSignedRoot(decoded, { kind, owner, spaceId: input.spaceId, keyDid: key.did, requestedExpiresAt: input.expiresAt, now: (options.now ?? Date.now)() });
6916
+ return new _ShareRoot({ kind, nodeOrigin, cid: decoded.cid, ownerDid: input.owner.ownerDid, spaceId: input.spaceId, ...times, authorization, key, options });
6952
6917
  } catch (error) {
6953
6918
  key.clear();
6954
6919
  if (error instanceof ShareRootError) throw error;
@@ -6959,9 +6924,7 @@ var ShareRoot = class _ShareRoot {
6959
6924
  static async restore(input) {
6960
6925
  const { record } = input;
6961
6926
  const options = { ...input.options, kind: record.kind };
6962
- if (record.version !== 1 || record.kind !== "share" && record.kind !== "agent" || record.scope !== "kv" && record.scope !== "kv+share-decrypt" || record.kind === "agent" && record.scope !== "kv") {
6963
- throw new ShareRootError("malformed-root", "share root record is not supported");
6964
- }
6927
+ if (record.version !== 1 || record.kind !== "share" && record.kind !== "agent") throw new ShareRootError("malformed-root", "share root record is not supported");
6965
6928
  if (record.keyDid !== input.key.did) throw new ShareRootError("audience-mismatch", "share root record belongs to another key");
6966
6929
  const owner = parseOwnerDid(record.ownerDid);
6967
6930
  assertSpace(record.spaceId, owner, record.kind);
@@ -6975,19 +6938,19 @@ var ShareRoot = class _ShareRoot {
6975
6938
  } catch (error) {
6976
6939
  throw new ShareRootError("malformed-root", errorMessage(error), { rootCid: record.cid });
6977
6940
  }
6978
- const times = validateSignedRoot(decoded, { kind: record.kind, scope: record.scope, owner, spaceId: record.spaceId, keyDid: input.key.did, now: (options.now ?? Date.now)() });
6979
- return new _ShareRoot({ kind: record.kind, scope: record.scope, nodeOrigin, cid: decoded.cid, ownerDid: record.ownerDid, spaceId: record.spaceId, ...times, authorization, key: input.key, options });
6941
+ const times = validateSignedRoot(decoded, { kind: record.kind, owner, spaceId: record.spaceId, keyDid: input.key.did, now: (options.now ?? Date.now)() });
6942
+ return new _ShareRoot({ kind: record.kind, nodeOrigin, cid: decoded.cid, ownerDid: record.ownerDid, spaceId: record.spaceId, ...times, authorization, key: input.key, options });
6980
6943
  }
6981
- /** The `Authorization` value the Node knows this root by (base64url CACAO). */
6944
+ /**
6945
+ * This root's CACAO as base64url, UNPADDED (the record form). As a Node
6946
+ * `Authorization` header it must be padded to a multiple of 4 characters:
6947
+ * the Node refuses an unpadded value otherwise.
6948
+ */
6982
6949
  get authorization() {
6983
6950
  return toBase64Url(this.#authorization);
6984
6951
  }
6985
- /** True when the root holds `:share` decrypt and may act as a policy owner (still gated per call). */
6986
- get policyCapable() {
6987
- return this.scope === "kv+share-decrypt";
6988
- }
6989
6952
  toRecord() {
6990
- return { version: 1, kind: this.kind, scope: this.scope, nodeOrigin: this.nodeOrigin, cid: this.cid, authorization: this.authorization, spaceId: this.spaceId, ownerDid: this.ownerDid, keyDid: this.keyDid, expiresAt: this.expiresAt.toISOString() };
6953
+ return { version: 1, kind: this.kind, nodeOrigin: this.nodeOrigin, cid: this.cid, authorization: this.authorization, spaceId: this.spaceId, ownerDid: this.ownerDid, keyDid: this.keyDid, expiresAt: this.expiresAt.toISOString() };
6991
6954
  }
6992
6955
  /** True while the root is live, its key is held, and it lasts at least until `expiresAt`. Revocation is not visible here. */
6993
6956
  covers(expiresAt, now = this.#now()) {
@@ -7024,25 +6987,6 @@ var ShareRoot = class _ShareRoot {
7024
6987
  });
7025
6988
  return { rootCid: this.cid, linkDid: input.linkDid, path: input.path, expiresAt: new Date(seconds(expiry) * 1e3), delegation };
7026
6989
  }
7027
- /**
7028
- * The only ShareRoot API that signs Policy/v3 (email/domain) owner material.
7029
- * Needs a `kv+share-decrypt` root, the `shareRootPolicyRoots` flag, a live
7030
- * root and key, and the root's Node advertising `enforce`; the returned
7031
- * signer re-checks all of it on every call. Until then addressed shares use
7032
- * per-share owner grants. Agent roots are bearer-only.
7033
- */
7034
- async policyRootSigner() {
7035
- await this.#assertPolicyGate();
7036
- const owner = parseOwnerDid(this.ownerDid);
7037
- return ownerPolicySigner({
7038
- ownerDid: this.keyDid,
7039
- authorityCid: this.cid,
7040
- notAfter: new Date(this.expiresAt),
7041
- gate: () => this.#assertPolicyGate(),
7042
- now: () => this.#now(),
7043
- inScope: (capability) => capability.kind === "encryption" ? isShareNetworkOf(capability.resource, owner) && capability.action === DECRYPT_ABILITY : capability.resource.startsWith(`${this.spaceId}/kv/${SHARE_ROOT_PREFIX}`) && capability.actions.every((action) => KV_ABILITIES.includes(action))
7044
- });
7045
- }
7046
6990
  /** Drop this root's key handle. It does not revoke the root: only the owner does that. */
7047
6991
  clear() {
7048
6992
  this.#key?.clear();
@@ -7052,17 +6996,6 @@ var ShareRoot = class _ShareRoot {
7052
6996
  if (this.#key === void 0) throw new ShareRootError("key-cleared", "share root key has been cleared", { rootCid: this.cid });
7053
6997
  return this.#key;
7054
6998
  }
7055
- async #assertPolicyGate() {
7056
- const key = this.#activeKey();
7057
- if (this.#options.shareRootPolicyRoots !== true) throw new ShareRootError("policy-roots-disabled", "share root policy roots are disabled (shareRootPolicyRoots is off)", { rootCid: this.cid });
7058
- if (this.kind !== "share") throw new ShareRootError("policy-roots-disabled", "agent roots are bearer-only", { rootCid: this.cid });
7059
- if (!this.policyCapable) throw new ShareRootError("root-not-policy-capable", "this share root holds no :share decrypt; re-root once the Node enforces", { rootCid: this.cid });
7060
- if (this.#now() >= this.expiresAt.getTime()) throw new ShareRootError("expired", "share root has expired", { rootCid: this.cid });
7061
- if (!await (this.#options.policyOwnerModeProbe ?? defaultPolicyOwnerModeProbe).isEnforced(this.nodeOrigin)) {
7062
- throw new ShareRootError("policy-roots-not-enforced-by-node", "the Node does not advertise owner-authority mode enforce", { rootCid: this.cid });
7063
- }
7064
- return key;
7065
- }
7066
6999
  #now() {
7067
7000
  return (this.#options.now ?? Date.now)();
7068
7001
  }
@@ -7070,17 +7003,14 @@ var ShareRoot = class _ShareRoot {
7070
7003
  async function ensureShareRoot(input) {
7071
7004
  const options = input.options ?? {};
7072
7005
  const kind = options.kind ?? "share";
7073
- const purpose = input.purpose ?? "bearer";
7074
7006
  const now = (options.now ?? Date.now)();
7075
7007
  const nodeOrigin = requireNodeOrigin(input.nodeOrigin);
7076
7008
  const lifetime = PROFILES[kind].maxLifetimeMs - SHARE_ROOT_REQUEST_MARGIN_MS;
7077
7009
  if (input.expiresAt.getTime() - now > Math.min(lifetime, SHARE_ROOT_LINK_MAX_LIFETIME_MS)) throw new ShareRootError("lifetime-exceeds-cap", "no share root can cover this expiry");
7078
- if (purpose === "policy") {
7079
- if (options.shareRootPolicyRoots !== true || kind !== "share") throw new ShareRootError("policy-roots-disabled", "share root policy roots are disabled");
7080
- if (!await (options.policyOwnerModeProbe ?? defaultPolicyOwnerModeProbe).isEnforced(nodeOrigin)) throw new ShareRootError("policy-roots-not-enforced-by-node", "the Node does not advertise owner-authority mode enforce");
7081
- }
7082
7010
  const { current } = input;
7083
- if (current !== void 0 && current.kind === kind && current.spaceId === input.spaceId && current.nodeOrigin === nodeOrigin && current.covers(input.expiresAt, now) && (purpose === "bearer" || current.policyCapable)) return { root: current, rerooted: false };
7011
+ if (current !== void 0 && current.kind === kind && current.spaceId === input.spaceId && current.nodeOrigin === nodeOrigin && current.covers(input.expiresAt, now)) {
7012
+ return { root: current, rerooted: false };
7013
+ }
7084
7014
  const factory = input.keyFactory ?? (() => createDelegatedShareKey({ extractable: false }));
7085
7015
  const root = await ShareRoot.create({
7086
7016
  owner: input.owner,
@@ -7094,19 +7024,150 @@ async function ensureShareRoot(input) {
7094
7024
  },
7095
7025
  options
7096
7026
  });
7097
- if (purpose === "policy" && !root.policyCapable) {
7098
- throw new ShareRootError("policy-roots-not-enforced-by-node", "the Node stopped advertising enforce while re-rooting; the new root is kv-only", { rootCid: root.cid });
7099
- }
7100
7027
  return { root, rerooted: true };
7101
7028
  }
7102
7029
 
7030
+ // src/artifact.ts
7031
+ var import_sha2563 = require("@noble/hashes/sha256");
7032
+ function parseArtifactVersionPath(path) {
7033
+ const match = /^artifacts\/([a-z0-9][a-z0-9_-]{2,62})\/versions\/([1-9][0-9]{0,14})\/([^/]+)$/.exec(path);
7034
+ if (match === null) return void 0;
7035
+ return { artifactId: match[1], version: Number(match[2]), file: match[3] };
7036
+ }
7037
+
7038
+ // src/share-network.ts
7039
+ var import_sha2564 = require("@noble/hashes/sha256");
7040
+ var SHARE_NETWORK_NAME_PREFIX = "share-";
7041
+ var NETWORK_SAFE_SHARE_ID = /^[a-z0-9][a-z0-9-]{15,35}$/;
7042
+ var NETWORK_URN = /^urn:tinycloud:encryption:did:pkh:eip155:([1-9]\d*):(0x[0-9a-fA-F]{40}):([a-z0-9][a-z0-9-]*)$/;
7043
+ var NETWORK_CREATE_ABILITY = "tinycloud.encryption/network.create";
7044
+ var NETWORK_REVOKE_ABILITY = "tinycloud.encryption/network.revoke";
7045
+ var DECRYPT_ABILITY = "tinycloud.encryption/decrypt";
7046
+ var NETWORK_ADMIN_TYPE = "tinycloud.encryption.network-admin/v1";
7047
+ var NETWORK_ALG = "x25519-aes256gcm/v1";
7048
+ function assertNetworkSafeShareId(shareId) {
7049
+ if (typeof shareId !== "string" || !NETWORK_SAFE_SHARE_ID.test(shareId)) {
7050
+ throw new ShareRootError("share-id-not-network-safe", "shareId must match ^[a-z0-9][a-z0-9-]{15,35}$ to name its encryption network");
7051
+ }
7052
+ }
7053
+ function perShareNetworkIdFor(ownerDid, shareId) {
7054
+ const owner = parseOwnerDid(ownerDid);
7055
+ assertNetworkSafeShareId(shareId);
7056
+ return `urn:tinycloud:encryption:did:pkh:eip155:${owner.chainId}:${owner.address}:${SHARE_NETWORK_NAME_PREFIX}${shareId}`;
7057
+ }
7058
+ function isPerShareNetworkOf(ownerDid, networkId) {
7059
+ const parsed = typeof networkId === "string" ? parseOwnerNetworkUrn(networkId) : void 0;
7060
+ if (parsed === void 0) return false;
7061
+ const shareId = parsed.name.slice(SHARE_NETWORK_NAME_PREFIX.length);
7062
+ if (!NETWORK_SAFE_SHARE_ID.test(shareId)) return false;
7063
+ return networkId === perShareNetworkIdFor(ownerDid, shareId);
7064
+ }
7065
+ function parseOwnerNetworkUrn(urn) {
7066
+ const match = typeof urn === "string" ? NETWORK_URN.exec(urn) : null;
7067
+ return match === null ? void 0 : { owner: { chainId: match[1], address: match[2].toLowerCase() }, name: match[3] };
7068
+ }
7069
+ function sameNetworkUrn(left, right) {
7070
+ const a = parseOwnerNetworkUrn(left);
7071
+ const b = parseOwnerNetworkUrn(right);
7072
+ return a !== void 0 && b !== void 0 && sameOwner(a.owner, b.owner) && a.name === b.name;
7073
+ }
7074
+ async function nodeDidOf(nodeOrigin, fetchFn) {
7075
+ let info;
7076
+ try {
7077
+ info = await readJsonCapped(await fetchFn(new URL("/info", nodeOrigin), { redirect: "error" }));
7078
+ } catch (error) {
7079
+ throw new ShareRootError("node-refused", `node identity is unavailable: ${errorMessage(error)}`);
7080
+ }
7081
+ const nodeDid = typeof info === "object" && info !== null && "nodeId" in info ? info.nodeId : void 0;
7082
+ if (typeof nodeDid !== "string" || !nodeDid.startsWith("did:key:")) throw new ShareRootError("node-refused", "node identity is unavailable");
7083
+ return nodeDid;
7084
+ }
7085
+ async function lookupShareNetwork(nodeOrigin, networkId, fetchFn) {
7086
+ const response = await fetchFn(new URL(`/encryption/networks/${encodeURIComponent(networkId)}`, nodeOrigin), { redirect: "error" });
7087
+ if (response.status === 404) return { state: "absent" };
7088
+ if (!response.ok) throw new ShareRootError("node-refused", `share network lookup failed: HTTP ${response.status}`);
7089
+ let body;
7090
+ try {
7091
+ body = await readJsonCapped(response);
7092
+ } catch (error) {
7093
+ throw new ShareRootError("node-refused", `share network descriptor is unreadable: ${errorMessage(error)}`);
7094
+ }
7095
+ const descriptor = typeof body === "object" && body !== null && "descriptor" in body ? body.descriptor : body;
7096
+ if (typeof descriptor !== "object" || descriptor === null || Array.isArray(descriptor) || !("state" in descriptor) || typeof descriptor.state !== "string" || !("networkId" in descriptor) || typeof descriptor.networkId !== "string") {
7097
+ throw new ShareRootError("node-refused", "share network descriptor is malformed");
7098
+ }
7099
+ return { state: descriptor.state, networkId: descriptor.networkId, descriptor };
7100
+ }
7101
+ function assertActiveShareNetwork(lookup, expectedNetworkId) {
7102
+ if (lookup.state !== "active" || !("descriptor" in lookup)) throw new ShareRootError("network-not-active", `share network is ${lookup.state}`);
7103
+ const { descriptor } = lookup;
7104
+ const expected = parseOwnerNetworkUrn(expectedNetworkId);
7105
+ if (expected === void 0) throw new ShareRootError("invalid-request", "network id must be a did:pkh-owned network URN");
7106
+ const threshold = descriptor.threshold;
7107
+ const ownerDid = descriptor.ownerDid;
7108
+ let ownerMatches = false;
7109
+ try {
7110
+ ownerMatches = typeof ownerDid === "string" && sameOwner(parseOwnerDid(ownerDid), expected.owner);
7111
+ } catch {
7112
+ ownerMatches = false;
7113
+ }
7114
+ if (!sameNetworkUrn(lookup.networkId, expectedNetworkId) || !ownerMatches || descriptor.name !== expected.name || descriptor.alg !== NETWORK_ALG || descriptor.keyVersion !== 1 || typeof threshold !== "object" || threshold === null || !("n" in threshold) || !("t" in threshold) || threshold.n !== 1 || threshold.t !== 1) throw new ShareRootError("network-not-active", "share network descriptor does not match the share");
7115
+ }
7116
+ async function sendNetworkAdmin(input) {
7117
+ const parsed = parseOwnerNetworkUrn(input.networkId);
7118
+ if (parsed === void 0) throw new ShareRootError("invalid-request", "network id must be a did:pkh-owned network URN");
7119
+ const body = input.action === NETWORK_CREATE_ABILITY ? canonicalize({ name: parsed.name, ownerDid: `did:pkh:eip155:${parsed.owner.chainId}:${parsed.owner.address}`, threshold: { n: 1, t: 1 } }) : canonicalize({});
7120
+ const now = seconds((input.now ?? Date.now)());
7121
+ const bodyHash = [...(0, import_sha2564.sha256)(new TextEncoder().encode(body))].map((byte) => byte.toString(16).padStart(2, "0")).join("");
7122
+ const invocation = await signCompactUcanAuthorization({
7123
+ issuerDid: input.signer.did,
7124
+ audienceDid: input.nodeDid,
7125
+ attenuation: { [input.networkId]: { [input.action]: [{}] } },
7126
+ facts: [{ type: NETWORK_ADMIN_TYPE, targetNode: input.nodeDid, networkId: input.networkId, bodyHash, action: input.action }],
7127
+ proofs: [input.proof],
7128
+ notBefore: now,
7129
+ expiresAt: now + 60,
7130
+ nonce: toBase64Url(crypto.getRandomValues(new Uint8Array(16))),
7131
+ sign: input.signer.sign
7132
+ });
7133
+ const path = input.action === NETWORK_CREATE_ABILITY ? "/encryption/networks" : `/encryption/networks/${encodeURIComponent(input.networkId)}/revoke`;
7134
+ const response = await input.fetch(new URL(path, input.nodeOrigin), {
7135
+ method: "POST",
7136
+ redirect: "error",
7137
+ headers: { Authorization: invocation.authorization, ...input.action === NETWORK_CREATE_ABILITY ? { "content-type": "application/json" } : {} },
7138
+ ...input.action === NETWORK_CREATE_ABILITY ? { body } : {}
7139
+ });
7140
+ await response.body?.cancel();
7141
+ return response.status;
7142
+ }
7143
+ async function retireShareNetwork(input) {
7144
+ const status = await sendNetworkAdmin({ ...input, action: NETWORK_REVOKE_ABILITY });
7145
+ const lookup = await lookupShareNetwork(input.nodeOrigin, input.networkId, input.fetch);
7146
+ if (lookup.state === "absent" && (status === 404 || status === 204)) return "absent";
7147
+ if (lookup.state === "revoked") return "retired";
7148
+ throw new ShareRootError("node-refused", `share network retire failed: HTTP ${status}, state ${lookup.state}`);
7149
+ }
7150
+
7103
7151
  // src/share-grant.ts
7104
- var ACTIONS = {
7152
+ var KV_ACTIONS = {
7105
7153
  view: ["tinycloud.kv/get", "tinycloud.kv/metadata"],
7106
7154
  edit: ["tinycloud.kv/get", "tinycloud.kv/metadata", "tinycloud.kv/put"]
7107
7155
  };
7108
- function wantedFor(spaceId, path, access, owner) {
7109
- return { [`${spaceId}/kv/${path}`]: ACTIONS[access], [shareNetworkId(owner)]: [DECRYPT_ABILITY] };
7156
+ var NETWORK_ACTIONS = [NETWORK_CREATE_ABILITY, NETWORK_REVOKE_ABILITY, DECRYPT_ABILITY];
7157
+ var SHARE_KV_ROOT = "xyz.tinycloud.share/";
7158
+ var EDIT_SHARE_READER_MAX_LIFETIME_MS = 60 * 6e4;
7159
+ var defaultFetch = (url, init) => globalThis.fetch(url, init);
7160
+ function assertShareGrantPath(path, shareId, access) {
7161
+ const inShare = isExactKeyUnder(path, `${SHARE_KV_ROOT}shares/${shareId}/`) && !path.slice(`${SHARE_KV_ROOT}shares/${shareId}/`.length).includes("/");
7162
+ const inArtifact = isExactKeyUnder(path, `${SHARE_KV_ROOT}artifacts/`) && parseArtifactVersionPath(path.slice(SHARE_KV_ROOT.length)) !== void 0;
7163
+ if (!inShare && !inArtifact) throw new ShareRootError("invalid-request", "share grant path must be xyz.tinycloud.share/shares/<shareId>/<file> or an artifact version file");
7164
+ if (inArtifact && access !== "view") throw new ShareRootError("invalid-request", "artifact versions are immutable: only view grants may name a version file");
7165
+ }
7166
+ function wantedFor(spec) {
7167
+ return { [`${spec.spaceId}/kv/${spec.path}`]: KV_ACTIONS[spec.access], [spec.networkId]: NETWORK_ACTIONS };
7168
+ }
7169
+ function rowOf(spec, keyDid, state, grantCid) {
7170
+ return { shareId: spec.shareId, nw: spec.networkId.slice(spec.networkId.lastIndexOf(":") + 1), networkId: spec.networkId, keyDid, access: spec.access, path: spec.path, state, ...grantCid === void 0 ? {} : { grantCid } };
7110
7171
  }
7111
7172
  var ShareGrant = class _ShareGrant {
7112
7173
  cid;
@@ -7114,44 +7175,61 @@ var ShareGrant = class _ShareGrant {
7114
7175
  spaceId;
7115
7176
  keyDid;
7116
7177
  nodeOrigin;
7178
+ shareId;
7179
+ networkId;
7117
7180
  path;
7118
7181
  access;
7119
7182
  issuedAt;
7120
7183
  expiresAt;
7184
+ #spec;
7121
7185
  #authorization;
7122
7186
  #key;
7123
7187
  #now;
7188
+ #fetch;
7124
7189
  constructor(input) {
7190
+ this.#spec = input.spec;
7125
7191
  this.cid = input.cid;
7126
- this.ownerDid = input.ownerDid;
7127
- this.spaceId = input.spaceId;
7192
+ this.ownerDid = input.spec.ownerDid;
7193
+ this.spaceId = input.spec.spaceId;
7128
7194
  this.keyDid = input.key.did;
7129
- this.nodeOrigin = input.nodeOrigin;
7130
- this.path = input.path;
7131
- this.access = input.access;
7195
+ this.nodeOrigin = input.spec.nodeOrigin;
7196
+ this.shareId = input.spec.shareId;
7197
+ this.networkId = input.spec.networkId;
7198
+ this.path = input.spec.path;
7199
+ this.access = input.spec.access;
7132
7200
  this.issuedAt = input.issuedAt;
7133
7201
  this.expiresAt = input.expiresAt;
7134
7202
  this.#authorization = input.authorization;
7135
7203
  this.#key = input.key;
7136
7204
  this.#now = input.now;
7205
+ this.#fetch = input.fetch;
7137
7206
  }
7138
7207
  /**
7139
- * Ask the owner for one per-share grant (one approval). Path, access,
7140
- * expiry, space and Node origin are checked before the wallet is asked; the
7141
- * signed CACAO is decoded and must match exactly, or the key is cleared and
7142
- * the error carries the imported CID (`mustRevoke` when it may be wider).
7208
+ * Step 0: check everything that does not need the owner, and make the key.
7209
+ * Also looks the share's network up (no approval): a shareId whose network
7210
+ * is retired (burned), or already exists from an earlier grant, is refused
7211
+ * with `share-id-in-use` before the prompt (see `resumeExistingNetwork`).
7212
+ * Throws `share-id-not-network-safe`, `invalid-request`, `lifetime-exceeds-cap`,
7213
+ * `share-id-in-use`, `node-refused` or `invalid-key` with no approval spent.
7143
7214
  */
7144
- static async create(input) {
7215
+ static async prepare(input) {
7145
7216
  const clock = input.now ?? Date.now;
7146
7217
  const now = clock();
7147
7218
  const owner = parseOwnerDid(input.owner.ownerDid);
7219
+ const networkId = perShareNetworkIdFor(input.owner.ownerDid, input.shareId);
7148
7220
  assertSpace(input.spaceId, owner, "share");
7149
7221
  const nodeOrigin = requireNodeOrigin(input.nodeOrigin);
7150
- assertLinkPath(input.path);
7151
7222
  if (input.access !== "view" && input.access !== "edit") throw new ShareRootError("invalid-request", "share grant access must be view or edit");
7223
+ assertShareGrantPath(input.path, input.shareId, input.access);
7152
7224
  const expiry = input.expiresAt instanceof Date ? input.expiresAt.getTime() : Number.NaN;
7153
7225
  if (!Number.isFinite(expiry) || expiry <= now) throw new ShareRootError("invalid-request", "share grant expiry must be in the future");
7154
7226
  if (expiry - now > SHARE_ROOT_LINK_MAX_LIFETIME_MS) throw new ShareRootError("lifetime-exceeds-cap", "share grant lifetime exceeds 30 days");
7227
+ const existing = await lookupShareNetwork(nodeOrigin, networkId, input.fetch ?? defaultFetch);
7228
+ if (existing.state !== "absent") {
7229
+ if (existing.state !== "active") throw new ShareRootError("share-id-in-use", `this shareId's network is ${existing.state}; a retired shareId is never reused`);
7230
+ if (input.resumeExistingNetwork !== true) throw new ShareRootError("share-id-in-use", "this shareId already has an active network from an earlier grant");
7231
+ assertActiveShareNetwork(existing, networkId);
7232
+ }
7155
7233
  const key = await (input.keyFactory ?? (() => createDelegatedShareKey({ extractable: false })))();
7156
7234
  try {
7157
7235
  await assertUsableKey(key);
@@ -7159,13 +7237,36 @@ var ShareGrant = class _ShareGrant {
7159
7237
  key.clear();
7160
7238
  throw error;
7161
7239
  }
7240
+ const spec = { owner, ownerDid: input.owner.ownerDid, spaceId: input.spaceId, nodeOrigin, shareId: input.shareId, networkId, path: input.path, access: input.access };
7241
+ let settled = false;
7242
+ return {
7243
+ shareId: spec.shareId,
7244
+ networkId,
7245
+ keyDid: key.did,
7246
+ row: rowOf(spec, key.did, "pending"),
7247
+ approve: async () => {
7248
+ if (settled) throw new ShareRootError("invalid-request", "this pending grant was already approved or discarded");
7249
+ settled = true;
7250
+ return _ShareGrant.#approve(spec, key, input.owner, input.expiresAt, clock, input.fetch ?? defaultFetch);
7251
+ },
7252
+ discard: () => {
7253
+ settled = true;
7254
+ key.clear();
7255
+ }
7256
+ };
7257
+ }
7258
+ /** `prepare` then `approve`, for callers that keep no `pending` row (tests, one-shot tools). */
7259
+ static async create(input) {
7260
+ return (await _ShareGrant.prepare(input)).approve();
7261
+ }
7262
+ static async #approve(spec, key, owner, expiresAt, clock, fetchFn) {
7162
7263
  const permissions = [
7163
- { service: "tinycloud.kv", path: input.path, actions: [...ACTIONS[input.access]] },
7164
- { service: "tinycloud.encryption", path: shareNetworkId(owner), actions: [DECRYPT_ABILITY] }
7264
+ { service: "tinycloud.kv", path: spec.path, actions: [...KV_ACTIONS[spec.access]] },
7265
+ { service: "tinycloud.encryption", path: spec.networkId, actions: [...NETWORK_ACTIONS] }
7165
7266
  ];
7166
7267
  let receipt;
7167
7268
  try {
7168
- receipt = await input.owner.createOwnerDelegation({ delegateDid: key.did, spaceId: input.spaceId, expiresAt: input.expiresAt, permissions });
7269
+ receipt = await owner.createOwnerDelegation({ delegateDid: key.did, spaceId: spec.spaceId, expiresAt, permissions });
7169
7270
  } catch (error) {
7170
7271
  key.clear();
7171
7272
  throw error;
@@ -7180,33 +7281,29 @@ var ShareGrant = class _ShareGrant {
7180
7281
  } catch (error) {
7181
7282
  throw new ShareRootError("malformed-root", errorMessage(error), { rootCid: receipt.delegationCid, mustRevoke: true });
7182
7283
  }
7183
- const times = validateSignedOwnerDelegation(decoded, {
7184
- label: "per-share grant",
7185
- owner,
7186
- keyDid: key.did,
7187
- wanted: wantedFor(input.spaceId, input.path, input.access, owner),
7188
- maxLifetimeMs: SHARE_ROOT_LINK_MAX_LIFETIME_MS,
7189
- requestedExpiresAt: input.expiresAt,
7190
- now: clock()
7191
- });
7192
- return new _ShareGrant({ cid: decoded.cid, ownerDid: input.owner.ownerDid, spaceId: input.spaceId, nodeOrigin, path: input.path, access: input.access, ...times, authorization, key, now: clock });
7284
+ const times = validateSignedOwnerDelegation(decoded, { label: "per-share grant", owner: spec.owner, keyDid: key.did, wanted: wantedFor(spec), maxLifetimeMs: SHARE_ROOT_LINK_MAX_LIFETIME_MS, requestedExpiresAt: expiresAt, now: clock() });
7285
+ return new _ShareGrant({ spec, cid: decoded.cid, ...times, authorization, key, now: clock, fetch: fetchFn });
7193
7286
  } catch (error) {
7194
7287
  key.clear();
7195
7288
  if (error instanceof ShareRootError) throw error;
7196
7289
  throw new ShareRootError("malformed-root", errorMessage(error), { rootCid: receipt.delegationCid, mustRevoke: true });
7197
7290
  }
7198
7291
  }
7199
- /** Rebuild a stored grant with its key (e.g. a CLI 0600 key file); the signed bytes are checked again. */
7292
+ /** Rebuild a stored grant with its key (e.g. a CLI 0600 key file); the signed bytes are checked again. Only record v2. */
7200
7293
  static async restore(input) {
7201
7294
  const { record } = input;
7202
7295
  const clock = input.now ?? Date.now;
7203
- if (record.version !== 1 || record.access !== "view" && record.access !== "edit") throw new ShareRootError("malformed-root", "share grant record is not supported");
7296
+ if (record.version !== 2) throw new ShareRootError("malformed-root", "share grant record is not supported (only per-share network grants, record v2)");
7297
+ if (record.access !== "view" && record.access !== "edit") throw new ShareRootError("malformed-root", "share grant record access is invalid");
7204
7298
  if (record.keyDid !== input.key.did) throw new ShareRootError("audience-mismatch", "share grant record belongs to another key");
7205
7299
  const owner = parseOwnerDid(record.ownerDid);
7300
+ const networkId = perShareNetworkIdFor(record.ownerDid, record.shareId);
7301
+ if (!sameNetworkUrn(networkId, record.networkId)) throw new ShareRootError("malformed-root", "share grant record network does not match its shareId");
7206
7302
  assertSpace(record.spaceId, owner, "share");
7207
7303
  const nodeOrigin = requireNodeOrigin(record.nodeOrigin);
7208
- assertLinkPath(record.path);
7304
+ assertShareGrantPath(record.path, record.shareId, record.access);
7209
7305
  await assertUsableKey(input.key);
7306
+ const spec = { owner, ownerDid: record.ownerDid, spaceId: record.spaceId, nodeOrigin, shareId: record.shareId, networkId, path: record.path, access: record.access };
7210
7307
  let authorization;
7211
7308
  let decoded;
7212
7309
  try {
@@ -7215,17 +7312,14 @@ var ShareGrant = class _ShareGrant {
7215
7312
  } catch (error) {
7216
7313
  throw new ShareRootError("malformed-root", errorMessage(error), { rootCid: record.cid });
7217
7314
  }
7218
- const times = validateSignedOwnerDelegation(decoded, {
7219
- label: "per-share grant",
7220
- owner,
7221
- keyDid: input.key.did,
7222
- wanted: wantedFor(record.spaceId, record.path, record.access, owner),
7223
- maxLifetimeMs: SHARE_ROOT_LINK_MAX_LIFETIME_MS,
7224
- now: clock()
7225
- });
7226
- return new _ShareGrant({ cid: decoded.cid, ownerDid: record.ownerDid, spaceId: record.spaceId, nodeOrigin, path: record.path, access: record.access, ...times, authorization, key: input.key, now: clock });
7315
+ const times = validateSignedOwnerDelegation(decoded, { label: "per-share grant", owner, keyDid: input.key.did, wanted: wantedFor(spec), maxLifetimeMs: SHARE_ROOT_LINK_MAX_LIFETIME_MS, now: clock() });
7316
+ return new _ShareGrant({ spec, cid: decoded.cid, ...times, authorization, key: input.key, now: clock, fetch: input.fetch ?? defaultFetch });
7227
7317
  }
7228
- /** The `Authorization` value the Node knows this grant by (base64url CACAO). */
7318
+ /**
7319
+ * This grant's CACAO as base64url, UNPADDED (the record form). As a Node
7320
+ * `Authorization` header it must be padded to a multiple of 4 characters:
7321
+ * the Node refuses an unpadded value otherwise.
7322
+ */
7229
7323
  get authorization() {
7230
7324
  return toBase64Url(this.#authorization);
7231
7325
  }
@@ -7234,31 +7328,84 @@ var ShareGrant = class _ShareGrant {
7234
7328
  return `${this.spaceId}/kv/${this.path}`;
7235
7329
  }
7236
7330
  toRecord() {
7237
- return { version: 1, cid: this.cid, authorization: this.authorization, spaceId: this.spaceId, ownerDid: this.ownerDid, keyDid: this.keyDid, nodeOrigin: this.nodeOrigin, path: this.path, access: this.access, expiresAt: this.expiresAt.toISOString() };
7331
+ return { version: 2, cid: this.cid, authorization: this.authorization, spaceId: this.spaceId, ownerDid: this.ownerDid, keyDid: this.keyDid, nodeOrigin: this.nodeOrigin, shareId: this.shareId, networkId: this.networkId, path: this.path, access: this.access, expiresAt: this.expiresAt.toISOString() };
7332
+ }
7333
+ /** The history/index row for `state`. */
7334
+ row(state) {
7335
+ return rowOf(this.#spec, this.keyDid, state, this.cid);
7336
+ }
7337
+ /**
7338
+ * Step 3: make sure the share's network exists and is usable. Creates it
7339
+ * when absent (key-signed, no approval), then decides on a fresh lookup
7340
+ * after any answer. Returns `networkId` (the grant's lowercase spelling;
7341
+ * the Node echoes the owner in EIP-55 case). A retired network is never
7342
+ * re-created (the Node answers 409): `network-not-active`.
7343
+ */
7344
+ async ensureNetwork() {
7345
+ const signer = this.#signer();
7346
+ const first = await lookupShareNetwork(this.nodeOrigin, this.networkId, this.#fetch);
7347
+ if (first.state === "absent") {
7348
+ const nodeDid = await nodeDidOf(this.nodeOrigin, this.#fetch);
7349
+ await sendNetworkAdmin({ nodeOrigin: this.nodeOrigin, nodeDid, networkId: this.networkId, action: NETWORK_CREATE_ABILITY, signer, proof: this.cid, fetch: this.#fetch, now: this.#now });
7350
+ return this.assertNetworkActive();
7351
+ }
7352
+ assertActiveShareNetwork(first, this.networkId);
7353
+ return this.networkId;
7354
+ }
7355
+ /** Throw `network-not-active` unless the share's network is `active` now (fresh lookup). */
7356
+ async assertNetworkActive() {
7357
+ assertActiveShareNetwork(await lookupShareNetwork(this.nodeOrigin, this.networkId, this.#fetch), this.networkId);
7358
+ return this.networkId;
7359
+ }
7360
+ /**
7361
+ * Retire the share's network with the per-share key (no approval):
7362
+ * PERMANENT, and every holder of decrypt on it (recipients included) is
7363
+ * refused at their next request. Idempotent: a repeat or a never-created
7364
+ * network counts as done.
7365
+ */
7366
+ async retireNetwork() {
7367
+ const signer = this.#signer();
7368
+ const nodeDid = await nodeDidOf(this.nodeOrigin, this.#fetch);
7369
+ return retireShareNetwork({ nodeOrigin: this.nodeOrigin, nodeDid, networkId: this.networkId, signer, proof: this.cid, fetch: this.#fetch, now: this.#now });
7370
+ }
7371
+ /** Step 4 check (I1): the stored object belongs to this share, on its exact key and network. */
7372
+ assertContentSource(contentSource) {
7373
+ if (contentSource.shareId !== this.shareId || contentSource.kvResource !== this.resource || contentSource.selector !== "exact" || !sameNetworkUrn(contentSource.encryptionNetwork, this.networkId)) throw new ShareRootError("invalid-request", "content source does not belong to this share's grant (shareId, exact key and network)");
7238
7374
  }
7239
7375
  /**
7240
7376
  * Policy/v3 owner signing for this one share. Every call checks the key is
7241
7377
  * held and the grant is live. Policy roots must be owned by the per-share
7242
7378
  * key, expire no later than the grant, and name only this grant's exact
7243
- * KV resource (with no action the grant lacks) and decrypt on `:share`.
7379
+ * KV resource (no action the grant lacks) and decrypt on this share's
7380
+ * network (parsed equality).
7244
7381
  */
7245
7382
  policySigner() {
7246
- const owner = parseOwnerDid(this.ownerDid);
7247
- const actions = ACTIONS[this.access];
7383
+ const actions = KV_ACTIONS[this.access];
7248
7384
  return ownerPolicySigner({
7249
7385
  ownerDid: this.keyDid,
7250
7386
  authorityCid: this.cid,
7251
7387
  notAfter: new Date(this.expiresAt),
7252
7388
  gate: async () => this.#activeKey(),
7253
7389
  now: this.#now,
7254
- inScope: (capability) => capability.kind === "encryption" ? isShareNetworkOf(capability.resource, owner) && capability.action === DECRYPT_ABILITY : capability.resource === this.resource && capability.selector === "exact" && capability.actions.every((action) => actions.includes(action))
7390
+ inScope: (capability) => capability.kind === "encryption" ? sameNetworkUrn(capability.resource, this.networkId) && capability.action === DECRYPT_ABILITY : capability.resource === this.resource && capability.selector === "exact" && capability.actions.every((action) => actions.includes(action))
7255
7391
  });
7256
7392
  }
7257
- /** `publishAddressedShare`'s `authority` for this share: the policy signer plus the caller's `registerPolicy`. */
7393
+ /**
7394
+ * Step 5: `publishAddressedShare`'s `authority` for this share. Its
7395
+ * `registerPolicy` first re-reads the network and refuses unless it is
7396
+ * `active` (released Nodes accept registrations on missing or retired
7397
+ * networks).
7398
+ */
7258
7399
  publishAuthority(registerPolicy) {
7259
- return { ...this.policySigner(), registerPolicy };
7400
+ return {
7401
+ ...this.policySigner(),
7402
+ registerPolicy: async (request) => {
7403
+ await this.assertNetworkActive();
7404
+ return registerPolicy(request);
7405
+ }
7406
+ };
7260
7407
  }
7261
- /** Drop the per-share key. It does not revoke the grant: only the owner does that. */
7408
+ /** Drop the per-share key. It does not retire the network or revoke the grant. */
7262
7409
  clear() {
7263
7410
  this.#key?.clear();
7264
7411
  this.#key = void 0;
@@ -7268,101 +7415,323 @@ var ShareGrant = class _ShareGrant {
7268
7415
  if (this.#now() >= this.expiresAt.getTime()) throw new ShareRootError("expired", "share grant has expired", { rootCid: this.cid });
7269
7416
  return this.#key;
7270
7417
  }
7271
- };
7272
-
7273
- // src/share-network.ts
7274
- var import_sha2563 = require("@noble/hashes/sha256");
7275
- var NETWORK_CREATE_ABILITY = "tinycloud.encryption/network.create";
7276
- var NETWORK_ADMIN_TYPE = "tinycloud.encryption.network-admin/v1";
7277
- var NETWORK_GRANT_LIFETIME_MS = 10 * 6e4;
7278
- function shareNetworkIdFor(ownerDid) {
7279
- parseOwnerDid(ownerDid);
7280
- return `urn:tinycloud:encryption:${ownerDid}:share`;
7281
- }
7282
- function hex(bytes2) {
7283
- return [...bytes2].map((byte) => byte.toString(16).padStart(2, "0")).join("");
7284
- }
7285
- async function existingShareNetwork(nodeOrigin, networkId, ownerDid, fetchFn) {
7286
- const response = await fetchFn(new URL(`/encryption/networks/${encodeURIComponent(networkId)}`, nodeOrigin), { redirect: "error" });
7287
- if (response.status === 404) return void 0;
7288
- if (!response.ok) throw new ShareRootError("invalid-request", `share network lookup failed: HTTP ${response.status}`);
7289
- const body = await response.json();
7290
- const descriptor = typeof body === "object" && body !== null && "descriptor" in body ? body.descriptor : body;
7291
- if (typeof descriptor !== "object" || descriptor === null || !("networkId" in descriptor) || !("ownerDid" in descriptor) || !("state" in descriptor)) {
7292
- throw new ShareRootError("invalid-request", "share network descriptor is malformed");
7418
+ #signer() {
7419
+ const key = this.#activeKey();
7420
+ return { did: key.did, sign: (bytes2) => key.sign(bytes2) };
7293
7421
  }
7294
- const { networkId: foundId, ownerDid: foundOwner, state } = descriptor;
7295
- const owner = parseOwnerDid(ownerDid);
7296
- if (typeof foundId !== "string" || !isShareNetworkOf(foundId, owner) || typeof foundOwner !== "string" || !sameOwner(parseOwnerDid(foundOwner), owner)) {
7297
- throw new ShareRootError("invalid-request", "share network is not owned by this owner");
7422
+ };
7423
+ async function revokeShareGrant(input) {
7424
+ const fetchFn = input.fetch ?? defaultFetch;
7425
+ const network = await input.grant.retireNetwork();
7426
+ try {
7427
+ const response = await fetchFn(new URL("/revoke", input.grant.nodeOrigin), { method: "POST", redirect: "error", headers: { Authorization: await input.owner.signOwnerRevocation(input.grant.cid) } });
7428
+ const status = response.status;
7429
+ if (!await isRevokeReceiptFor(response, input.grant.cid)) throw new Error(`HTTP ${status}: no receipt confirming this grant`);
7430
+ } catch (error) {
7431
+ throw new ShareRootError("grant-revoke-incomplete", `the share's network is retired (decrypt cut for everyone), but the grant revoke failed: ${errorMessage(error)}`, { rootCid: input.grant.cid });
7298
7432
  }
7299
- if (state === "revoked" || state === "failed") throw new ShareRootError("invalid-request", `share network is ${String(state)}`);
7300
- return foundId;
7433
+ return { network, grant: "revoked" };
7301
7434
  }
7302
- async function ensureShareNetwork(input) {
7435
+ async function openEditShare(input) {
7303
7436
  const clock = input.now ?? Date.now;
7304
- const fetchFn = input.fetch ?? ((url, init) => globalThis.fetch(url, init));
7437
+ const lifetime = input.lifetimeMs ?? EDIT_SHARE_READER_MAX_LIFETIME_MS;
7438
+ if (!Number.isFinite(lifetime) || lifetime <= 0 || lifetime > EDIT_SHARE_READER_MAX_LIFETIME_MS) throw new ShareRootError("lifetime-exceeds-cap", "an edit-share reader lives at most one hour");
7305
7439
  const owner = parseOwnerDid(input.owner.ownerDid);
7306
- assertSpace(input.spaceId, owner, "share");
7307
- const nodeOrigin = requireNodeOrigin(input.nodeOrigin);
7308
- const networkId = shareNetworkIdFor(input.owner.ownerDid);
7309
- const existing = await existingShareNetwork(nodeOrigin, networkId, input.owner.ownerDid, fetchFn);
7310
- if (existing !== void 0) return { networkId: existing, approvals: 0 };
7311
- const info = await (await fetchFn(new URL("/info", nodeOrigin), { redirect: "error" })).json();
7312
- const nodeDid = typeof info === "object" && info !== null && "nodeId" in info ? info.nodeId : void 0;
7313
- if (typeof nodeDid !== "string" || !nodeDid.startsWith("did:key:")) throw new ShareRootError("invalid-request", "node identity is unavailable");
7314
- const key = await (input.keyFactory ?? (() => createDelegatedShareKey({ extractable: false })))();
7440
+ if (!sameOwner(owner, parseOwnerDid(input.record.ownerDid))) throw new ShareRootError("invalid-request", "the share belongs to another owner");
7441
+ const networkId = perShareNetworkIdFor(input.record.ownerDid, input.record.shareId);
7442
+ if (!sameNetworkUrn(networkId, input.record.networkId)) throw new ShareRootError("invalid-request", "share network does not match its shareId");
7443
+ assertSpace(input.record.spaceId, owner, "share");
7444
+ assertShareGrantPath(input.record.path, input.record.shareId, "edit");
7445
+ const key = await createDelegatedShareKey({ extractable: false });
7446
+ const expiresAt = new Date(clock() + lifetime);
7447
+ let receipt;
7315
7448
  try {
7316
- await assertUsableKey(key);
7317
- const expiresAt = new Date(clock() + NETWORK_GRANT_LIFETIME_MS);
7318
- const receipt = await input.owner.createOwnerDelegation({
7449
+ receipt = await input.owner.createOwnerDelegation({
7319
7450
  delegateDid: key.did,
7320
- spaceId: input.spaceId,
7451
+ spaceId: input.record.spaceId,
7321
7452
  expiresAt,
7322
- permissions: [{ service: "tinycloud.encryption", path: networkId, actions: [NETWORK_CREATE_ABILITY] }]
7453
+ permissions: [
7454
+ { service: "tinycloud.kv", path: input.record.path, actions: ["tinycloud.kv/get"] },
7455
+ { service: "tinycloud.encryption", path: networkId, actions: [DECRYPT_ABILITY] }
7456
+ ]
7323
7457
  });
7458
+ } catch (error) {
7459
+ key.clear();
7460
+ throw error;
7461
+ }
7462
+ try {
7324
7463
  const authorization = new Uint8Array(receipt.signedDagCbor);
7325
7464
  const signedCid = ownerRootCacaoCid(authorization);
7326
- if (signedCid !== receipt.delegationCid) throw new ShareRootError("malformed-root", "signed network grant bytes do not match the imported delegation CID", { rootCid: receipt.delegationCid, signedCid, mustRevoke: true });
7327
- let decoded;
7328
- try {
7329
- decoded = decodeOwnerRootCacao(authorization);
7330
- } catch (error) {
7331
- throw new ShareRootError("malformed-root", errorMessage(error), { rootCid: receipt.delegationCid, mustRevoke: true });
7332
- }
7333
- validateSignedOwnerDelegation(decoded, {
7334
- label: "share network grant",
7465
+ if (signedCid !== receipt.delegationCid) throw new ShareRootError("malformed-root", "signed reader grant bytes do not match the imported delegation CID", { rootCid: receipt.delegationCid, signedCid, mustRevoke: true });
7466
+ validateSignedOwnerDelegation(decodeOwnerRootCacao(authorization), {
7467
+ label: "edit-share reader",
7335
7468
  owner,
7336
7469
  keyDid: key.did,
7337
- wanted: { [networkId]: [NETWORK_CREATE_ABILITY] },
7338
- maxLifetimeMs: NETWORK_GRANT_LIFETIME_MS,
7470
+ wanted: { [`${input.record.spaceId}/kv/${input.record.path}`]: ["tinycloud.kv/get"], [networkId]: [DECRYPT_ABILITY] },
7471
+ maxLifetimeMs: EDIT_SHARE_READER_MAX_LIFETIME_MS,
7339
7472
  requestedExpiresAt: expiresAt,
7340
7473
  now: clock()
7341
7474
  });
7342
- const body = { name: "share", ownerDid: input.owner.ownerDid, threshold: { n: 1, t: 1 } };
7343
- const canonicalBody = canonicalize(body);
7344
- const now = seconds(clock());
7345
- const invocation = await signCompactUcanAuthorization({
7346
- issuerDid: key.did,
7347
- audienceDid: nodeDid,
7348
- attenuation: { [networkId]: { [NETWORK_CREATE_ABILITY]: [{}] } },
7349
- facts: [{ type: NETWORK_ADMIN_TYPE, targetNode: nodeDid, networkId, bodyHash: hex((0, import_sha2563.sha256)(new TextEncoder().encode(canonicalBody))), action: NETWORK_CREATE_ABILITY }],
7350
- proofs: [decoded.cid],
7351
- notBefore: now,
7352
- expiresAt: now + 60,
7353
- nonce: toBase64Url(crypto.getRandomValues(new Uint8Array(16))),
7354
- sign: (bytes2) => key.sign(bytes2)
7355
- });
7356
- const response = await fetchFn(new URL("/encryption/networks", nodeOrigin), {
7357
- method: "POST",
7358
- redirect: "error",
7359
- headers: { Authorization: invocation.authorization, "content-type": "application/json" },
7360
- body: canonicalBody
7361
- });
7362
- if (!response.ok && response.status !== 409) throw new ShareRootError("invalid-request", `share network creation failed: HTTP ${response.status} ${(await response.text()).slice(0, 120)}`);
7363
- const created = await existingShareNetwork(nodeOrigin, networkId, input.owner.ownerDid, fetchFn);
7364
- if (created === void 0) throw new ShareRootError("invalid-request", "share network was not created");
7365
- return { networkId: created, approvals: 1 };
7475
+ } catch (error) {
7476
+ key.clear();
7477
+ if (error instanceof ShareRootError) throw error;
7478
+ throw new ShareRootError("malformed-root", errorMessage(error), { rootCid: receipt.delegationCid, mustRevoke: true });
7479
+ }
7480
+ let live = key;
7481
+ const clear = () => {
7482
+ live?.clear();
7483
+ live = void 0;
7484
+ };
7485
+ return {
7486
+ cid: receipt.delegationCid,
7487
+ keyDid: key.did,
7488
+ networkId,
7489
+ path: input.record.path,
7490
+ expiresAt,
7491
+ signer: {
7492
+ did: key.did,
7493
+ sign: async (bytes2) => {
7494
+ if (live === void 0 || clock() >= expiresAt.getTime()) throw new ShareRootError("expired", "edit-share reader has expired or was cleared");
7495
+ return live.sign(bytes2);
7496
+ }
7497
+ },
7498
+ clear
7499
+ };
7500
+ }
7501
+
7502
+ // src/share-control.ts
7503
+ var import_sha3 = require("@noble/hashes/sha3");
7504
+ var SHARE_CONTROL_HEADER_BUDGET_BYTES = 3e4;
7505
+ var SHARE_CONTROL_LIFETIME_MS = 10 * 6e4;
7506
+ var DELEGATION_REVOKE_ABILITY = "tinycloud.delegation/revoke";
7507
+ var RECAP_STATEMENT = "I further authorize the stated URI to perform the following actions on my behalf:";
7508
+ var PLACEHOLDER_KEY_DID = `did:key:z6Mk${"1".repeat(44)}`;
7509
+ function checksumAddress(address) {
7510
+ const lower = address.toLowerCase().slice(2);
7511
+ const hash = (0, import_sha3.keccak_256)(new TextEncoder().encode(lower));
7512
+ let out = "0x";
7513
+ for (let index = 0; index < lower.length; index++) {
7514
+ const nibble = hash[index >> 1] >> (index % 2 === 0 ? 4 : 0) & 15;
7515
+ out += nibble >= 8 ? lower[index].toUpperCase() : lower[index];
7516
+ }
7517
+ return out;
7518
+ }
7519
+ function reCapStatement(attenuation) {
7520
+ let index = 0;
7521
+ let lines = "";
7522
+ for (const resource of Object.keys(attenuation).sort()) {
7523
+ const namespaces = {};
7524
+ for (const ability of Object.keys(attenuation[resource]).sort()) {
7525
+ const slash = ability.indexOf("/");
7526
+ (namespaces[ability.slice(0, slash)] ??= []).push(ability.slice(slash + 1));
7527
+ }
7528
+ for (const namespace of Object.keys(namespaces).sort()) {
7529
+ lines += ` (${++index}) '${namespace}': ${namespaces[namespace].map((name) => `'${name}'`).join(", ")} for '${resource}'.`;
7530
+ }
7531
+ }
7532
+ return `${RECAP_STATEMENT}${lines}`;
7533
+ }
7534
+ function encodeDagCbor(value) {
7535
+ const output = [];
7536
+ const head = (major, argument) => {
7537
+ if (argument < 24) output.push(major << 5 | argument);
7538
+ else if (argument < 256) output.push(major << 5 | 24, argument);
7539
+ else if (argument < 65536) output.push(major << 5 | 25, argument >> 8, argument & 255);
7540
+ else output.push(major << 5 | 26, argument >>> 24 & 255, argument >>> 16 & 255, argument >>> 8 & 255, argument & 255);
7541
+ };
7542
+ const write = (item) => {
7543
+ if (typeof item === "number" && Number.isSafeInteger(item) && item >= 0 && item < 4294967296) head(0, item);
7544
+ else if (typeof item === "string") {
7545
+ const encoded = new TextEncoder().encode(item);
7546
+ head(3, encoded.length);
7547
+ output.push(...encoded);
7548
+ } else if (item instanceof Uint8Array) {
7549
+ head(2, item.length);
7550
+ output.push(...item);
7551
+ } else if (Array.isArray(item)) {
7552
+ head(4, item.length);
7553
+ item.forEach(write);
7554
+ } else if (typeof item === "object" && item !== null) {
7555
+ const entries = Object.entries(item).map(([key, entry]) => [new TextEncoder().encode(key), entry]);
7556
+ entries.sort(([left], [right]) => left.length - right.length || compareBytes(left, right));
7557
+ head(5, entries.length);
7558
+ for (const [key, entry] of entries) {
7559
+ head(3, key.length);
7560
+ output.push(...key);
7561
+ write(entry);
7562
+ }
7563
+ } else throw new Error("value is not encodable as a CACAO field");
7564
+ };
7565
+ write(value);
7566
+ return Uint8Array.from(output);
7567
+ }
7568
+ function compareBytes(left, right) {
7569
+ for (let index = 0; index < left.length; index++) if (left[index] !== right[index]) return left[index] - right[index];
7570
+ return 0;
7571
+ }
7572
+ function unsignedControl(input) {
7573
+ const address = checksumAddress(input.owner.address);
7574
+ const statement = reCapStatement(input.attenuation);
7575
+ const recap = `urn:recap:${toBase64Url(new TextEncoder().encode(JSON.stringify({ att: input.attenuation, prf: [] })))}`;
7576
+ const message = [
7577
+ `${input.domain} wants you to sign in with your Ethereum account:`,
7578
+ address,
7579
+ "",
7580
+ statement,
7581
+ "",
7582
+ `URI: ${input.keyDid}`,
7583
+ "Version: 1",
7584
+ `Chain ID: ${input.owner.chainId}`,
7585
+ `Nonce: ${input.nonce}`,
7586
+ `Issued At: ${input.issuedAt}`,
7587
+ `Expiration Time: ${input.expiresAt}`,
7588
+ "Resources:",
7589
+ `- ${recap}`
7590
+ ].join("\n");
7591
+ const payload = { aud: input.keyDid, domain: input.domain, exp: input.expiresAt, iat: input.issuedAt, iss: `did:pkh:eip155:${input.owner.chainId}:${address}`, nonce: input.nonce, resources: [recap], statement, version: 1 };
7592
+ return { message, payload };
7593
+ }
7594
+ function signedControlBytes(payload, signature) {
7595
+ return encodeDagCbor({ h: { t: "eip4361" }, p: payload, s: { t: "eip191", s: signature } });
7596
+ }
7597
+ function attenuationFor(targets, spaceDelegationResources) {
7598
+ const attenuation = {};
7599
+ for (const resource of spaceDelegationResources) attenuation[resource] = { [DELEGATION_REVOKE_ABILITY]: [{}] };
7600
+ for (const target of targets) attenuation[target.networkId] = { [DELEGATION_REVOKE_ABILITY]: [{}], [NETWORK_REVOKE_ABILITY]: [{}] };
7601
+ return attenuation;
7602
+ }
7603
+ var ISO_PLACEHOLDER = (/* @__PURE__ */ new Date(0)).toISOString();
7604
+ var NONCE_PLACEHOLDER = "0".repeat(32);
7605
+ function headerValue(bytes2) {
7606
+ const text2 = toBase64Url(bytes2);
7607
+ return text2 + "=".repeat((4 - text2.length % 4) % 4);
7608
+ }
7609
+ function headerLength(byteLength) {
7610
+ return 4 * Math.ceil(byteLength / 3);
7611
+ }
7612
+ function headerBytesOf(owner, domain, targets, spaceDelegationResources) {
7613
+ const { payload } = unsignedControl({ owner, domain, keyDid: PLACEHOLDER_KEY_DID, attenuation: attenuationFor(targets, spaceDelegationResources), issuedAt: ISO_PLACEHOLDER, expiresAt: ISO_PLACEHOLDER, nonce: NONCE_PLACEHOLDER });
7614
+ return headerLength(signedControlBytes(payload, new Uint8Array(65)).length);
7615
+ }
7616
+ var CONTROL_DOMAIN = /^[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?(?:\.[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?)*(?::\d{1,5})?$/;
7617
+ var CONTROL_SPACE_ROW = /^tinycloud:pkh:eip155:([1-9]\d*):(0x[0-9a-fA-F]{40}):([A-Za-z0-9_-]+)\/delegation$/;
7618
+ var GRANT_CID = /^b[a-z2-7]{20,}$/;
7619
+ function assertControlInputs(ownerDid, owner, domain, targets, spaceDelegationResources) {
7620
+ if (typeof domain !== "string" || !CONTROL_DOMAIN.test(domain)) throw new ShareRootError("invalid-request", "control domain must be a host name, optionally with a port");
7621
+ const seenRows = /* @__PURE__ */ new Set();
7622
+ for (const row of spaceDelegationResources) {
7623
+ const match = typeof row === "string" ? CONTROL_SPACE_ROW.exec(row) : null;
7624
+ if (match === null || !sameOwner({ chainId: match[1], address: match[2].toLowerCase() }, owner)) throw new ShareRootError("control-target-invalid", "control space rows must be <owner space>/delegation");
7625
+ if (seenRows.has(row)) throw new ShareRootError("control-target-invalid", "control space rows must not repeat");
7626
+ seenRows.add(row);
7627
+ }
7628
+ const seen = /* @__PURE__ */ new Set();
7629
+ for (const target of targets) {
7630
+ if (!isPerShareNetworkOf(ownerDid, target.networkId)) throw new ShareRootError("control-target-invalid", "control targets must be this owner's per-share networks (share-<shareId>)");
7631
+ if (target.grantCid !== void 0 && (typeof target.grantCid !== "string" || !GRANT_CID.test(target.grantCid))) throw new ShareRootError("control-target-invalid", "control target grant CID is invalid");
7632
+ if (seen.has(target.networkId)) throw new ShareRootError("control-target-invalid", "control targets must not repeat a network");
7633
+ seen.add(target.networkId);
7634
+ }
7635
+ }
7636
+ function planShareControlBatches(input) {
7637
+ const owner = parseOwnerDid(input.owner.ownerDid);
7638
+ const budget = input.budgetBytes ?? SHARE_CONTROL_HEADER_BUDGET_BYTES;
7639
+ if (!Number.isSafeInteger(budget) || budget <= 0 || budget > SHARE_CONTROL_HEADER_BUDGET_BYTES) throw new ShareRootError("invalid-request", `control budget must be 1..${SHARE_CONTROL_HEADER_BUDGET_BYTES} bytes`);
7640
+ const rows = (input.spaceIds ?? []).map((spaceId) => `${spaceId}/delegation`);
7641
+ assertControlInputs(input.owner.ownerDid, owner, input.owner.domain, input.targets, rows);
7642
+ const batches = [];
7643
+ let current = [];
7644
+ let size = 0;
7645
+ for (const target of input.targets) {
7646
+ const next = headerBytesOf(owner, input.owner.domain, [...current, target], rows);
7647
+ if (next <= budget) {
7648
+ current.push(target);
7649
+ size = next;
7650
+ continue;
7651
+ }
7652
+ if (current.length === 0) throw new ShareRootError("invalid-request", "one target and the space rows exceed the control budget");
7653
+ batches.push({ targets: current, spaceDelegationResources: rows, headerBytes: size });
7654
+ current = [target];
7655
+ size = headerBytesOf(owner, input.owner.domain, current, rows);
7656
+ if (size > budget) throw new ShareRootError("invalid-request", "one target and the space rows exceed the control budget");
7657
+ }
7658
+ if (current.length > 0) {
7659
+ batches.push({ targets: current, spaceDelegationResources: rows, headerBytes: size });
7660
+ } else if (rows.length > 0) {
7661
+ size = headerBytesOf(owner, input.owner.domain, [], rows);
7662
+ if (size > budget) throw new ShareRootError("invalid-request", "the space rows alone exceed the control budget");
7663
+ batches.push({ targets: [], spaceDelegationResources: rows, headerBytes: size });
7664
+ }
7665
+ return batches;
7666
+ }
7667
+ function signatureBytes(hex) {
7668
+ if (typeof hex !== "string" || !/^0x[0-9a-fA-F]{130}$/.test(hex)) throw new ShareRootError("invalid-request", "owner signature must be 65 bytes of 0x hex");
7669
+ const bytes2 = new Uint8Array(65);
7670
+ for (let index = 0; index < 65; index++) bytes2[index] = Number.parseInt(hex.slice(2 + index * 2, 4 + index * 2), 16);
7671
+ return bytes2;
7672
+ }
7673
+ async function runShareControlBatch(input) {
7674
+ const fetchFn = input.fetch ?? ((url, init) => globalThis.fetch(url, init));
7675
+ const clock = input.now ?? Date.now;
7676
+ const nodeOrigin = requireNodeOrigin(input.nodeOrigin);
7677
+ const owner = parseOwnerDid(input.owner.ownerDid);
7678
+ const { batch } = input;
7679
+ assertControlInputs(input.owner.ownerDid, owner, input.owner.domain, batch.targets, batch.spaceDelegationResources);
7680
+ if (batch.targets.length === 0 && batch.spaceDelegationResources.length === 0) throw new ShareRootError("invalid-request", "control batch is empty");
7681
+ const key = await (input.keyFactory ?? (() => createDelegatedShareKey({ extractable: false })))();
7682
+ try {
7683
+ const now = clock();
7684
+ const issuedAt = new Date(now).toISOString();
7685
+ const expiresAt = new Date(now + SHARE_CONTROL_LIFETIME_MS).toISOString();
7686
+ const nonce = [...crypto.getRandomValues(new Uint8Array(16))].map((byte) => byte.toString(16).padStart(2, "0")).join("");
7687
+ const control = unsignedControl({ owner, domain: input.owner.domain, keyDid: key.did, attenuation: attenuationFor(batch.targets, batch.spaceDelegationResources), issuedAt, expiresAt, nonce });
7688
+ const sized = headerLength(signedControlBytes(control.payload, new Uint8Array(65)).length);
7689
+ if (sized > SHARE_CONTROL_HEADER_BUDGET_BYTES) throw new ShareRootError("invalid-request", `control is ${sized} bytes, above the ${SHARE_CONTROL_HEADER_BUDGET_BYTES}-byte budget`);
7690
+ const bytes2 = signedControlBytes(control.payload, signatureBytes(await input.owner.signMessage(control.message)));
7691
+ const controlCid = ownerRootCacaoCid(bytes2);
7692
+ const imported = await fetchFn(new URL("/delegate", nodeOrigin), { method: "POST", redirect: "error", headers: { Authorization: headerValue(bytes2) } });
7693
+ if (!imported.ok) throw new ShareRootError("node-refused", `owner control import failed: HTTP ${imported.status} ${(await imported.text()).slice(0, 120)}`);
7694
+ await imported.body?.cancel();
7695
+ const nodeDid = await nodeDidOf(nodeOrigin, fetchFn);
7696
+ const signer = { did: key.did, sign: (data) => key.sign(data) };
7697
+ const networks = [];
7698
+ for (const target of batch.targets) {
7699
+ try {
7700
+ networks.push({ networkId: target.networkId, result: await retireShareNetwork({ nodeOrigin, nodeDid, networkId: target.networkId, signer, proof: controlCid, fetch: fetchFn, now: clock }) });
7701
+ } catch (error) {
7702
+ networks.push({ networkId: target.networkId, result: "failed", error: errorMessage(error) });
7703
+ }
7704
+ }
7705
+ const grants = [];
7706
+ for (const target of batch.targets) {
7707
+ if (target.grantCid === void 0) continue;
7708
+ const at = seconds(clock());
7709
+ const invocation = await signCompactUcanAuthorization({
7710
+ issuerDid: key.did,
7711
+ audienceDid: key.did,
7712
+ attenuation: { [`urn:cid:${target.grantCid}`]: { [DELEGATION_REVOKE_ABILITY]: [{}] } },
7713
+ facts: [{}],
7714
+ proofs: [controlCid],
7715
+ notBefore: at,
7716
+ expiresAt: at + 60,
7717
+ nonce: `urn:uuid:${crypto.randomUUID()}`,
7718
+ sign: signer.sign
7719
+ });
7720
+ const response = await fetchFn(new URL("/revoke", nodeOrigin), { method: "POST", redirect: "error", headers: { Authorization: invocation.authorization } });
7721
+ const status = response.status;
7722
+ if (await isRevokeReceiptFor(response, target.grantCid)) {
7723
+ grants.push({ grantCid: target.grantCid, result: "revoked", status });
7724
+ continue;
7725
+ }
7726
+ if (input.revokeFallback === void 0) {
7727
+ grants.push({ grantCid: target.grantCid, result: "refused", status });
7728
+ continue;
7729
+ }
7730
+ const fallback = await fetchFn(new URL("/revoke", nodeOrigin), { method: "POST", redirect: "error", headers: { Authorization: await input.revokeFallback.signOwnerRevocation(target.grantCid) } });
7731
+ const fallbackStatus = fallback.status;
7732
+ grants.push({ grantCid: target.grantCid, result: await isRevokeReceiptFor(fallback, target.grantCid) ? "revoked-by-owner" : "refused", status: fallbackStatus });
7733
+ }
7734
+ return { controlCid, networks, grants };
7366
7735
  } finally {
7367
7736
  key.clear();
7368
7737
  }
@@ -7370,8 +7739,10 @@ async function ensureShareNetwork(input) {
7370
7739
  // Annotate the CommonJS export names for ESM import in node:
7371
7740
  0 && (module.exports = {
7372
7741
  AGENT_SHARE_ROOT_MAX_LIFETIME_MS,
7373
- POLICY_OWNER_AUTHORITY_FEATURES,
7374
- POLICY_OWNER_AUTHORITY_PROBE_TTL_MS,
7742
+ EDIT_SHARE_READER_MAX_LIFETIME_MS,
7743
+ SHARE_CONTROL_HEADER_BUDGET_BYTES,
7744
+ SHARE_CONTROL_LIFETIME_MS,
7745
+ SHARE_NETWORK_NAME_PREFIX,
7375
7746
  SHARE_ROOT_CLOCK_SKEW_MS,
7376
7747
  SHARE_ROOT_LINK_MAX_LIFETIME_MS,
7377
7748
  SHARE_ROOT_MAX_LIFETIME_MS,
@@ -7380,15 +7751,17 @@ async function ensureShareNetwork(input) {
7380
7751
  ShareGrant,
7381
7752
  ShareRoot,
7382
7753
  ShareRootError,
7754
+ assertNetworkSafeShareId,
7383
7755
  assertNonExtractableShareKey,
7384
- createPolicyOwnerModeProbe,
7756
+ assertShareGrantPath,
7385
7757
  decodeOwnerRootCacao,
7386
- defaultPolicyOwnerModeProbe,
7387
- ensureShareNetwork,
7388
7758
  ensureShareRoot,
7759
+ openEditShare,
7389
7760
  ownerRootCacaoCid,
7390
- readPolicyOwnerAuthorityMode,
7391
- shareNetworkIdFor,
7761
+ perShareNetworkIdFor,
7762
+ planShareControlBatches,
7763
+ revokeShareGrant,
7764
+ runShareControlBatch,
7392
7765
  signShareKeyDelegation
7393
7766
  });
7394
7767
  //# sourceMappingURL=share-root.cjs.map