From 7ad7d86af24d50ff2e941e9c9859c9ca4d9dbf95 Mon Sep 17 00:00:00 2001 From: Saam Tehrani Date: Thu, 16 Jan 2025 01:47:33 -0700 Subject: [PATCH] add crypto module shim for older node versions --- .npmrc | 2 ++ package-lock.json | 21 ++++++--------------- package.json | 2 +- src/common/lib/crypto/keys/secp256k1.ts | 2 +- test/keys.ts | 2 +- test/shims.ts | 9 +++++++++ 6 files changed, 20 insertions(+), 18 deletions(-) create mode 100644 .npmrc create mode 100644 test/shims.ts diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..b75d685 --- /dev/null +++ b/.npmrc @@ -0,0 +1,2 @@ +@samantehrani:registry=https://npm.pkg.github.com/ +//npm.pkg.github.com/:_authToken=${NPM_TOKEN} diff --git a/package-lock.json b/package-lock.json index 46e22cf..c519067 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "1.15.5", "license": "MIT", "dependencies": { - "@solar-republic/wasm-secp256k1": "^0.4.2", + "@samantehrani/wasm-secp256k1": "^0.0.3", "arconnect": "^0.4.2", "asn1.js": "^5.4.1", "base64-js": "^1.5.1", @@ -45,12 +45,6 @@ "node": ">=18" } }, - "node_modules/@blake.regalia/belt": { - "version": "0.40.1", - "resolved": "https://registry.npmjs.org/@blake.regalia/belt/-/belt-0.40.1.tgz", - "integrity": "sha512-1KgtASOjGtzcC8IjL2gm4Ofwk9HvGW+l5VsqbKgxGrceEiVcccPf65eX20hek3p7prpNwbw9vImlXEU0Bii7Aw==", - "license": "ISC" - }, "node_modules/@cspotcode/source-map-support": { "version": "0.8.1", "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", @@ -194,14 +188,11 @@ "dev": true, "license": "MIT" }, - "node_modules/@solar-republic/wasm-secp256k1": { - "version": "0.4.2", - "resolved": "https://registry.npmjs.org/@solar-republic/wasm-secp256k1/-/wasm-secp256k1-0.4.2.tgz", - "integrity": "sha512-sGHHRFc4ZZGE7aVMp7mpKGJN6dqgo8gF3yzB/IBlBC8nk0OQ9+hk3ls6nGpcxDabXXpB0PAQW+vl/Xs7FnfvCg==", - "license": "MIT", - "dependencies": { - "@blake.regalia/belt": "^0.40.1" - } + "node_modules/@samantehrani/wasm-secp256k1": { + "version": "0.0.3", + "resolved": "https://npm.pkg.github.com/download/@samantehrani/wasm-secp256k1/0.0.3/0eb3cd0aa09c3903a1aa2cba47c62ddef5f000d8", + "integrity": "sha512-c/bcsOAZ24+3hxlGsQZC8Ni4iD8kYez7ZTu2EyYIzDjH/6hkrHXY8l+BQTIPw70ys59sguOt5zK5fow9uPaY/A==", + "license": "MIT" }, "node_modules/@tsconfig/node10": { "version": "1.0.11", diff --git a/package.json b/package.json index 7affdaf..c6e9062 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "clean:bundle": "rimraf bundles", "clean": "npm run clean:dist && npm run clean:package && npm run clean:bundle", "prepublishOnly": "npm run clean && npm run build", - "test": "mocha test/*.ts && echo \"NOW RUN => 'npm run test:web' <= \" ", + "test": "mocha --file test/shims.ts test/*.ts && echo \"NOW RUN => 'npm run test:web' <= \" ", "test:web": "npm run bundle:web && webpack --config-name web-tests && opener test/web/web.html", "prettier:check": "prettier --check \"src/**/*.ts\" \"test/**/*.ts\"", "prettier:write": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", diff --git a/src/common/lib/crypto/keys/secp256k1.ts b/src/common/lib/crypto/keys/secp256k1.ts index cbb3e6f..f479592 100644 --- a/src/common/lib/crypto/keys/secp256k1.ts +++ b/src/common/lib/crypto/keys/secp256k1.ts @@ -1,4 +1,4 @@ -import { Secp256k1, initWasmSecp256k1 } from "@solar-republic/wasm-secp256k1"; +import { Secp256k1, initWasmSecp256k1 } from "@samantehrani/wasm-secp256k1"; import { KeyType, KeyTypeByte, PrivateKey, PublicKey, SerializationParams, SigningParams, VerifyingParams } from "./interface"; import { bufferTob64Url, b64UrlToBuffer } from "../../utils"; diff --git a/test/keys.ts b/test/keys.ts index 9d300bb..d0e47fd 100644 --- a/test/keys.ts +++ b/test/keys.ts @@ -1,4 +1,4 @@ -import { readFileSync, writeFileSync } from "fs"; +import { readFileSync } from "fs"; import * as chai from "chai"; import { EllipticCurvePrivateKey, EllipticCurvePublicKey, SECP256k1PrivateKey, SECP256k1PublicKey } from "../src/common/lib/crypto/keys"; diff --git a/test/shims.ts b/test/shims.ts new file mode 100644 index 0000000..f16e3a6 --- /dev/null +++ b/test/shims.ts @@ -0,0 +1,9 @@ +(function () { + if (!globalThis.crypto) { + try { + globalThis.crypto = require('crypto'); + } catch (e) { + console.error('Failed to load crypto module in Node.js:', e); + } + } +})();