From aadd3f4ba3eaa4f9a6631477048ae8091a3b4435 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Tue, 11 Jul 2023 16:18:44 +0000 Subject: [PATCH 1/5] Rewrite imports: add .js extension, gh-86 --- .gitignore | 1 + package.json | 2 +- src/aes.ts | 2 +- src/blake2b.ts | 2 +- src/keccak.ts | 2 +- src/pbkdf2.ts | 2 +- src/ripemd160.ts | 2 +- src/scrypt.ts | 2 +- src/secp256k1-compat.ts | 4 ++-- src/sha256.ts | 2 +- src/sha512.ts | 2 +- src/utils.ts | 3 ++- test/package.json | 15 +++++++++++++++ test/test-vectors/aes.ts | 4 ++-- test/test-vectors/blake2b.ts | 4 ++-- test/test-vectors/hdkey.ts | 4 ++-- test/test-vectors/keccak.ts | 4 ++-- test/test-vectors/pbkdf2.ts | 4 ++-- test/test-vectors/random.ts | 4 ++-- test/test-vectors/ripemd160.ts | 4 ++-- test/test-vectors/scrypt.ts | 4 ++-- test/test-vectors/secp256k1-compat.ts | 6 +++--- test/test-vectors/secp256k1.ts | 2 +- test/test-vectors/sha256.ts | 4 ++-- test/test-vectors/sha512.ts | 4 ++-- 25 files changed, 53 insertions(+), 36 deletions(-) create mode 100644 test/package.json diff --git a/.gitignore b/.gitignore index 834f50e..30f39e8 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ /node_modules /.parcel-cache /esm +/test/node_modules diff --git a/package.json b/package.json index dcebd68..cb6af34 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ethereum-cryptography", - "version": "2.1.0", + "version": "2.1.1", "description": "All the cryptographic primitives used in Ethereum", "contributors": [ { diff --git a/src/aes.ts b/src/aes.ts index fd8c66e..dbd8e8d 100644 --- a/src/aes.ts +++ b/src/aes.ts @@ -1,5 +1,5 @@ import { crypto as cr } from "@noble/hashes/crypto"; -import { concatBytes, equalsBytes } from "./utils"; +import { concatBytes, equalsBytes } from "./utils.js"; const crypto: any = { web: cr }; diff --git a/src/blake2b.ts b/src/blake2b.ts index 9249138..a872848 100644 --- a/src/blake2b.ts +++ b/src/blake2b.ts @@ -1,5 +1,5 @@ import { blake2b as _blake2b } from "@noble/hashes/blake2b"; -import { assertBytes } from "./utils"; +import { assertBytes } from "./utils.js"; export const blake2b = (msg: Uint8Array, outputLength = 64): Uint8Array => { assertBytes(msg); diff --git a/src/keccak.ts b/src/keccak.ts index 991a70d..d977faf 100644 --- a/src/keccak.ts +++ b/src/keccak.ts @@ -6,7 +6,7 @@ import { keccak_512 } from "@noble/hashes/sha3"; import { Hash } from "@noble/hashes/utils"; -import { wrapHash } from "./utils"; +import { wrapHash } from "./utils.js"; // Expose create only for keccak256 interface K256 { diff --git a/src/pbkdf2.ts b/src/pbkdf2.ts index 5b21e7a..05fc2b9 100644 --- a/src/pbkdf2.ts +++ b/src/pbkdf2.ts @@ -4,7 +4,7 @@ import { } from "@noble/hashes/pbkdf2"; import { sha256 } from "@noble/hashes/sha256"; import { sha512 } from "@noble/hashes/sha512"; -import { assertBytes } from "./utils"; +import { assertBytes } from "./utils.js"; export async function pbkdf2( password: Uint8Array, diff --git a/src/ripemd160.ts b/src/ripemd160.ts index 4f2bfee..798e3cf 100644 --- a/src/ripemd160.ts +++ b/src/ripemd160.ts @@ -1,4 +1,4 @@ import { ripemd160 as _ripemd160 } from "@noble/hashes/ripemd160"; -import { wrapHash } from "./utils"; +import { wrapHash } from "./utils.js"; export const ripemd160 = wrapHash(_ripemd160); diff --git a/src/scrypt.ts b/src/scrypt.ts index b204e2a..489c761 100644 --- a/src/scrypt.ts +++ b/src/scrypt.ts @@ -1,5 +1,5 @@ import { scrypt as _sync, scryptAsync as _async } from "@noble/hashes/scrypt"; -import { assertBytes } from "./utils"; +import { assertBytes } from "./utils.js"; type OnProgressCallback = (progress: number) => void; diff --git a/src/secp256k1-compat.ts b/src/secp256k1-compat.ts index 87a5de8..f62705e 100644 --- a/src/secp256k1-compat.ts +++ b/src/secp256k1-compat.ts @@ -1,7 +1,7 @@ import { sha256 } from "@noble/hashes/sha256"; import { mod } from "@noble/curves/abstract/modular"; -import { secp256k1 } from "./secp256k1"; -import { assertBool, assertBytes, hexToBytes, toHex } from "./utils"; +import { secp256k1 } from "./secp256k1.js"; +import { assertBool, assertBytes, hexToBytes, toHex } from "./utils.js"; // Use `secp256k1` module directly. // This is a legacy compatibility layer for the npm package `secp256k1` via noble-secp256k1 diff --git a/src/sha256.ts b/src/sha256.ts index 8b3490b..941a9a4 100644 --- a/src/sha256.ts +++ b/src/sha256.ts @@ -1,4 +1,4 @@ import { sha256 as _sha256 } from "@noble/hashes/sha256"; -import { wrapHash } from "./utils"; +import { wrapHash } from "./utils.js"; export const sha256 = wrapHash(_sha256); diff --git a/src/sha512.ts b/src/sha512.ts index 6929aee..020ad15 100644 --- a/src/sha512.ts +++ b/src/sha512.ts @@ -1,4 +1,4 @@ import { sha512 as _sha512 } from "@noble/hashes/sha512"; -import { wrapHash } from "./utils"; +import { wrapHash } from "./utils.js"; export const sha512 = wrapHash(_sha512); diff --git a/src/utils.ts b/src/utils.ts index ecd712b..c792592 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,3 @@ -// buf.toString('hex') -> toHex(buf) import assert from "@noble/hashes/_assert"; import { hexToBytes as _hexToBytes } from "@noble/hashes/utils"; const assertBool = assert.bool; @@ -12,6 +11,8 @@ export { utf8ToBytes } from "@noble/hashes/utils"; +// buf.toString('hex') -> toHex(buf) + // Global symbols in both browsers and Node.js since v11 // See https://github.com/microsoft/TypeScript/issues/31535 declare const TextEncoder: any; diff --git a/test/package.json b/test/package.json new file mode 100644 index 0000000..3c751ee --- /dev/null +++ b/test/package.json @@ -0,0 +1,15 @@ +{ + "name": "test", + "version": "1.0.0", + "description": "", + "main": "karma.browserify.conf.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "author": "", + "license": "ISC", + "dependencies": { + "ethereum-cryptography": "file:.." + } +} diff --git a/test/test-vectors/aes.ts b/test/test-vectors/aes.ts index 8aeb202..103c8cd 100644 --- a/test/test-vectors/aes.ts +++ b/test/test-vectors/aes.ts @@ -1,5 +1,5 @@ -import { decrypt, encrypt } from "../../src/aes"; -import { hexToBytes, toHex } from "../../src/utils"; +import { decrypt, encrypt } from "ethereum-cryptography/aes"; +import { hexToBytes, toHex } from "ethereum-cryptography/utils"; import { deepStrictEqual, rejects } from "./assert"; // Test vectors taken from https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf const TEST_VECTORS = [ diff --git a/test/test-vectors/blake2b.ts b/test/test-vectors/blake2b.ts index 3485ed1..303ba0d 100644 --- a/test/test-vectors/blake2b.ts +++ b/test/test-vectors/blake2b.ts @@ -1,5 +1,5 @@ -import { blake2b } from "../../src/blake2b"; -import { hexToBytes, toHex } from "../../src/utils"; +import { blake2b } from "ethereum-cryptography/blake2b"; +import { hexToBytes, toHex } from "ethereum-cryptography/utils"; import { deepStrictEqual, throws } from "./assert"; // Vectors extracted from https://github.com/emilbayes/blake2b/blob/f0a7c7b550133eca5f5fc3b751ccfd2335ce736f/test-vectors.json const TEST_VECTORS = [ diff --git a/test/test-vectors/hdkey.ts b/test/test-vectors/hdkey.ts index 69f742e..48391aa 100644 --- a/test/test-vectors/hdkey.ts +++ b/test/test-vectors/hdkey.ts @@ -1,6 +1,6 @@ import { secp256k1 as secp } from "@noble/curves/secp256k1"; -import { HARDENED_OFFSET, HDKey } from "../../src/hdkey"; -import { hexToBytes, toHex } from "../../src/utils"; +import { HARDENED_OFFSET, HDKey } from "ethereum-cryptography/hdkey"; +import { hexToBytes, toHex } from "ethereum-cryptography/utils"; import { deepStrictEqual, throws } from "./assert"; // https://github.com/cryptocoinjs/hdkey/blob/42637e381bdef0c8f785b14f5b66a80dad969514/test/fixtures/hdkey.json const fixtures = [ diff --git a/test/test-vectors/keccak.ts b/test/test-vectors/keccak.ts index 38b4240..16ca619 100644 --- a/test/test-vectors/keccak.ts +++ b/test/test-vectors/keccak.ts @@ -1,5 +1,5 @@ -import { keccak224, keccak256, keccak384, keccak512 } from "../../src/keccak"; -import { hexToBytes, toHex, utf8ToBytes } from "../../src/utils"; +import { keccak224, keccak256, keccak384, keccak512 } from "ethereum-cryptography/keccak"; +import { hexToBytes, toHex, utf8ToBytes } from "ethereum-cryptography/utils"; import { deepStrictEqual } from "./assert"; export const keccak224Vectors = [ diff --git a/test/test-vectors/pbkdf2.ts b/test/test-vectors/pbkdf2.ts index 9de18bb..2a03b65 100644 --- a/test/test-vectors/pbkdf2.ts +++ b/test/test-vectors/pbkdf2.ts @@ -1,5 +1,5 @@ -import { pbkdf2 as pbkdf2Async, pbkdf2Sync } from "../../src/pbkdf2"; -import { toHex, utf8ToBytes } from "../../src/utils"; +import { pbkdf2 as pbkdf2Async, pbkdf2Sync } from "ethereum-cryptography/pbkdf2"; +import { toHex, utf8ToBytes } from "ethereum-cryptography/utils"; import { deepStrictEqual } from "./assert"; const TEST_VECTORS = [ diff --git a/test/test-vectors/random.ts b/test/test-vectors/random.ts index 49dc582..74f698a 100644 --- a/test/test-vectors/random.ts +++ b/test/test-vectors/random.ts @@ -1,5 +1,5 @@ -import { getRandomBytes, getRandomBytesSync } from "../../src/random"; -import { equalsBytes } from "../../src/utils"; +import { getRandomBytes, getRandomBytesSync } from "ethereum-cryptography/random"; +import { equalsBytes } from "ethereum-cryptography/utils"; import { deepStrictEqual } from "./assert"; describe("Random number generation", () => { diff --git a/test/test-vectors/ripemd160.ts b/test/test-vectors/ripemd160.ts index 8375b99..a4d0c87 100644 --- a/test/test-vectors/ripemd160.ts +++ b/test/test-vectors/ripemd160.ts @@ -1,5 +1,5 @@ -import { ripemd160 } from "../../src/ripemd160"; -import { toHex, utf8ToBytes } from "../../src/utils"; +import { ripemd160 } from "ethereum-cryptography/ripemd160"; +import { toHex, utf8ToBytes } from "ethereum-cryptography/utils"; import { deepStrictEqual } from "./assert"; const TEST_VECTORS = [ diff --git a/test/test-vectors/scrypt.ts b/test/test-vectors/scrypt.ts index 1da86ca..9a8edee 100644 --- a/test/test-vectors/scrypt.ts +++ b/test/test-vectors/scrypt.ts @@ -1,5 +1,5 @@ -import { scrypt as scryptAsync, scryptSync } from "../../src/scrypt"; -import { hexToBytes, toHex } from "../../src/utils"; +import { scrypt as scryptAsync, scryptSync } from "ethereum-cryptography/scrypt"; +import { hexToBytes, toHex } from "ethereum-cryptography/utils"; import { deepStrictEqual } from "./assert"; const TEST_VECTORS = [ diff --git a/test/test-vectors/secp256k1-compat.ts b/test/test-vectors/secp256k1-compat.ts index c0a23f7..09b7ed1 100644 --- a/test/test-vectors/secp256k1-compat.ts +++ b/test/test-vectors/secp256k1-compat.ts @@ -1,6 +1,6 @@ -import * as secp from "../../src/secp256k1-compat"; -import { sha256 } from "../../src/sha256"; -import { concatBytes, hexToBytes, toHex } from "../../src/utils"; +import * as secp from "ethereum-cryptography/secp256k1-compat"; +import { sha256 } from "ethereum-cryptography/sha256"; +import { concatBytes, hexToBytes, toHex } from "ethereum-cryptography/utils"; import { deepStrictEqual, throws } from "./assert"; import { VECTORS } from "./secp256k1_lib_vectors"; diff --git a/test/test-vectors/secp256k1.ts b/test/test-vectors/secp256k1.ts index 4b93a24..bf980f9 100644 --- a/test/test-vectors/secp256k1.ts +++ b/test/test-vectors/secp256k1.ts @@ -1,4 +1,4 @@ -import { secp256k1 } from "../../src/secp256k1"; +import { secp256k1 } from "ethereum-cryptography/secp256k1"; import { deepStrictEqual } from "./assert"; describe("secp256k1", () => { diff --git a/test/test-vectors/sha256.ts b/test/test-vectors/sha256.ts index 05c1bad..79ba1f4 100644 --- a/test/test-vectors/sha256.ts +++ b/test/test-vectors/sha256.ts @@ -1,5 +1,5 @@ -import { sha256 } from "../../src/sha256"; -import { toHex, utf8ToBytes } from "../../src/utils"; +import { sha256 } from "ethereum-cryptography/sha256"; +import { toHex, utf8ToBytes } from "ethereum-cryptography/utils"; import { deepStrictEqual } from "./assert"; const TEST_VECTORS = [ diff --git a/test/test-vectors/sha512.ts b/test/test-vectors/sha512.ts index 0dc2a79..d3835c1 100644 --- a/test/test-vectors/sha512.ts +++ b/test/test-vectors/sha512.ts @@ -1,5 +1,5 @@ -import { sha512 } from "../../src/sha512"; -import { toHex, utf8ToBytes } from "../../src/utils"; +import { sha512 } from "ethereum-cryptography/sha512"; +import { toHex, utf8ToBytes } from "ethereum-cryptography/utils"; import { deepStrictEqual } from "./assert"; const TEST_VECTORS = [ From b92d4db62128cbbccc4d17119e05a7a2475a4ced Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Tue, 11 Jul 2023 16:22:52 +0000 Subject: [PATCH 2/5] Fix build of tests --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index cb6af34..08271e0 100644 --- a/package.json +++ b/package.json @@ -296,7 +296,7 @@ "build": "npm-run-all build:tsc", "build:tsc": "tsc --project tsconfig.prod.json && tsc --project tsconfig.prod.esm.json", "test": "npm-run-all test:node", - "test:node": "mocha", + "test:node": "cd test && npm install && cd .. && mocha", "clean": "rimraf test-builds bip39 '*.js' '*.js.map' '*.d.ts' '*.d.ts.map' 'src/**/*.js'", "lint": "eslint", "lint:fix": "eslint --fix", From c7fca611ad5162367cdc38c34e836f266da95aeb Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Tue, 11 Jul 2023 16:29:54 +0000 Subject: [PATCH 3/5] Temporarily disable browser tests for release --- .github/workflows/nodejs.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 8834200..efbaba4 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -24,5 +24,4 @@ jobs: npm test - name: Run browser tests and lint run: | - npm run browser-tests npm run lint From f198641616b2fe49f4857825a06a5c0af3f3b44e Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Tue, 11 Jul 2023 22:24:02 +0000 Subject: [PATCH 4/5] Compile tests on their own --- package.json | 4 ++-- scripts/build-browser-tests.sh | 14 ++++++++++---- test/karma.browserify.conf.js | 8 ++++---- test/karma.parcel.conf.js | 8 ++++---- test/karma.rollup.conf.js | 8 ++++---- test/karma.webpack.conf.js | 8 ++++---- test/package.json | 10 ++++++++-- test/tsconfig.json | 17 +++++++++++++++++ 8 files changed, 53 insertions(+), 24 deletions(-) create mode 100644 test/tsconfig.json diff --git a/package.json b/package.json index 08271e0..abdb727 100644 --- a/package.json +++ b/package.json @@ -297,7 +297,7 @@ "build:tsc": "tsc --project tsconfig.prod.json && tsc --project tsconfig.prod.esm.json", "test": "npm-run-all test:node", "test:node": "cd test && npm install && cd .. && mocha", - "clean": "rimraf test-builds bip39 '*.js' '*.js.map' '*.d.ts' '*.d.ts.map' 'src/**/*.js'", + "clean": "rimraf test/test-builds bip39 '*.js' '*.js.map' '*.d.ts' '*.d.ts.map' 'src/**/*.js'", "lint": "eslint", "lint:fix": "eslint --fix", "browser-tests": "npm-run-all browser-tests:build browser-tests:test", @@ -361,4 +361,4 @@ "context": "browser" } } -} +} \ No newline at end of file diff --git a/scripts/build-browser-tests.sh b/scripts/build-browser-tests.sh index a42b2c6..cb5190a 100644 --- a/scripts/build-browser-tests.sh +++ b/scripts/build-browser-tests.sh @@ -1,16 +1,22 @@ set -e +cd ./test/ +echo "Install package to tests" +# Cleanup old module build +rm -rf ./node_modules +npm install + echo "Building tests with TypeScript" -npx tsc --project tsconfig.json +npx tsc --project ./tsconfig.json echo "Building tests with Parcel" -npx parcel build --no-cache --no-optimize test-builds/tsc/test/test-vectors/*.js --dist-dir test-builds/parcel --target parcel_tests +npx parcel build --no-cache --no-optimize ./test-builds/tsc/test/test-vectors/*.js --dist-dir ./test-builds/parcel --target parcel_tests echo "Building tests with Browserify" -npx browserify test-builds/tsc/test/test-vectors/*.js > test-builds/browserify-build.js +npx browserify ./test-builds/tsc/test/test-vectors/*.js > ./test-builds/browserify-build.js echo "Building tests with webpack" npx webpack --mode development ./test-builds/tsc/test/test-vectors/*.js --output-path ./test-builds echo "Building tests with Rollup" -rollup -c test/rollup.config.js +rollup -c ./rollup.config.js diff --git a/test/karma.browserify.conf.js b/test/karma.browserify.conf.js index 6d543ab..2ec476a 100644 --- a/test/karma.browserify.conf.js +++ b/test/karma.browserify.conf.js @@ -1,7 +1,7 @@ -module.exports = function(config) { +module.exports = function (config) { config.set({ frameworks: ["mocha"], - files: ["../test-builds/browserify-build.js"], + files: ["./test-builds/browserify-build.js"], colors: true, logLevel: config.LOG_INFO, browsers: ["ChromeHeadless"], @@ -12,7 +12,7 @@ module.exports = function(config) { concurrency: Infinity, reporters: ["mocha"], client: { - captureConsole: true - } + captureConsole: true, + }, }); }; diff --git a/test/karma.parcel.conf.js b/test/karma.parcel.conf.js index 3748fce..a614952 100644 --- a/test/karma.parcel.conf.js +++ b/test/karma.parcel.conf.js @@ -1,7 +1,7 @@ -module.exports = function(config) { +module.exports = function (config) { config.set({ frameworks: ["mocha"], - files: ["../test-builds/parcel/*.js"], + files: ["./test-builds/parcel/*.js"], colors: true, logLevel: config.LOG_INFO, browsers: ["ChromeHeadless"], @@ -12,7 +12,7 @@ module.exports = function(config) { concurrency: Infinity, reporters: ["mocha"], client: { - captureConsole: true - } + captureConsole: true, + }, }); }; diff --git a/test/karma.rollup.conf.js b/test/karma.rollup.conf.js index 1e63456..9c716dd 100644 --- a/test/karma.rollup.conf.js +++ b/test/karma.rollup.conf.js @@ -1,7 +1,7 @@ -module.exports = function(config) { +module.exports = function (config) { config.set({ frameworks: ["mocha"], - files: ["../test-builds/rollup/*.js"], + files: ["./test-builds/rollup/*.js"], colors: true, logLevel: config.LOG_INFO, browsers: ["ChromeHeadless"], @@ -12,7 +12,7 @@ module.exports = function(config) { concurrency: Infinity, reporters: ["mocha"], client: { - captureConsole: true - } + captureConsole: true, + }, }); }; diff --git a/test/karma.webpack.conf.js b/test/karma.webpack.conf.js index 30a5765..3e17fc6 100644 --- a/test/karma.webpack.conf.js +++ b/test/karma.webpack.conf.js @@ -1,7 +1,7 @@ -module.exports = function(config) { +module.exports = function (config) { config.set({ frameworks: ["mocha"], - files: ["../test-builds/main.js"], + files: ["./test-builds/main.js"], colors: true, logLevel: config.LOG_INFO, browsers: ["ChromeHeadless"], @@ -12,7 +12,7 @@ module.exports = function(config) { concurrency: Infinity, reporters: ["mocha"], client: { - captureConsole: true - } + captureConsole: true, + }, }); }; diff --git a/test/package.json b/test/package.json index 3c751ee..2fe985b 100644 --- a/test/package.json +++ b/test/package.json @@ -10,6 +10,12 @@ "author": "", "license": "ISC", "dependencies": { - "ethereum-cryptography": "file:.." + "ethereum-cryptography": "file:..", + "@types/mocha": "9.1.1" + }, + "targets": { + "parcel_tests": { + "context": "browser" + } } -} +} \ No newline at end of file diff --git a/test/tsconfig.json b/test/tsconfig.json new file mode 100644 index 0000000..2fc85b7 --- /dev/null +++ b/test/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "target": "es2020", + "module": "commonjs", + "strict": true, + "downlevelIteration": true, + "rootDirs": [ + "./" + ], + "outDir": "./test-builds/tsc", + "noUnusedLocals": true, + "declaration": true + }, + "include": [ + "./**/*.ts", + ] +} \ No newline at end of file From 7c33ac24e606513323c0931d53fc2fb0b2fd9ea4 Mon Sep 17 00:00:00 2001 From: Paul Miller Date: Tue, 11 Jul 2023 22:26:46 +0000 Subject: [PATCH 5/5] Bring back browser-tests to CI --- .github/workflows/nodejs.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index efbaba4..8834200 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -24,4 +24,5 @@ jobs: npm test - name: Run browser tests and lint run: | + npm run browser-tests npm run lint