diff --git a/.cspell.jsonc b/.cspell.jsonc index 529c195..0363706 100644 --- a/.cspell.jsonc +++ b/.cspell.jsonc @@ -16,18 +16,17 @@ "eciespy", "eth", "futoin-hkdf", - "helloworld", "hkdf", "js", "Npm", - "Prv", - "querystring", "secp256k1" ], // flagWords - list of words to be always considered incorrect // This is useful for offensive words and common spelling errors. // For example "hte" should be "the" - "flagWords": ["hte"], + "flagWords": [ + "hte" + ], "ignorePaths": [ ".git", ".github", diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index d647f6a..cff77bf 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -7,6 +7,9 @@ on: jobs: publish: runs-on: ubuntu-latest + permissions: + contents: read + id-token: write steps: - uses: actions/checkout@v4 - uses: pnpm/action-setup@v4 diff --git a/tests/crypt.test.ts b/tests/crypt.test.ts index 9416c95..926bd9e 100644 --- a/tests/crypt.test.ts +++ b/tests/crypt.test.ts @@ -12,7 +12,7 @@ describe("test encrypt and decrypt", () => { it("tests aes with random key", () => { const key = randomBytes(32); const data = Buffer.from("this is a test"); - expect(data.equals(aesDecrypt(key, aesEncrypt(key, data)))).toEqual(true); + expect(data).toStrictEqual(aesDecrypt(key, aesEncrypt(key, data))); }); it("tests aes decrypt with known key and TEXT", () => { @@ -25,27 +25,27 @@ describe("test encrypt and decrypt", () => { const data = Buffer.concat([nonce, tag, encrypted]); const decrypted = aesDecrypt(key, data); - expect(decrypted.toString()).toEqual(TEXT); + expect(decrypted.toString()).toStrictEqual(TEXT); }); it("tests encrypt/decrypt buffer", () => { const prv1 = new PrivateKey(); const encrypted1 = encrypt(prv1.publicKey.uncompressed, Buffer.from(TEXT)); - expect(decrypt(prv1.secret, encrypted1).toString()).toEqual(TEXT); + expect(decrypt(prv1.secret, encrypted1).toString()).toStrictEqual(TEXT); const prv2 = new PrivateKey(); const encrypted2 = encrypt(prv2.publicKey.compressed, Buffer.from(TEXT)); - expect(decrypt(prv2.secret, encrypted2).toString()).toEqual(TEXT); + expect(decrypt(prv2.secret, encrypted2).toString()).toStrictEqual(TEXT); }); it("tests encrypt/decrypt hex", () => { const prv1 = new PrivateKey(); const encrypted1 = encrypt(prv1.publicKey.toHex(), Buffer.from(TEXT)); - expect(decrypt(prv1.toHex(), encrypted1).toString()).toEqual(TEXT); + expect(decrypt(prv1.toHex(), encrypted1).toString()).toStrictEqual(TEXT); const prv2 = new PrivateKey(); const encrypted2 = encrypt(prv2.publicKey.toHex(), Buffer.from(TEXT)); - expect(decrypt(prv2.toHex(), encrypted2).toString()).toEqual(TEXT); + expect(decrypt(prv2.toHex(), encrypted2).toString()).toStrictEqual(TEXT); }); it("tests sk pk", () => { @@ -56,6 +56,6 @@ describe("test encrypt and decrypt", () => { "048e41409f2e109f2d704f0afd15d1ab53935fd443729913a7e8536b4cef8cf5773d4db7bbd99e9ed64595e24a251c9836f35d4c9842132443c17f6d501b3410d2" ); const enc = encrypt(pk.toHex(), Buffer.from(TEXT)); - expect(decrypt(sk.toHex(), enc).toString()).toEqual(TEXT); + expect(decrypt(sk.toHex(), enc).toString()).toStrictEqual(TEXT); }); }); diff --git a/tests/integration.test.ts b/tests/integration.test.ts index 917c5c3..d330aa1 100644 --- a/tests/integration.test.ts +++ b/tests/integration.test.ts @@ -6,7 +6,7 @@ import { decrypt, encrypt, PrivateKey, utils } from "../src/index"; const decodeHex = utils.decodeHex; const PYTHON_BACKEND = "https://demo.ecies.org/"; -const TEXT = "helloworld🌍"; +const TEXT = "hello world🌍"; describe("test encrypt and decrypt against python version", () => { it("tests encrypt", async () => { @@ -16,7 +16,7 @@ describe("test encrypt and decrypt against python version", () => { pub: sk.publicKey.toHex(), }); const decrypted = decrypt(sk.toHex(), decodeHex(await res.text())); - expect(decrypted.toString()).toEqual(TEXT); + expect(decrypted.toString()).toStrictEqual(TEXT); }); it("tests decrypt", async () => { @@ -26,7 +26,7 @@ describe("test encrypt and decrypt against python version", () => { data: encrypted.toString("hex"), prv: sk.toHex(), }); - expect(TEXT).toEqual(await res.text()); + expect(TEXT).toStrictEqual(await res.text()); }); }); @@ -37,8 +37,9 @@ async function eciesApi(url: string, body: { data: string; pub?: string; prv?: s "Content-Type": "application/x-www-form-urlencoded", }, }; - if (process.env.http_proxy !== undefined) { - config.dispatcher = new ProxyAgent(`${process.env.http_proxy}`); + const proxy = process.env.https_proxy || process.env.http_proxy; + if (proxy) { + config.dispatcher = new ProxyAgent(`${proxy}`); } return await fetch(url, { diff --git a/tests/keys.test.ts b/tests/keys.test.ts index a56f40c..f7dae45 100644 --- a/tests/keys.test.ts +++ b/tests/keys.test.ts @@ -3,8 +3,8 @@ import { describe, expect, it } from "vitest"; import { PrivateKey, PublicKey } from "../src/keys"; import { decodeHex } from "../src/utils"; -const ETH_PRVHEX = "0x95d3c5e483e9b1d4f5fc8e79b2deaf51362980de62dbb082a9a4257eef653d7d"; -const ETH_PUBHEX = +const ETH_SK_HEX = "0x95d3c5e483e9b1d4f5fc8e79b2deaf51362980de62dbb082a9a4257eef653d7d"; +const ETH_PK_HEX = "0x98afe4f150642cd05cc9d2fa36458ce0a58567daeaf5fde7333ba9b403011140" + "a4e28911fcf83ab1f457a30b4959efc4b9306f514a4c3711a16a80e3b47eb58b"; @@ -26,17 +26,17 @@ describe("test keys", () => { const prv = new PrivateKey(); const pub = PublicKey.fromHex(prv.publicKey.toHex(false)); - const isPubEqual = pub.uncompressed.equals(prv.publicKey.uncompressed); - expect(isPubEqual).toEqual(true); + expect(pub.equals(prv.publicKey)).toBe(true); + expect(pub).toStrictEqual(prv.publicKey); - const isFromHexWorking = prv.equals(PrivateKey.fromHex(prv.toHex())); - expect(isFromHexWorking).toEqual(true); + expect(prv.equals(PrivateKey.fromHex(prv.toHex()))).toBe(true); + expect(prv).toStrictEqual(PrivateKey.fromHex(prv.toHex())); }); it("tests eth key compatibility", () => { - const ethPrv = PrivateKey.fromHex(ETH_PRVHEX); - const ethPub = PublicKey.fromHex(ETH_PUBHEX); - expect(ethPub.equals(ethPrv.publicKey)).toEqual(true); + expect(PublicKey.fromHex(ETH_PK_HEX)).toStrictEqual( + PrivateKey.fromHex(ETH_SK_HEX).publicKey + ); }); it("tests multiply and hkdf", () => { @@ -47,14 +47,14 @@ describe("test keys", () => { const k1 = new PrivateKey(two); const k2 = new PrivateKey(three); - expect(k1.multiply(k2.publicKey).equals(k2.multiply(k1.publicKey))).toEqual(true); + expect(k1.multiply(k2.publicKey)).toStrictEqual(k2.multiply(k1.publicKey)); const derived = k1.encapsulate(k2.publicKey); const anotherDerived = k1.publicKey.decapsulate(k2); const knownDerived = Buffer.from( decodeHex("6f982d63e8590c9d9b5b4c1959ff80315d772edd8f60287c9361d548d5200f82") ); - expect(derived.equals(knownDerived)).toEqual(true); - expect(anotherDerived.equals(knownDerived)).toEqual(true); + expect(derived).toStrictEqual(knownDerived); + expect(anotherDerived).toStrictEqual(knownDerived); }); }); diff --git a/tests/utils.test.ts b/tests/utils.test.ts index 8e90362..3ef36f6 100644 --- a/tests/utils.test.ts +++ b/tests/utils.test.ts @@ -6,19 +6,19 @@ import { decodeHex, getValidSecret, remove0x } from "../src/utils"; describe("test string <-> buffer utils ", () => { it("should remove 0x", () => { - expect(remove0x("0x0011")).toEqual("0011"); - expect(remove0x("0011")).toEqual("0011"); - expect(remove0x("0X0022")).toEqual("0022"); - expect(remove0x("0022")).toEqual("0022"); + expect(remove0x("0x0011")).toStrictEqual("0011"); + expect(remove0x("0011")).toStrictEqual("0011"); + expect(remove0x("0X0022")).toStrictEqual("0022"); + expect(remove0x("0022")).toStrictEqual("0022"); }); it("should generate valid secret", () => { const key = getValidSecret(); - expect(secp256k1.privateKeyVerify(key)).toEqual(true); + expect(secp256k1.privateKeyVerify(key)).toBe(true); }); it("should convert hex to buffer", () => { const decoded = decodeHex("0x0011"); - expect(decoded.equals(Buffer.from([0, 0x11]))).toEqual(true); + expect(decoded).toStrictEqual(Buffer.from([0, 0x11])); }); });