Author

Topic: Base58 (P2SH) to Bech32 (P2WPKH) Converter (Read 351 times)

legendary
Activity: 3444
Merit: 10558
February 20, 2021, 12:08:12 AM
#8
I think you need the public key as you are once told using the hash160 public key which is also the
You don't need the public key if you already have the HASH160 digest of it and the hash found in a base58 address is the hash digest created from the public key and used in a bech32 address.
The real question which has me curious is why OP is trying to convert addresses?
hero member
Activity: 2590
Merit: 650
Want top-notch marketing for your project, Hire me
February 19, 2021, 03:21:32 PM
#7
This raises another question for me. Just how to get Bitcoin Addresses in (P2WPKH) format from ripemd160 hash?
I am not familiar with the Bech32 package. Are there any examples of generating [ripemd160 hash> Addresses]

I think you need the public key as you are once told using the hash160 public key which is also the ripemd160(sha256(publickey)) and add 0 Uint8 to the output of bech32 words.
You are going to use bech32 encode it with the prefix (bc) for bitcoin.
As it was illustrated on https://stackoverflow.com/questions/63670096/how-to-generate-bech32-address-from-the-public-key-bitcoin
legendary
Activity: 1568
Merit: 6660
bitcoincleanup.com / bitmixlist.org
February 19, 2021, 03:12:35 PM
#6

Just how to get Bitcoin Addresses in (P2WPKH) format from ripemd160 hash?

You need the public key in order to generate P2PKH or P2WPKH. So if the address is spent from, or signed with, then you can generate the corresponding P2WPKH.

No public key - no way.

He's trying to get the bech32 address from a ripemd160 hash not the whole script. It's definitely possible, because the address is just an encoding of the hash into bech32 format.

