| 25 | |
| 26 | |
| 27 | class CBitcoinAddress(object): |
| 28 | |
| 29 | def __new__(cls, s): |
| 30 | try: |
| 31 | return CBech32BitcoinAddress(s) |
| 32 | except bitcoin.bech32.Bech32Error: |
| 33 | pass |
| 34 | |
| 35 | try: |
| 36 | return CBase58BitcoinAddress(s) |
| 37 | except bitcoin.base58.Base58Error: |
| 38 | pass |
| 39 | |
| 40 | raise CBitcoinAddressError('Unrecognized encoding for bitcoin address') |
| 41 | |
| 42 | @classmethod |
| 43 | def from_scriptPubKey(cls, scriptPubKey): |
| 44 | """Convert a scriptPubKey to a subclass of CBitcoinAddress""" |
| 45 | try: |
| 46 | return CBech32BitcoinAddress.from_scriptPubKey(scriptPubKey) |
| 47 | except CBitcoinAddressError: |
| 48 | pass |
| 49 | |
| 50 | try: |
| 51 | return CBase58BitcoinAddress.from_scriptPubKey(scriptPubKey) |
| 52 | except CBitcoinAddressError: |
| 53 | pass |
| 54 | |
| 55 | raise CBitcoinAddressError('scriptPubKey is not in a recognized address format') |
| 56 | |
| 57 | |
| 58 | class CBitcoinAddressError(Exception): |
no outgoing calls