<p>Ok, I figured out how to do it.</p>

<p>You need 3 pieces of information to construct the bech32 address.</p>

<ul>
<li><code>hrp</code>: the human-readable part. This is <code>bc</code> for mainnet and <code>tb</code> for testnet</li>
<li><code>witver</code>: the witness version. This is <code>0</code> at the moment represented by the byte <code>0x00</code> but it can go up to <code>16</code> when they add more versions.</li>
<li><code>witprog</code>: 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 <strong>compressed</strong> public key i.e <code>ripemd160(sha256(compressed_pub_key))</code>. 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 <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#witness-program" rel="noreferrer">BIP141</a></li>
</ul>

<p>Once you have those 3 pieces, you can use the encode function in one of the <a href="https://github.com/sipa/bech32/tree/master/ref" rel="noreferrer">reference implementations</a></p>

<p>I have put together a small <a href="https://github.com/mcdallas/cryptotools" rel="noreferrer">pure Python library</a> for anyone interested how all this stuff works behind the hood.</p>

CC BY-SA 4.0
2022-05-29T11:36:04.713Z
Can you add some text vectors here or point to your test vectors?
Jus12
CC BY-SA 4.0
2022-05-29T21:45:41.580Z
@Jus12 https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#Test_vectors
Mike D
CC BY-SA 3.0
2018-03-01T10:02:33.587Z
2018-03-01T10:02:33.587Z
Mike D
<p>What are the steps to create a Bech32 address from an ECDSA public key? Bitcoin wiki has a nice <a href="https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses" rel="noreferrer">step-by-step guide</a> 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.</p>

CC BY-SA 3.0
2018-02-09T18:42:49.747Z
Maybe this can help you: https://github.com/sipa/bech32/tree/master/ref/python
pors
CC BY-SA 3.0
2018-02-09T22:04:41.540Z
@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
CC BY-SA 3.0
2018-02-11T05:43:07.593Z
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
CC BY-SA 3.0
2018-02-20T11:03:06.040Z
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
<address><bech32-address>
How to create a Bech32 address from a public key?
Mike D
# How to create a Bech32 address from a public key?

(Asked by: username_0 on 2018-02-09)

What are the steps to create a Bech32 address from an ECDSA public key? Bitcoin wiki has a nice [step-by-step guide](https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses) 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.

**Tags:** address, bech32-address

> **Comments:**
> **username_1**: Maybe this can help you: https://github.com/sipa/bech32/tree/master/ref/python  
> **username_0**: @username_1 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?  
> **username_2**: Welcome to Bitcoin.SE! For @username_1, you can help by posting your expanded comments as an answer and possibly including some 'how to use' information.  
> **username_3**: 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  

---

## 1 answers

### Answer by username_0 on 2018-03-01. Score: 16 | Accepted answer

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](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#witness-program)

Once you have those 3 pieces, you can use the encode function in one of the [reference implementations](https://github.com/sipa/bech32/tree/master/ref)

I have put together a small [pure Python library](https://github.com/mcdallas/cryptotools) for anyone interested how all this stuff works behind the hood.

> **Comments:**
> **username_4**: Can you add some text vectors here or point to your test vectors?  
> **username_0**: @username_4 https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#Test_vectors  

---