It is also possible to get the base58 address from a ripemd160 (otherwise we wouldn't be able to generate addresses!).

For base58 we just put 0x00 at the beginning of the ripemd160 hash to signal that it's a mainnet address. We copy this byte array and then it's hashed with SHA256(SHA256(x)) , so a double hash. The last 4 bytes of that are appended to the ripemd160 as a checksum. Then it's encoded in base58. So we have:

1 byte network indicator (0x00 mainnet)
20 bytes ripemd160 hash
4 bytes sha256(sha256([network indicator + ripemd160]))

Code:
import hashlib
import base58

# (h is hashlib ripemd160 hash object)

# Add 00 for 'main network'
rh = bytearray("\x00", "ascii") + h.digest()

# Hash it twice
eh = hashlib.sha256(rh)
eh2 = hashlib.sha256(eh.digest())

# Last 4 bytes of the double hashed RIPEMD-160 hash (incl. prefix) are the checksum
checksum = eh2.digest()[:4]

# Add checksum to RIPEMD160 extended hash
rh += checksum

address = base58.b58encode(bytes(rh))

bech32 is slightly more complicated. For that we not only need the ripemd160 hash, we also need the witness version byte, which is currently fixed at 0. We also need to pass the network type which in the case for segwit's is just "bc" (yes, bc, NOT bc1 or bc1q). Then pass these three pieces of data to Pieter Wuille's encode() function as described at https://bitcoin.stackexchange.com/questions/70507/how-to-create-a-bech32-address-from-a-public-key .



Naturally, using both of these scripts we can decode the base58 address into a ripemd160 [use bytearray(base58.b58decode(address).encode("ascii"))] and then pass that information to encode() to give us a bech32 address.
full member
Activity: 204
Merit: 437
February 19, 2021, 02:12:46 PM
#5

Just how to get Bitcoin Addresses in (P2WPKH) format from ripemd160 hash?


You need the public key in order to generate P2PKH or P2WPKH. So if the address is spent from, or signed with, then you can generate the corresponding P2WPKH.

No public key - no way.

EDIT:
Got a bit confused here. The hash is the same so one only has to change the encoding.

newbie
Activity: 30
Merit: 0
February 19, 2021, 02:04:08 PM
#4
This raises another question for me. Just how to get Bitcoin Addresses in (P2WPKH) format from ripemd160 hash?
I am not familiar with the Bech32 package. Are there any examples of generating [ripemd160 hash> Addresses]
newbie
Activity: 30
Merit: 0
February 19, 2021, 01:03:21 PM
#3
First of all, you still do not know how to differentiate Bitcoin addresses.

1. Any address that start from 1 are P2PKH (legacy) addresses
2. Bech32 addresses starts from bc1, they are referred to as native segwit
3. P2SH addresses which are nested segwit start from 3.
4. Also segwit addresses starts from 3. But, you using the wallet will know if your address is a segwit address.

Know that you can not generate one address type from another, it is not possible. But if the seed phrase is BIP39, just import it on electrum, it should be bech32 by default, but if not, go to setting to choose m/84'/0'/0'/0/0 derivational path, this will give you bech32 addresses.

If the seed is BIP39 and not yet supported by electrum (but definitely it should work), you will need to input the seed phrase on iamcoleman converted, it has to be done offline for security and safety. It is a converter that can help you to use the seed phrase to generate the derivation path for legacy, nested segwit and bech32 addresses. All you need to do is to use the tools for it to generate what you want by inputting your seed phrase on it offline.

I am thinking you are using wallet that only support legacy address (like atomic wallet), you can just use the iamcoleman too to generate the derivation paths, check for BIP44 and check if your address from the formal wallet is there, if there, then press on BIP84, import the master private key of BIP84 on electrum. This should work also.



I think with a package to decode in Base58

We receive ripemd160 hash of a Bitcoin address


Code:
python -m pip install base58


Here below is simple way to get the ripemd160 hash of a bitcoin address coded in base58 (python 2.7):



Code:
>>>import base58
>>>adr58 = '1Q2TWHE3GMdB6BZKafqwxXtWAWgFt5Jvm3'
>>>adr160 = base58.b58decode_check(adr58).encode('hex')[2:]
>>>print (adr160)
fc916f213a3d7f1369313d5fa30f6168f9446a2d


And then if ripemd160 hash is known: fc916f213a3d7f1369313d5fa30f6168f9446a2d

You can encode in "Bech32 (P2WPKH)" and get a Bitcoin address with the prefix  bc1q********************
legendary
Activity: 1512
Merit: 4795
February 19, 2021, 12:47:51 PM
#2
First of all, you still do not know how to differentiate Bitcoin addresses.

1. Any address that start from 1 are P2PKH (legacy) addresses
2. Bech32 addresses starts from bc1, they are referred to as native segwit
3. P2SH addresses which are nested segwit start from 3.
4. Also segwit addresses starts from 3. But, you using the wallet will know if your address is a segwit address.

Know that you can not generate one address type from another, it is not possible. But if the seed phrase is BIP39, just import it on electrum, it should be bech32 by default, but if not, go to setting to choose m/84'/0'/0'/0/0 derivational path, this will give you bech32 addresses.

If the seed is BIP39 and not yet supported by electrum (but definitely it should work), you will need to input the seed phrase on iamcoleman converted, it has to be done offline for security and safety. It is a converter that can help you to use the seed phrase to generate the derivation path for legacy, nested segwit and bech32 addresses. All you need to do is to use the tools for it to generate what you want by inputting your seed phrase on it offline.

I am thinking you are using wallet that only support legacy address (like atomic wallet), you can just use the iamcoleman too to generate the derivation paths, check for BIP44 and check if your address from the formal wallet is there, if there, then press on BIP84, import the master private key of BIP84 on electrum. This should work also.
newbie
Activity: 30
Merit: 0
February 19, 2021, 12:01:34 PM
#1

Is there a python script "Base58 (P2SH)" to "Bech32 (P2WPKH)" Converter?

I have a huge list of Bitcoin Addresses "Base58 (P2SH)" and I need to create a list of addresses from them in the format "Bech32 (P2WPKH)".



List: "Base58 (P2SH)"

Code:
135VnFgmZG1Mvm9Z2F4wwekLySD9JpwgAq
1LAqmtxdjfsM2xsVVthsMUPsZ4Lw2X2sen
1JaS1LCj7m8BfJyexEqMVmSYs8RJhcRasy
1PTyMf9Z2TagnkMJVFNBEzR95QFqH99NQf
1NGL7KxXiYDcxxxhapukprgm2xm3D4MSL4
...
...
...
e.t.c


Need to get!
List: "Bech32 (P2WPKH)"

Code:
bc1qzmylp874rg2st6pdlt8yjga3ek9pr96wuzelun
bc1q6frdj8zd93jsarrsmsr3kyhz4cdrwkcy2gxtfu
bc1qcrxt6gypzl7k4xunlq7vfqggq9kwl34zqlpgwh
bc1q7ek2qs3z4qk3h679rwmlkj6lx3nwgh0xhcjpzv
bc1qa9qd29y768h2m9z86g04mzferg9qtn7ucuq2u0



https://segwitaddress.org/bech32/#entropyRef
https://github.com/sipa/bech32/tree/master/ref
Jump to: