<blockquote>
<p>So is it correct that I can not transform a bech32 encoded address to a scriptPubKey?</p>
</blockquote>
<p>That is incorrect.</p>
<p>Addresses are by definition instructions for wallet on how to create transaction outputs. Since transaction outputs contain a scriptPubKey, that must imply that an address can be converted to one. Otherwise wallets wouldn't be able to send to it.</p>
<p>The compression referred to in that document refers to the use of compressed public keys (33 bytes ones) as opposed to uncompressed 65 bytes ones. This doesn't affect you, as bech32 P2WPKH addresses contain a <em>hash</em> of the public key, and that hash is what needs to be placed in the scriptPubKey. What that hash computed from is the receiver's business, not the sender's (you). The same is true for base58 P2PKH addresses, by the way; you also already deal with public key hashes there.</p>
<p>For an exact description of how to decode bech32 addresses, refer to the BIP that specifies them. It has pseudocode, links to reference code, and test vectors, including ones that give addresses and the scriptPubKey they translate to. The relevant documents are:</p>
<ul>
<li><a href="https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki" rel="nofollow noreferrer">BIP173</a> specifies bech32, and encoding of witness v0 addresses (P2WPKH, P2WSH).</li>
<li><a href="https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki" rel="nofollow noreferrer">BIP350</a> specifies bech32m, and encoding of witness v1 (and up) addresses (only P2TR currently).</li>
</ul>

CC BY-SA 4.0
2022-08-21T13:25:28.580Z
2022-08-21T13:25:28.580Z
Pieter Wuille
<p>Got it working in the end. This code was really helpful to me:
<a href="https://github.com/sipa/bech32/tree/master/ref" rel="nofollow noreferrer">https://github.com/sipa/bech32/tree/master/ref</a></p>

CC BY-SA 4.0
2022-08-22T03:01:33.587Z
2022-08-22T03:01:33.587Z
Niel Deckx
<p>My goal is to parse a BTC transaction and validate if the transaction was sent to a given BTC address. So I need to transform a BTC address to the scriptPubKey used in the transaction.</p>
<p>I got this working for base58Check encoded addresses as it's possible to decode the address and get the scriptPubKey.</p>
<p>However, I am not able to get this working for Bech32 encoded addresses. From the info found on the page linked below, it seems that it's not possible to decode the address to the scriptPubKey as compression is also applied: <a href="https://en.bitcoin.it/wiki/Bech32" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Bech32</a></p>
<p>So is it correct that I can not transform a bech32 encoded address to a scriptPubKey?</p>
<p>Thanks!</p>

<bech32-address><p2wpkh>
Bech32 encoded address to scriptPubKey
Niel Deckx
# Bech32 encoded address to scriptPubKey

(Asked by: username_0 on 2022-08-21)

My goal is to parse a BTC transaction and validate if the transaction was sent to a given BTC address. So I need to transform a BTC address to the scriptPubKey used in the transaction.

I got this working for base58Check encoded addresses as it's possible to decode the address and get the scriptPubKey.

However, I am not able to get this working for Bech32 encoded addresses. From the info found on the page linked below, it seems that it's not possible to decode the address to the scriptPubKey as compression is also applied: <https://en.bitcoin.it/wiki/Bech32>

So is it correct that I can not transform a bech32 encoded address to a scriptPubKey?

Thanks!

**Tags:** bech32-address, p2wpkh


---

## 2 answers

### Answer by username_1 on 2022-08-21. Score: 4

> So is it correct that I can not transform a bech32 encoded address to a scriptPubKey?

That is incorrect.

Addresses are by definition instructions for wallet on how to create transaction outputs. Since transaction outputs contain a scriptPubKey, that must imply that an address can be converted to one. Otherwise wallets wouldn't be able to send to it.

The compression referred to in that document refers to the use of compressed public keys (33 bytes ones) as opposed to uncompressed 65 bytes ones. This doesn't affect you, as bech32 P2WPKH addresses contain a *hash* of the public key, and that hash is what needs to be placed in the scriptPubKey. What that hash computed from is the receiver's business, not the sender's (you). The same is true for base58 P2PKH addresses, by the way; you also already deal with public key hashes there.

For an exact description of how to decode bech32 addresses, refer to the BIP that specifies them. It has pseudocode, links to reference code, and test vectors, including ones that give addresses and the scriptPubKey they translate to. The relevant documents are:

* [BIP173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki) specifies bech32, and encoding of witness v0 addresses (P2WPKH, P2WSH).
* [BIP350](https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki) specifies bech32m, and encoding of witness v1 (and up) addresses (only P2TR currently).


---

### Answer by username_0 on 2022-08-22. Score: 0

Got it working in the end. This code was really helpful to me:
<https://github.com/sipa/bech32/tree/master/ref>


---

