From 18e99bdcfeb3f57a08a9da5be588a454f625a944 Mon Sep 17 00:00:00 2001 From: Weiliang Li Date: Wed, 9 Oct 2024 01:09:59 +0900 Subject: [PATCH] Export all modules --- .github/workflows/ci.yml | 1 + CHANGELOG.md | 7 ++++ example/import.js | 4 ++- package.json | 28 ++++++++++++--- pnpm-lock.yaml | 77 ++++++++++++++++++---------------------- src/index.ts | 1 + src/utils/elliptic.ts | 2 +- src/utils/hash.ts | 6 ++++ src/utils/index.ts | 1 + src/utils/symmetric.ts | 6 ---- 10 files changed, 78 insertions(+), 55 deletions(-) create mode 100644 src/utils/hash.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ddbb812..10ecff2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -34,3 +34,4 @@ jobs: - run: cd example && pnpm install - run: node example/index.js + - run: node example/import.js diff --git a/CHANGELOG.md b/CHANGELOG.md index f37bc60..ee34d01 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,13 @@ # Changelog +## 0.4.9 + +- Add examples +- Update documentation +- Migrate to vitest +- Export all modules to allow full customization + ## 0.4.1 ~ 0.4.8 - Revamp util functions diff --git a/example/import.js b/example/import.js index 2d7f350..79a518f 100644 --- a/example/import.js +++ b/example/import.js @@ -1,8 +1,10 @@ -import { ECIES_CONFIG, utils } from "eciesjs"; +import { ECIES_CONFIG, utils as _utils } from "eciesjs"; import config from "eciesjs/config"; import consts from "eciesjs/consts"; +import utils from "eciesjs/utils"; console.log("ECIES_CONFIG:", ECIES_CONFIG) console.log("config:", config) console.log("consts:", consts) console.log("utils:", utils) +console.log("index utils:", _utils) diff --git a/package.json b/package.json index 6e3482b..bc49e0d 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "type": "git", "url": "git+https://github.com/ecies/js.git" }, - "version": "0.4.8", + "version": "0.4.9", "engines": { "node": ">=16.0.0" }, @@ -30,6 +30,24 @@ "files": [ "dist" ], + "exports": { + ".": { + "types": "./dist/index.d.ts", + "import": "./dist/index.js" + }, + "./config": { + "types": "./dist/config.d.ts", + "import": "./dist/config.js" + }, + "./consts": { + "types": "./dist/consts.d.ts", + "import": "./dist/consts.js" + }, + "./utils": { + "types": "./dist/utils/index.d.ts", + "import": "./dist/utils/index.js" + } + }, "scripts": { "build": "npx tsc", "test": "vitest" @@ -40,11 +58,11 @@ "@noble/hashes": "^1.5.0" }, "devDependencies": { - "@types/node": "^22.7.4", + "@types/node": "^22.7.5", "@vitest/coverage-v8": "2.1.2", - "typescript": "^5.6.2", - "undici": "^6.19.8", + "typescript": "^5.6.3", + "undici": "^6.20.0", "vitest": "^2.1.2" }, - "packageManager": "pnpm@9.12.0+sha512.4abf725084d7bcbafbd728bfc7bee61f2f791f977fd87542b3579dcb23504d170d46337945e4c66485cd12d588a0c0e570ed9c477e7ccdd8507cf05f3f92eaca" + "packageManager": "pnpm@9.12.1+sha512.e5a7e52a4183a02d5931057f7a0dbff9d5e9ce3161e33fa68ae392125b79282a8a8a470a51dfc8a0ed86221442eb2fb57019b0990ed24fab519bf0e1bc5ccfc4" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 4c0ca58..c2cf448 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -19,20 +19,20 @@ importers: version: 1.5.0 devDependencies: '@types/node': - specifier: ^22.7.4 - version: 22.7.4 + specifier: ^22.7.5 + version: 22.7.5 '@vitest/coverage-v8': specifier: 2.1.2 - version: 2.1.2(vitest@2.1.2(@types/node@22.7.4)) + version: 2.1.2(vitest@2.1.2(@types/node@22.7.5)) typescript: - specifier: ^5.6.2 - version: 5.6.2 + specifier: ^5.6.3 + version: 5.6.3 undici: - specifier: ^6.19.8 - version: 6.19.8 + specifier: ^6.20.0 + version: 6.20.0 vitest: specifier: ^2.1.2 - version: 2.1.2(@types/node@22.7.4) + version: 2.1.2(@types/node@22.7.5) packages: @@ -323,8 +323,8 @@ packages: '@types/estree@1.0.6': resolution: {integrity: sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==} - '@types/node@22.7.4': - resolution: {integrity: sha512-y+NPi1rFzDs1NdQHHToqeiX2TIS79SWEAw9GYhkkx8bD0ChpfqC+n2j5OXOCpzfojBEBt6DnEnnG9MY0zk1XLg==} + '@types/node@22.7.5': + resolution: {integrity: sha512-jML7s2NAzMWc//QSJ1a3prpk78cOPchGvXJsC3C6R6PSMoooztvRVQEz89gmBTBY1SPMaqo5teB4uNHPdetShQ==} '@vitest/coverage-v8@2.1.2': resolution: {integrity: sha512-b7kHrFrs2urS0cOk5N10lttI8UdJ/yP3nB4JYTREvR5o18cR99yPpK4gK8oQgI42BVv0ILWYUSYB7AXkAUDc0g==} @@ -453,9 +453,6 @@ packages: engines: {node: ^8.16.0 || ^10.6.0 || >=11.0.0} os: [darwin] - get-func-name@2.0.2: - resolution: {integrity: sha512-8vXOvuE167CtIc3OyItco7N/dpRtBbYOsPsXCz7X/PMnlGjYjSGuZJgM1Y7mmew7BKf9BqvLX2tnOVy1BBUsxQ==} - glob@10.4.5: resolution: {integrity: sha512-7Bv8RF0k6xjo7d4A/PxYLbUCfb6c+Vpd2/mB2yRDlew7Jb5hEXiCD9ibfO7wpk8i4sevK6DFny9h7EYbM3/sHg==} hasBin: true @@ -493,8 +490,8 @@ packages: jackspeak@3.4.3: resolution: {integrity: sha512-OGlZQpz2yfahA/Rd1Y8Cd9SIEsqvXkLVoSw/cgwhnhFMDbsQFeZYoJJ7bIZBS9BcamUW96asq/npPWugM+RQBw==} - loupe@3.1.1: - resolution: {integrity: sha512-edNu/8D5MKVfGVFRhFf8aAxiTM6Wumfz5XsaatSxlD3w4R1d/WEKUTydCdPGbl9K7QG/Ca3GnDV2sIKIpXRQcw==} + loupe@3.1.2: + resolution: {integrity: sha512-23I4pFZHmAemUnz8WZXbYRSKYj801VDaNv9ETuMh7IrMc7VuVVSo+Z9iLE3ni30+U48iDWfi30d3twAXBYmnCg==} lru-cache@10.4.3: resolution: {integrity: sha512-JNAzZcXrCt42VGLuYz0zfAzDfAvJWW6AfYlDBQyDV5DClI2m5sAmK+OIO7s59XfsRsWHp02jAJrRadPRGTt6SQ==} @@ -631,16 +628,16 @@ packages: resolution: {integrity: sha512-/OaKK0xYrs3DmxRYqL/yDc+FxFUVYhDlXMhRmv3z915w2HF1tnN1omB354j8VUGO/hbRzyD6Y3sA7v7GS/ceog==} engines: {node: '>=4'} - typescript@5.6.2: - resolution: {integrity: sha512-NW8ByodCSNCwZeghjN3o+JX5OFH0Ojg6sadjEKY4huZ52TqbJTJnDo5+Tw98lSy63NZvi4n+ez5m2u5d4PkZyw==} + typescript@5.6.3: + resolution: {integrity: sha512-hjcS1mhfuyi4WW8IWtjP7brDrG2cuDZukyrYrSauoXGNgx0S7zceP07adYkJycEr56BOUTNPzbInooiN3fn1qw==} engines: {node: '>=14.17'} hasBin: true undici-types@6.19.8: resolution: {integrity: sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==} - undici@6.19.8: - resolution: {integrity: sha512-U8uCCl2x9TK3WANvmBavymRzxbfFYG+tAu+fgx3zxQy3qdagQqBLwJVrdyO1TBfUXvfKveMKJZhpvUYoOjM+4g==} + undici@6.20.0: + resolution: {integrity: sha512-AITZfPuxubm31Sx0vr8bteSalEbs9wQb/BOBi9FPlD9Qpd6HxZ4Q0+hI742jBhkPb4RT2v5MQzaW5VhRVyj+9A==} engines: {node: '>=18.17'} vite-node@2.1.2: @@ -903,11 +900,11 @@ snapshots: '@types/estree@1.0.6': {} - '@types/node@22.7.4': + '@types/node@22.7.5': dependencies: undici-types: 6.19.8 - '@vitest/coverage-v8@2.1.2(vitest@2.1.2(@types/node@22.7.4))': + '@vitest/coverage-v8@2.1.2(vitest@2.1.2(@types/node@22.7.5))': dependencies: '@ampproject/remapping': 2.3.0 '@bcoe/v8-coverage': 0.2.3 @@ -921,7 +918,7 @@ snapshots: std-env: 3.7.0 test-exclude: 7.0.1 tinyrainbow: 1.2.0 - vitest: 2.1.2(@types/node@22.7.4) + vitest: 2.1.2(@types/node@22.7.5) transitivePeerDependencies: - supports-color @@ -932,13 +929,13 @@ snapshots: chai: 5.1.1 tinyrainbow: 1.2.0 - '@vitest/mocker@2.1.2(@vitest/spy@2.1.2)(vite@5.4.8(@types/node@22.7.4))': + '@vitest/mocker@2.1.2(@vitest/spy@2.1.2)(vite@5.4.8(@types/node@22.7.5))': dependencies: '@vitest/spy': 2.1.2 estree-walker: 3.0.3 magic-string: 0.30.11 optionalDependencies: - vite: 5.4.8(@types/node@22.7.4) + vite: 5.4.8(@types/node@22.7.5) '@vitest/pretty-format@2.1.2': dependencies: @@ -962,7 +959,7 @@ snapshots: '@vitest/utils@2.1.2': dependencies: '@vitest/pretty-format': 2.1.2 - loupe: 3.1.1 + loupe: 3.1.2 tinyrainbow: 1.2.0 ansi-regex@5.0.1: {} @@ -990,7 +987,7 @@ snapshots: assertion-error: 2.0.1 check-error: 2.1.1 deep-eql: 5.0.2 - loupe: 3.1.1 + loupe: 3.1.2 pathval: 2.0.0 check-error@2.1.1: {} @@ -1057,8 +1054,6 @@ snapshots: fsevents@2.3.3: optional: true - get-func-name@2.0.2: {} - glob@10.4.5: dependencies: foreground-child: 3.3.0 @@ -1103,9 +1098,7 @@ snapshots: optionalDependencies: '@pkgjs/parseargs': 0.11.0 - loupe@3.1.1: - dependencies: - get-func-name: 2.0.2 + loupe@3.1.2: {} lru-cache@10.4.3: {} @@ -1236,18 +1229,18 @@ snapshots: to-fast-properties@2.0.0: {} - typescript@5.6.2: {} + typescript@5.6.3: {} undici-types@6.19.8: {} - undici@6.19.8: {} + undici@6.20.0: {} - vite-node@2.1.2(@types/node@22.7.4): + vite-node@2.1.2(@types/node@22.7.5): dependencies: cac: 6.7.14 debug: 4.3.7 pathe: 1.1.2 - vite: 5.4.8(@types/node@22.7.4) + vite: 5.4.8(@types/node@22.7.5) transitivePeerDependencies: - '@types/node' - less @@ -1259,19 +1252,19 @@ snapshots: - supports-color - terser - vite@5.4.8(@types/node@22.7.4): + vite@5.4.8(@types/node@22.7.5): dependencies: esbuild: 0.21.5 postcss: 8.4.47 rollup: 4.24.0 optionalDependencies: - '@types/node': 22.7.4 + '@types/node': 22.7.5 fsevents: 2.3.3 - vitest@2.1.2(@types/node@22.7.4): + vitest@2.1.2(@types/node@22.7.5): dependencies: '@vitest/expect': 2.1.2 - '@vitest/mocker': 2.1.2(@vitest/spy@2.1.2)(vite@5.4.8(@types/node@22.7.4)) + '@vitest/mocker': 2.1.2(@vitest/spy@2.1.2)(vite@5.4.8(@types/node@22.7.5)) '@vitest/pretty-format': 2.1.2 '@vitest/runner': 2.1.2 '@vitest/snapshot': 2.1.2 @@ -1286,11 +1279,11 @@ snapshots: tinyexec: 0.3.0 tinypool: 1.0.1 tinyrainbow: 1.2.0 - vite: 5.4.8(@types/node@22.7.4) - vite-node: 2.1.2(@types/node@22.7.4) + vite: 5.4.8(@types/node@22.7.5) + vite-node: 2.1.2(@types/node@22.7.5) why-is-node-running: 2.3.0 optionalDependencies: - '@types/node': 22.7.4 + '@types/node': 22.7.5 transitivePeerDependencies: - less - lightningcss diff --git a/src/index.ts b/src/index.ts index a03c6f8..146b71a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -48,6 +48,7 @@ export function decrypt(receiverRawSK: string | Uint8Array, msg: Uint8Array): Bu export { ECIES_CONFIG } from "./config"; export { PrivateKey, PublicKey } from "./keys"; +/** @deprecated - use `import utils from "eciesjs/utils"` instead. */ export const utils = { // TODO: review these before 0.5.0 aesEncrypt, diff --git a/src/utils/elliptic.ts b/src/utils/elliptic.ts index 5f5bdd4..9c90f78 100644 --- a/src/utils/elliptic.ts +++ b/src/utils/elliptic.ts @@ -5,8 +5,8 @@ import { secp256k1 } from "@noble/curves/secp256k1"; import { ellipticCurve } from "../config"; import { ETH_PUBLIC_KEY_SIZE, SECRET_KEY_LENGTH } from "../consts"; +import { deriveKey } from "./hash"; import { decodeHex } from "./hex"; -import { deriveKey } from "./symmetric"; export const isValidPrivateKey = (secret: Uint8Array): boolean => // on secp256k1: only key ∈ (0, group order) is valid diff --git a/src/utils/hash.ts b/src/utils/hash.ts new file mode 100644 index 0000000..1c268b2 --- /dev/null +++ b/src/utils/hash.ts @@ -0,0 +1,6 @@ +import { hkdf } from "@noble/hashes/hkdf"; +import { sha256 } from "@noble/hashes/sha256"; + +export const deriveKey = (master: Uint8Array): Uint8Array => + // 32 bytes shared secret for aes256 and xchacha20 derived from HKDF-SHA256 + hkdf(sha256, master, undefined, undefined, 32); diff --git a/src/utils/index.ts b/src/utils/index.ts index 0e5eaab..a831de4 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,4 +1,5 @@ // under this folder no `Buffer` export * from "./elliptic"; +export * from "./hash"; export * from "./hex"; export * from "./symmetric"; diff --git a/src/utils/symmetric.ts b/src/utils/symmetric.ts index ac6a2e3..03b941d 100644 --- a/src/utils/symmetric.ts +++ b/src/utils/symmetric.ts @@ -1,8 +1,6 @@ import { xchacha20poly1305 as xchacha20 } from "@noble/ciphers/chacha"; import { Cipher, concatBytes } from "@noble/ciphers/utils"; import { randomBytes } from "@noble/ciphers/webcrypto"; -import { hkdf } from "@noble/hashes/hkdf"; -import { sha256 } from "@noble/hashes/sha256"; import { symmetricAlgorithm, symmetricNonceLength } from "../config"; import { AEAD_TAG_LENGTH, XCHACHA20_NONCE_LENGTH } from "../consts"; @@ -20,10 +18,6 @@ export const aesEncrypt = symEncrypt; // TODO: delete /** @deprecated - use `symDecrypt` instead. */ export const aesDecrypt = symDecrypt; // TODO: delete -export const deriveKey = (master: Uint8Array): Uint8Array => - // 32 bytes shared secret for aes256 and xchacha20 derived from HKDF-SHA256 - hkdf(sha256, master, undefined, undefined, 32); - function _exec(is_encryption: boolean, key: Uint8Array, data: Uint8Array): Uint8Array { const algorithm = symmetricAlgorithm(); const callback = is_encryption ? _encrypt : _decrypt;