A Base58-encoded Bitcoin address
| 102 | |
| 103 | |
| 104 | class CBase58BitcoinAddress(bitcoin.base58.CBase58Data, CBitcoinAddress): |
| 105 | """A Base58-encoded Bitcoin address""" |
| 106 | |
| 107 | @classmethod |
| 108 | def from_bytes(cls, data, nVersion): |
| 109 | self = super(CBase58BitcoinAddress, cls).from_bytes(data, nVersion) |
| 110 | |
| 111 | if nVersion == bitcoin.params.BASE58_PREFIXES['SCRIPT_ADDR']: |
| 112 | self.__class__ = P2SHBitcoinAddress |
| 113 | |
| 114 | elif nVersion == bitcoin.params.BASE58_PREFIXES['PUBKEY_ADDR']: |
| 115 | self.__class__ = P2PKHBitcoinAddress |
| 116 | |
| 117 | else: |
| 118 | raise CBitcoinAddressError('Version %d not a recognized Bitcoin Address' % nVersion) |
| 119 | |
| 120 | return self |
| 121 | |
| 122 | @classmethod |
| 123 | def from_scriptPubKey(cls, scriptPubKey): |
| 124 | """Convert a scriptPubKey to a CBitcoinAddress |
| 125 | |
| 126 | Returns a CBitcoinAddress subclass, either P2SHBitcoinAddress or |
| 127 | P2PKHBitcoinAddress. If the scriptPubKey is not recognized |
| 128 | CBitcoinAddressError will be raised. |
| 129 | """ |
| 130 | try: |
| 131 | return P2SHBitcoinAddress.from_scriptPubKey(scriptPubKey) |
| 132 | except CBitcoinAddressError: |
| 133 | pass |
| 134 | |
| 135 | try: |
| 136 | return P2PKHBitcoinAddress.from_scriptPubKey(scriptPubKey) |
| 137 | except CBitcoinAddressError: |
| 138 | pass |
| 139 | |
| 140 | raise CBitcoinAddressError('scriptPubKey not a valid base58-encoded address') |
| 141 | |
| 142 | |
| 143 | class P2SHBitcoinAddress(CBase58BitcoinAddress): |