Refactor: Extract shared ECC utilities into dedicated module#521
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 acrossbitcoin-service.ts,cpfp-service.ts, andrbf-service.ts, improving maintainability and reducing the risk of inconsistencies.Type of Change
Changes Made
New Module:
services/ecc-utils.tsECC Validation Functions
validateECCLibrary(): Basic ECC functionality validation (private key validation, point generation)validateECCLibraryFull(): Comprehensive validation including signing and verificationinitializeBitcoinJsWithECC(): Centralized bitcoinjs-lib initialization with ECCTransaction Size Estimation
estimateTransactionSize(): Unified transaction size calculation for SegWit P2WPKHP2WPKH_INPUT_SIZE,P2WPKH_OUTPUT_SIZE,TX_BASE_SIZEAddress Derivation Utilities
deriveAddressIndexAndChainFromAddress(): Find BIP32 path (index + chain) for an addressderiveAddressIndexFromAddress(): Legacy function for external chain onlyfindNextUnusedAddressIndex(): Determine next available address indexgenerateChangeAddress(): Generate change address (chain 1)generateCancellationAddress(): Generate address for RBF cancellationCache Management
clearAddressIndexCache(): Clear cached address derivationsgetAddressIndexCacheSize(): Query cache size for debuggingUpdated Files
services/bitcoin-service.tsestimateTransactionSize()functiongenerateChangeAddress()functioncreateTransaction()ecc-utils.tsvalidateECCLibraryFull()services/cpfp-service.tsestimateTransactionSize()functiongenerateChangeAddress()functionderiveAddressIndexAndChainFromAddress()functionderiveAddressIndexFromAddress()functionecc-utils.tsclearCPFPAddressIndexCache()to delegate toecc-utilscreateCPFPTransaction()Benefits
Testing Performed
Manual Testing
https://claude.ai/code/session_01ExDrbBrFwxPbWtZEumt1hD