Title: Get the P2WPKH(Bech32) using the Public Key Hash (Hash 160) Python Post by: seoincorporation on August 22, 2022, 08:13:39 PM Hello guys. I have been reading and learning a lot about Bitcoin address, but i get stuck when i try to create a Bech32 address from the Public Key Hash (Hash 160) of my Base58 address.
Technical Discussion: Getting the public hash from my Base58 address: Code: >>> import binascii, hashlib, base58 Now, getting my public hash from my Bech32 address: Code: >>> import bech32 Now, the problem is that i can't find the way to generate the address starting from the Script Hash for Bech32 address. I know there is a bech32.encode command, but i wasn't able to make it work and there is not much information about it. This process is simple for Base58: Code: >>> base58.b58encode_check(binascii.unhexlify('00'+"7773d807892cb200e6a4785428294452c9e3b4b9")).decode() But how can i do that for Bench32? ---------------- UPDATE ---------------- I found the way, thanks for the support guys. Code: >>> import bech32 Title: Re: Get the P2WPKH(Bech32) using the Script Hash. (Python) Post by: achow101 on August 22, 2022, 11:59:06 PM It is not possible to go from script hash to witness public key has (wpkh). Scripts and pubkeys are different things.
However I don't think that's what you are trying to do. What do you mean by "script hash" in this case? Title: Re: Get the P2WPKH(Bech32) using the Public Key Hash (Hash 160) (Python) Post by: seoincorporation on August 23, 2022, 01:08:35 AM It is not possible to go from script hash to witness public key has (wpkh). Scripts and pubkeys are different things. However I don't think that's what you are trying to do. What do you mean by "script hash" in this case? I just edit the tittle achow101, you was right "script hash" wasn't clear, the right term is Public Key Hash (Hash 160). What i mean, if we go to this site, we can see the publick key hash of any address: https://privatekeys.pw/address/bitcoin/1BtcBoSSnqe8mFJCUEyCNmo3EcF8Yzhpnc And we can use python to get that publick key hash and i explain in the first post. The way to get the Base58 (P2PKH) from the Public Key Hash (Hash 160) simple, for example: Code: >>>base58.b58encode_check(binascii.unhexlify('00'+"7773d807892cb200e6a4785428294452c9e3b4b9")).decode() I'm looking for a python way to get the P2WPKH(Bech32) address from the Public Key Hash (Hash 160). I know with that same Public Key Hash (Hash 160): 7773d807892cb200e6a4785428294452c9e3b4b9 we have this P2WPKH address:bc1qwaeaspuf9jeqpe4y0p2zs22y2ty78d9e6nvjgf... And that's what i want to get with python. Title: Re: Get the P2WPKH(Bech32) using the Public Key Hash (Hash 160) Python Post by: pooya87 on August 23, 2022, 03:56:24 AM It's pretty much the same, you have to find a python bech32 library and then call its bech32_encode method instead of the b58encode_check method and pass 3 things to that method: hrp (=bc for mainnet), witness version (=0 for P2WPKH) and the 160 bit hash (no prepended 0x00 though).
This is how Electrum does it and you can use this code since it is tested and safe: https://github.com/spesmilo/electrum/blob/6650e6bbae12a79e12667857ee039f1b1f30c7e3/electrum/segwit_addr.py#L82 Title: Re: Get the P2WPKH(Bech32) using the Public Key Hash (Hash 160) Python Post by: BlackHatCoiner on August 23, 2022, 11:33:00 AM So, we have this Public Key Hash:
Code: 7773D807892CB200E6A4785428294452C9E3B4B9 And we want to make it a Witness Public Key Hash. Bech32 encoding converts this to a 5-bit unsigned int array: Code: 0E1D191D10011C0905121900011915040F010A02100A0A040A0B041E070D0519 To do this, I wrote some code, just for exercise. It's in C#: Code: public string squashBits(string scriptHash) { To explain the above, your 160 bits script hash is an 4-bit array of unsigned integers. Code: 0111 0111 0111 0011 1101 1000 0000 0111 1000 1001 0010 1100 1011 0010 0000 0000 1110 0110 1010 0100 0111 1000 0101 0100 0010 1000 0010 1001 0100 0100 0101 0010 1100 1001 1110 0011 1011 0100 1011 1001 To convert it to an array of 5-bit integers, you need to "squash" the bits, like that: Code: 01110 11101 11001 11101 10000 00001 11100 01001 00101 10010 11001 00000 00001 11001 10101 00100 01111 00001 01010 00010 10000 01010 01010 00100 01010 01011 00100 11110 00111 01101 00101 11001 Resulting in "0E1D191D10011C0905121900011915040F010A02100A0A040A0B041E070D0519". To continue further, you need to add the witness byte in front (0x00): Code: 000E1D191D10011C0905121900011915040F010A02100A0A040A0B041E070D0519 And compute the checksum of the above, which is specified here: https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki. Besides Electrum, it's also written in Python by Pieter Wuille: https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py. Later, it's a matter of bech32 encoding. Title: Re: Get the P2WPKH(Bech32) using the Public Key Hash (Hash 160) Python Post by: PawGo on August 23, 2022, 01:26:13 PM Hello guys. I have beer reading and learning a lot about Bitcoin address Do not drink when you code ;) But I understand you were looking for Ballmer peak: https://xkcd.com/323/ Now, the problem is that i can't find the way to generate the address starting from the Script Hash for Bech32 address. I know there is a bech32.encode command, but i wasn't able to make it work and there is not much information about it. Which library do you use? Have you seen that example? https://github.com/bitcoinjs/bech32 You may test encoding/decoding live here: https://slowli.github.io/bech32-buffer/ Title: Re: Get the P2WPKH(Bech32) using the Public Key Hash (Hash 160) Python Post by: seoincorporation on August 23, 2022, 02:49:27 PM Thanks for your replies guys and sorry for that beer typo :P
Yesterday i find this example of encode: https://bitcoin.stackexchange.com/questions/91748/how-to-use-python-reference-for-encoding-a-bech32-address Code: import bech32 But sadly it prints and error if i try with my Hash160... And is fun to see how there is only one example of that encode command. And after following the white rabbit i found this book: https://books.google.com.mx/books?id=_H52EAAAQBAJ&pg=PT403#v=onepage&q&f=false And when it try with the book example, it worked fine.. here is the code: Code: >>> import bech32 |