@tinycloud/share-sdk 0.3.0-beta.0 → 0.3.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
@@ -5806,7 +5806,7 @@ async function signCompactUcanAuthorization(input) {
5806
5806
  const publicKey = ed25519PublicKeyFromDidKey(principal);
5807
5807
  const header = {
5808
5808
  alg: "EdDSA",
5809
- jwk: { alg: "EdDSA", crv: "Ed25519", kty: "OKP", x: encodeBase64Url(publicKey) },
5809
+ jwk: { alg: "EdDSA", crv: "Ed25519", kty: "OKP", x: toBase64Url(publicKey) },
5810
5810
  typ: "JWT",
5811
5811
  ucv: "0.10.0"
5812
5812
  };
@@ -5820,13 +5820,13 @@ async function signCompactUcanAuthorization(input) {
5820
5820
  nnc: input.nonce,
5821
5821
  prf: input.proofs
5822
5822
  };
5823
- const protectedSegment = encodeBase64Url(new TextEncoder().encode(canonicalize(header)));
5824
- const payloadSegment = encodeBase64Url(new TextEncoder().encode(canonicalize(payload)));
5823
+ const protectedSegment = toBase64Url(new TextEncoder().encode(canonicalize(header)));
5824
+ const payloadSegment = toBase64Url(new TextEncoder().encode(canonicalize(payload)));
5825
5825
  const signingInput = new TextEncoder().encode(`${protectedSegment}.${payloadSegment}`);
5826
5826
  const signature = await input.sign(signingInput);
5827
5827
  if (signature.length !== 64) throw new TypeError("compact Authorization signature must be Ed25519");
5828
5828
  return verifyCompactUcanAuthorization(
5829
- `${protectedSegment}.${payloadSegment}.${encodeBase64Url(signature)}`
5829
+ `${protectedSegment}.${payloadSegment}.${toBase64Url(signature)}`
5830
5830
  );
5831
5831
  }
