Bitcoin Forum
May 27, 2024, 03:16:56 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: How to convert bech32 <> hash160 in python?  (Read 186 times)
samueljj (OP)
Newbie
*
Offline Offline

Activity: 2
Merit: 0


View Profile
March 27, 2022, 10:15:58 PM
Last edit: March 28, 2022, 01:36:32 PM by samueljj
 #1

like this script:

Code:
import base58

f = open("addresses.txt", "r")
for line in f:
try:
hash1 = base58.b58decode_check(line.strip())
open('hash160.txt', 'a').write(hash1.hex()[-40:] + '\n')
except:
print('False:', line.strip())
pass
f.close()
BitMaxz
Legendary
*
Offline Offline

Activity: 3262
Merit: 2977


:( My PC broke due trying to recover an old wallet


View Profile WWW
March 27, 2022, 11:05:36 PM
Merited by pooya87 (2), ABCbits (1)
 #2

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: 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

█▀▀▀











█▄▄▄
▀▀▀▀▀▀▀▀▀▀▀
e
▄▄▄▄▄▄▄▄▄▄▄
█████████████
████████████▄███
██▐███████▄█████▀
█████████▄████▀
███▐████▄███▀
████▐██████▀
█████▀█████
███████████▄
████████████▄
██▄█████▀█████▄
▄█████████▀█████▀
███████████▀██▀
████▀█████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
c.h.
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
▀▀▀█











▄▄▄█
▄██████▄▄▄
█████████████▄▄
███████████████
███████████████
███████████████
███████████████
███░░█████████
███▌▐█████████
█████████████
███████████▀
██████████▀
████████▀
▀██▀▀
samueljj (OP)
Newbie
*
Offline Offline

Activity: 2
Merit: 0


View Profile
March 28, 2022, 03:36:49 AM
Last edit: March 28, 2022, 05:39:46 PM by samueljj
 #3

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: Question
Is there a way to get to the hash160 address from bc1q34aq5drpuwy3wgl9lhup9892qp6svr8ldzyy7c

Thaks a lot🙏

Code:
import bech32

f = open("addresses.txt", "r")
for line in f:
hash1 = bech32.decode("bc", line.strip())
try:
hash2 = bytes(hash1[1])
open('hash160.txt', 'a').write(hash2.hex() + '\n')
except:
print('False:', line.strip())
pass
f.close()
NotATether
Legendary
*
Offline Offline

Activity: 1610
Merit: 6761


bitcoincleanup.com / bitmixlist.org


View Profile WWW
March 29, 2022, 06:33:50 AM
 #4

Use Peter Todd's python-bitcoinlib 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.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
pooya87
Legendary
*
Offline Offline

Activity: 3458
Merit: 10593



View Profile
March 29, 2022, 07:25:02 AM
Merited by LeGaulois (1), ABCbits (1), NotATether (1), n0nce (1)
 #5

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.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!