Skip to content

Instantly share code, notes, and snippets.

@wakiyamap
Created October 28, 2020 04:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wakiyamap/6ffc694d50df5262bb39efae658e2348 to your computer and use it in GitHub Desktop.
Save wakiyamap/6ffc694d50df5262bb39efae658e2348 to your computer and use it in GitHub Desktop.
test
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