@tinycloud/share-sdk 0.2.0-beta.1 → 0.2.0-beta.2
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 +38 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +38 -2
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -8659,6 +8659,42 @@ function bytes2(value, label) {
|
|
|
8659
8659
|
if (decoded.length !== 64 || toBase64Url(decoded) !== value) throw new Error(`${label} is invalid`);
|
|
8660
8660
|
return decoded;
|
|
8661
8661
|
}
|
|
8662
|
+
var BASE64_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
8663
|
+
function toBase64(bytes3) {
|
|
8664
|
+
let out = "";
|
|
8665
|
+
for (let i = 0; i < bytes3.length; i += 3) {
|
|
8666
|
+
const b0 = bytes3[i];
|
|
8667
|
+
const b1 = i + 1 < bytes3.length ? bytes3[i + 1] : 0;
|
|
8668
|
+
const b2 = i + 2 < bytes3.length ? bytes3[i + 2] : 0;
|
|
8669
|
+
out += BASE64_ALPHABET[b0 >> 2 & 63];
|
|
8670
|
+
out += BASE64_ALPHABET[(b0 << 4 | b1 >> 4) & 63];
|
|
8671
|
+
out += i + 1 < bytes3.length ? BASE64_ALPHABET[(b1 << 2 | b2 >> 6) & 63] : "=";
|
|
8672
|
+
out += i + 2 < bytes3.length ? BASE64_ALPHABET[b2 & 63] : "=";
|
|
8673
|
+
}
|
|
8674
|
+
return out;
|
|
8675
|
+
}
|
|
8676
|
+
function fromBase64(value, label) {
|
|
8677
|
+
if (!/^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(value)) {
|
|
8678
|
+
throw new Error(`${label} is invalid`);
|
|
8679
|
+
}
|
|
8680
|
+
const padding = value.endsWith("==") ? 2 : value.endsWith("=") ? 1 : 0;
|
|
8681
|
+
const out = new Uint8Array(value.length / 4 * 3 - padding);
|
|
8682
|
+
let outIdx = 0;
|
|
8683
|
+
for (let i = 0; i < value.length; i += 4) {
|
|
8684
|
+
const v0 = BASE64_ALPHABET.indexOf(value[i]);
|
|
8685
|
+
const v1 = BASE64_ALPHABET.indexOf(value[i + 1]);
|
|
8686
|
+
const v2 = value[i + 2] === "=" ? 0 : BASE64_ALPHABET.indexOf(value[i + 2]);
|
|
8687
|
+
const v3 = value[i + 3] === "=" ? 0 : BASE64_ALPHABET.indexOf(value[i + 3]);
|
|
8688
|
+
const b0 = v0 << 2 | v1 >> 4;
|
|
8689
|
+
const b1 = (v1 & 15) << 4 | v2 >> 2;
|
|
8690
|
+
const b2 = (v2 & 3) << 6 | v3;
|
|
8691
|
+
if (outIdx < out.length) out[outIdx++] = b0;
|
|
8692
|
+
if (outIdx < out.length) out[outIdx++] = b1;
|
|
8693
|
+
if (outIdx < out.length) out[outIdx++] = b2;
|
|
8694
|
+
}
|
|
8695
|
+
if (toBase64(out) !== value) throw new Error(`${label} is invalid`);
|
|
8696
|
+
return out;
|
|
8697
|
+
}
|
|
8662
8698
|
async function digest3(value) {
|
|
8663
8699
|
return toBase64Url(new Uint8Array(await crypto.subtle.digest("SHA-256", new TextEncoder().encode(canonicalize(value)))));
|
|
8664
8700
|
}
|
|
@@ -8914,9 +8950,9 @@ var ShareRecipientClient = class {
|
|
|
8914
8950
|
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(sha2566(new TextEncoder().encode(`${invocation.cid}${bodyHash}`))) || typeof value.wrappedKey !== "string" || typeof value.nodeSignature !== "string") throw new Error("v3 decrypt response binding is invalid");
|
|
8915
8951
|
const unsigned = { ...value };
|
|
8916
8952
|
delete unsigned.nodeSignature;
|
|
8917
|
-
const signature =
|
|
8953
|
+
const signature = fromBase64(value.nodeSignature, "v3 decrypt response signature");
|
|
8918
8954
|
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");
|
|
8919
|
-
const wrapped =
|
|
8955
|
+
const wrapped = fromBase64(value.wrappedKey, "v3 wrapped content key");
|
|
8920
8956
|
if (wrapped.length < 60) throw new Error("v3 wrapped content key is malformed");
|
|
8921
8957
|
const shared = x25519.getSharedSecret(receiverPrivateKey, wrapped.slice(0, 32));
|
|
8922
8958
|
const symmetricKey = await aesGcmDecrypt(shared, wrapped.slice(32));
|