@tinycloud/cli 0.9.1-beta.2 → 0.9.1-beta.4

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
@@ -18077,7 +18077,7 @@ async function signCompactUcanAuthorization(input) {
18077
18077
  const publicKey = ed25519PublicKeyFromDidKey(principal);
18078
18078
  const header = {
18079
18079
  alg: "EdDSA",
18080
- jwk: { alg: "EdDSA", crv: "Ed25519", kty: "OKP", x: encodeBase64Url(publicKey) },
18080
+ jwk: { alg: "EdDSA", crv: "Ed25519", kty: "OKP", x: toBase64Url(publicKey) },
18081
18081
  typ: "JWT",
18082
18082
  ucv: "0.10.0"
18083
18083
  };
@@ -18091,13 +18091,13 @@ async function signCompactUcanAuthorization(input) {
18091
18091
  nnc: input.nonce,
18092
18092
  prf: input.proofs
18093
18093
  };
18094
- const protectedSegment = encodeBase64Url(new TextEncoder().encode(canonicalize2(header)));
18095
- const payloadSegment = encodeBase64Url(new TextEncoder().encode(canonicalize2(payload)));
18094
+ const protectedSegment = toBase64Url(new TextEncoder().encode(canonicalize2(header)));
18095
+ const payloadSegment = toBase64Url(new TextEncoder().encode(canonicalize2(payload)));
18096
18096
  const signingInput = new TextEncoder().encode(`${protectedSegment}.${payloadSegment}`);
18097
18097
  const signature = await input.sign(signingInput);
18098
18098
  if (signature.length !== 64) throw new TypeError("compact Authorization signature must be Ed25519");
18099
18099
  return verifyCompactUcanAuthorization(
18100
- `${protectedSegment}.${payloadSegment}.${encodeBase64Url(signature)}`
18100
+ `${protectedSegment}.${payloadSegment}.${toBase64Url(signature)}`
18101
18101
  );
18102
18102
  }
