| 302 | |
| 303 | |
| 304 | class P2WPKHBitcoinAddress(CBech32BitcoinAddress): |
| 305 | |
| 306 | @classmethod |
| 307 | def from_scriptPubKey(cls, scriptPubKey): |
| 308 | """Convert a scriptPubKey to a P2WPKH address |
| 309 | |
| 310 | Raises CBitcoinAddressError if the scriptPubKey isn't of the correct |
| 311 | form. |
| 312 | """ |
| 313 | if scriptPubKey.is_witness_v0_keyhash(): |
| 314 | return cls.from_bytes(0, scriptPubKey[2:22]) |
| 315 | else: |
| 316 | raise CBitcoinAddressError('not a P2WPKH scriptPubKey') |
| 317 | |
| 318 | def to_scriptPubKey(self): |
| 319 | """Convert an address to a scriptPubKey""" |
| 320 | assert self.witver == 0 |
| 321 | return script.CScript([0, self]) |
| 322 | |
| 323 | def to_redeemScript(self): |
| 324 | return script.CScript([script.OP_DUP, script.OP_HASH160, self, script.OP_EQUALVERIFY, script.OP_CHECKSIG]) |
| 325 | |
| 326 | class CKey(object): |
| 327 | """An encapsulated private key |