Skip to content

Commit

Permalink
Use TypeScript (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid authored Mar 22, 2021
1 parent 0ed0bb2 commit 3ee8dc7
Show file tree
Hide file tree
Showing 24 changed files with 377 additions and 186 deletions.
24 changes: 24 additions & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
env: {
node: true,
browser: true,
es2020: true,
},
parser: "@typescript-eslint/parser",
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
],
rules: {
"@typescript-eslint/explicit-module-boundary-types": "off",
},
overrides: [
{
files: ["*.ts"],
rules: {
"@typescript-eslint/explicit-module-boundary-types": ["error"],
},
},
],
};
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/benches/node_modules
/lib
/node_modules
/target
/tests/browser
/types

/lib/secp256k1*
/package-lock.json
7 changes: 0 additions & 7 deletions .prettierignore

This file was deleted.

23 changes: 14 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
.PHONY: build-js
build-js:
npx tsc

.PHONY: build-node-%
build-node-%: export PAIR = $(subst +, ,$(subst build-node-,,$@))
build-node-%:
Expand All @@ -18,43 +22,44 @@ build-node-debug-%:
.PHONY: build-wasm
build-wasm:
RUSTFLAGS="-C link-args=-zstack-size=655360" cargo build --package secp256k1-wasm --target wasm32-unknown-unknown --release
cp -f target/wasm32-unknown-unknown/release/secp256k1_wasm.wasm lib/secp256k1.wasm
mkdir -p lib && cp -f target/wasm32-unknown-unknown/release/secp256k1_wasm.wasm lib/secp256k1.wasm
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

.PHONY: build-wasm-debug
build-wasm-debug:
RUSTFLAGS="-C link-args=-zstack-size=655360" cargo build --package secp256k1-wasm --target wasm32-unknown-unknown
cp -f target/wasm32-unknown-unknown/debug/secp256k1_wasm.wasm lib/secp256k1.wasm
mkdir -p lib && cp -f target/wasm32-unknown-unknown/debug/secp256k1_wasm.wasm lib/secp256k1.wasm

.PHONY: clean
clean:
rm -rf benches/node_modules lib/secp256k1* target node_modules tests/browser
rm -rf benches/node_modules lib node_modules target tests/browser types

