13

What are the steps to create a Bech32 address from an ECDSA public key? Bitcoin wiki has a nice step-by-step guide to create an address from a public key but what about Bech32 addresses? I've read BIP173 but I don't understand what to give as an input.

4
  • Maybe this can help you: github.com/sipa/bech32/tree/master/ref/python
    – pors
    Commented Feb 9, 2018 at 18:42
  • 1
    @pors I have seen this repo but I do not know what to input. I understand I am to use the bech32_encode function but what do I put in there? The public key? The hash160? the P2WSH? How do I create that?
    – Mike D
    Commented Feb 9, 2018 at 22:04
  • Welcome to Bitcoin.SE! For @pors, you can help by posting your expanded comments as an answer and possibly including some 'how to use' information.
    – Willtech
    Commented Feb 11, 2018 at 5:43
  • nullius has created a tool in december, to play with bech32 addresses in C. Easy usable, and code is documented as well. See here: bitcointalk.org/index.php?topic=2664728.msg27178628#msg27178628 Commented Feb 20, 2018 at 11:03

1 Answer 1

16

Ok, I figured out how to do it.

You need 3 pieces of information to construct the bech32 address.

  • hrp: the human-readable part. This is bc for mainnet and tb for testnet
  • witver: the witness version. This is 0 at the moment represented by the byte 0x00 but it can go up to 16 when they add more versions.
  • witprog: the witness programm. In case you want a Pay-to-witness-public-key (P2WPK) address which is the most common ones, this is the 20-byte hash160 of the compressed public key i.e ripemd160(sha256(compressed_pub_key)). In case you want a Pay-to-witness-script-hash (P2WSH) address, this is the 32-byte sha256 of the scriptPubKey which is the script that will need to evaluate to True for someone to be able to spend the output. More on this in BIP141

Once you have those 3 pieces, you can use the encode function in one of the reference implementations

I have put together a small pure Python library for anyone interested how all this stuff works behind the hood.

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.