@tinycloud/share-sdk 0.2.0-beta.1 → 0.2.0

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
@@ -8742,6 +8742,42 @@ function bytes2(value, label) {
8742
8742
  if (decoded.length !== 64 || toBase64Url(decoded) !== value) throw new Error(`${label} is invalid`);
8743
8743
  return decoded;
8744
8744
  }
8745
+ var BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
8746
+ function toBase64(bytes3) {
8747
+ let out = "";
8748
+ for (let i = 0; i < bytes3.length; i += 3) {
8749
+ const b0 = bytes3[i];
8750
+ const b1 = i + 1 < bytes3.length ? bytes3[i + 1] : 0;
8751
+ const b2 = i + 2 < bytes3.length ? bytes3[i + 2] : 0;
8752
+ out += BASE64_ALPHABET[b0 >> 2 & 63];
8753
+ out += BASE64_ALPHABET[(b0 << 4 | b1 >> 4) & 63];
8754
+ out += i + 1 < bytes3.length ? BASE64_ALPHABET[(b1 << 2 | b2 >> 6) & 63] : "=";
8755
+ out += i + 2 < bytes3.length ? BASE64_ALPHABET[b2 & 63] : "=";
8756
+ }
8757
+ return out;
8758
+ }
8759
+ function fromBase64(value, label) {
8760
+ if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value)) {
8761
+ throw new Error(`${label} is invalid`);
8762
+ }
8763
+ const padding = value.endsWith("==") ? 2 : value.endsWith("=") ? 1 : 0;
8764
+ const out = new Uint8Array(value.length / 4 * 3 - padding);
8765
+ let outIdx = 0;
8766
+ for (let i = 0; i < value.length; i += 4) {
8767
+ const v0 = BASE64_ALPHABET.indexOf(value[i]);
8768
+ const v1 = BASE64_ALPHABET.indexOf(value[i + 1]);
8769
+ const v2 = value[i + 2] === "=" ? 0 : BASE64_ALPHABET.indexOf(value[i + 2]);
8770
+ const v3 = value[i + 3] === "=" ? 0 : BASE64_ALPHABET.indexOf(value[i + 3]);
8771
+ const b0 = v0 << 2 | v1 >> 4;
8772
+ const b1 = (v1 & 15) << 4 | v2 >> 2;
8773
+ const b2 = (v2 & 3) << 6 | v3;
8774
+ if (outIdx < out.length) out[outIdx++] = b0;
8775
+ if (outIdx < out.length) out[outIdx++] = b1;
8776
+ if (outIdx < out.length) out[outIdx++] = b2;
8777
+ }
8778
+ if (toBase64(out) !== value) throw new Error(`${label} is invalid`);
8779
+ return out;
8780
+ }
8745
8781
  async function digest3(value) {
8746
8782
  return toBase64Url(new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(canonicalize(value)))));
8747
8783
  }
@@ -8997,9 +9033,9 @@ var ShareRecipientClient = class {
8997
9033
  if (Object.keys(value).length !== allowed.length || Object.keys(value).some((key) => !allowed.includes(key)) || value.type !== "tinycloud.encryption.decrypt-result/v1" || value.targetNode !== body.targetNode || value.nodeId !== body.targetNode || value.networkId !== body.networkId || value.invocationCid !== invocation.cid || value.encryptedSymmetricKeyHash !== body.encryptedSymmetricKeyHash || value.receiverPublicKeyHash !== receiverPublicKeyHash || value.alg !== body.alg || value.keyVersion !== body.keyVersion || value.requestHash !== hex((0, import_sha2564.sha256)(new TextEncoder().encode(`${invocation.cid}${bodyHash}`))) || typeof value.wrappedKey !== "string" || typeof value.nodeSignature !== "string") throw new Error("v3 decrypt response binding is invalid");
8998
9034
  const unsigned = { ...value };
8999
9035
  delete unsigned.nodeSignature;
9000
- const signature = fromBase64Url(value.nodeSignature);
9036
+ const signature = fromBase64(value.nodeSignature, "v3 decrypt response signature");
9001
9037
  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");
9002
- const wrapped = fromBase64Url(value.wrappedKey);
9038
+ const wrapped = fromBase64(value.wrappedKey, "v3 wrapped content key");
9003
9039
  if (wrapped.length < 60) throw new Error("v3 wrapped content key is malformed");
9004
9040
  const shared = import_ed255198.x25519.getSharedSecret(receiverPrivateKey, wrapped.slice(0, 32));
9005
9041
  const symmetricKey = await aesGcmDecrypt(shared, wrapped.slice(32));