How to use python reference for encoding a bech32 address?

0

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"

Darius

Posted 2019-11-18T05:59:23.667

Reputation: 103

Answers

2

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

Andrew Chow

Posted 2019-11-18T05:59:23.667

Reputation: 40 910