-
Notifications
You must be signed in to change notification settings - Fork 148
Expand file tree
/
Copy pathaddress.py
More file actions
367 lines (305 loc) · 11.9 KB
/
address.py
File metadata and controls
367 lines (305 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# Copyright (C) 2018-2025 The python-bitcoin-utils developers
#
# This file is part of python-bitcoin-utils
#
# It is subject to the license terms in the LICENSE file found in the top-level
# directory of this distribution.
#
# No part of python-bitcoin-utils, including this file, may be copied, modified,
# propagated, or distributed except according to the terms contained in the
# LICENSE file.
from typing import Optional, Union, Literal, Type, Any
from bitcoinutils.constants import (
P2PKH_ADDRESS, P2SH_ADDRESS, P2WPKH_ADDRESS_V0, P2WSH_ADDRESS_V0, P2TR_ADDRESS_V1
)
from bitcoinutils.keys import (
Address, P2pkhAddress, P2shAddress, SegwitAddress, P2wpkhAddress, P2wshAddress, P2trAddress
)
from bitcoinutils.script import Script
AddressType = Literal[
"p2pkh", "p2sh", "p2wpkhv0", "p2wshv0", "p2trv1"
]
AddressClass = Union[
Type[P2pkhAddress], Type[P2shAddress], Type[P2wpkhAddress], Type[P2wshAddress], Type[P2trAddress]
]
AddressInstance = Union[
P2pkhAddress, P2shAddress, P2wpkhAddress, P2wshAddress, P2trAddress
]
class UnifiedAddress:
"""Unified Bitcoin address class that can handle all address types and conversions.
This class wraps the existing address classes and provides conversion methods
between different address types.
Attributes
----------
address : AddressInstance
The wrapped address object
address_type : str
The type of address (p2pkh, p2sh, p2wpkhv0, p2wshv0, p2trv1)
Methods
-------
from_address(address_str, address_type=None)
Creates a UnifiedAddress from an address string (classmethod)
from_script(script, address_type="p2sh")
Creates a UnifiedAddress from a script (classmethod)
from_hash160(hash_str, address_type="p2pkh")
Creates a UnifiedAddress from a hash160 string (classmethod)
from_witness_program(witness_program, address_type="p2wpkhv0")
Creates a UnifiedAddress from a witness program (classmethod)
to_script_pub_key()
Returns the script pubkey for the address
to_address_type(address_type)
Converts the address to a different type if possible
to_string()
Returns the address as a string
"""
def __init__(self, address: AddressInstance):
"""Initialize with an existing address object.
Parameters
----------
address : AddressInstance
An instance of one of the address classes
"""
self.address = address
self.address_type = address.get_type()
@classmethod
def from_address(cls, address_str: str, address_type: Optional[AddressType] = None) -> 'UnifiedAddress':
"""Create a UnifiedAddress from an address string.
Parameters
----------
address_str : str
The address string
address_type : str, optional
The type of address if known (otherwise will be auto-detected)
Returns
-------
UnifiedAddress
A new UnifiedAddress object
Raises
------
ValueError
If the address is invalid
"""
# Auto-detect address type if not provided
if address_type is None:
address_type = cls._detect_address_type(address_str)
# Create address object based on type
if address_type == P2PKH_ADDRESS:
address = P2pkhAddress(address=address_str)
elif address_type == P2SH_ADDRESS:
address = P2shAddress(address=address_str)
elif address_type == P2WPKH_ADDRESS_V0:
address = P2wpkhAddress(address=address_str)
elif address_type == P2WSH_ADDRESS_V0:
address = P2wshAddress(address=address_str)
elif address_type == P2TR_ADDRESS_V1:
address = P2trAddress(address=address_str)
else:
raise ValueError(f"Unsupported address type: {address_type}")
return cls(address)
@classmethod
def from_script(cls, script: Script, address_type: AddressType = P2SH_ADDRESS) -> 'UnifiedAddress':
"""Create a UnifiedAddress from a script.
Parameters
----------
script : Script
The script
address_type : str, optional
The type of address to create (default is P2SH)
Returns
-------
UnifiedAddress
A new UnifiedAddress object
Raises
------
ValueError
If the address type is not supported for scripts
"""
if address_type == P2SH_ADDRESS:
address = P2shAddress(script=script)
elif address_type == P2WSH_ADDRESS_V0:
address = P2wshAddress(script=script)
else:
raise ValueError(f"Cannot create {address_type} directly from script")
return cls(address)
@classmethod
def from_hash160(cls, hash_str: str, address_type: AddressType = P2PKH_ADDRESS) -> 'UnifiedAddress':
"""Create a UnifiedAddress from a hash160 string.
Parameters
----------
hash_str : str
The hash160 hex string
address_type : str, optional
The type of address to create (default is P2PKH)
Returns
-------
UnifiedAddress
A new UnifiedAddress object
Raises
------
ValueError
If the address type is not supported for hash160
"""
if address_type == P2PKH_ADDRESS:
address = P2pkhAddress(hash160=hash_str)
elif address_type == P2SH_ADDRESS:
address = P2shAddress(hash160=hash_str)
else:
raise ValueError(f"Cannot create {address_type} directly from hash160")
return cls(address)
@classmethod
def from_witness_program(cls, witness_program: str, address_type: AddressType = P2WPKH_ADDRESS_V0) -> 'UnifiedAddress':
"""Create a UnifiedAddress from a witness program.
Parameters
----------
witness_program : str
The witness program hex string
address_type : str, optional
The type of address to create (default is P2WPKH)
Returns
-------
UnifiedAddress
A new UnifiedAddress object
Raises
------
ValueError
If the address type is not supported for witness program
"""
if address_type == P2WPKH_ADDRESS_V0:
address = P2wpkhAddress(witness_program=witness_program)
elif address_type == P2WSH_ADDRESS_V0:
address = P2wshAddress(witness_program=witness_program)
elif address_type == P2TR_ADDRESS_V1:
address = P2trAddress(witness_program=witness_program)
else:
raise ValueError(f"Cannot create {address_type} from witness program")
return cls(address)
@staticmethod
def _detect_address_type(address_str: str) -> AddressType:
"""Detect the address type from an address string.
Parameters
----------
address_str : str
The address string
Returns
-------
str
The detected address type
Raises
------
ValueError
If the address type cannot be detected
"""
# Try each address type until one works
try:
# Try P2PKH
P2pkhAddress(address=address_str)
return P2PKH_ADDRESS
except ValueError:
pass
try:
# Try P2SH
P2shAddress(address=address_str)
return P2SH_ADDRESS
except ValueError:
pass
try:
# Try P2WPKH
P2wpkhAddress(address=address_str)
return P2WPKH_ADDRESS_V0
except (ValueError, TypeError):
pass
try:
# Try P2WSH
P2wshAddress(address=address_str)
return P2WSH_ADDRESS_V0
except (ValueError, TypeError):
pass
try:
# Try P2TR
P2trAddress(address=address_str)
return P2TR_ADDRESS_V1
except (ValueError, TypeError):
pass
raise ValueError(f"Could not detect address type for {address_str}")
def to_script_pub_key(self) -> Script:
"""Get the scriptPubKey for this address.
Returns
-------
Script
The scriptPubKey for this address
"""
return self.address.to_script_pub_key()
def to_address_type(self, address_type: AddressType) -> 'UnifiedAddress':
"""Convert the address to a different type if possible.
Parameters
----------
address_type : str
The target address type
Returns
-------
UnifiedAddress
A new UnifiedAddress object of the requested type
Raises
------
ValueError
If conversion to the requested type is not possible
"""
# If already the requested type, return self
if self.address_type == address_type:
return self
# P2PKH -> P2WPKH, P2SH-P2WPKH conversions
if self.address_type == P2PKH_ADDRESS:
# Extract the hash160
hash160 = self.address.to_hash160()
if address_type == P2WPKH_ADDRESS_V0:
# P2PKH -> P2WPKH
address = P2wpkhAddress(witness_program=hash160)
return UnifiedAddress(address)
elif address_type == P2SH_ADDRESS:
# P2PKH -> P2SH-P2WPKH (nested SegWit)
# Create P2WPKH scriptPubKey
p2wpkh_script = Script(['OP_0', hash160])
# Create P2SH address from that script
address = P2shAddress(script=p2wpkh_script)
return UnifiedAddress(address)
# P2SH -> P2WSH (only if it's a nested SegWit)
# This is a limited case and generally requires knowing the redeem script
# P2WPKH -> P2PKH, P2SH-P2WPKH
if self.address_type == P2WPKH_ADDRESS_V0:
# Extract the witness program
witness_program = self.address.to_witness_program()
if address_type == P2PKH_ADDRESS:
# P2WPKH -> P2PKH
address = P2pkhAddress(hash160=witness_program)
return UnifiedAddress(address)
elif address_type == P2SH_ADDRESS:
# P2WPKH -> P2SH-P2WPKH (nested SegWit)
p2wpkh_script = Script(['OP_0', witness_program])
address = P2shAddress(script=p2wpkh_script)
return UnifiedAddress(address)
# P2WSH -> P2SH-P2WSH
if self.address_type == P2WSH_ADDRESS_V0 and address_type == P2SH_ADDRESS:
witness_program = self.address.to_witness_program()
p2wsh_script = Script(['OP_0', witness_program])
address = P2shAddress(script=p2wsh_script)
return UnifiedAddress(address)
# No other direct conversions are possible without additional data
raise ValueError(f"Cannot convert from {self.address_type} to {address_type}")
def to_string(self) -> str:
"""Get the address as a string.
Returns
-------
str
The address string
"""
return self.address.to_string()
def __str__(self) -> str:
return self.to_string()
def __repr__(self) -> str:
return f"UnifiedAddress('{self.to_string()}', '{self.address_type}')"
def __eq__(self, other: Any) -> bool:
if isinstance(other, UnifiedAddress):
return self.to_string() == other.to_string()
elif isinstance(other, str):
return self.to_string() == other
return False