@tinycloud/node-sdk 2.9.0 → 2.10.0-beta.1

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
@@ -14963,10 +14963,10 @@ function randomBytes(length) {
14963
14963
  logger21.throwArgumentError("invalid length", "length", length);
14964
14964
  }
14965
14965
  const result = new Uint8Array(length);
14966
- crypto.getRandomValues(result);
14966
+ crypto2.getRandomValues(result);
14967
14967
  return arrayify(result);
14968
14968
  }
14969
- var logger21, anyGlobal, crypto;
14969
+ var logger21, anyGlobal, crypto2;
14970
14970
  var init_random = __esm({
14971
14971
  "../../node_modules/@ethersproject/random/lib.esm/random.js"() {
14972
14972
  "use strict";
@@ -14975,10 +14975,10 @@ var init_random = __esm({
14975
14975
  init_version16();
14976
14976
  logger21 = new Logger(version16);
14977
14977
  anyGlobal = getGlobal();
14978
- crypto = anyGlobal.crypto || anyGlobal.msCrypto;
14979
- if (!crypto || !crypto.getRandomValues) {
14978
+ crypto2 = anyGlobal.crypto || anyGlobal.msCrypto;
14979
+ if (!crypto2 || !crypto2.getRandomValues) {
14980
14980
  logger21.warn("WARNING: Missing strong random number source");
14981
- crypto = {
14981
+ crypto2 = {
14982
14982
  getRandomValues: function(buffer) {
14983
14983
  return logger21.throwError("no secure random source avaialble", Logger.errors.UNSUPPORTED_OPERATION, {
14984
14984
  operation: "crypto.getRandomValues"
@@ -18753,8 +18753,8 @@ function decodeAuthorizationBytes(authorization) {
18753
18753
  if (unpadded === void 0 || remainder === 1 || paddingLength !== 0 && paddingLength !== expectedPaddingLength) {
18754
18754
  throw new Error("Delegation Authorization is not canonical base64url DAG-CBOR");
18755
18755
  }
18756
- const decoded = Uint8Array.from(Buffer.from(unpadded, "base64url"));
18757
- if (Buffer.from(decoded).toString("base64url") !== unpadded) {
18756
+ const decoded = base64UrlDecode(unpadded);
18757
+ if (base64UrlEncode(decoded) !== unpadded) {
18758
18758
  throw new Error("Delegation Authorization is not canonical base64url DAG-CBOR");
18759
18759
  }
18760
18760
  return decoded;
@@ -20910,7 +20910,8 @@ var _TinyCloudNode = class _TinyCloudNode {
20910
20910
  const did = binding?.did ?? this.did;
20911
20911
  const address = binding?.address ?? this._address;
20912
20912
  const chainId = binding?.chainId ?? this._chainId;
20913
- const crypto2 = this.createEncryptionCrypto();
20913
+ const crypto3 = this.createEncryptionCrypto();
20914
+ const descriptorCache = /* @__PURE__ */ new Map();
20914
20915
  const transport = {
20915
20916
  postDecrypt: async ({ networkId, authorization, canonicalBody, signal }) => {
20916
20917
  graph.assertActive();
@@ -20944,7 +20945,7 @@ var _TinyCloudNode = class _TinyCloudNode {
20944
20945
  }
20945
20946
  };
20946
20947
  return new import_sdk_core7.EncryptionService({
20947
- crypto: crypto2,
20948
+ crypto: crypto3,
20948
20949
  signer: {
20949
20950
  signDecryptInvocation: async (input) => {
20950
20951
  graph.assertActive();
@@ -20967,7 +20968,18 @@ var _TinyCloudNode = class _TinyCloudNode {
20967
20968
  node: {
20968
20969
  fetchByNetworkId: async (networkId) => {
20969
20970
  graph.assertActive();
20970
- const descriptor = await this.fetchEncryptionNetworkAt(host, networkId, graph.fetch);
20971
+ let pending = descriptorCache.get(networkId);
20972
+ if (!pending) {
20973
+ pending = this.fetchEncryptionNetworkAt(host, networkId, graph.fetch).then((descriptor2) => {
20974
+ if (descriptor2 === null) descriptorCache.delete(networkId);
20975
+ return descriptor2;
20976
+ }).catch((error) => {
20977
+ descriptorCache.delete(networkId);
20978
+ throw error;
20979
+ });
20980
+ descriptorCache.set(networkId, pending);
20981
+ }
20982
+ const descriptor = await pending;
20971
20983
  graph.assertActive();
20972
20984
  return descriptor;
20973
20985
  }
@@ -21342,6 +21354,63 @@ var _TinyCloudNode = class _TinyCloudNode {
21342
21354
  }
21343
21355
  };
21344
21356
  }
21357
+ /** Register a policy that is already bound to an activated owner delegation. */
21358
+ async registerOwnerSharePolicy(params) {
21359
+ this._serviceGraph.assertActive();
21360
+ if (params.policy.bytes.byteLength > 2 * 1024 * 1024) throw new Error("Owner share policy is too large");
21361
+ const response = await fetch(`${this.config.host}/share/v2/policies`, {
21362
+ method: "POST",
21363
+ headers: { "content-type": "application/json", accept: "application/json" },
21364
+ body: JSON.stringify({
21365
+ policy: { bytes: base64UrlEncode(params.policy.bytes), cid: params.policy.cid, proof: params.policy.proof },
21366
+ ownerDelegation: { cid: params.ownerDelegation.delegationCid, dagCbor: base64UrlEncode(params.ownerDelegation.signedDagCbor) },
21367
+ enforcementDelegation: params.enforcementDelegation,
21368
+ contentSourceDigest: params.contentSourceDigest
21369
+ })
21370
+ });
21371
+ if (!response.ok) {
21372
+ let code = "";
21373
+ try {
21374
+ const body = await response.clone().json();
21375
+ if (typeof body.error?.code === "string") code = ` ${body.error.code}`;
21376
+ } catch {
21377
+ }
21378
+ throw new Error(`Owner share policy registration failed: ${response.status}${code}`);
21379
+ }
21380
+ const responseBytes = new Uint8Array(await response.arrayBuffer());
21381
+ return (0, import_sdk_core7.validateOwnerSharePolicyRegistrationBytes)(responseBytes, params);
21382
+ }
21383
+ /** Authorize a short-lived, one-use v2 delivery using the authenticated invocation chain. */
21384
+ async authorizeShareDelivery(input) {
21385
+ const session = this.currentTinyCloudSession();
21386
+ const serviceSession = this._serviceContext?.session;
21387
+ if (!session || !serviceSession) throw new Error("Share delivery requires an authenticated session");
21388
+ const body = {
21389
+ envelopeCid: input.envelopeCid,
21390
+ shareCid: input.shareCid,
21391
+ shareId: input.shareId,
21392
+ registrationCid: input.registrationCid,
21393
+ policyCid: input.policyCid,
21394
+ delegationCid: input.delegationCid,
21395
+ enforcementDelegationCid: input.enforcementDelegationCid,
21396
+ recipientEmail: input.recipientEmail,
21397
+ shareUrl: input.shareUrl,
21398
+ documentName: input.documentName,
21399
+ jti: base64UrlEncode(crypto.getRandomValues(new Uint8Array(16))),
21400
+ expiresAt: input.expiresAt
21401
+ };
21402
+ const requestBodyDigest = base64UrlEncode(new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode((0, import_sdk_core7.canonicalizeEncryptionJson)(body)))));
21403
+ const request = { ...body, requestBodyDigest };
21404
+ const authorization = authorizationHeader(this.invokeAnyWithRuntimePermissions(serviceSession, [{ spaceId: session.spaceId, service: "kv", path: input.resourcePath, action: "tinycloud.kv/get" }]));
21405
+ const response = await fetch(`${this.config.host}/share/v2/deliveries/authorize`, {
21406
+ method: "POST",
21407
+ headers: { accept: "application/json", "content-type": "application/json", authorization },
21408
+ body: (0, import_sdk_core7.canonicalizeEncryptionJson)(request)
21409
+ });
21410
+ if (!response.ok) throw new Error(`Share delivery authorization failed: ${response.status}`);
21411
+ const responseBytes = new Uint8Array(await response.arrayBuffer());
21412
+ return (0, import_sdk_core7.validateShareDeliveryAuthorizationBytes)(responseBytes, { request, nodeProof: input.nodeProof, credentialsAudience: input.credentialsAudience });
21413
+ }
21345
21414
  async createRootDelegationForSharing(params, assertActive) {
21346
21415
  try {
21347
21416
  return (await this.createOwnerDelegation({
@@ -21510,13 +21579,13 @@ var _TinyCloudNode = class _TinyCloudNode {
21510
21579
  ownerDid,
21511
21580
  threshold: { n: 1, t: 1 }
21512
21581
  };
21513
- const crypto2 = this.createEncryptionCrypto();
21582
+ const crypto3 = this.createEncryptionCrypto();
21514
21583
  const facts = {
21515
21584
  type: NETWORK_ADMIN_TYPE,
21516
21585
  targetNode,
21517
21586
  networkId,
21518
21587
  bodyHash: (0, import_sdk_core7.canonicalHashHex)(
21519
- crypto2.sha256,
21588
+ crypto3.sha256,
21520
21589
  body
21521
21590
  ),
21522
21591
  action: NETWORK_CREATE_ACTION2