Skip to content

Refactor: Extract shared ECC utilities into dedicated module#521

Merged
jamespepper81 merged 1 commit into
devfrom
claude/consolidate-ecc-validation-DDzUc
Jan 28, 2026
Merged

Refactor: Extract shared ECC utilities into dedicated module#521
jamespepper81 merged 1 commit into
devfrom
claude/consolidate-ecc-validation-DDzUc

Conversation

@jamespepper81
Copy link
Copy Markdown
Contributor

Description

This PR consolidates duplicated ECC validation, transaction size estimation, and address derivation logic into a new shared utility module (ecc-utils.ts). This eliminates code duplication across bitcoin-service.ts, cpfp-service.ts, and rbf-service.ts, improving maintainability and reducing the risk of inconsistencies.

Type of Change

  • 🐛 Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • 📚 Documentation update
  • 🎨 UI/UX improvement
  • ♻️ Code refactoring (no functional changes)
  • ⚡ Performance improvement
  • 🔒 Security improvement
  • 🧪 Test coverage improvement
  • 🔧 Build/CI improvement

Changes Made

New Module: services/ecc-utils.ts

  • ECC Validation Functions

    • validateECCLibrary(): Basic ECC functionality validation (private key validation, point generation)
    • validateECCLibraryFull(): Comprehensive validation including signing and verification
    • initializeBitcoinJsWithECC(): Centralized bitcoinjs-lib initialization with ECC
  • Transaction Size Estimation

    • estimateTransactionSize(): Unified transaction size calculation for SegWit P2WPKH
    • Exported constants: P2WPKH_INPUT_SIZE, P2WPKH_OUTPUT_SIZE, TX_BASE_SIZE
  • Address Derivation Utilities

    • deriveAddressIndexAndChainFromAddress(): Find BIP32 path (index + chain) for an address
    • deriveAddressIndexFromAddress(): Legacy function for external chain only
    • findNextUnusedAddressIndex(): Determine next available address index
    • generateChangeAddress(): Generate change address (chain 1)
    • generateCancellationAddress(): Generate address for RBF cancellation
  • Cache Management

    • clearAddressIndexCache(): Clear cached address derivations
    • getAddressIndexCacheSize(): Query cache size for debugging

Updated Files

services/bitcoin-service.ts

  • Removed duplicate estimateTransactionSize() function
  • Removed duplicate generateChangeAddress() function
  • Removed ~110 lines of ECC validation code from createTransaction()
  • Replaced with imports from ecc-utils.ts
  • Simplified ECC initialization to use validateECCLibraryFull()

services/cpfp-service.ts

  • Removed duplicate estimateTransactionSize() function
  • Removed duplicate generateChangeAddress() function
  • Removed duplicate deriveAddressIndexAndChainFromAddress() function
  • Removed duplicate deriveAddressIndexFromAddress() function
  • Removed ~260 lines of duplicated code
  • Replaced with imports from ecc-utils.ts
  • Updated clearCPFPAddressIndexCache() to delegate to ecc-utils
  • Simplified ECC initialization in createCPFPTransaction()

Benefits

  1. DRY Principle: Eliminates ~370 lines of duplicated code
  2. Consistency: Single source of truth for ECC validation and address derivation
  3. Maintainability: Changes to ECC logic only need to be made in one place
  4. Testability: Utilities can now be unit tested independently
  5. Documentation: Comprehensive JSDoc comments explain each function's purpose and behavior
  6. Performance: Shared caching mechanism benefits all services

Testing Performed

Manual Testing

  • Verified transaction creation still works correctly
  • Verified CPFP transaction creation still works correctly
  • Verified address derivation produces consistent results

https://claude.ai/code/session_01ExDrbBrFwxPbWtZEumt1hD

Extract duplicated ECC validation, transaction size estimation, and
address derivation utilities into a new services/ecc-utils.ts module.

This consolidation:
- Creates ECCLibrary interface for type-safe ECC library handling
- Centralizes validateECCLibrary() and validateECCLibraryFull() functions
- Unifies estimateTransactionSize() with documented constants
- Consolidates address derivation utilities (deriveAddressIndexAndChainFromAddress,
  deriveAddressIndexFromAddress, findNextUnusedAddressIndex)
- Centralizes generateChangeAddress() and generateCancellationAddress()
- Provides single shared address index cache

The same validation and derivation logic was previously duplicated across
bitcoin-service.ts, rbf-service.ts, and cpfp-service.ts (~985 lines removed).

https://claude.ai/code/session_01ExDrbBrFwxPbWtZEumt1hD
@jamespepper81 jamespepper81 merged commit ad41d59 into dev Jan 28, 2026
3 checks passed
@jamespepper81 jamespepper81 deleted the claude/consolidate-ecc-validation-DDzUc branch January 28, 2026 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants