From 2612c32a7312cb1f31ec13aca9c95feb6f73d1d6 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 4 Sep 2025 07:36:06 -0700 Subject: [PATCH] Eliminate self-references to allow esbuild to use anonymous classes. --- src/lib/crypto/bip39.ts | 4 ++-- src/lib/crypto/blake2b.ts | 27 ++++++++++++++------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/lib/crypto/bip39.ts b/src/lib/crypto/bip39.ts index 130fe89..545cb71 100644 --- a/src/lib/crypto/bip39.ts +++ b/src/lib/crypto/bip39.ts @@ -230,11 +230,11 @@ export class Bip39 { if (this.#blake2bSeed == null) { let bits = 0n for (const word of this.#phrase) { - const wordIndex = Bip39.wordlist.indexOf(word) + const wordIndex = (this.constructor as typeof Bip39).wordlist.indexOf(word) if (wordIndex === -1) { throw new RangeError('Word not found in BIP-39 list') } - bits = (bits << 11n) | BigInt(Bip39.wordlist.indexOf(word)) + bits = (bits << 11n) | BigInt((this.constructor as typeof Bip39).wordlist.indexOf(word)) } bits >>= 8n this.#blake2bSeed = new Uint8Array(32) diff --git a/src/lib/crypto/blake2b.ts b/src/lib/crypto/blake2b.ts index 29fc4e7..829dc50 100644 --- a/src/lib/crypto/blake2b.ts +++ b/src/lib/crypto/blake2b.ts @@ -29,7 +29,7 @@ export class Blake2b { new DataView(b).setUint32(0, index, false) const s = new Uint8Array(seed) const i = new Uint8Array(b) - const sk = new Blake2b(32).update(s).update(i).digest() + const sk = new this(32).update(s).update(i).digest() return Promise.resolve(sk.buffer) } @@ -69,13 +69,13 @@ export class Blake2b { } #G (r: number, i: number, a: number, b: number, c: number, d: number): void { - this.#v[a] += this.#v[b] + this.#m[Blake2b.SIGMA[r][2 * i + 0]] + this.#v[a] += this.#v[b] + this.#m[(this.constructor as typeof Blake2b).SIGMA[r][2 * i + 0]] this.#v[d] ^= this.#v[a] this.#v[d] = (this.#v[d] >> 32n) | (this.#v[d] << 32n) this.#v[c] += this.#v[d] this.#v[b] ^= this.#v[c] this.#v[b] = (this.#v[b] >> 24n) | (this.#v[b] << 40n) - this.#v[a] += this.#v[b] + this.#m[Blake2b.SIGMA[r][2 * i + 1]] + this.#v[a] += this.#v[b] + this.#m[(this.constructor as typeof Blake2b).SIGMA[r][2 * i + 1]] this.#v[d] ^= this.#v[a] this.#v[d] = (this.#v[d] >> 16n) | (this.#v[d] << 48n) this.#v[c] += this.#v[d] @@ -105,7 +105,7 @@ export class Blake2b { // init work variables for (let i = 0; i < 8; i++) { this.#v[i] = this.#h[i] - this.#v[i + 8] = Blake2b.IV[i] + this.#v[i + 8] = (this.constructor as typeof Blake2b).IV[i] } // lo 64 bits of counter @@ -210,37 +210,38 @@ export class Blake2b { */ constructor (length: number, key?: Uint8Array, salt?: Uint8Array, personal?: Uint8Array) constructor (length: unknown, key?: unknown, salt?: unknown, personal?: unknown) { + const B = this.constructor as typeof Blake2b if (length == null) { throw new TypeError(`length is required`) } if (typeof length !== 'number') { throw new TypeError(`length must be number`) } - if (length < Blake2b.OUTBYTES_MIN || length > Blake2b.OUTBYTES_MAX) { - throw new RangeError(`length must be ${Blake2b.OUTBYTES_MIN}-${Blake2b.OUTBYTES_MAX} bytes`) + if (length < B.OUTBYTES_MIN || length > B.OUTBYTES_MAX) { + throw new RangeError(`length must be ${B.OUTBYTES_MIN}-${B.OUTBYTES_MAX} bytes`) } if (key != null) { if (!(key instanceof Uint8Array)) { throw new TypeError(`key must be Uint8Array or Buffer`) } - if (key.length < Blake2b.KEYBYTES_MIN || key.length > Blake2b.KEYBYTES_MAX) { - throw new RangeError(`key must be ${Blake2b.KEYBYTES_MIN}-${Blake2b.KEYBYTES_MAX} bytes`) + if (key.length < B.KEYBYTES_MIN || key.length > B.KEYBYTES_MAX) { + throw new RangeError(`key must be ${B.KEYBYTES_MIN}-${B.KEYBYTES_MAX} bytes`) } } if (salt != null) { if (!(salt instanceof Uint8Array)) { throw new TypeError(`salt must be Uint8Array or Buffer`) } - if (salt.length !== Blake2b.SALTBYTES) { - throw new RangeError(`salt must be ${Blake2b.SALTBYTES} bytes`) + if (salt.length !== B.SALTBYTES) { + throw new RangeError(`salt must be ${B.SALTBYTES} bytes`) } } if (personal != null) { if (!(personal instanceof Uint8Array)) { throw new TypeError(`personal must be Uint8Array or Buffer`) } - if (personal.length !== Blake2b.PERSONALBYTES) { - throw new RangeError(`personal must be ${Blake2b.PERSONALBYTES} bytes`) + if (personal.length !== B.PERSONALBYTES) { + throw new RangeError(`personal must be ${B.PERSONALBYTES} bytes`) } } @@ -257,7 +258,7 @@ export class Blake2b { // initialize hash state for (let i = 0; i < 8; i++) { - this.#h[i] = Blake2b.IV[i] ^ this.#parameter_view.getBigUint64(i * 8, true) + this.#h[i] = B.IV[i] ^ this.#parameter_view.getBigUint64(i * 8, true) } // key the hash, if applicable -- 2.47.3