Skip to content

Commit 1868953

Browse files
committed
make crypto lib injectable
1 parent 2b0242f commit 1868953

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { ec as EC } from "elliptic";
22

33
const ec = new EC("secp256k1");
44
// eslint-disable-next-line @typescript-eslint/no-explicit-any, n/no-unsupported-features/node-builtins
5-
const browserCrypto = globalThis.crypto || (globalThis as any).msCrypto || {};
5+
let browserCrypto = globalThis.crypto || (globalThis as any).msCrypto || {};
66
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7-
const subtle = browserCrypto.subtle || (browserCrypto as any).webkitSubtle;
7+
let subtle = browserCrypto.subtle || (browserCrypto as any).webkitSubtle;
88

99
const EC_GROUP_ORDER = Buffer.from("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", "hex");
1010
const ZERO32 = Buffer.alloc(32, 0);
@@ -16,6 +16,12 @@ export interface Ecies {
1616
mac: Buffer;
1717
}
1818

19+
export function injectCryptoLib(cryptoLib: unknown) {
20+
browserCrypto = cryptoLib;
21+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
22+
subtle = browserCrypto.subtle || (browserCrypto as any).webkitSubtle;
23+
}
24+
1925
function assert(condition: boolean, message: string) {
2026
if (!condition) {
2127
throw new Error(message || "Assertion failed");

0 commit comments

Comments
 (0)