18103
18103
  function verifyCompactUcanAuthorization(authorization, expectedCid) {
@@ -18105,9 +18105,9 @@ function verifyCompactUcanAuthorization(authorization, expectedCid) {
18105
18105
  const segments = authorization.split(".");
18106
18106
  if (segments.length !== 3 || segments.some((segment) => segment.length === 0)) throw new TypeError("compact Authorization must contain three segments");
18107
18107
  const [headerSegment, payloadSegment, signatureSegment] = segments;
18108
- const headerBytes = decodeBase64Url(headerSegment);
18109
- const payloadBytes = decodeBase64Url(payloadSegment);
18110
- const signature = decodeBase64Url(signatureSegment);
18108
+ const headerBytes = fromBase64Url(headerSegment);
18109
+ const payloadBytes = fromBase64Url(payloadSegment);
18110
+ const signature = fromBase64Url(signatureSegment);
18111
18111
  const decoder = new TextDecoder("utf-8", { fatal: true });
18112
18112
  const header = JSON.parse(decoder.decode(headerBytes));
18113
18113
  const payload = JSON.parse(decoder.decode(payloadBytes));
@@ -18120,7 +18120,7 @@ function verifyCompactUcanAuthorization(authorization, expectedCid) {
18120
18120
  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");
18121
18121
  const principal = payload.iss.split("#", 1)[0];
18122
18122
  const publicKey = ed25519PublicKeyFromDidKey(principal);
18123
- if (!equal(publicKey, decodeBase64Url(jwk.x))) throw new TypeError("compact Authorization JWK does not bind issuer");
18123
+ if (!equal(publicKey, fromBase64Url(jwk.x))) throw new TypeError("compact Authorization JWK does not bind issuer");
18124
18124
  if (!ed25519.verify(signature, new TextEncoder().encode(`${headerSegment}.${payloadSegment}`), publicKey, { zip215: false })) throw new TypeError("compact Authorization signature is invalid");
18125
18125
  const cid2 = CID.createV1(85, create(30, blake3(new TextEncoder().encode(authorization)))).toString();
18126
18126
  if (expectedCid !== void 0 && cid2 !== expectedCid) throw new TypeError("compact Authorization CID mismatch");
@@ -18133,16 +18133,6 @@ function object(value, label) {
18133
18133
  function assertExactKeys(value, keys, label) {
18134
18134
  if (Object.keys(value).length !== keys.length || Object.keys(value).some((key) => !keys.includes(key))) throw new TypeError(`${label} has unknown or missing fields`);
18135
18135
  }
18136
- function decodeBase64Url(value) {
18137
- if (!/^[A-Za-z0-9_-]+$/.test(value) || value.length % 4 === 1) throw new TypeError("compact Authorization segment is not base64url");
18138
- 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));
18139
- const encoded = typeof Buffer !== "undefined" ? Buffer.from(bytes3).toString("base64url") : btoa(String.fromCharCode(...bytes3)).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
18140
- if (encoded !== value) throw new TypeError("compact Authorization segment is not canonical");
18141
- return bytes3;
18142
- }
18143
- function encodeBase64Url(value) {
18144
- return typeof Buffer !== "undefined" ? Buffer.from(value).toString("base64url") : btoa(String.fromCharCode(...value)).replace(/=/g, "").replace(/\+/g, "-").replace(/\//g, "_");
18145
- }
18146
18136
  function equal(left, right) {
18147
18137
  return left.length === right.length && left.every((byte, index) => byte === right[index]);
18148
18138
  }
@@ -25772,7 +25762,7 @@ var init_dist3 = __esm({
25772
25762
  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");
25773
25763
  const encrypted = parseV3InlineEncryptedEnvelope(bytes3, envelope);
25774
25764
  const receiverPrivateKey = crypto.getRandomValues(new Uint8Array(32));
25775
- const receiverPublicKey = toBase64Url(x25519.getPublicKey(receiverPrivateKey));
25765
+ const receiverPublicKey = toBase64(x25519.getPublicKey(receiverPrivateKey));
25776
25766
  const receiverPublicKeyHash = canonicalHashHex2(receiverPublicKey);
25777
25767
  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 };
25778
25768
  const bodyHash = hex2(sha2562(new TextEncoder().encode(canonicalize2(body))));
@@ -25801,12 +25791,12 @@ var init_dist3 = __esm({
25801
25791
  const signature = fromBase64(value.nodeSignature, "v3 decrypt response signature");
25802
25792
  if (signature.length !== 64 || !ed25519.verify(signature, new TextEncoder().encode(canonicalize2(unsigned)), ed25519PublicKeyFromDidKey(body.targetNode), { zip215: false })) throw new Error("v3 decrypt response signature is invalid");
25803
25793
  const wrapped = fromBase64(value.wrappedKey, "v3 wrapped content key");
25804
- if (wrapped.length < 60) throw new Error("v3 wrapped content key is malformed");
25794
+ if (wrapped.length < 61 || wrapped[32] !== 1) throw new Error("v3 wrapped content key is malformed");
25805
25795
  const shared = x25519.getSharedSecret(receiverPrivateKey, wrapped.slice(0, 32));
25806
- const symmetricKey = await aesGcmDecrypt(shared, wrapped.slice(32));
25796
+ const symmetricKey = await aesGcmDecrypt(shared, wrapped.slice(33));
25807
25797
  shared.fill(0);
25808
25798
  if (symmetricKey.length !== 32) throw new Error("v3 content key is malformed");
25809
- const plaintext = await aesGcmDecrypt(symmetricKey, fromBase64Url(encrypted.ciphertext));
25799
+ const plaintext = await aesGcmDecrypt(symmetricKey, fromBase64(encrypted.ciphertext, "v3 encrypted content"));
25810
25800
  this.v3ContentKey?.fill(0);
25811
25801
  this.v3ContentKey = symmetricKey;
25812
25802
  this.v3ContentEnvelope = encrypted;
@@ -27315,7 +27305,7 @@ async function verifyPkhSignature(did, payload, signature) {
27315
27305
  }
27316
27306
  function verifyDidKeySignature(did, payload, signature) {
27317
27307
  const publicKey = ed25519PublicKeyFromDidKey2(did);
27318
- const signatureBytes = decodeBase64Url3(signature);
27308
+ const signatureBytes = decodeBase64Url4(signature);
27319
27309
  if (signatureBytes.length !== 64) {
27320
27310
  throw new LocationRecordValidationError(
27321
27311
  "did:key signature must be a base64url Ed25519 signature"
@@ -27345,7 +27335,7 @@ function ed25519PublicKeyFromDidKey2(did) {
27345
27335
  "did:key must be an Ed25519 public key"
27346
27336
  );
27347
27337
  }
27348
- function decodeBase64Url3(value) {
27338
+ function decodeBase64Url4(value) {
27349
27339
  const alphabet2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
27350
27340
  const bytes22 = [];
27351
27341
  let buffer = 0;