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

Bitcoin Transaction CompactSize Serialization in Go

permalink THAMER
#bitcoin#bitcoin-script#golang#transactions#utxo

CompactSize prefixes vector counts and byte-string lengths. Values below 253 occupy one byte; larger values use a discriminator followed by a little-endian integer. It is not the Script numeric encoding.

Go implementation

Go · 16 lines
34func compact(b *bytes.Buffer, n uint64) {
35	switch {
36	case n < 253:
37		b.WriteByte(byte(n))
38	case n <= 65535:
39		b.WriteByte(253)
40		binary.Write(b, binary.LittleEndian, uint16(n))
41	case n <= 0xffffffff:
42		b.WriteByte(254)
43		binary.Write(b, binary.LittleEndian, uint32(n))
44	default:
45		b.WriteByte(255)
46		binary.Write(b, binary.LittleEndian, n)
47	}
48}
49func blob(b *bytes.Buffer, data []byte) { compact(b, uint64(len(data))); b.Write(data) }

Practical exercise

Encode 252 and 253 and compare fc with fdfd00.

Local verification

From the repository directory golang-bitcoin/006-transactions, run:

go test -race -v ./...
go vet ./...
Go · 57 lines
1package tx
2
3import (
4	"encoding/hex"
5	"strings"
6	"testing"
7)
8
9func fixture() Transaction {
10	return Transaction{Version: 2, Inputs: []Input{{Previous: OutPoint{Hash: [32]byte{1}, Index: 3}, Sequence: 0xfffffffd}}, Outputs: []Output{{Value: 9000, Script: []byte{0x51}}}}
11}
12func TestSerialization(t *testing.T) {
13	x := fixture()
14	want := "020000000101" + strings.Repeat("00", 31) + "0300000000fdffffff012823000000000000015100000000"
15	if got := hex.EncodeToString(x.Serialize(false)); got != want {
16		t.Fatal(got, want)
17	}
18	if x.TXID() != x.WTXID() {
19		t.Fatal("legacy hash mismatch")
20	}
21}
22func TestWitness(t *testing.T) {
23	x := fixture()
24	id := x.TXID()
25	x.Inputs[0].Witness = [][]byte{{1, 2}}
26	if x.TXID() != id || x.WTXID() == id {
27		t.Fatal("hash separation")
28	}
29	if x.Weight() != 250 || x.VSize() != 63 {
30		t.Fatal(x.Weight(), x.VSize())
31	}
32}
33func TestFee(t *testing.T) {
34	x := fixture()
35	coins := map[OutPoint]Output{x.Inputs[0].Previous: {Value: 10000}}
36	f, e := x.Fee(coins)
37	if e != nil || f != 1000 {
38		t.Fatal(f, e)
39	}
40	if _, e = x.Fee(nil); e == nil {
41		t.Fatal("missing coin")
42	}
43	x.Inputs = append(x.Inputs, x.Inputs[0])
44	if _, e = x.Fee(coins); e == nil {
45		t.Fatal("duplicate")
46	}
47}
48func TestAmounts(t *testing.T) {
49	x := fixture()
50	coins := map[OutPoint]Output{x.Inputs[0].Previous: {Value: 10000}}
51	for _, v := range []int64{-1, 10001, MaxMoney + 1} {
52		x.Outputs[0].Value = v
53		if _, e := x.Fee(coins); e == nil {
54			t.Fatal(v)
55		}
56	}
57}

Scope and security

This offline educational serializer uses synthetic data. It does not parse untrusted transactions, sign, broadcast, execute scripts, enforce locktimes, or validate consensus and mempool policy. The supplied UTXO map is not authenticated chain state. Do not use these examples to spend real funds.

References

https://developer.bitcoin.org/reference/transactions.html

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

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

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

https://github.com/btcsuite/btcd/tree/master/wire

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