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

address.go

TEXT FILE

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

Download / RawBack to folder
Go · 171 lines
1package addresses
2
3import (
4	"bytes"
5	"crypto/sha256"
6	"errors"
7	"math/big"
8	"strings"
9)
10
11const alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
12const charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
13
14func checksum(b []byte) []byte { a := sha256.Sum256(b); c := sha256.Sum256(a[:]); return c[:4] }
15func Base58Check(version byte, payload []byte) string {
16	b := append([]byte{version}, payload...)
17	b = append(b, checksum(b)...)
18	n := new(big.Int).SetBytes(b)
19	base := big.NewInt(58)
20	r := new(big.Int)
21	out := ""
22	for n.Sign() > 0 {
23		n.QuoRem(n, base, r)
24		out = string(alphabet[r.Int64()]) + out
25	}
26	for _, v := range b {
27		if v != 0 {
28			break
29		}
30		out = "1" + out
31	}
32	return out
33}
34func DecodeBase58(s string) (byte, []byte, error) {
35	if len(s) == 0 || len(s) > 128 {
36		return 0, nil, errors.New("length")
37	}
38	n := new(big.Int)
39	for _, c := range s {
40		i := strings.IndexRune(alphabet, c)
41		if i < 0 {
42			return 0, nil, errors.New("alphabet")
43		}
44		n.Mul(n, big.NewInt(58))
45		n.Add(n, big.NewInt(int64(i)))
46	}
47	z := 0
48	for z < len(s) && s[z] == '1' {
49		z++
50	}
51	b := append(make([]byte, z), n.Bytes()...)
52	if len(b) < 5 || !bytes.Equal(checksum(b[:len(b)-4]), b[len(b)-4:]) {
53		return 0, nil, errors.New("checksum")
54	}
55	return b[0], b[1 : len(b)-4], nil
56}
57func polymod(v []byte) uint32 {
58	c := uint32(1)
59	g := []uint32{0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3}
60	for _, x := range v {
61		top := c >> 25
62		c = (c&0x1ffffff)<<5 ^ uint32(x)
63		for i, a := range g {
64			if top>>i&1 != 0 {
65				c ^= a
66			}
67		}
68	}
69	return c
70}
71func expand(h string) []byte {
72	v := []byte{}
73	for _, c := range []byte(h) {
74		v = append(v, c>>5)
75	}
76	v = append(v, 0)
77	for _, c := range []byte(h) {
78		v = append(v, c&31)
79	}
80	return v
81}
82func convert(data []byte, from, to uint, pad bool) ([]byte, error) {
83	var acc uint32
84	var bits uint
85	out := []byte{}
86	mask := uint32(1<<to) - 1
87	for _, v := range data {
88		if uint32(v)>>from != 0 {
89			return nil, errors.New("range")
90		}
91		acc = acc<<from | uint32(v)
92		bits += from
93		for bits >= to {
94			bits -= to
95			out = append(out, byte(acc>>bits&mask))
96		}
97	}
98	if pad {
99		if bits > 0 {
100			out = append(out, byte(acc<<(to-bits)&mask))
101		}
102	} else if bits >= from || (acc<<(to-bits)&mask) != 0 {
103		return nil, errors.New("padding")
104	}
105	return out, nil
106}
107func valid(hrp string, v byte, p []byte) bool {
108	return (hrp == "bc" || hrp == "tb" || hrp == "bcrt") && v <= 16 && len(p) >= 2 && len(p) <= 40 && (v != 0 || len(p) == 20 || len(p) == 32)
109}
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}
130func DecodeWitness(s, expected string) (byte, []byte, error) {
131	bad := errors.New("invalid witness address")
132	if len(s) > 90 || len(s) < 8 || (s != strings.ToLower(s) && s != strings.ToUpper(s)) {
133		return 0, nil, bad
134	}
135	s = strings.ToLower(s)
136	i := strings.LastIndexByte(s, '1')
137	if i < 1 || i+7 >= len(s) || s[:i] != expected {
138		return 0, nil, bad
139	}
140	d := []byte{}
141	for _, c := range s[i+1:] {
142		n := strings.IndexRune(charset, c)
143		if n < 0 {
144			return 0, nil, bad
145		}
146		d = append(d, byte(n))
147	}
148	v := d[0]
149	k := uint32(1)
150	if v != 0 {
151		k = 0x2bc830a3
152	}
153	if polymod(append(expand(expected), d...)) != k {
154		return 0, nil, bad
155	}
156	p, e := convert(d[1:len(d)-6], 5, 8, false)
157	if e != nil || !valid(expected, v, p) {
158		return 0, nil, bad
159	}
160	return v, p, nil
161}
162func WitnessScript(v byte, p []byte) ([]byte, error) {
163	if !valid("bc", v, p) {
164		return nil, errors.New("invalid program")
165	}
166	op := byte(0)
167	if v > 0 {
168		op = 0x50 + v
169	}
170	return append([]byte{op, byte(len(p))}, p...), nil
171}
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