From f436690cb703069e80658a0387d42bcb62e69f04 Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Sat, 13 Mar 2021 19:26:56 +0300 Subject: [PATCH] Add test in browser env (#4) --- .gitignore | 7 ++--- .prettierignore | 4 +++ Cargo.lock | 2 +- Makefile | 24 ++++++++++++---- lib/index.js | 53 +++++++++++++--------------------- lib/secp256k1.browser.js | 2 ++ lib/secp256k1.js | 14 +++++---- lib/validate.js | 60 +++++++++++++-------------------------- lib/validate_wasm.js | 38 ------------------------- lib/wasm_error.js | 20 +++++++++++++ lib/wasm_rand.browser.js | 5 ++++ lib/wasm_rand.js | 5 ++++ package.json | 16 +++++++++-- secp256k1-wasm/Cargo.toml | 4 +-- secp256k1-wasm/src/lib.rs | 11 ++++--- tests/browser.webpack.js | 31 ++++++++++++++++++++ tests/ecdsa.js | 14 ++++----- tests/index.js | 20 ++++++++++--- tests/points.js | 24 +++++++--------- tests/privates.js | 16 +++++------ tests/util.js | 11 +++---- util/wasm-strip.js | 4 +-- 22 files changed, 204 insertions(+), 181 deletions(-) create mode 100644 lib/secp256k1.browser.js delete mode 100644 lib/validate_wasm.js create mode 100644 lib/wasm_error.js create mode 100644 lib/wasm_rand.browser.js create mode 100644 lib/wasm_rand.js create mode 100644 tests/browser.webpack.js diff --git a/.gitignore b/.gitignore index da80dfb..111f816 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,6 @@ -/coverage /node_modules /target +/tests/browser -lib/secp256k1.wasm -package-lock.json -tests/bundle.js +/lib/secp256k1.wasm +/package-lock.json diff --git a/.prettierignore b/.prettierignore index b59605f..c87afff 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,2 +1,6 @@ /target +/tests/browser +/tests/fixtures /util/gen-fixtures/secp256k1 + +package-lock.json diff --git a/Cargo.lock b/Cargo.lock index c02642b..fce2217 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,7 +17,7 @@ dependencies = [ ] [[package]] -name = "tiny-secp256k1-wasm" +name = "secp256k1-wasm" version = "0.0.0" dependencies = [ "secp256k1-sys", diff --git a/Makefile b/Makefile index 60e9e92..42f9de8 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,20 @@ -build-wasm-cp = cp -f target/wasm32-unknown-unknown/$(1)/tiny_secp256k1_wasm.wasm lib/secp256k1.wasm +build-wasm-location = lib/secp256k1.wasm +build-wasm-cp = mkdir -p lib && cp -f target/wasm32-unknown-unknown/$(1)/secp256k1_wasm.wasm $(build-wasm-location) build-wasm: cargo build --target wasm32-unknown-unknown --release $(call build-wasm-cp,release) - wasm-opt --strip-debug --strip-producers --output lib/secp256k1.wasm lib/secp256k1.wasm - node ./util/wasm-strip.js lib/secp256k1.wasm - wasm-opt -O4 --output lib/secp256k1.wasm lib/secp256k1.wasm + wasm-opt --strip-debug --strip-producers --output $(build-wasm-location) $(build-wasm-location) + node ./util/wasm-strip.js $(build-wasm-location) + wasm-opt -O4 --output $(build-wasm-location) $(build-wasm-location) build-wasm-debug: cargo build --target wasm32-unknown-unknown $(call build-wasm-cp,debug) +clean: + rm -rf target node_modules tests/browser + format: cargo-fmt npx prettier -w . @@ -20,5 +24,13 @@ lint: cargo clippy --target wasm32-unknown-unknown npx prettier -c . -test: - npx tape tests/index.js | npx tap-difflet -p +test: test-browser test-node + +test-browser-build: + npx webpack build -c tests/browser.webpack.js + +test-browser: build-wasm-debug test-browser-build + cat tests/browser/index.js | npx browser-run --static tests/browser | npx tap-difflet -p + +test-node: build-wasm-debug + node --experimental-json-modules tests/index.js | npx tap-difflet -p diff --git a/lib/index.js b/lib/index.js index 94a0cd4..37e4c51 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,5 +1,7 @@ -const validate = require("./validate"); -const wasm = require("./secp256k1"); +import * as validate from "./validate.js"; +import wasm from "./secp256k1.js"; + +export const __initializeContext = wasm.initializeContext; const WASM_BUFFER = new Uint8Array(wasm.memory.buffer); const WASM_PRIVATE_KEY_PTR = wasm.PRIVATE_INPUT.value; @@ -56,19 +58,19 @@ function __isPoint(p) { } } -function isPoint(p) { +export function isPoint(p) { return validate.isPoint(p) && __isPoint(p); } -function isPointCompressed(p) { +export function isPointCompressed(p) { return validate.isPointCompressed(p) && __isPoint(p); } -function isPrivate(x) { +export function isPrivate(x) { return validate.isPrivate(x); } -function pointAdd(pA, pB, compressed) { +export function pointAdd(pA, pB, compressed) { validate.validatePoint(pA); validate.validatePoint(pB); const outputlen = assumeCompression(compressed, pA); @@ -85,7 +87,7 @@ function pointAdd(pA, pB, compressed) { } } -function pointAddScalar(p, tweak, compressed) { +export function pointAddScalar(p, tweak, compressed) { validate.validatePoint(p); validate.validateTweak(tweak); let outputlen = assumeCompression(compressed, p); @@ -102,7 +104,7 @@ function pointAddScalar(p, tweak, compressed) { } } -function pointCompress(p, compressed) { +export function pointCompress(p, compressed) { validate.validatePoint(p); const outputlen = assumeCompression(compressed, p); @@ -115,7 +117,7 @@ function pointCompress(p, compressed) { } } -function pointFromScalar(d, compressed = true) { +export function pointFromScalar(d, compressed = true) { validate.validatePrivate(d); const outputlen = assumeCompression(compressed); @@ -130,7 +132,7 @@ function pointFromScalar(d, compressed = true) { } } -function pointMultiply(p, tweak, compressed) { +export function pointMultiply(p, tweak, compressed) { validate.validatePoint(p); validate.validateTweak(tweak); let outputlen = assumeCompression(compressed, p); @@ -147,7 +149,7 @@ function pointMultiply(p, tweak, compressed) { } } -function privateAdd(d, tweak) { +export function privateAdd(d, tweak) { validate.validatePrivate(d); validate.validateTweak(tweak); @@ -163,12 +165,12 @@ function privateAdd(d, tweak) { } } -function privateSub(d, tweak) { +export function privateSub(d, tweak) { validate.validatePrivate(d); validate.validateTweak(tweak); - // We can not pass zero tweak to WASM, because use `secp256k1_ec_seckey_negate` for tweak negate. - // (because zero is not valid seckey) + // We can not pass zero tweak to WASM, because WASM use `secp256k1_ec_seckey_negate` for tweak negate. + // (zero is not valid seckey) if (validate.isZero(tweak)) { return new Uint8Array(d); } @@ -185,11 +187,11 @@ function privateSub(d, tweak) { } } -function sign(h, d) { +export function sign(h, d) { return signWithEntropy(h, d); } -function signWithEntropy(h, d, e) { +export function signWithEntropy(h, d, e) { validate.validateHash(h); validate.validatePrivate(d); validate.validateExtraData(e); @@ -208,7 +210,7 @@ function signWithEntropy(h, d, e) { } } -function verify(h, Q, signature, strict = false) { +export function verify(h, Q, signature, strict = false) { validate.validateHash(h); validate.validatePoint(Q); validate.validateSignature(signature); @@ -224,20 +226,3 @@ function verify(h, Q, signature, strict = false) { SIGNATURE_INPUT.fill(0); } } - -module.exports = { - __initializeContext: wasm.initializeContext, - isPoint, - isPointCompressed, - isPrivate, - pointAdd, - pointAddScalar, - pointCompress, - pointFromScalar, - pointMultiply, - privateAdd, - privateSub, - sign, - signWithEntropy, - verify, -}; diff --git a/lib/secp256k1.browser.js b/lib/secp256k1.browser.js new file mode 100644 index 0000000..3cfdbed --- /dev/null +++ b/lib/secp256k1.browser.js @@ -0,0 +1,2 @@ +import * as wasm from "./secp256k1.wasm"; +export default wasm; diff --git a/lib/secp256k1.js b/lib/secp256k1.js index d6b1a12..0fb1c28 100644 --- a/lib/secp256k1.js +++ b/lib/secp256k1.js @@ -1,13 +1,15 @@ -const { readFileSync } = require("fs"); -const { join } = require("path"); -const validate_wasm = require("./validate_wasm"); +import { readFileSync } from "fs"; +import { URL } from "url"; +import * as wasm_error from "./wasm_error.js"; +import * as wasm_rand from "./wasm_rand.js"; -const binary = readFileSync(join(__dirname, "secp256k1.wasm")); +const binary = readFileSync(new URL("secp256k1.wasm", import.meta.url)); const imports = { - "./validate_wasm.js": validate_wasm, + "./wasm_error.js": wasm_error, + "./wasm_rand.js": wasm_rand, }; const mod = new WebAssembly.Module(binary); const instance = new WebAssembly.Instance(mod, imports); -module.exports = instance.exports; +export default instance.exports; diff --git a/lib/validate.js b/lib/validate.js index 2cf9ab7..3031fdd 100644 --- a/lib/validate.js +++ b/lib/validate.js @@ -1,4 +1,4 @@ -const { +import { ERROR_BAD_PRIVATE, ERROR_BAD_POINT, ERROR_BAD_TWEAK, @@ -6,15 +6,15 @@ const { ERROR_BAD_HASH, ERROR_BAD_EXTRA_DATA, ERROR_BAD_SIGNATURE, -} = require("./validate_wasm"); +} from "./wasm_error.js"; -const PRIVATE_KEY_SIZE = 32; -const PUBLIC_KEY_COMPRESSED_SIZE = 33; -const PUBLIC_KEY_UNCOMPRESSED_SIZE = 65; -const TWEAK_SIZE = 32; -const HASH_SIZE = 32; -const EXTRA_DATA_SIZE = 32; -const SIGNATURE_SIZE = 64; +export const PRIVATE_KEY_SIZE = 32; +export const PUBLIC_KEY_COMPRESSED_SIZE = 33; +export const PUBLIC_KEY_UNCOMPRESSED_SIZE = 65; +export const TWEAK_SIZE = 32; +export const HASH_SIZE = 32; +export const EXTRA_DATA_SIZE = 32; +export const SIGNATURE_SIZE = 64; const BN32_ZERO = new Uint8Array(32); const BN32_N = new Uint8Array([ @@ -65,11 +65,11 @@ function cmpBN32(data1, data2) { return 0; } -function isZero(x) { +export function isZero(x) { return cmpBN32(x, BN32_ZERO) === 0; } -function isPrivate(x) { +export function isPrivate(x) { return ( isUint8Array(x) && x.length === PRIVATE_KEY_SIZE && @@ -78,7 +78,7 @@ function isPrivate(x) { ); } -function isPoint(p) { +export function isPoint(p) { return ( isUint8Array(p) && (p.length === PUBLIC_KEY_COMPRESSED_SIZE || @@ -86,7 +86,7 @@ function isPoint(p) { ); } -function isPointCompressed(p) { +export function isPointCompressed(p) { return isUint8Array(p) && p.length === PUBLIC_KEY_COMPRESSED_SIZE; } @@ -115,48 +115,26 @@ function isSignature(signature) { ); } -function validatePrivate(d) { +export function validatePrivate(d) { if (!isPrivate(d)) throwError(ERROR_BAD_PRIVATE); } -function validatePoint(p) { +export function validatePoint(p) { if (!isPoint(p)) throwError(ERROR_BAD_POINT); } -function validateTweak(tweak) { +export function validateTweak(tweak) { if (!isTweak(tweak)) throwError(ERROR_BAD_TWEAK); } -function validateHash(h) { +export function validateHash(h) { if (!isHash(h)) throwError(ERROR_BAD_HASH); } -function validateExtraData(e) { +export function validateExtraData(e) { if (!isExtraData(e)) throwError(ERROR_BAD_EXTRA_DATA); } -function validateSignature(signature) { +export function validateSignature(signature) { if (!isSignature(signature)) throwError(ERROR_BAD_SIGNATURE); } - -module.exports = { - PRIVATE_KEY_SIZE, - PUBLIC_KEY_COMPRESSED_SIZE, - PUBLIC_KEY_UNCOMPRESSED_SIZE, - TWEAK_SIZE, - HASH_SIZE, - EXTRA_DATA_SIZE, - SIGNATURE_SIZE, - - isZero, - isPrivate, - isPoint, - isPointCompressed, - - validatePrivate, - validatePoint, - validateTweak, - validateHash, - validateExtraData, - validateSignature, -}; diff --git a/lib/validate_wasm.js b/lib/validate_wasm.js deleted file mode 100644 index c369286..0000000 --- a/lib/validate_wasm.js +++ /dev/null @@ -1,38 +0,0 @@ -const { randomBytes } = require("crypto"); - -const ERROR_BAD_PRIVATE = 0; -const ERROR_BAD_POINT = 1; -const ERROR_BAD_TWEAK = 2; -const ERROR_BAD_HASH = 3; -const ERROR_BAD_SIGNATURE = 4; -const ERROR_BAD_EXTRA_DATA = 5; - -const ERRORS_MESSAGES = { - [ERROR_BAD_PRIVATE]: "Expected Private", - [ERROR_BAD_POINT]: "Expected Point", - [ERROR_BAD_TWEAK]: "Expected Tweak", - [ERROR_BAD_HASH]: "Expected Hash", - [ERROR_BAD_SIGNATURE]: "Expected Signature", - [ERROR_BAD_EXTRA_DATA]: "Expected Extra Data (32 bytes)", -}; - -function generateInt32() { - return randomBytes(4).readInt32BE(0); -} - -function throwError(errcode) { - const message = ERRORS_MESSAGES[errcode] || `Unknow error code: ${errcode}`; - throw new TypeError(message); -} - -module.exports = { - ERROR_BAD_PRIVATE, - ERROR_BAD_POINT, - ERROR_BAD_TWEAK, - ERROR_BAD_HASH, - ERROR_BAD_SIGNATURE, - ERROR_BAD_EXTRA_DATA, - - generateInt32, - throwError, -}; diff --git a/lib/wasm_error.js b/lib/wasm_error.js new file mode 100644 index 0000000..c367181 --- /dev/null +++ b/lib/wasm_error.js @@ -0,0 +1,20 @@ +export const ERROR_BAD_PRIVATE = 0; +export const ERROR_BAD_POINT = 1; +export const ERROR_BAD_TWEAK = 2; +export const ERROR_BAD_HASH = 3; +export const ERROR_BAD_SIGNATURE = 4; +export const ERROR_BAD_EXTRA_DATA = 5; + +const ERRORS_MESSAGES = { + [ERROR_BAD_PRIVATE]: "Expected Private", + [ERROR_BAD_POINT]: "Expected Point", + [ERROR_BAD_TWEAK]: "Expected Tweak", + [ERROR_BAD_HASH]: "Expected Hash", + [ERROR_BAD_SIGNATURE]: "Expected Signature", + [ERROR_BAD_EXTRA_DATA]: "Expected Extra Data (32 bytes)", +}; + +export function throwError(errcode) { + const message = ERRORS_MESSAGES[errcode] || `Unknow error code: ${errcode}`; + throw new TypeError(message); +} diff --git a/lib/wasm_rand.browser.js b/lib/wasm_rand.browser.js new file mode 100644 index 0000000..f8acd54 --- /dev/null +++ b/lib/wasm_rand.browser.js @@ -0,0 +1,5 @@ +export function generateInt32() { + const array = new Uint8Array(4); + window.crypto.getRandomValues(array); + return (array[0] << 3) + (array[1] << 2) + (array[2] << 1) + array[1]; +} diff --git a/lib/wasm_rand.js b/lib/wasm_rand.js new file mode 100644 index 0000000..6ac3af1 --- /dev/null +++ b/lib/wasm_rand.js @@ -0,0 +1,5 @@ +import { randomBytes } from "crypto"; + +export function generateInt32() { + return randomBytes(4).readInt32BE(0); +} diff --git a/package.json b/package.json index 0b796bc..22c5f4b 100644 --- a/package.json +++ b/package.json @@ -11,12 +11,24 @@ "url": "https://github.com/bitcoinjs/tiny-secp256k1.git" }, "license": "MIT", - "main": "lib/index.js", + "type": "module", + "main": "./lib/index.js", + "browser": { + "./lib/secp256k1.js": "./lib/secp256k1.browser.js", + "./lib/wasm_rand.js": "./lib/wasm_rand.browser.js" + }, "devDependencies": { "binaryen": "^100.0.0", + "browser-run": "^8.0.0", + "buffer": "^6.0.3", + "path-browserify": "^1.0.1", "prettier": "^2.2.1", + "process": "^0.11.10", + "stream-browserify": "^3.0.0", "tap-difflet": "^0.7.2", - "tape": "^5.2.2" + "tape": "^5.2.2", + "webpack": "^5.24.4", + "webpack-cli": "^4.5.0" }, "engines": { "node": ">=12.0.0" diff --git a/secp256k1-wasm/Cargo.toml b/secp256k1-wasm/Cargo.toml index 630a733..b6c3ba2 100644 --- a/secp256k1-wasm/Cargo.toml +++ b/secp256k1-wasm/Cargo.toml @@ -1,9 +1,9 @@ [package] -name = "tiny-secp256k1-wasm" +name = "secp256k1-wasm" version = "0.0.0" authors = ["Kirill Fomichev "] edition = "2018" -description = "A Rust library for building tiny-secp256k1 WASM" +description = "A Rust library for building tiny-secp256k1 WASM." license = "MIT" publish = false diff --git a/secp256k1-wasm/src/lib.rs b/secp256k1-wasm/src/lib.rs index 0ff06d4..3bba7ae 100644 --- a/secp256k1-wasm/src/lib.rs +++ b/secp256k1-wasm/src/lib.rs @@ -21,15 +21,18 @@ use secp256k1_sys::{ SECP256K1_START_VERIFY, }; -#[link(wasm_import_module = "./validate_wasm.js")] +#[link(wasm_import_module = "./wasm_error.js")] extern "C" { - #[link_name = "generateInt32"] - fn generate_int32() -> i32; - #[link_name = "throwError"] fn throw_error(errcode: usize); } +#[link(wasm_import_module = "./wasm_rand.js")] +extern "C" { + #[link_name = "generateInt32"] + fn generate_int32() -> i32; +} + const PRIVATE_KEY_SIZE: usize = 32; const PUBLIC_KEY_COMPRESSED_SIZE: usize = 33; const PUBLIC_KEY_UNCOMPRESSED_SIZE: usize = 65; diff --git a/tests/browser.webpack.js b/tests/browser.webpack.js new file mode 100644 index 0000000..223abd4 --- /dev/null +++ b/tests/browser.webpack.js @@ -0,0 +1,31 @@ +import { createRequire } from "module"; +import { URL } from "url"; +import webpack from "webpack"; + +const require = createRequire(import.meta.url); + +export default { + target: "web", + mode: "development", + entry: "./tests/index.js", + output: { + path: new URL("browser", import.meta.url).pathname, + filename: "index.js", + }, + experiments: { + asyncWebAssembly: true, + }, + plugins: [ + new webpack.ProvidePlugin({ + process: "process/browser", + }), + ], + resolve: { + fallback: { + buffer: require.resolve("buffer"), + fs: false, + path: require.resolve("path-browserify"), + stream: require.resolve("stream-browserify"), + }, + }, +}; diff --git a/tests/ecdsa.js b/tests/ecdsa.js index 8909eec..a4dd866 100644 --- a/tests/ecdsa.js +++ b/tests/ecdsa.js @@ -1,6 +1,6 @@ -const tape = require("tape"); -const { fromHex, toHex } = require("./util"); -const fecdsa = require("./fixtures/ecdsa.json"); +import { test } from "tape"; +import { fromHex, toHex } from "./util.js"; +import fecdsa from "./fixtures/ecdsa.json"; const buf1 = fromHex( "0000000000000000000000000000000000000000000000000000000000000000" @@ -29,8 +29,8 @@ function corrupt(x) { return x; } -function test(binding) { - tape("sign", (t) => { +export default function (binding) { + test("sign", (t) => { for (const f of fecdsa.valid) { const d = fromHex(f.d); const m = fromHex(f.m); @@ -111,7 +111,7 @@ function test(binding) { t.end(); }); - tape("verify", (t) => { + test("verify", (t) => { for (const f of fecdsa.valid) { const d = fromHex(f.d); const Q = binding.pointFromScalar(d, true); @@ -174,5 +174,3 @@ function test(binding) { t.end(); }); } - -module.exports = test; diff --git a/tests/index.js b/tests/index.js index 508e28a..2e0c00d 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1,5 +1,17 @@ -const wasm = require("../"); +import { test } from "tape"; +import * as wasm from "../lib/index.js"; -require("./ecdsa")(wasm); -require("./points")(wasm); -require("./privates")(wasm); +import test_ecdsa from "./ecdsa.js"; +import test_points from "./points.js"; +import test_privates from "./privates.js"; + +test_ecdsa(wasm); +test_points(wasm); +test_privates(wasm); + +// Closing browser if launched through `browser-run`. +test.onFinish(() => { + try { + window.close(); + } catch (_err) {} +}); diff --git a/tests/points.js b/tests/points.js index e5caea5..027f4d4 100644 --- a/tests/points.js +++ b/tests/points.js @@ -1,9 +1,9 @@ -const tape = require("tape"); -const { fromHex } = require("./util"); -const fpoints = require("./fixtures/points.json"); +import { test } from "tape"; +import { fromHex } from "./util.js"; +import fpoints from "./fixtures/points.json"; -function test(binding) { - tape("isPoint", (t) => { +export default function (binding) { + test("isPoint", (t) => { for (const f of fpoints.valid.isPoint) { const p = fromHex(f.P); t.equal( @@ -16,7 +16,7 @@ function test(binding) { t.end(); }); - tape("isPointCompressed", (t) => { + test("isPointCompressed", (t) => { for (const f of fpoints.valid.isPoint) { if (!f.expected) continue; const p = fromHex(f.P); @@ -31,7 +31,7 @@ function test(binding) { t.end(); }); - tape("pointAdd", (t) => { + test("pointAdd", (t) => { for (const f of fpoints.valid.pointAdd) { const p = fromHex(f.P); const q = fromHex(f.Q); @@ -67,7 +67,7 @@ function test(binding) { t.end(); }); - tape("pointAddScalar", (t) => { + test("pointAddScalar", (t) => { for (const f of fpoints.valid.pointAddScalar) { const p = fromHex(f.P); const d = fromHex(f.d); @@ -103,7 +103,7 @@ function test(binding) { t.end(); }); - tape("pointCompress", (t) => { + test("pointCompress", (t) => { for (const f of fpoints.valid.pointCompress) { const p = fromHex(f.P); const expected = fromHex(f.expected); @@ -128,7 +128,7 @@ function test(binding) { t.end(); }); - tape("pointFromScalar", (t) => { + test("pointFromScalar", (t) => { for (const f of fpoints.valid.pointFromScalar) { const d = fromHex(f.d); const expected = fromHex(f.expected); @@ -162,7 +162,7 @@ function test(binding) { t.end(); }); - tape("pointMultiply", (t) => { + test("pointMultiply", (t) => { for (const f of fpoints.valid.pointMultiply) { const p = fromHex(f.P); const d = fromHex(f.d); @@ -198,5 +198,3 @@ function test(binding) { t.end(); }); } - -module.exports = test; diff --git a/tests/privates.js b/tests/privates.js index 79158d7..8d3d382 100644 --- a/tests/privates.js +++ b/tests/privates.js @@ -1,9 +1,9 @@ -const tape = require("tape"); -const { fromHex } = require("./util"); -const fprivates = require("./fixtures/privates.json"); +import { test } from "tape"; +import { fromHex } from "./util.js"; +import fprivates from "./fixtures/privates.json"; -function test(binding) { - tape("isPrivate", (t) => { +export default function (binding) { + test("isPrivate", (t) => { for (const f of fprivates.valid.isPrivate) { const d = fromHex(f.d); @@ -17,7 +17,7 @@ function test(binding) { t.end(); }); - tape("privateAdd", (t) => { + test("privateAdd", (t) => { for (const f of fprivates.valid.privateAdd) { const d = fromHex(f.d); const tweak = fromHex(f.tweak); @@ -46,7 +46,7 @@ function test(binding) { t.end(); }); - tape("privateSub", (t) => { + test("privateSub", (t) => { for (const f of fprivates.valid.privateSub) { const d = fromHex(f.d); const tweak = fromHex(f.tweak); @@ -75,5 +75,3 @@ function test(binding) { t.end(); }); } - -module.exports = test; diff --git a/tests/util.js b/tests/util.js index e48fd6e..7215e12 100644 --- a/tests/util.js +++ b/tests/util.js @@ -1,12 +1,9 @@ -function fromHex(data) { +import { Buffer } from "buffer"; + +export function fromHex(data) { return new Uint8Array(Buffer.from(data, "hex")); } -function toHex(data) { +export function toHex(data) { return Buffer.from(data).toString("hex"); } - -module.exports = { - fromHex, - toHex, -}; diff --git a/util/wasm-strip.js b/util/wasm-strip.js index 18d1c08..0889de7 100644 --- a/util/wasm-strip.js +++ b/util/wasm-strip.js @@ -1,5 +1,5 @@ -const fs = require("fs"); -const binaryen = require("binaryen"); +import * as fs from "fs"; +import binaryen from "binaryen"; const NOT_USED_FUNCTIONS = [ "rustsecp256k1_v0_4_0_default_error_callback_fn",