BIP84 Native SegWit Wallet Derivation Paths
BIP84 uses purpose 84 for native P2WPKH. The witness program is a 20-byte public key hash and the address uses Bech32. The path alone does not determine the address without the derived public key and network.
Go implementation
65func ParsePath(path string) ([]uint32, error) {
66 parts := strings.Split(path, "/")
67 if parts[0] != "m" {
68 return nil, errors.New("expected m")
69 }
70 var out []uint32
71 for _, p := range parts[1:] {
72 hard := strings.HasSuffix(p, "'")
73 if hard {
74 p = strings.TrimSuffix(p, "'")
75 }
76 if p == "" || strings.Trim(p, "0123456789") != "" {
77 return nil, errors.New("invalid index")
78 }
79 n, e := strconv.ParseUint(p, 10, 31)
80 if e != nil {
81 return nil, e
82 }
83 v := uint32(n)
84 if hard {
85 v |= Hardened
86 }
87 out = append(out, v)
88 }
89 return out, nil
90}Practical exercise
Parse m/84'/0'/0'/0/0. Compare the account prefix with the BIP49 example.
Local verification
From the repository directory golang-bitcoin/004-hd-wallets, run:
go test -race -v ./...
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-0084.mediawiki
https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
https://thamer.org/repo/golang-bitcoin/004-hd-wallets/
