<p>I have found the answer thanks to <a href="https://github.com/modood/btckeygen" rel="nofollow noreferrer">modood/btckeygen</a>.</p>
<p>Here is the relevant code extracted from the repo.</p>
<pre><code>witnessProg := btcutil.Hash160(pubkey.SerializeCompressed())
addressWitnessPubKeyHash, err := btcutil.NewAddressWitnessPubKeyHash(witnessProg, chainParams)
if err != nil {
    panic(err)
}
address := addressWitnessPubKeyHash.EncodeAddress()
</code></pre>

CC BY-SA 4.0
2020-09-02T02:42:12.670Z
2020-09-02T02:42:12.670Z
3tbraden
<p>I am trying to generate bech32/segwit address in golang with the following code:</p>
<pre><code>var pubkey *btcec.PubKey
address, err := btcutil.NewAddressPubKey(pubkey.SerializeCompressed(), chainParams)
if err != nil {
    log.Fatal(err)
}
address.SetFormat(btcutil.PKFCompressed)
bech32Bytes, err := bech32.ConvertBits(address.ScriptAddress(), 8, 5, true)
if err != nil {
    log.Fatal(err)
}
segwitaddr, err := bech32.Encode(&quot;tb&quot;, bech32Bytes)
if err != nil {
    log.Fatal(err)
}
</code></pre>
<p>and also more simply</p>
<pre><code>var e *hdkeychain.ExtendedKey
address, err := e.Address(chainParams)
bech32Bytes, err := bech32.ConvertBits(address.ScriptAddress(), 8, 5, true)
if err != nil {
    log.Fatal(err)
}
segwitaddr, err := bech32.Encode(&quot;tb&quot;, bech32Bytes)
if err != nil {
    log.Fatal(err)
}
</code></pre>
<p>But neither <code>segwitaddr</code> generated match the bip84 address I got from <a href="https://iancoleman.io/bip39/" rel="nofollow noreferrer">https://iancoleman.io/bip39/</a>. What is wrong with the code above or how to properly get a bech32 address from a pub key?</p>
<p>I have looked into another <a href="https://bitcoin.stackexchange.com/questions/70507/how-to-create-a-bech32-address-from-a-public-key">post</a> on the same question, but could not figure out how to prepare the data needed in golang. More specifically, I am confused in preparing the input <code>[]byte</code> to call <a href="https://godoc.org/github.com/btcsuite/btcutil/bech32#Encode" rel="nofollow noreferrer"><code>bech32.Encode()</code></a> and using <a href="https://godoc.org/github.com/btcsuite/btcutil/bech32#ConvertBits" rel="nofollow noreferrer"><code>bech32.ConvertBits()</code></a>.</p>

<address><segregated-witness><bech32-address><go-lang>
how to generate bech32/segwit address from pubkey in golang
3tbraden
# how to generate bech32/segwit address from pubkey in golang

(Asked by: username_0 on 2020-08-20)

I am trying to generate bech32/segwit address in golang with the following code:

```
var pubkey *btcec.PubKey
address, err := btcutil.NewAddressPubKey(pubkey.SerializeCompressed(), chainParams)
if err != nil {
    log.Fatal(err)
}
address.SetFormat(btcutil.PKFCompressed)
bech32Bytes, err := bech32.ConvertBits(address.ScriptAddress(), 8, 5, true)
if err != nil {
    log.Fatal(err)
}
segwitaddr, err := bech32.Encode("tb", bech32Bytes)
if err != nil {
    log.Fatal(err)
}

```

and also more simply

```
var e *hdkeychain.ExtendedKey
address, err := e.Address(chainParams)
bech32Bytes, err := bech32.ConvertBits(address.ScriptAddress(), 8, 5, true)
if err != nil {
    log.Fatal(err)
}
segwitaddr, err := bech32.Encode("tb", bech32Bytes)
if err != nil {
    log.Fatal(err)
}

```

But neither `segwitaddr` generated match the bip84 address I got from <https://iancoleman.io/bip39/>. What is wrong with the code above or how to properly get a bech32 address from a pub key?

I have looked into another [post](https://bitcoin.stackexchange.com/questions/70507/how-to-create-a-bech32-address-from-a-public-key) on the same question, but could not figure out how to prepare the data needed in golang. More specifically, I am confused in preparing the input `[]byte` to call [`bech32.Encode()`](https://godoc.org/github.com/btcsuite/btcutil/bech32#Encode) and using [`bech32.ConvertBits()`](https://godoc.org/github.com/btcsuite/btcutil/bech32#ConvertBits).

**Tags:** address, segregated-witness, bech32-address, go-lang


---

## 1 answers

### Answer by username_0 on 2020-09-02. Score: 0 | Accepted answer

I have found the answer thanks to [modood/btckeygen](https://github.com/modood/btckeygen).

Here is the relevant code extracted from the repo.

```
witnessProg := btcutil.Hash160(pubkey.SerializeCompressed())
addressWitnessPubKeyHash, err := btcutil.NewAddressWitnessPubKeyHash(witnessProg, chainParams)
if err != nil {
    panic(err)
}
address := addressWitnessPubKeyHash.EncodeAddress()

```


---

