Skip to content

Rewrite imports: add .js extension, gh-86 #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/node_modules
/.parcel-cache
/esm
/test/node_modules
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ethereum-cryptography",
"version": "2.1.0",
"version": "2.1.1",
"description": "All the cryptographic primitives used in Ethereum",
"contributors": [
{
Expand Down Expand Up @@ -296,8 +296,8 @@
"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",
"clean": "rimraf test-builds bip39 '*.js' '*.js.map' '*.d.ts' '*.d.ts.map' 'src/**/*.js'",
"test:node": "cd test && npm install && cd .. && mocha",
"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",
Expand Down Expand Up @@ -361,4 +361,4 @@
"context": "browser"
}
}
}
}
14 changes: 10 additions & 4 deletions scripts/build-browser-tests.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/aes.ts
Original file line number Diff line number Diff line change
@@ -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 };

Expand Down
2 changes: 1 addition & 1 deletion src/blake2b.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/keccak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/pbkdf2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/ripemd160.ts
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion src/scrypt.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/secp256k1-compat.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/sha256.ts
Original file line number Diff line number Diff line change
@@ -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);
2 changes: 1 addition & 1 deletion src/sha512.ts
Original file line number Diff line number Diff line change
@@ -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);
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions test/karma.browserify.conf.js
Original file line number Diff line number Diff line change
@@ -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"],
Expand All @@ -12,7 +12,7 @@ module.exports = function(config) {
concurrency: Infinity,
reporters: ["mocha"],
client: {
captureConsole: true
}
captureConsole: true,
},
});
};
8 changes: 4 additions & 4 deletions test/karma.parcel.conf.js
Original file line number Diff line number Diff line change
@@ -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"],
Expand All @@ -12,7 +12,7 @@ module.exports = function(config) {
concurrency: Infinity,
reporters: ["mocha"],
client: {
captureConsole: true
}
captureConsole: true,
},
});
};
8 changes: 4 additions & 4 deletions test/karma.rollup.conf.js
Original file line number Diff line number Diff line change
@@ -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"],
Expand All @@ -12,7 +12,7 @@ module.exports = function(config) {
concurrency: Infinity,
reporters: ["mocha"],
client: {
captureConsole: true
}
captureConsole: true,
},
});
};
8 changes: 4 additions & 4 deletions test/karma.webpack.conf.js
Original file line number Diff line number Diff line change
@@ -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"],
Expand All @@ -12,7 +12,7 @@ module.exports = function(config) {
concurrency: Infinity,
reporters: ["mocha"],
client: {
captureConsole: true
}
captureConsole: true,
},
});
};
21 changes: 21 additions & 0 deletions test/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"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:..",
"@types/mocha": "9.1.1"
},
"targets": {
"parcel_tests": {
"context": "browser"
}
}
}
4 changes: 2 additions & 2 deletions test/test-vectors/aes.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions test/test-vectors/blake2b.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions test/test-vectors/hdkey.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions test/test-vectors/keccak.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions test/test-vectors/pbkdf2.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions test/test-vectors/random.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
4 changes: 2 additions & 2 deletions test/test-vectors/ripemd160.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions test/test-vectors/scrypt.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
6 changes: 3 additions & 3 deletions test/test-vectors/secp256k1-compat.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down
2 changes: 1 addition & 1 deletion test/test-vectors/secp256k1.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { secp256k1 } from "../../src/secp256k1";
import { secp256k1 } from "ethereum-cryptography/secp256k1";
import { deepStrictEqual } from "./assert";

describe("secp256k1", () => {
Expand Down
4 changes: 2 additions & 2 deletions test/test-vectors/sha256.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
4 changes: 2 additions & 2 deletions test/test-vectors/sha512.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down
17 changes: 17 additions & 0 deletions test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"strict": true,
"downlevelIteration": true,
"rootDirs": [
"./"
],
"outDir": "./test-builds/tsc",
"noUnusedLocals": true,
"declaration": true
},
"include": [
"./**/*.ts",
]
}