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

hd.go

TEXT FILE

3.1 KB · Public source code for developers, search crawlers and AI agents

Download / RawBack to folder
Go · 113 lines
1// Package hd demonstrates private BIP32 derivation and BIP39 seed stretching.
2// Educational only: big.Int arithmetic is not constant-time.
3package hd
4
5import (
6	"crypto/hmac"
7	"crypto/sha512"
8	"encoding/binary"
9	"errors"
10	"math/big"
11	"strconv"
12	"strings"
13)
14
15var order, _ = new(big.Int).SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", 16)
16
17const Hardened uint32 = 1 << 31
18
19type ExtendedPrivate struct{ Key, Chain [32]byte }
20
21func mac(key, data []byte) []byte { h := hmac.New(sha512.New, key); h.Write(data); return h.Sum(nil) }
22func valid(k []byte) bool         { n := new(big.Int).SetBytes(k); return n.Sign() > 0 && n.Cmp(order) < 0 }
23
24// Master implements BIP32 master key generation for 128–512 bit seeds.
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}
38
39// ChildHardened implements CKDpriv for hardened indices only.
40// Invalid children are reported, not silently retried at another index.
41func (p ExtendedPrivate) ChildHardened(index uint32) (ExtendedPrivate, error) {
42	var out ExtendedPrivate
43	if index < Hardened || !valid(p.Key[:]) {
44		return out, errors.New("invalid hardened derivation input")
45	}
46	var data [37]byte
47	copy(data[1:33], p.Key[:])
48	binary.BigEndian.PutUint32(data[33:], index)
49	i := mac(p.Chain[:], data[:])
50	left := new(big.Int).SetBytes(i[:32])
51	if left.Cmp(order) >= 0 {
52		return out, errors.New("invalid child tweak")
53	}
54	left.Add(left, new(big.Int).SetBytes(p.Key[:]))
55	left.Mod(left, order)
56	if left.Sign() == 0 {
57		return out, errors.New("zero child scalar")
58	}
59	left.FillBytes(out.Key[:])
60	copy(out.Chain[:], i[32:])
61	return out, nil
62}
63
64// ParsePath parses absolute private derivation paths, with apostrophe hardening.
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}
91
92// SeedASCII demonstrates BIP39 PBKDF2 for ASCII inputs only (already NFKD).
93// It does not validate mnemonic words or checksum, or generate mnemonics.
94func SeedASCII(mnemonic, passphrase string) ([64]byte, error) {
95	var out [64]byte
96	for _, s := range []string{mnemonic, passphrase} {
97		for _, r := range s {
98			if r > 127 {
99				return out, errors.New("ASCII only; full BIP39 requires NFKD")
100			}
101		}
102	}
103	salt := append([]byte("mnemonic"+passphrase), 0, 0, 0, 1)
104	u := mac([]byte(mnemonic), salt)
105	copy(out[:], u)
106	for j := 1; j < 2048; j++ {
107		u = mac([]byte(mnemonic), u)
108		for k := range out {
109			out[k] ^= u[k]
110		}
111	}
112	return out, nil
113}
Read-only source · Select a line number to link directly · Raw download above

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