From 9bceb96fe8310a0f9895f461b282aef86eee9de4 Mon Sep 17 00:00:00 2001 From: mariuspod <14898268+mariuspod@users.noreply.github.com> Date: Fri, 16 Dec 2022 15:08:14 +0100 Subject: [PATCH 1/2] fix: don't use static constructor field with global scope which results in a race condition between worker threads. --- src/secp256k1.cc | 8 ++++---- src/secp256k1.h | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/secp256k1.cc b/src/secp256k1.cc index e97d4a6..40ea6a5 100644 --- a/src/secp256k1.cc +++ b/src/secp256k1.cc @@ -26,8 +26,7 @@ } while (0) // Secp256k1 -Napi::FunctionReference Secp256k1Addon::constructor; -unsigned int Secp256k1Addon::secp256k1_context_flags = +const unsigned int Secp256k1Addon::secp256k1_context_flags = SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY; Napi::Value Secp256k1Addon::Init(Napi::Env env) { @@ -66,8 +65,9 @@ Napi::Value Secp256k1Addon::Init(Napi::Env env) { InstanceMethod("ecdh", &Secp256k1Addon::ECDH), }); - constructor = Napi::Persistent(func); - constructor.SuppressDestruct(); + Napi::FunctionReference* constructor = new Napi::FunctionReference(); + *constructor = Napi::Persistent(func); + env.SetInstanceData(constructor); return func; } diff --git a/src/secp256k1.h b/src/secp256k1.h index 6dabbf3..5f3e125 100644 --- a/src/secp256k1.h +++ b/src/secp256k1.h @@ -28,8 +28,7 @@ class Secp256k1Addon : public Napi::ObjectWrap { }; private: - static Napi::FunctionReference constructor; - static unsigned int secp256k1_context_flags; + static const unsigned int secp256k1_context_flags; const secp256k1_context* ctx_; ECDSASignData ecdsa_sign_data; ECDHData ecdh_data; From e4d890d2d012aafa7c1a6f480cff9107b1b0d339 Mon Sep 17 00:00:00 2001 From: mariuspod <14898268+mariuspod@users.noreply.github.com> Date: Tue, 20 Dec 2022 10:45:58 +0100 Subject: [PATCH 2/2] feat: bump node-addon-api to ^3.2.1 which adds instance data to env. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index e950e1d..7b90f6e 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,7 @@ }, "dependencies": { "elliptic": "^6.5.4", - "node-addon-api": "^2.0.0", + "node-addon-api": "^3.2.1", "node-gyp-build": "^4.2.0" }, "devDependencies": {