@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.js CHANGED
@@ -5723,7 +5723,7 @@ async function signCompactUcanAuthorization(input) {
5723
5723
  const publicKey = ed25519PublicKeyFromDidKey(principal);
5724
5724
  const header = {
5725
5725
  alg: "EdDSA",
5726
- jwk: { alg: "EdDSA", crv: "Ed25519", kty: "OKP", x: encodeBase64Url(publicKey) },
5726
+ jwk: { alg: "EdDSA", crv: "Ed25519", kty: "OKP", x: toBase64Url(publicKey) },
5727
5727
  typ: "JWT",
5728
5728
  ucv: "0.10.0"
5729
5729
  };
@@ -5737,13 +5737,13 @@ async function signCompactUcanAuthorization(input) {
5737
5737
  nnc: input.nonce,
5738
5738
  prf: input.proofs
5739
5739
  };
5740
- const protectedSegment = encodeBase64Url(new TextEncoder().encode(canonicalize(header)));
5741
- const payloadSegment = encodeBase64Url(new TextEncoder().encode(canonicalize(payload)));
5740
+ const protectedSegment = toBase64Url(new TextEncoder().encode(canonicalize(header)));
5741
+ const payloadSegment = toBase64Url(new TextEncoder().encode(canonicalize(payload)));
5742
5742
  const signingInput = new TextEncoder().encode(`${protectedSegment}.${payloadSegment}`);
5743
5743
  const signature = await input.sign(signingInput);
5744
5744
  if (signature.length !== 64) throw new TypeError("compact Authorization signature must be Ed25519");
5745
5745
  return verifyCompactUcanAuthorization(
5746
- `${protectedSegment}.${payloadSegment}.${encodeBase64Url(signature)}`
5746
+ `${protectedSegment}.${payloadSegment}.${toBase64Url(signature)}`
5747
5747
  );
5748
5748
  }
