Title: How to convert bech32 <> hash160 in python? Post by: samueljj on March 27, 2022, 10:15:58 PM like this script:
Code: import base58 Title: Re: How to convert bech32 address to hash160 in python? Post by: BitMaxz on March 27, 2022, 11:05:36 PM I search a bit and found a python code that can convert bech32 to hash160 I don't if this is the exact script you looking for but read this
Quote from: https://bitcoin.stackexchange.com/questions/90644/most-efficient-way-to-store-segwit-address-in-a-database Quote from: Question Is there a way to get to the hash160 address from bc1q34aq5drpuwy3wgl9lhup9892qp6svr8ldzyy7c Yes, you could use the reference implementations in various languages to encode and decode a bech32 address. For example, I have decoded the sample address you mentioned in the question, bc1q34aq5drpuwy3wgl9lhup9892qp6svr8ldzyy7c, using python below. The decode function gives us the witness version (0 in this case) and a byte array. import bech32 address = "bc1q34aq5drpuwy3wgl9lhup9892qp6svr8ldzyy7c" address_decoded = bech32.decode("bc", address) address_decoded # a tuple containing witness version and bytearray of the address >>> (0, [141, 122, 10, 52, 97, 227, 137, 23, 35, 229, 253, 248, 18, 156, 170, 0, 117, 6, 12, 255]) bytes(address_decoded[1]) >>> b'\x8dz\n4a\xe3\x89\x17#\xe5\xfd\xf8\x12\x9c\xaa\x00u\x06\x0c\xff' Also, check this Github link below and might be useful on converting bech32 - https://github.com/petertodd/python-bitcoinlib/blob/master/bitcoin/wallet.py Title: Re: How to convert bech32 address to hash160 in python? Post by: samueljj on March 28, 2022, 03:36:49 AM I search a bit and found a python code that can convert bech32 to hash160 I don't if this is the exact script you looking for but read this Quote from: https://bitcoin.stackexchange.com/questions/90644/most-efficient-way-to-store-segwit-address-in-a-database Quote from: Question Is there a way to get to the hash160 address from bc1q34aq5drpuwy3wgl9lhup9892qp6svr8ldzyy7c Code: import bech32 Title: Re: How to convert bech32 <> hash160 in python? Post by: NotATether on March 29, 2022, 06:33:50 AM Use Peter Todd's python-bitcoinlib (https://github.com/petertodd/python-bitcoinlib/blob/6980e177c2fc7b09b37e6361caae4c76701ac8cf/bitcoin/segwit_addr.py) on Github which has python functions for encoding and decoding segwit addresses.
Note that these functions take a witness program and not a script as an argument. Normally a witness version is just 0x00 followed by the Hash160, but the encode function has a dedicated parameter for the witness version so you just have to specify the Hash160 inside the witness program. Title: Re: How to convert bech32 <> hash160 in python? Post by: pooya87 on March 29, 2022, 07:25:02 AM Normally a witness version is just 0x00 followed by the Hash160 To be complete, so far we have defined the following witness programs and versions:version 0 followed by hash160 aka P2WPKH version 0 followed by hash256 aka P2WSH version 1 followed by tweaked pubkey (256 bit) aka P2TR It is also worth noting that version 0 followed by any other size with make the script invalid whereas other versions including 1 can be followed by other program sizes which makes them non-standard. |