]> git.codecow.com Git - libnemo.git/commitdiff
Remove crypto module destructuring. Move constants into class to fix usage in workers.
authorChris Duncan <chris@zoso.dev>
Thu, 3 Jul 2025 19:34:31 +0000 (12:34 -0700)
committerChris Duncan <chris@zoso.dev>
Thu, 3 Jul 2025 19:34:31 +0000 (12:34 -0700)
src/lib/entropy.ts

index 5c4902e3a92ceefd4ae290b22d922a15b272e901..f3563a6e768cb76cb90ab20cf1d02cb94e4ee04d 100644 (file)
@@ -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<ArrayBuffer>
 
        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<ArrayBuffer>): Promise<Entropy> {
                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