How to create a Bech32 address from a public key?

5

1

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.

Mike D

Posted 2018-02-09T14:59:29.423

Reputation: 1 571

Maybe this can help you: https://github.com/sipa/bech32/tree/master/ref/python

pors 2018-02-09T18:42:49.747

@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 2018-02-09T22:04:41.540

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 2018-02-11T05:43:07.593

nullius has created a tool in december, to play with bech32 addresses in C. Easy usable, and code is documented as well. See here: https://bitcointalk.org/index.php?topic=2664728.msg27178628#msg27178628

pebwindkraft 2018-02-20T11:03:06.040

Answers

4

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.

Mike D

Posted 2018-02-09T14:59:29.423

Reputation: 1 571