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.
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
bcfor mainnet and
witver: the witness version. This is
0at the moment represented by the byte
0x00but it can go up to
16when 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.