From f1b1d0584f31a437e8a854b72e9c256c505f47db Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 3 Jul 2025 12:34:31 -0700 Subject: [PATCH] Remove crypto module destructuring. Move constants into class to fix usage in workers. --- src/lib/entropy.ts | 44 +++++++++++++++++++++----------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/lib/entropy.ts b/src/lib/entropy.ts index 5c4902e..f3563a6 100644 --- a/src/lib/entropy.ts +++ b/src/lib/entropy.ts @@ -3,11 +3,6 @@ import { bytes, hex } from './convert' -const { crypto } = globalThis -const MIN = 16 -const MAX = 32 -const MOD = 4 - /** * Represents a cryptographically strong source of entropy suitable for use in * BIP-39 mnemonic phrase generation and consequently BIP-44 key derivation. @@ -19,6 +14,9 @@ const MOD = 4 */ export class Entropy { static #isInternal: boolean = false + static MIN: 16 = 16 + static MAX: 32 = 32 + static MOD: 4 = 4 #bytes: Uint8Array get bits (): string { return bytes.toBin(this.#bytes) } @@ -49,15 +47,15 @@ export class Entropy { if (typeof size !== 'number') { throw new TypeError(`Entropy cannot use ${typeof size} as a size`) } - if (size < MIN || size > MAX) { - throw new RangeError(`Entropy must be ${MIN}-${MAX} bytes`) + if (size < this.MIN || size > this.MAX) { + throw new RangeError(`Entropy must be ${this.MIN}-${this.MAX} bytes`) } - if (size % MOD !== 0) { - throw new RangeError(`Entropy must be a multiple of ${MOD} bytes`) + if (size % this.MOD !== 0) { + throw new RangeError(`Entropy must be a multiple of ${this.MOD} bytes`) } } Entropy.#isInternal = true - resolve(new this(crypto.getRandomValues(new Uint8Array(size ?? MAX)))) + resolve(new this(globalThis.crypto.getRandomValues(new Uint8Array(size ?? this.MAX)))) }) } @@ -79,11 +77,11 @@ export class Entropy { static async import (input: string | ArrayBuffer | Uint8Array): Promise { return new Promise((resolve, reject) => { if (typeof input === 'string') { - if (input.length < MIN * 2 || input.length > MAX * 2) { - throw new RangeError(`Entropy must be ${MIN * 2}-${MAX * 2} characters`) + if (input.length < this.MIN * 2 || input.length > this.MAX * 2) { + throw new RangeError(`Entropy must be ${this.MIN * 2}-${this.MAX * 2} characters`) } - if (input.length % MOD * 2 !== 0) { - throw new RangeError(`Entropy must be a multiple of ${MOD * 2} characters`) + if (input.length % this.MOD * 2 !== 0) { + throw new RangeError(`Entropy must be a multiple of ${this.MOD * 2} characters`) } if (!/^[0-9a-fA-F]+$/i.test(input)) { throw new RangeError('Entropy contains invalid hexadecimal characters') @@ -93,11 +91,11 @@ export class Entropy { } if (input instanceof ArrayBuffer) { - if (input.byteLength < MIN || input.byteLength > MAX) { - throw new Error(`Entropy must be ${MIN}-${MAX} bytes`) + if (input.byteLength < this.MIN || input.byteLength > this.MAX) { + throw new Error(`Entropy must be ${this.MIN}-${this.MAX} bytes`) } - if (input.byteLength % MOD !== 0) { - throw new RangeError(`Entropy must be a multiple of ${MOD} bytes`) + if (input.byteLength % this.MOD !== 0) { + throw new RangeError(`Entropy must be a multiple of ${this.MOD} bytes`) } Entropy.#isInternal = true resolve(new this(new Uint8Array(input))) @@ -107,11 +105,11 @@ export class Entropy { if (!(input.buffer instanceof ArrayBuffer)) { throw new TypeError(`Entropy imported bytes must be backed by an ArrayBuffer.`) } - if (input.length < MIN || input.length > MAX) { - throw new Error(`Entropy must be ${MIN}-${MAX} bytes`) + if (input.length < this.MIN || input.length > this.MAX) { + throw new Error(`Entropy must be ${this.MIN}-${this.MAX} bytes`) } - if (input.length % MOD !== 0) { - throw new RangeError(`Entropy must be a multiple of ${MOD} bytes`) + if (input.length % this.MOD !== 0) { + throw new RangeError(`Entropy must be a multiple of ${this.MOD} bytes`) } Entropy.#isInternal = true resolve(new this(input)) @@ -126,7 +124,7 @@ export class Entropy { */ destroy (): boolean { try { - crypto.getRandomValues(this.#bytes) + globalThis.crypto.getRandomValues(this.#bytes) return true } catch (err) { return false -- 2.47.3