.PHONY: format
format:
cargo-fmt
npx prettier -w .
npx eslint benches/*.{js,json} src_ts/*.ts tests/*.js util/*.js *.json *.cjs --fix
npx sort-package-json package.json benches/package.json

.PHONY: lint
lint:
cargo fmt -- --check
cargo clippy --target wasm32-unknown-unknown
npx prettier -c .
cargo clippy --package secp256k1-node
cargo clippy --package secp256k1-wasm --target wasm32-unknown-unknown
npx eslint benches/*.{js,json} src_ts/*.ts tests/*.js util/*.js *.json *.cjs

.PHONY: test
test: test-browser test-node

.PHONY: test-browser-build
test-browser-build:
test-browser-build: build-js build-wasm-debug
npx webpack build -c tests/browser.webpack.js

.PHONY: test-browser
test-browser: build-wasm-debug test-browser-build
test-browser: test-browser-build
cat tests/browser/index.js | npx browser-run --static tests/browser | npx tap-difflet -p

.PHONY: test-node
test-node: build-node-debug build-wasm-debug
test-node: build-js build-node-debug build-wasm-debug
node --experimental-json-modules tests/index.js | npx tap-difflet -p
101 changes: 0 additions & 101 deletions lib/api.js

This file was deleted.

20 changes: 0 additions & 20 deletions lib/validate_error.js

This file was deleted.

2 changes: 0 additions & 2 deletions lib/wasm_loader.browser.js

This file was deleted.

15 changes: 0 additions & 15 deletions lib/wasm_loader.js

This file was deleted.

11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,32 @@
"./lib/rand.js": "./lib/rand.browser.js",
"./lib/wasm_loader.js": "./lib/wasm_loader.browser.js"
},
"types": "./types/index.d.ts",
"scripts": {
"postinstall": "patch-package --patch-dir ./util/patches"
},
"devDependencies": {
"@types/node": "^14.14.35",
"@typescript-eslint/eslint-plugin": "^4.18.0",
"@typescript-eslint/parser": "^4.18.0",
"binaryen": "^100.0.0",
"browser-run": "^8.0.0",
"buffer": "^6.0.3",
"eslint": "^7.22.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"patch-package": "^6.4.7",
"path-browserify": "^1.0.1",
"prettier": "^2.2.1",
"process": "^0.11.10",
"sort-package-json": "^1.49.0",
"stream-browserify": "^3.0.0",
"tap-difflet": "^0.7.2",
"tape": "^5.2.2",
"typescript": "^4.2.3",
"webpack": "^5.24.4",
"webpack-cli": "^4.5.0"
},
"engines": {
"node": ">=12.0.0"
"node": ">=14.0.0"
}
}
5 changes: 4 additions & 1 deletion secp256k1-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ pub static EXTRA_DATA_INPUT: [u8; EXTRA_DATA_SIZE] = [0; EXTRA_DATA_SIZE];
pub static mut SIGNATURE_INPUT: [u8; SIGNATURE_SIZE] = [0; SIGNATURE_SIZE];

macro_rules! jstry {
($value:expr) => {
jstry!($value, ())
};
($value:expr, $ret:expr) => {
match $value {
Ok(value) => value,
Expand Down Expand Up @@ -150,7 +153,7 @@ pub extern "C" fn point_add_scalar(inputlen: usize, outputlen: usize) -> i32 {
#[export_name = "pointCompress"]
pub extern "C" fn point_compress(inputlen: usize, outputlen: usize) {
unsafe {
let pk = jstry!(pubkey_parse(PUBLIC_KEY_INPUT.as_ptr(), inputlen), ());
let pk = jstry!(pubkey_parse(PUBLIC_KEY_INPUT.as_ptr(), inputlen));
pubkey_serialize(&pk, PUBLIC_KEY_INPUT.as_mut_ptr(), outputlen);
}
}
Expand Down
1 change: 1 addition & 0 deletions secp256k1/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::missing_safety_doc)]
#![cfg_attr(not(any(test, feature = "std")), no_std)]
#![feature(core_intrinsics)]

Expand Down
24 changes: 17 additions & 7 deletions lib/addon.js → src_ts/addon.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { join } from "path";
import createApi from "./api.js";
import { Secp256k1InternalApi, Secp256k1Api } from "./api.js";
import { generateSeed } from "./rand.js";
import { throwError } from "./validate_error.js";

function getLibExt() {
function getLibExt(): string {
switch (process.platform) {
case "darwin":
return "dylib";
Expand All @@ -15,24 +16,33 @@ function getLibExt() {
case "android":
case "sunos":
return "so";
default:
return "¯\\_(ツ)_/¯";
}
}

function getPrebuildLibLocation() {
function getPrebuildLibLocation(): string {
const name = `secp256k1-${process.arch}-${process.platform}.${getLibExt()}`;
return new URL(name, import.meta.url).pathname;
}

function getLocalBuildLibLocation(mode) {
function getLocalBuildLibLocation(mode: string): string {
const path = join("..", "target", mode, "libsecp256k1_node.so");
return new URL(path, import.meta.url).pathname;
}

export function loadAddon(location) {
function dlopen(location: string): Secp256k1InternalApi {
const module = { exports: { throwError, generateSeed } };
// Suppress TS2339: Property 'dlopen' does not exist on type 'Process'.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
process.dlopen(module, location);
return (module.exports as unknown) as Secp256k1InternalApi;
}

export function loadAddon(location: string): Secp256k1Api | null {
try {
const module = { exports: { throwError, generateSeed } };
process.dlopen(module, location);
return createApi(module.exports);
return createApi(dlopen(location));
} catch (_error) {
return null;
}
Expand Down
Loading

0 comments on commit 3ee8dc7

Please sign in to comment.