5749
5749
  function verifyCompactUcanAuthorization(authorization, expectedCid) {
@@ -5751,9 +5751,9 @@ function verifyCompactUcanAuthorization(authorization, expectedCid) {
5751
5751
  const segments = authorization.split(".");
5752
5752
  if (segments.length !== 3 || segments.some((segment) => segment.length === 0)) throw new TypeError("compact Authorization must contain three segments");
5753
5753
  const [headerSegment, payloadSegment, signatureSegment] = segments;
5754
- const headerBytes = decodeBase64Url(headerSegment);
5755
- const payloadBytes = decodeBase64Url(payloadSegment);
5756
- const signature = decodeBase64Url(signatureSegment);
5754
+ const headerBytes = fromBase64Url(headerSegment);
5755
+ const payloadBytes = fromBase64Url(payloadSegment);
5756
+ const signature = fromBase64Url(signatureSegment);
5757
5757
  const decoder = new TextDecoder("utf-8", { fatal: true });
5758
5758
  const header = JSON.parse(decoder.decode(headerBytes));
5759
5759
  const payload = JSON.parse(decoder.decode(payloadBytes));
@@ -5766,7 +5766,7 @@ function verifyCompactUcanAuthorization(authorization, expectedCid) {
5766
5766
  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");
5767
5767
  const principal = payload.iss.split("#", 1)[0];
5768
5768
  const publicKey = ed25519PublicKeyFromDidKey(principal);
5769
- if (!equal(publicKey, decodeBase64Url(jwk.x))) throw new TypeError("compact Authorization JWK does not bind issuer");
5769
+ if (!equal(publicKey, fromBase64Url(jwk.x))) throw new TypeError("compact Authorization JWK does not bind issuer");
5770
5770
  if (!ed255192.verify(signature, new TextEncoder().encode(`${headerSegment}.${payloadSegment}`), publicKey, { zip215: false })) throw new TypeError("compact Authorization signature is invalid");
5771
5771
  const cid2 = CID.createV1(85, create(30, blake3(new TextEncoder().encode(authorization)))).toString();
5772
5772
  if (expectedCid !== void 0 && cid2 !== expectedCid) throw new TypeError("compact Authorization CID mismatch");
@@ -5779,16 +5779,6 @@ function object(value, label) {
5779
5779
  function assertExactKeys(value, keys, label) {
5780
5780
  if (Object.keys(value).length !== keys.length || Object.keys(value).some((key) => !keys.includes(key))) throw new TypeError(`${label} has unknown or missing fields`);
5781
5781
  }
5782
- function decodeBase64Url(value) {
5783
- if (!/^[A-Za-z0-9_-]+$/.test(value) || value.length % 4 === 1) throw new TypeError("compact Authorization segment is not base64url");
5784
- 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));
5785
- const encoded = typeof Buffer !== "undefined" ? Buffer.from(bytes3).toString("base64url") : btoa(String.fromCharCode(...bytes3)).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
5786
- if (encoded !== value) throw new TypeError("compact Authorization segment is not canonical");
5787
- return bytes3;
5788
- }
5789
- function encodeBase64Url(value) {
5790
- return typeof Buffer !== "undefined" ? Buffer.from(value).toString("base64url") : btoa(String.fromCharCode(...value)).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
5791
- }
5792
5782
  function equal(left, right) {
5793
5783
  return left.length === right.length && left.every((byte, index) => byte === right[index]);
5794
5784
  }
@@ -9187,7 +9177,7 @@ var ShareRecipientClient = class {
9187
9177
  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");
9188
9178
  const encrypted = parseV3InlineEncryptedEnvelope(bytes3, envelope);
9189
9179
  const receiverPrivateKey = crypto.getRandomValues(new Uint8Array(32));
9190
- const receiverPublicKey = toBase64Url(x25519.getPublicKey(receiverPrivateKey));
9180
+ const receiverPublicKey = toBase64(x25519.getPublicKey(receiverPrivateKey));
9191
9181
  const receiverPublicKeyHash = canonicalHashHex(receiverPublicKey);
9192
9182
  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 };
9193
9183
  const bodyHash = hex2(sha2566(new TextEncoder().encode(canonicalize(body))));
@@ -9216,12 +9206,12 @@ var ShareRecipientClient = class {
9216
9206
  const signature = fromBase64(value.nodeSignature, "v3 decrypt response signature");
9217
9207
  if (signature.length !== 64 || !ed255198.verify(signature, new TextEncoder().encode(canonicalize(unsigned)), ed25519PublicKeyFromDidKey(body.targetNode), { zip215: false })) throw new Error("v3 decrypt response signature is invalid");
9218
9208
  const wrapped = fromBase64(value.wrappedKey, "v3 wrapped content key");
9219
- if (wrapped.length < 60) throw new Error("v3 wrapped content key is malformed");
9209
+ if (wrapped.length < 61 || wrapped[32] !== 1) throw new Error("v3 wrapped content key is malformed");
9220
9210
  const shared = x25519.getSharedSecret(receiverPrivateKey, wrapped.slice(0, 32));
9221
- const symmetricKey = await aesGcmDecrypt(shared, wrapped.slice(32));
9211
+ const symmetricKey = await aesGcmDecrypt(shared, wrapped.slice(33));
9222
9212
  shared.fill(0);
9223
9213
  if (symmetricKey.length !== 32) throw new Error("v3 content key is malformed");
9224
- const plaintext = await aesGcmDecrypt(symmetricKey, fromBase64Url(encrypted.ciphertext));
9214
+ const plaintext = await aesGcmDecrypt(symmetricKey, fromBase64(encrypted.ciphertext, "v3 encrypted content"));
9225
9215
  this.v3ContentKey?.fill(0);
9226
9216
  this.v3ContentKey = symmetricKey;
9227
9217
  this.v3ContentEnvelope = encrypted;