A Bech32-encoded Bitcoin address
| 60 | |
| 61 | |
| 62 | class CBech32BitcoinAddress(bitcoin.bech32.CBech32Data, CBitcoinAddress): |
| 63 | """A Bech32-encoded Bitcoin address""" |
| 64 | |
| 65 | @classmethod |
| 66 | def from_bytes(cls, witver, witprog): |
| 67 | |
| 68 | assert witver == 0 |
| 69 | self = super(CBech32BitcoinAddress, cls).from_bytes( |
| 70 | witver, |
| 71 | bytes(witprog) |
| 72 | ) |
| 73 | |
| 74 | if len(self) == 32: |
| 75 | self.__class__ = P2WSHBitcoinAddress |
| 76 | elif len(self) == 20: |
| 77 | self.__class__ = P2WPKHBitcoinAddress |
| 78 | else: |
| 79 | raise CBitcoinAddressError('witness program does not match any known segwit address format') |
| 80 | |
| 81 | return self |
| 82 | |
| 83 | @classmethod |
| 84 | def from_scriptPubKey(cls, scriptPubKey): |
| 85 | """Convert a scriptPubKey to a CBech32BitcoinAddress |
| 86 | |
| 87 | Returns a CBech32BitcoinAddress subclass, either P2WSHBitcoinAddress or |
| 88 | P2WPKHBitcoinAddress. If the scriptPubKey is not recognized |
| 89 | CBitcoinAddressError will be raised. |
| 90 | """ |
| 91 | try: |
| 92 | return P2WSHBitcoinAddress.from_scriptPubKey(scriptPubKey) |
| 93 | except CBitcoinAddressError: |
| 94 | pass |
| 95 | |
| 96 | try: |
| 97 | return P2WPKHBitcoinAddress.from_scriptPubKey(scriptPubKey) |
| 98 | except CBitcoinAddressError: |
| 99 | pass |
| 100 | |
| 101 | raise CBitcoinAddressError('scriptPubKey not a valid bech32-encoded address') |
| 102 | |
| 103 | |
| 104 | class CBase58BitcoinAddress(bitcoin.base58.CBase58Data, CBitcoinAddress): |