Have fun converting base58 and bech32 addresses to hash160, python style:
import segwit_addr
import base58
import argparse
import sys
import hashlib
import binascii
parser = argparse.ArgumentParser()
parser.add_argument('address', help='Enter bech32 address')
args = parser.parse_args()
address = sys.argv[1]
def validate_base58(a):
base58Decoder = base58.b58decode(a).hex()
prefixAndHash = base58Decoder[:len(base58Decoder)-8]
checksum = base58Decoder[len(base58Decoder)-8:]
hash = prefixAndHash
for x in range(1,3):
hash = hashlib.sha256(binascii.unhexlify(hash)).hexdigest()
#print("Hash#", x, " : ", hash)
#print("--------------------------------------")
if(checksum == hash[:8]):
return True
else:
return False
if validate_base58(address) is True:
base58_decoded = base58.b58decode_check(address).hex()[2:]
print (base58_decoded)
else:
decoded = segwit_addr.decode('bc', address)
if decoded[0] == 0:
#print(decoded[2])
hash160_bin = []
for x in decoded[1]:
hash160_bin.append(f'{x:08b}')
hash160_hex = []
for x in hash160_bin:
hash160_hex.append(f'{int(x, 2):X}')
print(''.join(hash160_hex))
and running it:
# python3 segwit_addr_decode.py 1P5ZEDWTKTFGxQjZphgWPQUpe554WKDfHQ
f22f5563839ba6ba5aa8d3726fcbc675cb3e4c9e
# python3 segwit_addr_decode.py 34xp4vRoCGJym3xR7yCVPFHoCNxv4Twseo
23e522dfc6656a8fda3d47b4fa53f7585ac758cd
# python3 segwit_addr_decode.py bc1qxv55wuzz4qsfgss3uq2zwg5y88d7qv5hg67d2d
3329477042A820944211E01427228439DBE03297
useful information on bech32 encoding: https://en.bitcoin.it/wiki/Bech32
test segwit address: https://bitcoin.sipa.be/bech32/demo/demo.html
test base58 encoded address: http://lenschulwitz.com/base58