From 17a4b39b07a3c75e435c6cf88599ad898b37e38b Mon Sep 17 00:00:00 2001 From: Weiliang Li Date: Mon, 28 Oct 2024 18:09:03 +0900 Subject: [PATCH] Revamp elliptic utils --- .cspell.jsonc | 2 - CHANGELOG.md | 1 + example/browser/package.json | 2 +- example/browser/script.js | 3 +- example/browser/vite.config.ts | 8 +- package.json | 6 +- pnpm-lock.yaml | 291 +++++++++++++++++---------------- src/utils/elliptic.ts | 33 ++-- tests/integration.test.ts | 6 +- tests/utils/hex.test.ts | 2 +- 10 files changed, 192 insertions(+), 162 deletions(-) diff --git a/.cspell.jsonc b/.cspell.jsonc index 1b7b833..14443f5 100644 --- a/.cspell.jsonc +++ b/.cspell.jsonc @@ -7,7 +7,6 @@ "words": [ "aes", "aes-256-gcm", - "axios", "chacha", "Codacy", "Codecov", @@ -20,7 +19,6 @@ "hkdf", "js", "Npm", - "Prv", "secp256k1", "xchacha", "xchacha20" diff --git a/CHANGELOG.md b/CHANGELOG.md index c576ce8..2b10862 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - Revamp encapsulate/decapsulate - Revamp symmetric encryption/decryption +- Revamp elliptic utils ## 0.4.10 diff --git a/example/browser/package.json b/example/browser/package.json index de4ee1c..442ad9c 100644 --- a/example/browser/package.json +++ b/example/browser/package.json @@ -16,7 +16,7 @@ "eciesjs": "file:../.." }, "devDependencies": { - "vite": "6.0.0-beta.4", + "vite": "6.0.0-beta.5", "vite-bundle-visualizer": "^1.2.1" } } diff --git a/example/browser/script.js b/example/browser/script.js index 0f2dc41..465f1b6 100644 --- a/example/browser/script.js +++ b/example/browser/script.js @@ -29,7 +29,8 @@ export function setup(encryptedElement, textElement, decryptedElement) { const _decrypt = () => { encryptedElement.innerHTML = `click me to encrypt`; if (encrypted) { - textElement.innerHTML = `${decoder.decode(decrypt(sk.secret, encrypted))}`; + const decrypted = decoder.decode(decrypt(sk.secret, encrypted)); + textElement.innerHTML = `${decrypted}`; decryptedElement.innerHTML = `decrypted:`; encrypted = undefined; } else { diff --git a/example/browser/vite.config.ts b/example/browser/vite.config.ts index b0c43a4..c3c0b17 100644 --- a/example/browser/vite.config.ts +++ b/example/browser/vite.config.ts @@ -5,8 +5,12 @@ export default defineConfig({ rollupOptions: { output: { manualChunks(id) { - if (id.includes("@noble")) { - return "noble"; + if (id.includes("@noble/curves")) { + return "noble-curves"; + } else if (id.includes("@noble/ciphers")) { + return "noble-ciphers"; + } else if (id.includes("@noble/hashes")) { + return "noble-hashes"; } else if (id.includes("buffer")) { return "buffer"; } diff --git a/package.json b/package.json index 510d3e0..bc5702d 100644 --- a/package.json +++ b/package.json @@ -61,11 +61,11 @@ "@noble/hashes": "^1.5.0" }, "devDependencies": { - "@types/node": "^22.8.1", - "@vitest/coverage-v8": "^2.1.3", + "@types/node": "^22.8.2", + "@vitest/coverage-v8": "^2.1.4", "typescript": "^5.6.3", "undici": "^6.20.1", - "vitest": "^2.1.3" + "vitest": "^2.1.4" }, "packageManager": "pnpm@9.12.2+sha512.22721b3a11f81661ae1ec68ce1a7b879425a1ca5b991c975b074ac220b187ce56c708fe5db69f4c962c989452eee76c82877f4ee80f474cebd61ee13461b6228" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cd075eb..e86ebb2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -22,11 +22,11 @@ importers: version: 1.5.0 devDependencies: '@types/node': - specifier: ^22.8.1 - version: 22.8.1 + specifier: ^22.8.2 + version: 22.8.2 '@vitest/coverage-v8': - specifier: ^2.1.3 - version: 2.1.3(vitest@2.1.3(@types/node@22.8.1)) + specifier: ^2.1.4 + version: 2.1.4(vitest@2.1.4(@types/node@22.8.2)) typescript: specifier: ^5.6.3 version: 5.6.3 @@ -34,8 +34,8 @@ importers: specifier: ^6.20.1 version: 6.20.1 vitest: - specifier: ^2.1.3 - version: 2.1.3(@types/node@22.8.1) + specifier: ^2.1.4 + version: 2.1.4(@types/node@22.8.2) packages: @@ -249,114 +249,118 @@ packages: resolution: {integrity: sha512-+1VkjdD0QBLPodGrJUeqarH8VAIvQODIbwh9XpP5Syisf7YoQgsJKPNFoqqLQlu+VQ/tVSshMR6loPMn8U+dPg==} engines: {node: '>=14'} - '@rollup/rollup-android-arm-eabi@4.24.1': - resolution: {integrity: sha512-j2llrtCTwNu68yp1wybgkTUW8CrR8AZvGZzIO/qwNAetVP3FHidylyz1s0dU2zXG9uqqpoUIhWKmMypGMcdM2Q==} + '@rollup/rollup-android-arm-eabi@4.24.2': + resolution: {integrity: sha512-ufoveNTKDg9t/b7nqI3lwbCG/9IJMhADBNjjz/Jn6LxIZxD7T5L8l2uO/wD99945F1Oo8FvgbbZJRguyk/BdzA==} cpu: [arm] os: [android] - '@rollup/rollup-android-arm64@4.24.1': - resolution: {integrity: sha512-y65R3hM9sJVAXV3qh/dJ5o2OCVzwy6d994qmi+rGw1i1onYY5AoV9dREDYoizaZvc9esEqOs07CyFgPzz4DBqg==} + '@rollup/rollup-android-arm64@4.24.2': + resolution: {integrity: sha512-iZoYCiJz3Uek4NI0J06/ZxUgwAfNzqltK0MptPDO4OR0a88R4h0DSELMsflS6ibMCJ4PnLvq8f7O1d7WexUvIA==} cpu: [arm64] os: [android] - '@rollup/rollup-darwin-arm64@4.24.1': - resolution: {integrity: sha512-K9iOc75U9HpDffjop9qVPwNoBEPXS0Q6RrVSvh13gs38ynurJ2+HuS7NJbsx+fwiDA+eJYfBi7sablI8G2/3oA==} + '@rollup/rollup-darwin-arm64@4.24.2': + resolution: {integrity: sha512-/UhrIxobHYCBfhi5paTkUDQ0w+jckjRZDZ1kcBL132WeHZQ6+S5v9jQPVGLVrLbNUebdIRpIt00lQ+4Z7ys4Rg==} cpu: [arm64] os: [darwin] - '@rollup/rollup-darwin-x64@4.24.1': - resolution: {integrity: sha512-Ufz0fX79W9937euBI4qEdh2xLb0Lzo4GiZ7xxDpueEZxWdPbow6gnTRokSzSgtqRFs1vFgcgm7Ci/KnOo15MIg==} + '@rollup/rollup-darwin-x64@4.24.2': + resolution: {integrity: sha512-1F/jrfhxJtWILusgx63WeTvGTwE4vmsT9+e/z7cZLKU8sBMddwqw3UV5ERfOV+H1FuRK3YREZ46J4Gy0aP3qDA==} cpu: [x64] os: [darwin] - '@rollup/rollup-freebsd-x64@4.24.1': - resolution: {integrity: sha512-IfG1khuwe10V2EBfFIrcd7P6X0stdhHQM71NyaG5TPgy6dXr2nzAa5TMNFA35tr41gihUPqp/w8StayYG7jXYw==} + '@rollup/rollup-freebsd-arm64@4.24.2': + resolution: {integrity: sha512-1YWOpFcGuC6iGAS4EI+o3BV2/6S0H+m9kFOIlyFtp4xIX5rjSnL3AwbTBxROX0c8yWtiWM7ZI6mEPTI7VkSpZw==} + cpu: [arm64] + os: [freebsd] + + '@rollup/rollup-freebsd-x64@4.24.2': + resolution: {integrity: sha512-3qAqTewYrCdnOD9Gl9yvPoAoFAVmPJsBvleabvx4bnu1Kt6DrB2OALeRVag7BdWGWLhP1yooeMLEi6r2nYSOjg==} cpu: [x64] os: [freebsd] - '@rollup/rollup-linux-arm-gnueabihf@4.24.1': - resolution: {integrity: sha512-W+drJRBL1+N1/zaq+8y/CtQ3VP5wxMXwCy7obFl9r5jJ5EFNEYAqchuPfYTleYOoA46bwXAprCL+OVK3BTrWWw==} + '@rollup/rollup-linux-arm-gnueabihf@4.24.2': + resolution: {integrity: sha512-ArdGtPHjLqWkqQuoVQ6a5UC5ebdX8INPuJuJNWRe0RGa/YNhVvxeWmCTFQ7LdmNCSUzVZzxAvUznKaYx645Rig==} cpu: [arm] os: [linux] - '@rollup/rollup-linux-arm-musleabihf@4.24.1': - resolution: {integrity: sha512-mKngr0zxo4FMSDqiq4F4G/1IPqjpNO7MyjAM6+YxDIADO4ZSI4m05bZYD4po12Jid6+n9YJRWdIcvi4JztMVcw==} + '@rollup/rollup-linux-arm-musleabihf@4.24.2': + resolution: {integrity: sha512-B6UHHeNnnih8xH6wRKB0mOcJGvjZTww1FV59HqJoTJ5da9LCG6R4SEBt6uPqzlawv1LoEXSS0d4fBlHNWl6iYw==} cpu: [arm] os: [linux] - '@rollup/rollup-linux-arm64-gnu@4.24.1': - resolution: {integrity: sha512-Rh12WITgvLydYFR9XAjmCRArU71nMfi5lDVLhpRV8dR2sCGtZESVkfD66mi3owp4q1scwysT35nNMPleRTQOow==} + '@rollup/rollup-linux-arm64-gnu@4.24.2': + resolution: {integrity: sha512-kr3gqzczJjSAncwOS6i7fpb4dlqcvLidqrX5hpGBIM1wtt0QEVtf4wFaAwVv8QygFU8iWUMYEoJZWuWxyua4GQ==} cpu: [arm64] os: [linux] - '@rollup/rollup-linux-arm64-musl@4.24.1': - resolution: {integrity: sha512-zOLu7V1iBpJMIrrmZjpmAZ9txFlnGgqQMnjNmRrqmV1vQaou9SIT3qI3JE1kt+DQE8zCdB3n2/mAjIU90AfjEg==} + '@rollup/rollup-linux-arm64-musl@4.24.2': + resolution: {integrity: sha512-TDdHLKCWgPuq9vQcmyLrhg/bgbOvIQ8rtWQK7MRxJ9nvaxKx38NvY7/Lo6cYuEnNHqf6rMqnivOIPIQt6H2AoA==} cpu: [arm64] os: [linux] - '@rollup/rollup-linux-powerpc64le-gnu@4.24.1': - resolution: {integrity: sha512-h9ipTGhMzTBAJL/bg1HsElhGPWLGeCKE8JkxgvrJ5O/S1MXH9RxMUTl++tzlpzxdOBCAGqygZIMBj3wIDf/kJw==} + '@rollup/rollup-linux-powerpc64le-gnu@4.24.2': + resolution: {integrity: sha512-xv9vS648T3X4AxFFZGWeB5Dou8ilsv4VVqJ0+loOIgDO20zIhYfDLkk5xoQiej2RiSQkld9ijF/fhLeonrz2mw==} cpu: [ppc64] os: [linux] - '@rollup/rollup-linux-riscv64-gnu@4.24.1': - resolution: {integrity: sha512-PNKCMA1xRBARR7/j6KXMSB1z0/eGenC/t2wdQl5et3jnrHA+igIaLVNUEPfnVjmZIZJign7u/dobvV2VkPxMiw==} + '@rollup/rollup-linux-riscv64-gnu@4.24.2': + resolution: {integrity: sha512-tbtXwnofRoTt223WUZYiUnbxhGAOVul/3StZ947U4A5NNjnQJV5irKMm76G0LGItWs6y+SCjUn/Q0WaMLkEskg==} cpu: [riscv64] os: [linux] - '@rollup/rollup-linux-s390x-gnu@4.24.1': - resolution: {integrity: sha512-mkl3uWq/ix18gAfzBUIecSwioPyJkbR6QXVaNuOGM7Qbs7f1EfDLP4XtLSJx4GL6mO8GrKhB3cmhUc3zjUrQSg==} + '@rollup/rollup-linux-s390x-gnu@4.24.2': + resolution: {integrity: sha512-gc97UebApwdsSNT3q79glOSPdfwgwj5ELuiyuiMY3pEWMxeVqLGKfpDFoum4ujivzxn6veUPzkGuSYoh5deQ2Q==} cpu: [s390x] os: [linux] - '@rollup/rollup-linux-x64-gnu@4.24.1': - resolution: {integrity: sha512-j0RPQWteEXAAxRQI+IcX3i7WQb7hFe7CW94H3l0edBVyJMIPOlr/hqc5CGG1FBDW9gNr0ZC2IzwSta1iSNJIoA==} + '@rollup/rollup-linux-x64-gnu@4.24.2': + resolution: {integrity: sha512-jOG/0nXb3z+EM6SioY8RofqqmZ+9NKYvJ6QQaa9Mvd3RQxlH68/jcB/lpyVt4lCiqr04IyaC34NzhUqcXbB5FQ==} cpu: [x64] os: [linux] - '@rollup/rollup-linux-x64-musl@4.24.1': - resolution: {integrity: sha512-UrwXowd3gyT+/ijoeSzMyHHGUaV3WhiJL77eTZE8/Pq+9K6auacIJ264biAUhHJ3FjAHsXNhzEmxGnj4tpDz2g==} + '@rollup/rollup-linux-x64-musl@4.24.2': + resolution: {integrity: sha512-XAo7cJec80NWx9LlZFEJQxqKOMz/lX3geWs2iNT5CHIERLFfd90f3RYLLjiCBm1IMaQ4VOX/lTC9lWfzzQm14Q==} cpu: [x64] os: [linux] - '@rollup/rollup-win32-arm64-msvc@4.24.1': - resolution: {integrity: sha512-wexHPBkBa2/tPhbGcxLqOM2AFZ7BQsZ0pk3dVxRL5Ec0SsXnkpcMucZ4j4woyoD5DbRdFP6Roptd9TRsGVTvUA==} + '@rollup/rollup-win32-arm64-msvc@4.24.2': + resolution: {integrity: sha512-A+JAs4+EhsTjnPQvo9XY/DC0ztaws3vfqzrMNMKlwQXuniBKOIIvAAI8M0fBYiTCxQnElYu7mLk7JrhlQ+HeOw==} cpu: [arm64] os: [win32] - '@rollup/rollup-win32-ia32-msvc@4.24.1': - resolution: {integrity: sha512-IW2axCCdiC+kgj5/50Mt5v8qG0LYaDichBGKXM4Oo2NaWStAs0oQp1dqVzCV1XOXNvNNDRFw0EaT+JMs6BX+WQ==} + '@rollup/rollup-win32-ia32-msvc@4.24.2': + resolution: {integrity: sha512-ZhcrakbqA1SCiJRMKSU64AZcYzlZ/9M5LaYil9QWxx9vLnkQ9Vnkve17Qn4SjlipqIIBFKjBES6Zxhnvh0EAEw==} cpu: [ia32] os: [win32] - '@rollup/rollup-win32-x64-msvc@4.24.1': - resolution: {integrity: sha512-b9IK2buRXwm7owl4Hd8fselCQ7/gr2WaErv0e/IPgRQuJfFS+O0cFJA4t13+FKAZeQh97iEyBG06g613IJLirQ==} + '@rollup/rollup-win32-x64-msvc@4.24.2': + resolution: {integrity: sha512-2mLH46K1u3r6uwc95hU+OR9q/ggYMpnS7pSp83Ece1HUQgF9Nh/QwTK5rcgbFnV9j+08yBrU5sA/P0RK2MSBNA==} cpu: [x64] os: [win32] '@types/estree@1.0.6': resolution: {integrity: sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==} - '@types/node@22.8.1': - resolution: {integrity: sha512-k6Gi8Yyo8EtrNtkHXutUu2corfDf9su95VYVP10aGYMMROM6SAItZi0w1XszA6RtWTHSVp5OeFof37w0IEqCQg==} + '@types/node@22.8.2': + resolution: {integrity: sha512-NzaRNFV+FZkvK/KLCsNdTvID0SThyrs5SHB6tsD/lajr22FGC73N2QeDPM2wHtVde8mgcXuSsHQkH5cX1pbPLw==} - '@vitest/coverage-v8@2.1.3': - resolution: {integrity: sha512-2OJ3c7UPoFSmBZwqD2VEkUw6A/tzPF0LmW0ZZhhB8PFxuc+9IBG/FaSM+RLEenc7ljzFvGN+G0nGQoZnh7sy2A==} + '@vitest/coverage-v8@2.1.4': + resolution: {integrity: sha512-FPKQuJfR6VTfcNMcGpqInmtJuVXFSCd9HQltYncfR01AzXhLucMEtQ5SinPdZxsT5x/5BK7I5qFJ5/ApGCmyTQ==} peerDependencies: - '@vitest/browser': 2.1.3 - vitest: 2.1.3 + '@vitest/browser': 2.1.4 + vitest: 2.1.4 peerDependenciesMeta: '@vitest/browser': optional: true - '@vitest/expect@2.1.3': - resolution: {integrity: sha512-SNBoPubeCJhZ48agjXruCI57DvxcsivVDdWz+SSsmjTT4QN/DfHk3zB/xKsJqMs26bLZ/pNRLnCf0j679i0uWQ==} + '@vitest/expect@2.1.4': + resolution: {integrity: sha512-DOETT0Oh1avie/D/o2sgMHGrzYUFFo3zqESB2Hn70z6QB1HrS2IQ9z5DfyTqU8sg4Bpu13zZe9V4+UTNQlUeQA==} - '@vitest/mocker@2.1.3': - resolution: {integrity: sha512-eSpdY/eJDuOvuTA3ASzCjdithHa+GIF1L4PqtEELl6Qa3XafdMLBpBlZCIUCX2J+Q6sNmjmxtosAG62fK4BlqQ==} + '@vitest/mocker@2.1.4': + resolution: {integrity: sha512-Ky/O1Lc0QBbutJdW0rqLeFNbuLEyS+mIPiNdlVlp2/yhJ0SbyYqObS5IHdhferJud8MbbwMnexg4jordE5cCoQ==} peerDependencies: - '@vitest/spy': 2.1.3 - msw: ^2.3.5 + msw: ^2.4.9 vite: ^5.0.0 peerDependenciesMeta: msw: @@ -364,20 +368,20 @@ packages: vite: optional: true - '@vitest/pretty-format@2.1.3': - resolution: {integrity: sha512-XH1XdtoLZCpqV59KRbPrIhFCOO0hErxrQCMcvnQete3Vibb9UeIOX02uFPfVn3Z9ZXsq78etlfyhnkmIZSzIwQ==} + '@vitest/pretty-format@2.1.4': + resolution: {integrity: sha512-L95zIAkEuTDbUX1IsjRl+vyBSLh3PwLLgKpghl37aCK9Jvw0iP+wKwIFhfjdUtA2myLgjrG6VU6JCFLv8q/3Ww==} - '@vitest/runner@2.1.3': - resolution: {integrity: sha512-JGzpWqmFJ4fq5ZKHtVO3Xuy1iF2rHGV4d/pdzgkYHm1+gOzNZtqjvyiaDGJytRyMU54qkxpNzCx+PErzJ1/JqQ==} + '@vitest/runner@2.1.4': + resolution: {integrity: sha512-sKRautINI9XICAMl2bjxQM8VfCMTB0EbsBc/EDFA57V6UQevEKY/TOPOF5nzcvCALltiLfXWbq4MaAwWx/YxIA==} - '@vitest/snapshot@2.1.3': - resolution: {integrity: sha512-qWC2mWc7VAXmjAkEKxrScWHWFyCQx/cmiZtuGqMi+WwqQJ2iURsVY4ZfAK6dVo6K2smKRU6l3BPwqEBvhnpQGg==} + '@vitest/snapshot@2.1.4': + resolution: {integrity: sha512-3Kab14fn/5QZRog5BPj6Rs8dc4B+mim27XaKWFWHWA87R56AKjHTGcBFKpvZKDzC4u5Wd0w/qKsUIio3KzWW4Q==} - '@vitest/spy@2.1.3': - resolution: {integrity: sha512-Nb2UzbcUswzeSP7JksMDaqsI43Sj5+Kry6ry6jQJT4b5gAK+NS9NED6mDb8FlMRCX8m5guaHCDZmqYMMWRy5nQ==} + '@vitest/spy@2.1.4': + resolution: {integrity: sha512-4JOxa+UAizJgpZfaCPKK2smq9d8mmjZVPMt2kOsg/R8QkoRzydHH1qHxIYNvr1zlEaFj4SXiaaJWxq/LPLKaLg==} - '@vitest/utils@2.1.3': - resolution: {integrity: sha512-xpiVfDSg1RrYT0tX6czgerkpcKFmFOF/gCr30+Mve5V2kewCy4Prn1/NDMSRwaSmT7PRaOF83wu+bEtsY1wrvA==} + '@vitest/utils@2.1.4': + resolution: {integrity: sha512-MXDnZn0Awl2S86PSNIim5PWXgIAx8CIkzu35mBdSApUip6RFOGXBCf3YFyeEu8n1IHk4bWD46DeYFu9mQlFIRg==} ansi-regex@5.0.1: resolution: {integrity: sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==} @@ -458,6 +462,10 @@ packages: estree-walker@3.0.3: resolution: {integrity: sha512-7RUKfXgSMMkzt6ZuXmqapOurLGPPfgj6l9uRZ7lRGolvk0y2yocc35LdcxKC5PQZdn2DMqioAQ2NoWcrTKmm6g==} + expect-type@1.1.0: + resolution: {integrity: sha512-bFi65yM+xZgk+u/KRIpekdSYkTB5W1pEf0Lt8Q8Msh7b+eQ7LXVtIB1Bkm4fvclDEL1b2CZkMhv2mOeF8tMdkA==} + engines: {node: '>=12.0.0'} + foreground-child@3.3.0: resolution: {integrity: sha512-Ld2g8rrAyMYFXBhEqMz8ZAHBi4J4uS1i/CxGMDnjyFWddMXLVcDp051DZfu+t7+ab7Wv6SMqpWmyFIj5UbfFvg==} engines: {node: '>=14'} @@ -561,8 +569,8 @@ packages: resolution: {integrity: sha512-56rxCq7G/XfB4EkXq9Egn5GCqugWvDFjafDOThIdMBsI15iqPqR5r15TfSr1YPYeEI19YeaXMCbY6u88Y76GLQ==} engines: {node: ^10 || ^12 || >=14} - rollup@4.24.1: - resolution: {integrity: sha512-2lhtdsnyxlfBAZVh9tfriEc1nV9HxjQGnqEpd7z7cWXuLbI4jHWDhAvw6JGs0AVcnYqv0gL7Mjuj/utxW2wPBw==} + rollup@4.24.2: + resolution: {integrity: sha512-do/DFGq5g6rdDhdpPq5qb2ecoczeK6y+2UAjdJ5trjQJj5f1AiVdLRWRc9A9/fFukfvJRgM0UXzxBIYMovm5ww==} engines: {node: '>=18.0.0', npm: '>=8.0.0'} hasBin: true @@ -650,8 +658,8 @@ packages: resolution: {integrity: sha512-AjQF1QsmqfJys+LXfGTNum+qw4S88CojRInG/6t31W/1fk6G59s92bnAvGz5Cmur+kQv2SURXEvvudLmbrE8QA==} engines: {node: '>=18.17'} - vite-node@2.1.3: - resolution: {integrity: sha512-I1JadzO+xYX887S39Do+paRePCKoiDrWRRjp9kkG5he0t7RXNvPAJPCQSJqbGN4uCrFFeS3Kj3sLqY8NMYBEdA==} + vite-node@2.1.4: + resolution: {integrity: sha512-kqa9v+oi4HwkG6g8ufRnb5AeplcRw8jUF6/7/Qz1qRQOXHImG8YnLbB+LLszENwFnoBl9xIf9nVdCFzNd7GQEg==} engines: {node: ^18.0.0 || >=20.0.0} hasBin: true @@ -686,15 +694,15 @@ packages: terser: optional: true - vitest@2.1.3: - resolution: {integrity: sha512-Zrxbg/WiIvUP2uEzelDNTXmEMJXuzJ1kCpbDvaKByFA9MNeO95V+7r/3ti0qzJzrxdyuUw5VduN7k+D3VmVOSA==} + vitest@2.1.4: + resolution: {integrity: sha512-eDjxbVAJw1UJJCHr5xr/xM86Zx+YxIEXGAR+bmnEID7z9qWfoxpHw0zdobz+TQAFOLT+nEXz3+gx6nUJ7RgmlQ==} engines: {node: ^18.0.0 || >=20.0.0} hasBin: true peerDependencies: '@edge-runtime/vm': '*' '@types/node': ^18.0.0 || >=20.0.0 - '@vitest/browser': 2.1.3 - '@vitest/ui': 2.1.3 + '@vitest/browser': 2.1.4 + '@vitest/ui': 2.1.4 happy-dom: '*' jsdom: '*' peerDependenciesMeta: @@ -863,64 +871,67 @@ snapshots: '@pkgjs/parseargs@0.11.0': optional: true - '@rollup/rollup-android-arm-eabi@4.24.1': + '@rollup/rollup-android-arm-eabi@4.24.2': optional: true - '@rollup/rollup-android-arm64@4.24.1': + '@rollup/rollup-android-arm64@4.24.2': optional: true - '@rollup/rollup-darwin-arm64@4.24.1': + '@rollup/rollup-darwin-arm64@4.24.2': optional: true - '@rollup/rollup-darwin-x64@4.24.1': + '@rollup/rollup-darwin-x64@4.24.2': optional: true - '@rollup/rollup-freebsd-x64@4.24.1': + '@rollup/rollup-freebsd-arm64@4.24.2': optional: true - '@rollup/rollup-linux-arm-gnueabihf@4.24.1': + '@rollup/rollup-freebsd-x64@4.24.2': optional: true - '@rollup/rollup-linux-arm-musleabihf@4.24.1': + '@rollup/rollup-linux-arm-gnueabihf@4.24.2': optional: true - '@rollup/rollup-linux-arm64-gnu@4.24.1': + '@rollup/rollup-linux-arm-musleabihf@4.24.2': optional: true - '@rollup/rollup-linux-arm64-musl@4.24.1': + '@rollup/rollup-linux-arm64-gnu@4.24.2': optional: true - '@rollup/rollup-linux-powerpc64le-gnu@4.24.1': + '@rollup/rollup-linux-arm64-musl@4.24.2': optional: true - '@rollup/rollup-linux-riscv64-gnu@4.24.1': + '@rollup/rollup-linux-powerpc64le-gnu@4.24.2': optional: true - '@rollup/rollup-linux-s390x-gnu@4.24.1': + '@rollup/rollup-linux-riscv64-gnu@4.24.2': optional: true - '@rollup/rollup-linux-x64-gnu@4.24.1': + '@rollup/rollup-linux-s390x-gnu@4.24.2': optional: true - '@rollup/rollup-linux-x64-musl@4.24.1': + '@rollup/rollup-linux-x64-gnu@4.24.2': optional: true - '@rollup/rollup-win32-arm64-msvc@4.24.1': + '@rollup/rollup-linux-x64-musl@4.24.2': optional: true - '@rollup/rollup-win32-ia32-msvc@4.24.1': + '@rollup/rollup-win32-arm64-msvc@4.24.2': optional: true - '@rollup/rollup-win32-x64-msvc@4.24.1': + '@rollup/rollup-win32-ia32-msvc@4.24.2': + optional: true + + '@rollup/rollup-win32-x64-msvc@4.24.2': optional: true '@types/estree@1.0.6': {} - '@types/node@22.8.1': + '@types/node@22.8.2': dependencies: undici-types: 6.19.8 - '@vitest/coverage-v8@2.1.3(vitest@2.1.3(@types/node@22.8.1))': + '@vitest/coverage-v8@2.1.4(vitest@2.1.4(@types/node@22.8.2))': dependencies: '@ampproject/remapping': 2.3.0 '@bcoe/v8-coverage': 0.2.3 @@ -934,47 +945,47 @@ snapshots: std-env: 3.7.0 test-exclude: 7.0.1 tinyrainbow: 1.2.0 - vitest: 2.1.3(@types/node@22.8.1) + vitest: 2.1.4(@types/node@22.8.2) transitivePeerDependencies: - supports-color - '@vitest/expect@2.1.3': + '@vitest/expect@2.1.4': dependencies: - '@vitest/spy': 2.1.3 - '@vitest/utils': 2.1.3 + '@vitest/spy': 2.1.4 + '@vitest/utils': 2.1.4 chai: 5.1.2 tinyrainbow: 1.2.0 - '@vitest/mocker@2.1.3(@vitest/spy@2.1.3)(vite@5.4.10(@types/node@22.8.1))': + '@vitest/mocker@2.1.4(vite@5.4.10(@types/node@22.8.2))': dependencies: - '@vitest/spy': 2.1.3 + '@vitest/spy': 2.1.4 estree-walker: 3.0.3 magic-string: 0.30.12 optionalDependencies: - vite: 5.4.10(@types/node@22.8.1) + vite: 5.4.10(@types/node@22.8.2) - '@vitest/pretty-format@2.1.3': + '@vitest/pretty-format@2.1.4': dependencies: tinyrainbow: 1.2.0 - '@vitest/runner@2.1.3': + '@vitest/runner@2.1.4': dependencies: - '@vitest/utils': 2.1.3 + '@vitest/utils': 2.1.4 pathe: 1.1.2 - '@vitest/snapshot@2.1.3': + '@vitest/snapshot@2.1.4': dependencies: - '@vitest/pretty-format': 2.1.3 + '@vitest/pretty-format': 2.1.4 magic-string: 0.30.12 pathe: 1.1.2 - '@vitest/spy@2.1.3': + '@vitest/spy@2.1.4': dependencies: tinyspy: 3.0.2 - '@vitest/utils@2.1.3': + '@vitest/utils@2.1.4': dependencies: - '@vitest/pretty-format': 2.1.3 + '@vitest/pretty-format': 2.1.4 loupe: 3.1.2 tinyrainbow: 1.2.0 @@ -1062,6 +1073,8 @@ snapshots: dependencies: '@types/estree': 1.0.6 + expect-type@1.1.0: {} + foreground-child@3.3.0: dependencies: cross-spawn: 7.0.3 @@ -1163,27 +1176,28 @@ snapshots: picocolors: 1.1.1 source-map-js: 1.2.1 - rollup@4.24.1: + rollup@4.24.2: dependencies: '@types/estree': 1.0.6 optionalDependencies: - '@rollup/rollup-android-arm-eabi': 4.24.1 - '@rollup/rollup-android-arm64': 4.24.1 - '@rollup/rollup-darwin-arm64': 4.24.1 - '@rollup/rollup-darwin-x64': 4.24.1 - '@rollup/rollup-freebsd-x64': 4.24.1 - '@rollup/rollup-linux-arm-gnueabihf': 4.24.1 - '@rollup/rollup-linux-arm-musleabihf': 4.24.1 - '@rollup/rollup-linux-arm64-gnu': 4.24.1 - '@rollup/rollup-linux-arm64-musl': 4.24.1 - '@rollup/rollup-linux-powerpc64le-gnu': 4.24.1 - '@rollup/rollup-linux-riscv64-gnu': 4.24.1 - '@rollup/rollup-linux-s390x-gnu': 4.24.1 - '@rollup/rollup-linux-x64-gnu': 4.24.1 - '@rollup/rollup-linux-x64-musl': 4.24.1 - '@rollup/rollup-win32-arm64-msvc': 4.24.1 - '@rollup/rollup-win32-ia32-msvc': 4.24.1 - '@rollup/rollup-win32-x64-msvc': 4.24.1 + '@rollup/rollup-android-arm-eabi': 4.24.2 + '@rollup/rollup-android-arm64': 4.24.2 + '@rollup/rollup-darwin-arm64': 4.24.2 + '@rollup/rollup-darwin-x64': 4.24.2 + '@rollup/rollup-freebsd-arm64': 4.24.2 + '@rollup/rollup-freebsd-x64': 4.24.2 + '@rollup/rollup-linux-arm-gnueabihf': 4.24.2 + '@rollup/rollup-linux-arm-musleabihf': 4.24.2 + '@rollup/rollup-linux-arm64-gnu': 4.24.2 + '@rollup/rollup-linux-arm64-musl': 4.24.2 + '@rollup/rollup-linux-powerpc64le-gnu': 4.24.2 + '@rollup/rollup-linux-riscv64-gnu': 4.24.2 + '@rollup/rollup-linux-s390x-gnu': 4.24.2 + '@rollup/rollup-linux-x64-gnu': 4.24.2 + '@rollup/rollup-linux-x64-musl': 4.24.2 + '@rollup/rollup-win32-arm64-msvc': 4.24.2 + '@rollup/rollup-win32-ia32-msvc': 4.24.2 + '@rollup/rollup-win32-x64-msvc': 4.24.2 fsevents: 2.3.3 semver@7.6.3: {} @@ -1250,12 +1264,12 @@ snapshots: undici@6.20.1: {} - vite-node@2.1.3(@types/node@22.8.1): + vite-node@2.1.4(@types/node@22.8.2): dependencies: cac: 6.7.14 debug: 4.3.7 pathe: 1.1.2 - vite: 5.4.10(@types/node@22.8.1) + vite: 5.4.10(@types/node@22.8.2) transitivePeerDependencies: - '@types/node' - less @@ -1267,26 +1281,27 @@ snapshots: - supports-color - terser - vite@5.4.10(@types/node@22.8.1): + vite@5.4.10(@types/node@22.8.2): dependencies: esbuild: 0.21.5 postcss: 8.4.47 - rollup: 4.24.1 + rollup: 4.24.2 optionalDependencies: - '@types/node': 22.8.1 + '@types/node': 22.8.2 fsevents: 2.3.3 - vitest@2.1.3(@types/node@22.8.1): + vitest@2.1.4(@types/node@22.8.2): dependencies: - '@vitest/expect': 2.1.3 - '@vitest/mocker': 2.1.3(@vitest/spy@2.1.3)(vite@5.4.10(@types/node@22.8.1)) - '@vitest/pretty-format': 2.1.3 - '@vitest/runner': 2.1.3 - '@vitest/snapshot': 2.1.3 - '@vitest/spy': 2.1.3 - '@vitest/utils': 2.1.3 + '@vitest/expect': 2.1.4 + '@vitest/mocker': 2.1.4(vite@5.4.10(@types/node@22.8.2)) + '@vitest/pretty-format': 2.1.4 + '@vitest/runner': 2.1.4 + '@vitest/snapshot': 2.1.4 + '@vitest/spy': 2.1.4 + '@vitest/utils': 2.1.4 chai: 5.1.2 debug: 4.3.7 + expect-type: 1.1.0 magic-string: 0.30.12 pathe: 1.1.2 std-env: 3.7.0 @@ -1294,11 +1309,11 @@ snapshots: tinyexec: 0.3.1 tinypool: 1.0.1 tinyrainbow: 1.2.0 - vite: 5.4.10(@types/node@22.8.1) - vite-node: 2.1.3(@types/node@22.8.1) + vite: 5.4.10(@types/node@22.8.2) + vite-node: 2.1.4(@types/node@22.8.2) why-is-node-running: 2.3.0 optionalDependencies: - '@types/node': 22.8.1 + '@types/node': 22.8.2 transitivePeerDependencies: - less - lightningcss diff --git a/src/utils/elliptic.ts b/src/utils/elliptic.ts index c490d92..781f33b 100644 --- a/src/utils/elliptic.ts +++ b/src/utils/elliptic.ts @@ -2,7 +2,7 @@ import { randomBytes } from "@noble/ciphers/webcrypto"; import { ed25519, x25519 } from "@noble/curves/ed25519"; import { secp256k1 } from "@noble/curves/secp256k1"; -import { ellipticCurve } from "../config"; +import { ellipticCurve, EllipticCurve } from "../config"; import { ETH_PUBLIC_KEY_SIZE, SECRET_KEY_LENGTH } from "../consts"; import { decodeHex } from "./hex"; @@ -18,6 +18,7 @@ export const isValidPrivateKey = (secret: Uint8Array): boolean => // on secp256k1: only key ∈ (0, group order) is valid // on curve25519: any 32-byte key is valid _exec( + ellipticCurve(), (curve) => curve.utils.isValidPrivateKey(secret), () => true, () => true @@ -25,6 +26,7 @@ export const isValidPrivateKey = (secret: Uint8Array): boolean => export const getPublicKey = (secret: Uint8Array): Uint8Array => _exec( + ellipticCurve(), (curve) => curve.getPublicKey(secret), (curve) => curve.getPublicKey(secret), (curve) => curve.getPublicKey(secret) @@ -36,19 +38,16 @@ export const getSharedPoint = ( compressed?: boolean ): Uint8Array => _exec( + ellipticCurve(), (curve) => curve.getSharedSecret(sk, pk, compressed), (curve) => curve.getSharedSecret(sk, pk), - (curve) => { - // Note: scalar is hashed from sk - const { scalar } = curve.utils.getExtendedPublicKey(sk); - const point = curve.ExtendedPoint.fromHex(pk).multiply(scalar); - return point.toRawBytes(); - } + (curve) => getSharedPointOnEd25519(curve, sk, pk) ); export const convertPublicKeyFormat = (pk: Uint8Array, compressed: boolean): Uint8Array => // only for secp256k1 _exec( + ellipticCurve(), (curve) => curve.getSharedSecret(BigInt(1), pk, compressed), () => pk, () => pk @@ -57,6 +56,7 @@ export const convertPublicKeyFormat = (pk: Uint8Array, compressed: boolean): Uin export const hexToPublicKey = (hex: string): Uint8Array => { const decoded = decodeHex(hex); return _exec( + ellipticCurve(), () => compatEthPublicKey(decoded), () => decoded, () => decoded @@ -64,11 +64,11 @@ export const hexToPublicKey = (hex: string): Uint8Array => { }; function _exec( - secp256k1Callback: (curve: typeof secp256k1) => T, - x25519Callback: (curve: typeof x25519) => T, - ed25519Callback: (curve: typeof ed25519) => T + curve: EllipticCurve, + secp256k1Callback: (curveFn: typeof secp256k1) => T, + x25519Callback: (curveFn: typeof x25519) => T, + ed25519Callback: (curveFn: typeof ed25519) => T ): T { - const curve = ellipticCurve(); if (curve === "secp256k1") { return secp256k1Callback(secp256k1); } else if (curve === "x25519") { @@ -89,3 +89,14 @@ const compatEthPublicKey = (pk: Uint8Array): Uint8Array => { } return pk; }; + +const getSharedPointOnEd25519 = ( + curve: typeof ed25519, + sk: Uint8Array, + pk: Uint8Array +): Uint8Array => { + // Note: scalar is hashed from sk + const { scalar } = curve.utils.getExtendedPublicKey(sk); + const point = curve.ExtendedPoint.fromHex(pk).multiply(scalar); + return point.toRawBytes(); +}; diff --git a/tests/integration.test.ts b/tests/integration.test.ts index a4297bc..ec387f7 100644 --- a/tests/integration.test.ts +++ b/tests/integration.test.ts @@ -1,6 +1,6 @@ import { describe, expect, it } from "vitest"; -import { bytesToHex } from "@noble/ciphers/utils"; +import { bytesToHex, bytesToUtf8 } from "@noble/ciphers/utils"; import { fetch, ProxyAgent, RequestInit } from "undici"; import { decrypt, encrypt, PrivateKey } from "../src"; @@ -17,8 +17,8 @@ describe("test encrypt and decrypt against python version", () => { data: TEXT, pub: sk.publicKey.toHex(), }); - const decrypted = decrypt(sk.toHex(), decodeHex(await res.text())); - expect(decrypted.toString()).toStrictEqual(TEXT); + const decrypted = bytesToUtf8(decrypt(sk.toHex(), decodeHex(await res.text()))); + expect(decrypted).toStrictEqual(TEXT); }); it("tests decrypt", async () => { diff --git a/tests/utils/hex.test.ts b/tests/utils/hex.test.ts index 252ee60..273530f 100644 --- a/tests/utils/hex.test.ts +++ b/tests/utils/hex.test.ts @@ -10,7 +10,7 @@ describe("test hex", () => { expect(remove0x("0X0022")).toBe("0022"); }); - it("converts hex to buffer", () => { + it("converts hex string to Uint8Array", () => { expect(decodeHex("0x0011")).toStrictEqual(Uint8Array.from([0, 0x11])); expect(decodeHex("0X0022")).toStrictEqual(Uint8Array.from([0, 0x22])); });