test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bitcoin.base58 | |
import bitcoin.bech32 | |
""" | |
Converts a base58 bitcoin address into a 21 byte bytes object | |
""" | |
address = "bc1qqmpt7u5e9hfznljta5gnvhyvfd2kdd0r90hwue" | |
try: | |
bech32 = bitcoin.bech32.CBech32Data(address) | |
witver = (0x80 + bech32.witver).to_bytes(1, byteorder='big') # mark the first byte for segwit | |
witprog = bech32.to_bytes() | |
if not (0 <= bech32.witver <= 16): | |
raise Exception('impossible witness version') | |
print(len(witprog)) | |
if len(witprog) == 20: | |
print("p2wpkh") | |
print(bech32.witver) | |
print(witprog) | |
elif len(witprog) == 32: | |
print("p2wsh") | |
print(bech32.witver) | |
print(witprog) | |
raise Exception('p2wsh still not supported for sending') | |
else: | |
raise Exception('unexpected length for segwit') | |
except Exception as ne: | |
try: | |
short_address_bytes = bitcoin.base58.decode(address)[:-4] | |
print("base58") | |
print(short_address_bytes) | |
except bitcoin.base58.InvalidBase58Error as e: | |
raise e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment