From 26f94ad48a22dff9610acf2c68e6789c4787ac28 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 3 Feb 2026 03:58:31 +0000 Subject: [PATCH 1/5] Initial plan From 389933ed051bd8d7f95079c6d24c84c0209f27eb Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 3 Feb 2026 04:02:14 +0000 Subject: [PATCH 2/5] Add runtime Buffer availability checks to prevent initialization order issues Co-authored-by: Corey-Code <37006206+Corey-Code@users.noreply.github.com> --- src/lib/bitcoin/transaction.ts | 11 +++++++++++ src/lib/crypto/bitcoin.ts | 24 ++++++++++++++++++----- src/lib/crypto/evm.ts | 35 ++++++++++++++++++++++++++-------- src/lib/crypto/keyring.ts | 21 ++++++++++++++++---- 4 files changed, 74 insertions(+), 17 deletions(-) diff --git a/src/lib/bitcoin/transaction.ts b/src/lib/bitcoin/transaction.ts index 0b1182a..8d87f96 100644 --- a/src/lib/bitcoin/transaction.ts +++ b/src/lib/bitcoin/transaction.ts @@ -29,6 +29,17 @@ if (typeof globalThis.Buffer === 'undefined') { globalThis.Buffer = Buffer; } +/** + * Runtime check to ensure Buffer is available + * This provides a safeguard against initialization order issues + */ +function ensureBuffer(): typeof Buffer { + if (typeof globalThis.Buffer === 'undefined') { + globalThis.Buffer = Buffer; + } + return globalThis.Buffer; +} + // ============================================================================ // Types // ============================================================================ diff --git a/src/lib/crypto/bitcoin.ts b/src/lib/crypto/bitcoin.ts index 6735cce..1fe5903 100644 --- a/src/lib/crypto/bitcoin.ts +++ b/src/lib/crypto/bitcoin.ts @@ -18,6 +18,17 @@ import * as secp256k1 from '@noble/secp256k1'; import { hmac } from '@noble/hashes/hmac'; import { sha512 } from '@noble/hashes/sha512'; +/** + * Runtime check to ensure Buffer is available + * This provides a safeguard against initialization order issues + */ +function ensureBuffer(): typeof Buffer { + if (typeof globalThis.Buffer === 'undefined') { + globalThis.Buffer = Buffer; + } + return globalThis.Buffer; +} + // UTXO network parameters for address generation export const UTXO_NETWORKS = { // Bitcoin @@ -293,13 +304,14 @@ function deriveChild( // Add IL to parent key (mod n) const n = BigInt('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141'); - const parentBig = BigInt('0x' + Buffer.from(parentKey).toString('hex')); - const ILBig = BigInt('0x' + Buffer.from(IL).toString('hex')); + const BufferImpl = ensureBuffer(); + const parentBig = BigInt('0x' + BufferImpl.from(parentKey).toString('hex')); + const ILBig = BigInt('0x' + BufferImpl.from(IL).toString('hex')); const childKey = (parentBig + ILBig) % n; const keyHex = childKey.toString(16).padStart(64, '0'); return { - key: new Uint8Array(Buffer.from(keyHex, 'hex')), + key: new Uint8Array(BufferImpl.from(keyHex, 'hex')), chainCode: new Uint8Array(IR), // Return copy }; } finally { @@ -333,7 +345,8 @@ function isValidPrivateKey(key: Uint8Array): boolean { // Check if less than curve order n const n = BigInt('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141'); - const keyBig = BigInt('0x' + Buffer.from(key).toString('hex')); + const BufferImpl = ensureBuffer(); + const keyBig = BigInt('0x' + BufferImpl.from(key).toString('hex')); return keyBig < n; } @@ -564,7 +577,8 @@ function base58CheckEncode(payload: Uint8Array, version: number | number[]): str const bytes = new Uint8Array([...data, ...checksum]); // Convert to base58 - let num = BigInt('0x' + Buffer.from(bytes).toString('hex')); + const BufferImpl = ensureBuffer(); + let num = BigInt('0x' + BufferImpl.from(bytes).toString('hex')); let result = ''; while (num > 0n) { diff --git a/src/lib/crypto/evm.ts b/src/lib/crypto/evm.ts index 18c9a40..9684725 100644 --- a/src/lib/crypto/evm.ts +++ b/src/lib/crypto/evm.ts @@ -16,6 +16,18 @@ import { keccak_256 } from '@noble/hashes/sha3'; if (typeof globalThis.Buffer === 'undefined') { globalThis.Buffer = Buffer; } + +/** + * Runtime check to ensure Buffer is available + * This provides a safeguard against initialization order issues + */ +function ensureBuffer(): typeof Buffer { + if (typeof globalThis.Buffer === 'undefined') { + globalThis.Buffer = Buffer; + } + return globalThis.Buffer; +} + /** * EVM Key Pair */ @@ -45,7 +57,8 @@ function deriveChild( ): { key: Uint8Array; chainCode: Uint8Array } { // Curve order for secp256k1 const n = BigInt('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141'); - const parentBig = BigInt('0x' + Buffer.from(parentKey).toString('hex')); + const BufferImpl = ensureBuffer(); + const parentBig = BigInt('0x' + BufferImpl.from(parentKey).toString('hex')); let currentIndex = index; const intermediates: Uint8Array[] = []; @@ -84,7 +97,8 @@ function deriveChild( const IR = I.slice(32); intermediates.push(IL); - const ILBig = BigInt('0x' + Buffer.from(IL).toString('hex')); + const BufferImpl = ensureBuffer(); + const ILBig = BigInt('0x' + BufferImpl.from(IL).toString('hex')); // BIP32: if IL == 0 or IL >= n, discard this child and try next index if (ILBig === 0n || ILBig >= n) { @@ -103,7 +117,7 @@ function deriveChild( const keyHex = childKey.toString(16).padStart(64, '0'); return { - key: new Uint8Array(Buffer.from(keyHex, 'hex')), + key: new Uint8Array(BufferImpl.from(keyHex, 'hex')), chainCode: new Uint8Array(IR), }; } @@ -148,7 +162,8 @@ function isValidPrivateKey(key: Uint8Array): boolean { // Check if less than curve order n const n = BigInt('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141'); - const keyBig = BigInt('0x' + Buffer.from(key).toString('hex')); + const BufferImpl = ensureBuffer(); + const keyBig = BigInt('0x' + BufferImpl.from(key).toString('hex')); return keyBig < n; } @@ -235,7 +250,8 @@ export async function deriveEvmKeyPairFromSeed( const addressBytes = addressHash.slice(-20); // Convert to checksummed address - const address = toChecksumAddress('0x' + Buffer.from(addressBytes).toString('hex')); + const BufferImpl = ensureBuffer(); + const address = toChecksumAddress('0x' + BufferImpl.from(addressBytes).toString('hex')); // Create copies for return (originals will be zeroed) const privateKeyCopy = key.slice() as Uint8Array; @@ -274,7 +290,8 @@ export async function deriveEvmKeyPair( */ export function toChecksumAddress(address: string): string { const addr = address.toLowerCase().replace('0x', ''); - const hash = Buffer.from(keccak_256(new TextEncoder().encode(addr))).toString('hex'); + const BufferImpl = ensureBuffer(); + const hash = BufferImpl.from(keccak_256(new TextEncoder().encode(addr))).toString('hex'); let checksumAddress = '0x'; for (let i = 0; i < addr.length; i++) { @@ -359,7 +376,8 @@ export function hasValidChecksum(address: string): boolean { * Get private key as hex string */ export function privateKeyToHex(privateKey: Uint8Array): string { - return '0x' + Buffer.from(privateKey).toString('hex'); + const BufferImpl = ensureBuffer(); + return '0x' + BufferImpl.from(privateKey).toString('hex'); } /** @@ -367,5 +385,6 @@ export function privateKeyToHex(privateKey: Uint8Array): string { */ export function hexToPrivateKey(hex: string): Uint8Array { const cleanHex = hex.startsWith('0x') ? hex.slice(2) : hex; - return new Uint8Array(Buffer.from(cleanHex, 'hex')); + const BufferImpl = ensureBuffer(); + return new Uint8Array(BufferImpl.from(cleanHex, 'hex')); } diff --git a/src/lib/crypto/keyring.ts b/src/lib/crypto/keyring.ts index 29c2bde..51b1a19 100644 --- a/src/lib/crypto/keyring.ts +++ b/src/lib/crypto/keyring.ts @@ -4,6 +4,17 @@ if (typeof globalThis.Buffer === 'undefined') { globalThis.Buffer = Buffer; } +/** + * Runtime check to ensure Buffer is available + * This provides a safeguard against initialization order issues + */ +function ensureBuffer(): typeof Buffer { + if (typeof globalThis.Buffer === 'undefined') { + globalThis.Buffer = Buffer; + } + return globalThis.Buffer; +} + import { DirectSecp256k1HdWallet, makeCosmoshubPath } from '@cosmjs/proto-signing'; import { AminoSignResponse, @@ -234,11 +245,12 @@ export class Keyring { const response = await this.signAmino(signerAddress, signDoc); + const BufferImpl = ensureBuffer(); return { signature: response.signature.signature, pub_key: { type: 'tendermint/PubKeySecp256k1', - value: Buffer.from(account.pubKey).toString('base64'), + value: BufferImpl.from(account.pubKey).toString('base64'), }, }; } @@ -254,15 +266,16 @@ export class Keyring { ): Promise { try { // Decode the signature and public key from base64 - const signatureBytes = Buffer.from(signature.signature, 'base64'); - const pubKeyBytes = Buffer.from(signature.pub_key.value, 'base64'); + const BufferImpl = ensureBuffer(); + const signatureBytes = BufferImpl.from(signature.signature, 'base64'); + const pubKeyBytes = BufferImpl.from(signature.pub_key.value, 'base64'); // Verify the public key matches the signer address // Derive address from public key and compare const account = this.accounts.find((acc) => acc.address === signerAddress); if (account) { // If we have the account, verify the pubkey matches - const storedPubKeyBase64 = Buffer.from(account.pubKey).toString('base64'); + const storedPubKeyBase64 = BufferImpl.from(account.pubKey).toString('base64'); if (storedPubKeyBase64 !== signature.pub_key.value) { return false; } From ecb797f3391becb2962343f78ccd1639e8fcebcb Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 3 Feb 2026 04:04:17 +0000 Subject: [PATCH 3/5] Refactor: Extract ensureBuffer to shared utility module Co-authored-by: Corey-Code <37006206+Corey-Code@users.noreply.github.com> --- src/lib/bitcoin/transaction.ts | 18 +----------------- src/lib/buffer-polyfill.ts | 26 ++++++++++++++++++++++++++ src/lib/crypto/bitcoin.ts | 18 +----------------- src/lib/crypto/evm.ts | 19 +------------------ src/lib/crypto/keyring.ts | 18 +----------------- 5 files changed, 30 insertions(+), 69 deletions(-) create mode 100644 src/lib/buffer-polyfill.ts diff --git a/src/lib/bitcoin/transaction.ts b/src/lib/bitcoin/transaction.ts index 8d87f96..02f239f 100644 --- a/src/lib/bitcoin/transaction.ts +++ b/src/lib/bitcoin/transaction.ts @@ -17,29 +17,13 @@ * - Legacy signature scripts */ -import { Buffer } from 'buffer'; +import { ensureBuffer } from '../buffer-polyfill'; import { sha256 } from '@noble/hashes/sha256'; import { ripemd160 } from '@noble/hashes/ripemd160'; import * as secp256k1 from '@noble/secp256k1'; import { UTXO } from './client'; import { BitcoinNetworkConfig } from '../networks/types'; -// Ensure Buffer is available -if (typeof globalThis.Buffer === 'undefined') { - globalThis.Buffer = Buffer; -} - -/** - * Runtime check to ensure Buffer is available - * This provides a safeguard against initialization order issues - */ -function ensureBuffer(): typeof Buffer { - if (typeof globalThis.Buffer === 'undefined') { - globalThis.Buffer = Buffer; - } - return globalThis.Buffer; -} - // ============================================================================ // Types // ============================================================================ diff --git a/src/lib/buffer-polyfill.ts b/src/lib/buffer-polyfill.ts new file mode 100644 index 0000000..7732793 --- /dev/null +++ b/src/lib/buffer-polyfill.ts @@ -0,0 +1,26 @@ +/** + * Buffer Polyfill Utility + * + * Provides runtime checks to ensure the Buffer polyfill is available. + * This prevents issues with module initialization order in browser environments. + */ + +import { Buffer } from 'buffer'; + +// Ensure Buffer is available globally on initial import +if (typeof globalThis.Buffer === 'undefined') { + globalThis.Buffer = Buffer; +} + +/** + * Runtime check to ensure Buffer is available + * This provides a safeguard against initialization order issues + * + * @returns The Buffer constructor, guaranteed to be available + */ +export function ensureBuffer(): typeof Buffer { + if (typeof globalThis.Buffer === 'undefined') { + globalThis.Buffer = Buffer; + } + return globalThis.Buffer; +} diff --git a/src/lib/crypto/bitcoin.ts b/src/lib/crypto/bitcoin.ts index 1fe5903..14abed5 100644 --- a/src/lib/crypto/bitcoin.ts +++ b/src/lib/crypto/bitcoin.ts @@ -6,29 +6,13 @@ * BIP84 (native SegWit), and BIP141 (SegWit encoding). */ -import { Buffer } from 'buffer'; -// Polyfill Buffer for browser environment -if (typeof globalThis.Buffer === 'undefined') { - globalThis.Buffer = Buffer; -} - +import { ensureBuffer } from '../buffer-polyfill'; import { sha256 } from '@noble/hashes/sha256'; import { ripemd160 } from '@noble/hashes/ripemd160'; import * as secp256k1 from '@noble/secp256k1'; import { hmac } from '@noble/hashes/hmac'; import { sha512 } from '@noble/hashes/sha512'; -/** - * Runtime check to ensure Buffer is available - * This provides a safeguard against initialization order issues - */ -function ensureBuffer(): typeof Buffer { - if (typeof globalThis.Buffer === 'undefined') { - globalThis.Buffer = Buffer; - } - return globalThis.Buffer; -} - // UTXO network parameters for address generation export const UTXO_NETWORKS = { // Bitcoin diff --git a/src/lib/crypto/evm.ts b/src/lib/crypto/evm.ts index 9684725..9beca34 100644 --- a/src/lib/crypto/evm.ts +++ b/src/lib/crypto/evm.ts @@ -5,29 +5,13 @@ * Uses BIP32/BIP44 with coin type 60 for Ethereum-compatible chains. */ -import { Buffer } from 'buffer'; +import { ensureBuffer } from '../buffer-polyfill'; import * as bip39 from 'bip39'; import * as secp256k1 from '@noble/secp256k1'; import { hmac } from '@noble/hashes/hmac'; import { sha512 } from '@noble/hashes/sha512'; import { keccak_256 } from '@noble/hashes/sha3'; -// Polyfill Buffer for browser environment -if (typeof globalThis.Buffer === 'undefined') { - globalThis.Buffer = Buffer; -} - -/** - * Runtime check to ensure Buffer is available - * This provides a safeguard against initialization order issues - */ -function ensureBuffer(): typeof Buffer { - if (typeof globalThis.Buffer === 'undefined') { - globalThis.Buffer = Buffer; - } - return globalThis.Buffer; -} - /** * EVM Key Pair */ @@ -97,7 +81,6 @@ function deriveChild( const IR = I.slice(32); intermediates.push(IL); - const BufferImpl = ensureBuffer(); const ILBig = BigInt('0x' + BufferImpl.from(IL).toString('hex')); // BIP32: if IL == 0 or IL >= n, discard this child and try next index diff --git a/src/lib/crypto/keyring.ts b/src/lib/crypto/keyring.ts index 51b1a19..d8981d0 100644 --- a/src/lib/crypto/keyring.ts +++ b/src/lib/crypto/keyring.ts @@ -1,20 +1,4 @@ -import { Buffer } from 'buffer'; -// Polyfill Buffer for browser environment (needed by bip39) -if (typeof globalThis.Buffer === 'undefined') { - globalThis.Buffer = Buffer; -} - -/** - * Runtime check to ensure Buffer is available - * This provides a safeguard against initialization order issues - */ -function ensureBuffer(): typeof Buffer { - if (typeof globalThis.Buffer === 'undefined') { - globalThis.Buffer = Buffer; - } - return globalThis.Buffer; -} - +import { ensureBuffer } from '../buffer-polyfill'; import { DirectSecp256k1HdWallet, makeCosmoshubPath } from '@cosmjs/proto-signing'; import { AminoSignResponse, From 49a14e2b96cd9b50d9a2837772c7cd0e09cf9d6f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 3 Feb 2026 04:06:52 +0000 Subject: [PATCH 4/5] Optimize Buffer checks: call ensureBuffer once at module level Co-authored-by: Corey-Code <37006206+Corey-Code@users.noreply.github.com> --- src/lib/bitcoin/transaction.ts | 1 - src/lib/buffer-polyfill.ts | 2 +- src/lib/crypto/bitcoin.ts | 6 +++--- src/lib/crypto/evm.ts | 9 +++------ src/lib/crypto/keyring.ts | 5 +++-- 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/lib/bitcoin/transaction.ts b/src/lib/bitcoin/transaction.ts index 02f239f..ffd473f 100644 --- a/src/lib/bitcoin/transaction.ts +++ b/src/lib/bitcoin/transaction.ts @@ -17,7 +17,6 @@ * - Legacy signature scripts */ -import { ensureBuffer } from '../buffer-polyfill'; import { sha256 } from '@noble/hashes/sha256'; import { ripemd160 } from '@noble/hashes/ripemd160'; import * as secp256k1 from '@noble/secp256k1'; diff --git a/src/lib/buffer-polyfill.ts b/src/lib/buffer-polyfill.ts index 7732793..07d464f 100644 --- a/src/lib/buffer-polyfill.ts +++ b/src/lib/buffer-polyfill.ts @@ -15,7 +15,7 @@ if (typeof globalThis.Buffer === 'undefined') { /** * Runtime check to ensure Buffer is available * This provides a safeguard against initialization order issues - * + * * @returns The Buffer constructor, guaranteed to be available */ export function ensureBuffer(): typeof Buffer { diff --git a/src/lib/crypto/bitcoin.ts b/src/lib/crypto/bitcoin.ts index 14abed5..d62ce06 100644 --- a/src/lib/crypto/bitcoin.ts +++ b/src/lib/crypto/bitcoin.ts @@ -13,6 +13,9 @@ import * as secp256k1 from '@noble/secp256k1'; import { hmac } from '@noble/hashes/hmac'; import { sha512 } from '@noble/hashes/sha512'; +// Ensure Buffer is available at module load time +const BufferImpl = ensureBuffer(); + // UTXO network parameters for address generation export const UTXO_NETWORKS = { // Bitcoin @@ -288,7 +291,6 @@ function deriveChild( // Add IL to parent key (mod n) const n = BigInt('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141'); - const BufferImpl = ensureBuffer(); const parentBig = BigInt('0x' + BufferImpl.from(parentKey).toString('hex')); const ILBig = BigInt('0x' + BufferImpl.from(IL).toString('hex')); const childKey = (parentBig + ILBig) % n; @@ -329,7 +331,6 @@ function isValidPrivateKey(key: Uint8Array): boolean { // Check if less than curve order n const n = BigInt('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141'); - const BufferImpl = ensureBuffer(); const keyBig = BigInt('0x' + BufferImpl.from(key).toString('hex')); return keyBig < n; } @@ -561,7 +562,6 @@ function base58CheckEncode(payload: Uint8Array, version: number | number[]): str const bytes = new Uint8Array([...data, ...checksum]); // Convert to base58 - const BufferImpl = ensureBuffer(); let num = BigInt('0x' + BufferImpl.from(bytes).toString('hex')); let result = ''; diff --git a/src/lib/crypto/evm.ts b/src/lib/crypto/evm.ts index 9beca34..62860b4 100644 --- a/src/lib/crypto/evm.ts +++ b/src/lib/crypto/evm.ts @@ -12,6 +12,9 @@ import { hmac } from '@noble/hashes/hmac'; import { sha512 } from '@noble/hashes/sha512'; import { keccak_256 } from '@noble/hashes/sha3'; +// Ensure Buffer is available at module load time +const BufferImpl = ensureBuffer(); + /** * EVM Key Pair */ @@ -41,7 +44,6 @@ function deriveChild( ): { key: Uint8Array; chainCode: Uint8Array } { // Curve order for secp256k1 const n = BigInt('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141'); - const BufferImpl = ensureBuffer(); const parentBig = BigInt('0x' + BufferImpl.from(parentKey).toString('hex')); let currentIndex = index; @@ -145,7 +147,6 @@ function isValidPrivateKey(key: Uint8Array): boolean { // Check if less than curve order n const n = BigInt('0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141'); - const BufferImpl = ensureBuffer(); const keyBig = BigInt('0x' + BufferImpl.from(key).toString('hex')); return keyBig < n; } @@ -233,7 +234,6 @@ export async function deriveEvmKeyPairFromSeed( const addressBytes = addressHash.slice(-20); // Convert to checksummed address - const BufferImpl = ensureBuffer(); const address = toChecksumAddress('0x' + BufferImpl.from(addressBytes).toString('hex')); // Create copies for return (originals will be zeroed) @@ -273,7 +273,6 @@ export async function deriveEvmKeyPair( */ export function toChecksumAddress(address: string): string { const addr = address.toLowerCase().replace('0x', ''); - const BufferImpl = ensureBuffer(); const hash = BufferImpl.from(keccak_256(new TextEncoder().encode(addr))).toString('hex'); let checksumAddress = '0x'; @@ -359,7 +358,6 @@ export function hasValidChecksum(address: string): boolean { * Get private key as hex string */ export function privateKeyToHex(privateKey: Uint8Array): string { - const BufferImpl = ensureBuffer(); return '0x' + BufferImpl.from(privateKey).toString('hex'); } @@ -368,6 +366,5 @@ export function privateKeyToHex(privateKey: Uint8Array): string { */ export function hexToPrivateKey(hex: string): Uint8Array { const cleanHex = hex.startsWith('0x') ? hex.slice(2) : hex; - const BufferImpl = ensureBuffer(); return new Uint8Array(BufferImpl.from(cleanHex, 'hex')); } diff --git a/src/lib/crypto/keyring.ts b/src/lib/crypto/keyring.ts index d8981d0..3032264 100644 --- a/src/lib/crypto/keyring.ts +++ b/src/lib/crypto/keyring.ts @@ -24,6 +24,9 @@ import { import { deriveEvmKeyPair, getEvmDerivationPath } from './evm'; import { networkRegistry } from '@/lib/networks'; +// Ensure Buffer is available at module load time +const BufferImpl = ensureBuffer(); + export interface KeyringAccount { id: string; name: string; @@ -229,7 +232,6 @@ export class Keyring { const response = await this.signAmino(signerAddress, signDoc); - const BufferImpl = ensureBuffer(); return { signature: response.signature.signature, pub_key: { @@ -250,7 +252,6 @@ export class Keyring { ): Promise { try { // Decode the signature and public key from base64 - const BufferImpl = ensureBuffer(); const signatureBytes = BufferImpl.from(signature.signature, 'base64'); const pubKeyBytes = BufferImpl.from(signature.pub_key.value, 'base64'); From 1100c205f147ebb9da0f52c039769f21f7f67554 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 3 Feb 2026 04:08:28 +0000 Subject: [PATCH 5/5] Add documentation explaining defensive Buffer check in ensureBuffer Co-authored-by: Corey-Code <37006206+Corey-Code@users.noreply.github.com> --- src/lib/buffer-polyfill.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/lib/buffer-polyfill.ts b/src/lib/buffer-polyfill.ts index 07d464f..884b222 100644 --- a/src/lib/buffer-polyfill.ts +++ b/src/lib/buffer-polyfill.ts @@ -16,6 +16,11 @@ if (typeof globalThis.Buffer === 'undefined') { * Runtime check to ensure Buffer is available * This provides a safeguard against initialization order issues * + * Note: While the module-level check (lines 11-13) should guarantee Buffer + * is available, this function provides an additional safety layer for edge cases + * where modules might be loaded in unexpected order or the module initialization + * is bypassed (e.g., dynamic imports, circular dependencies). + * * @returns The Buffer constructor, guaranteed to be available */ export function ensureBuffer(): typeof Buffer {