<p>There are 2 cases where the version byte in the witness program is either 0 or 1.<br />
If the version byte is 0, you need a 20-byte HASH160 of the public key. Reference <a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#witness-program" rel="nofollow noreferrer">wiki</a>.</p>
<pre><code>witnessProg := btcutil.Hash160(w.SerializePubKey())
p2wpkh, err := btcutil.NewAddressWitnessPubKeyHash(witnessProg, chainParams)
</code></pre>
<p>But I am not really sure about the case where version byte is 1.</p>
<p>Edit: previous implementation was probably wrong, fixed after some more research.</p>

CC BY-SA 4.0
2020-08-21T04:09:49.667Z
2020-09-02T02:48:14.850Z
2020-09-02T02:48:14.850Z
3tbraden
<p>How can WIF be easily converted to WitnessPubKeyHash in Go?</p>

<p>We need to get the public key from WIF and than calculate witness program as <code>ripemd160(sha256(compressed_pub_key))</code>.</p>

<p>But is there a function in <code>btcutil</code> to convert the public key to <code>witnessProg</code> required by <a href="https://github.com/btcsuite/btcutil/blob/master/address.go#L520" rel="noreferrer">NewAddressWitnessPubKeyHash</a> function:</p>



<pre><code>func NewAddressWitnessPubKeyHash(witnessProg []byte, net *chaincfg.Params) (*AddressWitnessPubKeyHash, error)
</code></pre>

<address><p2wpkh>
Convert WIF to Witness PubKey Hash Address in Go
Kuba
# Convert WIF to Witness PubKey Hash Address in Go

(Asked by: username_0 on 2019-05-03)

How can WIF be easily converted to WitnessPubKeyHash in Go?
We need to get the public key from WIF and than calculate witness program as ripemd160(sha256(compressed_pub_key)).
But is there a function in btcutil to convert the public key to witnessProg required by NewAddressWitnessPubKeyHash function:
func NewAddressWitnessPubKeyHash(witnessProg []byte, net *chaincfg.Params) (*AddressWitnessPubKeyHash, error)

**Tags:** address, p2wpkh


---

## 1 answers

### Answer by username_1 on 2020-08-21. Score: 1

There are 2 cases where the version byte in the witness program is either 0 or 1.
If the version byte is 0, you need a 20-byte HASH160 of the public key. Reference wiki.
witnessProg := btcutil.Hash160(w.SerializePubKey())
p2wpkh, err := btcutil.NewAddressWitnessPubKeyHash(witnessProg, chainParams)

But I am not really sure about the case where version byte is 1.
Edit: previous implementation was probably wrong, fixed after some more research.


---

