Bitcoin Stack Exchange is a question and answer site for Bitcoin crypto-currency enthusiasts. It only takes a minute to sign up.
Sign up to join this communityAnybody can ask a question
Anybody can answer
The best answers are voted up and rise to the top
Could someone provide an example of how to use the python reference implementation for generating a bech32 address?
For example, generating a mainnet bech32 address for this scriptPubKey: "0014751e76e8199196d454941c45d1b3a323f1433bd6"
The reference implementation provides a handy all-in-one encode function. To encode the scriptPubKey 0014751e76e8199196d454941c45d1b3a323f1433bd6
as a mainnet bech32 address, you would do:
import bech32 import binascii spk = binascii.unhexlify('0014751e76e8199196d454941c45d1b3a323f1433bd6') version = spk[0] - 0x50 if spk[0] else 0 program = spk[2:] print(bech32.encode('bc', version, program))
To add to Andrew's answer I recently used the above code snippet to generate bech32m addresses from P2TR scriptPubKeys.
I cloned the bech32 repo and then created a file for the code snippet in the bech/ref/python
directory.
I took a P2TR scriptPubKey from the BIP 341 test vectors e.g. 5120712447206d7a5238acc7ff53fbe94a3b64539ad291c7cdbc490b7577e4b17df5
Then this slightly adjusted code generated the associated bech32m address (bc1pwyjywgrd0ffr3tx8laflh6228dj98xkjj8rum0zfpd6h0e930h6saqxrrm
).
import segwit_addr import binascii spk = binascii.unhexlify('5120712447206d7a5238acc7ff53fbe94a3b64539ad291c7cdbc490b7577e4b17df5') version = spk[0] - 0x50 if spk[0] else 0 program = spk[2:] print(segwit_addr.encode('bc', version, program))
You can get bonuses upto $100 FREE BONUS when you:
π° Install these recommended apps:
π² SocialGood - 100% Crypto Back on Everyday Shopping
π² xPortal - The DeFi For The Next Billion
π² CryptoTab Browser - Lightweight, fast, and ready to mine!
π° Register on these recommended exchanges:
π‘ Binanceπ‘ Bitfinexπ‘ Bitmartπ‘ Bittrexπ‘ Bitget
π‘ CoinExπ‘ Crypto.comπ‘ Gate.ioπ‘ Huobiπ‘ Kucoin.
Comments