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

wire.go

TEXT FILE

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

Download / RawBack to folder
Go · 144 lines
1package wirelesson
2
3import (
4	"bytes"
5	"crypto/sha256"
6	"encoding/binary"
7	"errors"
8	"io"
9)
10
11const Mainnet uint32 = 0xd9b4bef9
12const Testnet3 uint32 = 0x0709110b
13const MaxPayload = 4_000_000 // educational receive limit
14
15func CompactSize(n uint64) []byte {
16	var b [9]byte
17	switch {
18	case n < 253:
19		return []byte{byte(n)}
20	case n <= 65535:
21		b[0] = 253
22		binary.LittleEndian.PutUint16(b[1:], uint16(n))
23		return b[:3]
24	case n <= 0xffffffff:
25		b[0] = 254
26		binary.LittleEndian.PutUint32(b[1:], uint32(n))
27		return b[:5]
28	default:
29		b[0] = 255
30		binary.LittleEndian.PutUint64(b[1:], n)
31		return b[:]
32	}
33}
34
35func ReadCompactSize(r io.Reader) (uint64, error) {
36	var b [8]byte
37	if _, e := io.ReadFull(r, b[:1]); e != nil {
38		return 0, e
39	}
40	p := b[0]
41	size := 0
42	var min uint64
43	switch p {
44	case 253:
45		size = 2
46		min = 253
47	case 254:
48		size = 4
49		min = 65536
50	case 255:
51		size = 8
52		min = 0x100000000
53	default:
54		return uint64(p), nil
55	}
56	b = [8]byte{}
57	if _, e := io.ReadFull(r, b[:size]); e != nil {
58		return 0, e
59	}
60	n := binary.LittleEndian.Uint64(b[:])
61	if n < min {
62		return 0, errors.New("noncanonical CompactSize")
63	}
64	return n, nil
65}
66
67func Checksum(payload []byte) [4]byte {
68	a := sha256.Sum256(payload)
69	b := sha256.Sum256(a[:])
70	return [4]byte(b[:4])
71}
72
73func validCommand(s string) bool {
74	if len(s) == 0 || len(s) > 12 {
75		return false
76	}
77	for _, c := range []byte(s) {
78		if c < 32 || c > 126 {
79			return false
80		}
81	}
82	return true
83}
84
85func Encode(magic uint32, command string, payload []byte) ([]byte, error) {
86	if !validCommand(command) || len(payload) > MaxPayload {
87		return nil, errors.New("invalid message")
88	}
89	b := make([]byte, 24+len(payload))
90	binary.LittleEndian.PutUint32(b, magic)
91	copy(b[4:16], command)
92	binary.LittleEndian.PutUint32(b[16:20], uint32(len(payload)))
93	c := Checksum(payload)
94	copy(b[20:24], c[:])
95	copy(b[24:], payload)
96	return b, nil
97}
98
99func Decode(r io.Reader, magic uint32) (string, []byte, error) {
100	var h [24]byte
101	if _, e := io.ReadFull(r, h[:]); e != nil {
102		return "", nil, e
103	}
104	if binary.LittleEndian.Uint32(h[:4]) != magic {
105		return "", nil, errors.New("wrong network")
106	}
107	cmd := string(bytes.TrimRight(h[4:16], "\x00"))
108	if !validCommand(cmd) {
109		return "", nil, errors.New("invalid command")
110	}
111	n := binary.LittleEndian.Uint32(h[16:20])
112	if n > MaxPayload {
113		return "", nil, errors.New("payload limit")
114	}
115	p := make([]byte, int(n))
116	if _, e := io.ReadFull(r, p); e != nil {
117		return "", nil, e
118	}
119	c := Checksum(p)
120	if !bytes.Equal(c[:], h[20:24]) {
121		return "", nil, errors.New("checksum mismatch")
122	}
123	return cmd, p, nil
124}
125
126func NoncePayload(n uint64) []byte {
127	b := make([]byte, 8)
128	binary.LittleEndian.PutUint64(b, n)
129	return b
130}
131func ReadNonce(p []byte) (uint64, error) {
132	if len(p) != 8 {
133		return 0, errors.New("nonce must be eight bytes")
134	}
135	return binary.LittleEndian.Uint64(p), nil
136}
137
138// VersionPrefix decodes only the mandatory initial fields, not the full version message.
139func VersionPrefix(p []byte) (int32, uint64, int64, error) {
140	if len(p) < 20 {
141		return 0, 0, 0, io.ErrUnexpectedEOF
142	}
143	return int32(binary.LittleEndian.Uint32(p)), binary.LittleEndian.Uint64(p[4:]), int64(binary.LittleEndian.Uint64(p[12:])), nil
144}
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