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

Testing Bitcoin HD Wallet Recovery Foundations in Go

permalink THAMER
#bip32#bitcoin#bitcoin-script#golang#hd-wallet

Recovery tests should compare known seeds, master scalars, chain codes, derivation paths and eventually output scripts. Round trips alone can hide matching bugs. Public reference fixtures are useful for regression tests but must never protect funds.

Go implementation

Go · 13 lines
25func Master(seed []byte) (ExtendedPrivate, error) {
26	var out ExtendedPrivate
27	if len(seed) < 16 || len(seed) > 64 {
28		return out, errors.New("seed length")
29	}
30	i := mac([]byte("Bitcoin seed"), seed)
31	if !valid(i[:32]) {
32		return out, errors.New("invalid master scalar")
33	}
34	copy(out.Key[:], i[:32])
35	copy(out.Chain[:], i[32:])
36	return out, nil
37}
Source: /repo/golang-bitcoin/004-hd-wallets/hd.go · Raw file · Read-only excerpt

Practical exercise

Run all tests, then inspect TestInvalid and TestPath. Full wallet recovery also requires normal derivation, descriptors, discovery and network state; none is implemented here.

Local verification

From the repository directory golang-bitcoin/004-hd-wallets, run:

go test -race -v ./...
Go · 56 lines
1package hd
2
3import (
4	"encoding/hex"
5	"reflect"
6	"testing"
7)
8
9func TestMaster(t *testing.T) {
10	seed, _ := hex.DecodeString("000102030405060708090a0b0c0d0e0f")
11	m, e := Master(seed)
12	if e != nil {
13		t.Fatal(e)
14	}
15	if hex.EncodeToString(m.Key[:]) != "e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35" || hex.EncodeToString(m.Chain[:]) != "873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508" {
16		t.Fatal("master vector")
17	}
18	c, e := m.ChildHardened(Hardened)
19	if e != nil {
20		t.Fatal(e)
21	}
22	if hex.EncodeToString(c.Chain[:]) != "47fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141" {
23		t.Fatal("child chain")
24	}
25	if _, e = m.ChildHardened(0); e == nil {
26		t.Fatal("normal index accepted")
27	}
28}
29func TestSeed(t *testing.T) {
30	s, e := SeedASCII("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about", "TREZOR")
31	if e != nil || hex.EncodeToString(s[:]) != "c55257c360c07c72029aebc1b53c05ed0362ada38ead3e3e9efa3708e53495531f09a6987599d18264c1e1c92f2cf141630c7a3c4ab7c81b2f001698e7463b04" {
32		t.Fatal("seed vector", e)
33	}
34	if _, e = SeedASCII("é", ""); e == nil {
35		t.Fatal("non ASCII accepted")
36	}
37}
38func TestPath(t *testing.T) {
39	p, e := ParsePath("m/84'/0'/0'/0/7")
40	if e != nil || !reflect.DeepEqual(p, []uint32{Hardened + 84, Hardened, Hardened, 0, 7}) {
41		t.Fatal(p, e)
42	}
43	for _, s := range []string{"M/0", "m/", "m/-1", "m/2147483648", "m/+1"} {
44		if _, e := ParsePath(s); e == nil {
45			t.Fatal(s)
46		}
47	}
48}
49func TestInvalid(t *testing.T) {
50	if _, e := Master(nil); e == nil {
51		t.Fatal("short seed")
52	}
53	if _, e := (ExtendedPrivate{}).ChildHardened(Hardened); e == nil {
54		t.Fatal("zero parent")
55	}
56}

Scope and security

This is an offline educational implementation, not a production wallet. Private scalar arithmetic uses math/big, which is not constant-time. No real secrets, mnemonic generation, extended-key serialization, normal child derivation, public child derivation or transaction signing are provided.

References

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

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

https://thamer.org/repo/golang-bitcoin/004-hd-wallets/

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