Skip to content

Refactoring and Code Reuse#522

Merged
jamespepper81 merged 5 commits into
mainfrom
dev
Jan 28, 2026
Merged

Refactoring and Code Reuse#522
jamespepper81 merged 5 commits into
mainfrom
dev

Conversation

@jamespepper81
Copy link
Copy Markdown
Contributor

This pull request refactors Bitcoin and CPFP service modules to centralize cryptographic and address utility functions into a shared ecc-utils module, improving code maintainability and reducing duplication. It also enhances Bitcoin address validation logic to be more robust and accurate, and streamlines ECC library validation and initialization.

The most important changes are:

Refactoring and Code Reuse:

  • Moved utility functions such as estimateTransactionSize, generateChangeAddress, and deriveAddressIndexAndChainFromAddress from bitcoin-service.ts and cpfp-service.ts into a shared ecc-utils module, and updated imports accordingly. This reduces code duplication and centralizes cryptographic/address logic. [1] [2] [3] [4]

  • Removed local cache and cache-clearing logic from cpfp-service.ts, instead re-exporting cache clearing from ecc-utils for backward compatibility.

Validation and Initialization Improvements:

  • Replaced verbose, duplicated ECC library validation logic in both services with a call to a new shared validateECCLibraryFull function, simplifying and unifying the validation process. [1] [2] [3] [4]

Bitcoin Address Validation:

  • Improved isValidBitcoinAddress to use bitcoinjs-lib for checksum and format validation, supporting all major Bitcoin address types (P2WPKH, P2WSH, P2TR, P2PKH, P2SH) with proper error handling.

BIP32 Derivation Consistency:

  • Fixed BIP32 path derivation in transaction signing to use derivePath instead of derive, ensuring compatibility and correctness.

jamespepper81 and others added 5 commits January 27, 2026 23:56
Updated checksums for multiple pods in Podfile.lock, reflecting changes in dependency versions or sources. No source code changes included.
…tion

Critical security fix for address validation:
- Replace basic prefix/length checks with proper cryptographic validation
- Validate Bech32 checksums for bc1 addresses (P2WPKH, P2WSH, P2TR)
- Validate Base58Check checksums for legacy (P2PKH) and P2SH addresses
- Add support for Taproot (P2TR/bc1p) address validation
- Fix derivePath vs derive method usage for BIP32 path derivation

This prevents users from accidentally sending funds to malformed addresses
that passed the previous basic validation.

https://claude.ai/code/session_01VfafSJryJaC3SeCBcTi4ES
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
…ation-DDzUc

Refactor: Extract shared ECC utilities into dedicated module
Comment thread services/rbf-service.ts
Comment on lines +10 to +19
import {
validateECCLibrary,
validateECCLibraryFull,
estimateTransactionSize,
deriveAddressIndexAndChainFromAddress,
deriveAddressIndexFromAddress,
findNextUnusedAddressIndex,
generateCancellationAddress,
clearAddressIndexCache,
} from './ecc-utils';
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@claude can you check these are not required
validateECCLibrary
deriveAddressIndexFromAddress
findNextUnusedAddressIndex

@jamespepper81 jamespepper81 merged commit eef33ac into main Jan 28, 2026
13 checks passed
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