THAMER
Sovereign Bitcoin Wallet

WALLET AI BITCOIN

Your Bitcoin Wallet. Your Keys. Your Engine.

Self-hosted Bitcoin software built for developers, applications, and AI agents.

CREATE WALLET

Native SegWit P2WPKH Addresses in Go

permalink THAMER
#address-encoding#bitcoin#bitcoin-script#encodewitness#golang

Version zero with a 20-byte program represents P2WPKH. It uses Bech32, not Bech32m. The program is HASH160 of the public key.

Go implementation

The Stage 003 educational package uses only the Go standard library. The relevant function is shown below.

Go · 20 lines
110func EncodeWitness(hrp string, v byte, p []byte) (string, error) {
111	if !valid(hrp, v, p) {
112		return "", errors.New("witness parameters")
113	}
114	d, _ := convert(p, 8, 5, true)
115	d = append([]byte{v}, d...)
116	k := uint32(1)
117	if v != 0 {
118		k = 0x2bc830a3
119	}
120	c := polymod(append(append(expand(hrp), d...), make([]byte, 6)...)) ^ k
121	s := hrp + "1"
122	for _, x := range d {
123		s += string(charset[x])
124	}
125	for i := 5; i >= 0; i-- {
126		s += string(charset[(c>>uint(5*i))&31])
127	}
128	return s, nil
129}

Practical exercise

The BIP173 fixture 751e76e8199196d454941c45d1b3a323f1433bd6 encodes to bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4.

Testing locally

From the downloaded repository directory golang-bitcoin/003-addresses, run:

go test -race ./...
Go · 58 lines
1package addresses
2
3import (
4	"bytes"
5	"encoding/hex"
6	"strings"
7	"testing"
8)
9
10func TestBase58(t *testing.T) {
11	p := make([]byte, 20)
12	s := Base58Check(0, p)
13	if s != "1111111111111111111114oLvT2" {
14		t.Fatal(s)
15	}
16	v, q, e := DecodeBase58(s)
17	if e != nil || v != 0 || !bytes.Equal(p, q) {
18		t.Fatal(v, q, e)
19	}
20	if _, _, e = DecodeBase58(s[:len(s)-1] + "3"); e == nil {
21		t.Fatal("checksum")
22	}
23}
24func TestWitness(t *testing.T) {
25	p, _ := hex.DecodeString("751e76e8199196d454941c45d1b3a323f1433bd6")
26	s, e := EncodeWitness("bc", 0, p)
27	if e != nil || s != "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4" {
28		t.Fatal(s, e)
29	}
30	for _, h := range []string{"bc", "tb", "bcrt"} {
31		for _, v := range []byte{0, 1, 16} {
32			p := make([]byte, 32)
33			s, e := EncodeWitness(h, v, p)
34			if e != nil {
35				t.Fatal(e)
36			}
37			w, q, e := DecodeWitness(strings.ToUpper(s), h)
38			if e != nil || w != v || !bytes.Equal(p, q) {
39				t.Fatal(s, e)
40			}
41			if _, _, e = DecodeWitness(s, "xx"); e == nil {
42				t.Fatal("network")
43			}
44			if _, _, e = DecodeWitness("B"+s[1:], h); e == nil {
45				t.Fatal("mixed case")
46			}
47		}
48	}
49}
50func TestScript(t *testing.T) {
51	s, e := WitnessScript(1, make([]byte, 32))
52	if e != nil || len(s) != 34 || s[0] != 0x51 || s[1] != 32 {
53		t.Fatal(s, e)
54	}
55	if _, e = EncodeWitness("bc", 0, make([]byte, 21)); e == nil {
56		t.Fatal("length")
57	}
58}

The tests use public fixtures and do not connect to a node or broadcast transactions. These examples teach encoding, not wallet ownership, consensus validation or production payment authorization. Use maintained libraries and complete reference vectors for production.

References

https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki

https://pkg.go.dev/github.com/btcsuite/btcd/btcutil

Questions & Answers

Explore all questions →

Latest from the Lab

THAMER illustrated developer portrait and signature

ABOUT THE DEVELOPER

Independent Software Developer specializing in Bitcoin Script, Golang, and AI Agents.

Software engineering · Independent research · Open-source projects