python-bitcoin-utils icon indicating copy to clipboard operation
python-bitcoin-utils copied to clipboard

TODO : Implement Unified Address Object for Multi-Type Address Support

Open JAGADISHSUNILPEDNEKAR opened this issue 10 months ago • 2 comments

Unified Address Object Implementation

This PR introduces a new UnifiedAddress class that provides a unified way to handle all Bitcoin address types (P2PKH, P2SH, P2WPKH, P2WSH, P2TR) and enables conversion between compatible address types.

Changes:

  • Created a new address.py module with the UnifiedAddress class
  • Added support for detecting address types automatically
  • Implemented conversion logic between compatible address formats
  • Added functionality to create addresses from various sources (strings, hashes, scripts, witness programs)
  • Added comprehensive tests for all address functionality

Key Features:

  • Simplified API for handling different address types uniformly
  • Easy conversion between address formats (e.g., P2PKH → P2WPKH)
  • Support for nested SegWit addresses (P2SH-P2WPKH, P2SH-P2WSH)
  • Complete type-hinting for better development experience
  • Comprehensive validation of address formats and conversions

Example Usage:

# Create from existing addresses
unified = UnifiedAddress(p2pkh_address)

# Convert between address types
p2wpkh = unified.to_address_type(P2WPKH_ADDRESS_V0)

# Create from address string with auto-detection
address = UnifiedAddress.from_address("tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx")

All the tests pass Screenshot 2025-03-30 at 12 59 05 AM

JAGADISHSUNILPEDNEKAR avatar Apr 02 '25 07:04 JAGADISHSUNILPEDNEKAR

HI @karask Just checking on this PR

JAGADISHSUNILPEDNEKAR avatar Apr 05 '25 12:04 JAGADISHSUNILPEDNEKAR

Not sure that a unified object is required. The TODO mentions that but it was meant to generalise the existing Address object and see how to clean it up. I feel like this change adds more complexity for the user.

I will keep it here and re-examine it later. Thanks for the contribution.

karask avatar May 08 '25 08:05 karask