5832
5832
  function verifyCompactUcanAuthorization(authorization, expectedCid) {
@@ -5834,9 +5834,9 @@ function verifyCompactUcanAuthorization(authorization, expectedCid) {
5834
5834
  const segments = authorization.split(".");
5835
5835
  if (segments.length !== 3 || segments.some((segment) => segment.length === 0)) throw new TypeError("compact Authorization must contain three segments");
5836
5836
  const [headerSegment, payloadSegment, signatureSegment] = segments;
5837
- const headerBytes = decodeBase64Url(headerSegment);
5838
- const payloadBytes = decodeBase64Url(payloadSegment);
5839
- const signature = decodeBase64Url(signatureSegment);
5837
+ const headerBytes = fromBase64Url(headerSegment);
5838
+ const payloadBytes = fromBase64Url(payloadSegment);
5839
+ const signature = fromBase64Url(signatureSegment);
5840
5840
  const decoder = new TextDecoder("utf-8", { fatal: true });
5841
5841
  const header = JSON.parse(decoder.decode(headerBytes));
5842
5842
  const payload = JSON.parse(decoder.decode(payloadBytes));
@@ -5849,7 +5849,7 @@ function verifyCompactUcanAuthorization(authorization, expectedCid) {
5849
5849
  if (typeof payload.iss !== "string" || typeof payload.aud !== "string" || typeof payload.nnc !== "string" || !Number.isInteger(payload.nbf) || !Number.isInteger(payload.exp) || payload.nbf >= payload.exp || !Array.isArray(payload.prf) || payload.prf.some((proof) => typeof proof !== "string") || !Array.isArray(payload.fct) || payload.fct.length !== 1) throw new TypeError("compact Authorization payload is invalid");
5850
5850
  const principal = payload.iss.split("#", 1)[0];
5851
5851
  const publicKey = ed25519PublicKeyFromDidKey(principal);
5852
- if (!equal(publicKey, decodeBase64Url(jwk.x))) throw new TypeError("compact Authorization JWK does not bind issuer");
5852
+ if (!equal(publicKey, fromBase64Url(jwk.x))) throw new TypeError("compact Authorization JWK does not bind issuer");
5853
5853
  if (!import_ed255192.ed25519.verify(signature, new TextEncoder().encode(`${headerSegment}.${payloadSegment}`), publicKey, { zip215: false })) throw new TypeError("compact Authorization signature is invalid");
5854
5854
  const cid2 = CID.createV1(85, create(30, (0, import_blake3.blake3)(new TextEncoder().encode(authorization)))).toString();
5855
5855
  if (expectedCid !== void 0 && cid2 !== expectedCid) throw new TypeError("compact Authorization CID mismatch");
@@ -5862,16 +5862,6 @@ function object(value, label) {
5862
5862
  function assertExactKeys(value, keys, label) {
5863
5863
  if (Object.keys(value).length !== keys.length || Object.keys(value).some((key) => !keys.includes(key))) throw new TypeError(`${label} has unknown or missing fields`);
5864
5864
  }
5865
- function decodeBase64Url(value) {
5866
- if (!/^[A-Za-z0-9_-]+$/.test(value) || value.length % 4 === 1) throw new TypeError("compact Authorization segment is not base64url");
5867
- const bytes3 = typeof Buffer !== "undefined" ? new Uint8Array(Buffer.from(value, "base64url")) : Uint8Array.from(atob(value.replace(/-/g, "+").replace(/_/g, "/").padEnd(Math.ceil(value.length / 4) * 4, "=")), (character) => character.charCodeAt(0));
5868
- const encoded = typeof Buffer !== "undefined" ? Buffer.from(bytes3).toString("base64url") : btoa(String.fromCharCode(...bytes3)).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
5869
- if (encoded !== value) throw new TypeError("compact Authorization segment is not canonical");
5870
- return bytes3;
5871
- }
5872
- function encodeBase64Url(value) {
5873
- return typeof Buffer !== "undefined" ? Buffer.from(value).toString("base64url") : btoa(String.fromCharCode(...value)).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
5874
- }
5875
5865
  function equal(left, right) {
5876
5866
  return left.length === right.length && left.every((byte, index) => byte === right[index]);
5877
5867
  }
@@ -9270,7 +9260,7 @@ var ShareRecipientClient = class {
9270
9260
  if (envelope.version !== 3 || this.session === void 0 || this.v3Authorization === void 0 || this.v3NodeAudience === void 0 || signer === void 0) throw new Error("v3 policy session signer is required");
9271
9261
  const encrypted = parseV3InlineEncryptedEnvelope(bytes3, envelope);
9272
9262
  const receiverPrivateKey = crypto.getRandomValues(new Uint8Array(32));
9273
- const receiverPublicKey = toBase64Url(import_ed255198.x25519.getPublicKey(receiverPrivateKey));
9263
+ const receiverPublicKey = toBase64(import_ed255198.x25519.getPublicKey(receiverPrivateKey));
9274
9264
  const receiverPublicKeyHash = canonicalHashHex(receiverPublicKey);
9275
9265
  const body = { type: "tinycloud.encryption.decrypt/v1", targetNode: this.v3NodeAudience, networkId: encrypted.networkId, alg: encrypted.alg, keyVersion: encrypted.keyVersion, encryptedSymmetricKey: encrypted.encryptedSymmetricKey, encryptedSymmetricKeyHash: encrypted.encryptedSymmetricKeyHash, receiverPublicKey, receiverPublicKeyHash };
9276
9266
  const bodyHash = hex2((0, import_sha2564.sha256)(new TextEncoder().encode(canonicalize(body))));
@@ -9299,12 +9289,12 @@ var ShareRecipientClient = class {
9299
9289
  const signature = fromBase64(value.nodeSignature, "v3 decrypt response signature");
9300
9290
  if (signature.length !== 64 || !import_ed255198.ed25519.verify(signature, new TextEncoder().encode(canonicalize(unsigned)), ed25519PublicKeyFromDidKey(body.targetNode), { zip215: false })) throw new Error("v3 decrypt response signature is invalid");
9301
9291
  const wrapped = fromBase64(value.wrappedKey, "v3 wrapped content key");
9302
- if (wrapped.length < 60) throw new Error("v3 wrapped content key is malformed");
9292
+ if (wrapped.length < 61 || wrapped[32] !== 1) throw new Error("v3 wrapped content key is malformed");
9303
9293
  const shared = import_ed255198.x25519.getSharedSecret(receiverPrivateKey, wrapped.slice(0, 32));
9304
- const symmetricKey = await aesGcmDecrypt(shared, wrapped.slice(32));
9294
+ const symmetricKey = await aesGcmDecrypt(shared, wrapped.slice(33));
9305
9295
  shared.fill(0);
9306
9296
  if (symmetricKey.length !== 32) throw new Error("v3 content key is malformed");
9307
- const plaintext = await aesGcmDecrypt(symmetricKey, fromBase64Url(encrypted.ciphertext));
9297
+ const plaintext = await aesGcmDecrypt(symmetricKey, fromBase64(encrypted.ciphertext, "v3 encrypted content"));
9308
9298
  this.v3ContentKey?.fill(0);
9309
9299
  this.v3ContentKey = symmetricKey;
9310
9300
  this.v3ContentEnvelope = encrypted;