A minimal, spec-focused, and educational implementation of the Bitcoin Partially Signed Bitcoin Transaction (PSBT) format written in Go.
This library aims to provide a clean and readable reference implementation of PSBT while staying close to the official Bitcoin Improvement Proposals.
This project implements the PSBT specifications defined in:
BIP 174 — PSBT v0 (Partially Signed Bitcoin Transactions)
BIP 370 — PSBT v2 (PSBT Version 2)
These specifications define a standardized format for constructing, signing, and finalizing Bitcoin transactions across multiple participants.
🎓 Educational — clear and readable implementation for learning PSBT internals
📜 Spec-focused — follows the BIP specifications closely
🧱 Simple architecture — minimal abstractions and easy to understand
🔄 Multi-version support — supports PSBT v0 and PSBT v2
🛡 Safe defaults — built-in validation and structural checks
✅ PSBT decoding from binary format
✅ Global/Input/Output map parsing
✅ Version detection (v0 / v2)
✅ Duplicate key detection
✅ CompactSize integer parsing
✅ Structural validation
✅ Unsigned transaction parsing (v0)
✅ Transaction reconstruction (v2)
✅ Field extraction helpers
✅ Decode() — parse PSBT
✅ DecodeAndValidate() — safe decoding
✅ GetVersion() — detect PSBT version
✅ IsV0() / IsV2() helpers
✅ Reconstruct() — build transaction from PSBT
psbt/
├── psbt.go # PSBT decoding logic
├── reconstruct.go # Transaction reconstruction
├── fields.go # PSBT field extraction helpers
├── version.go # Version helpers
├── api.go # Public convenience APIs
├── errors.go # Custom error types
│
├── types/
│ └── types.go # Core PSBT + transaction structures
│
├── internal/
│ ├── parser/
│ │ └── map.go # PSBT map parsing
│ │
│ ├── compactsize/ # Bitcoin CompactSize integers
│ │ ├── read.go
│ │ ├── write.go
│ │ └── frombytes.go
│ │
│ └── validate/
│ └── validate.go # Structural PSBT validation
│
├── go.mod
└── README.md
go get github.com/Techlateef/psbtpackage main
import (
"fmt"
"os"
"github.com/Techlateef/psbt"
)
func main() {
file, err := os.Open("example.psbt")
if err != nil {
panic(err)
}
defer file.Close()
psbtData, err := psbt.DecodeAndValidate(file)
if err != nil {
panic(err)
}
fmt.Printf(
"PSBT contains %d inputs and %d outputs\n",
len(psbtData.Inputs),
len(psbtData.Outputs),
)
}version, err := psbt.GetVersion(p)
if err != nil {
panic(err)
}
fmt.Println("PSBT Version:", version)
isV2, _ := psbt.IsV2(p)
if isV2 {
fmt.Println("PSBT is version 2")
}tx, err := psbt.Reconstruct(p)
if err != nil {
panic(err)
}
fmt.Println("Transaction Version:", tx.Version)
fmt.Println("Inputs:", len(tx.Inputs))
fmt.Println("Outputs:", len(tx.Outputs))package main
import (
"fmt"
"os"
"github.com/Techlateef/psbt"
)
func main() {
file, err := os.Open("example.psbt")
if err != nil {
panic(err)
}
defer file.Close()
p, err := psbt.Decode(file)
if err != nil {
panic(err)
}
// Access global map data
for _, kv := range p.Global.Pairs {
fmt.Printf("Global key: %x, value: %x\n", kv.Key, kv.Value)
}
// Access input maps
for i, input := range p.Inputs {
fmt.Printf("Input %d has %d key-value pairs\n", i, len(input.Pairs))
}
// Access output maps
for i, output := range p.Outputs {
fmt.Printf("Output %d has %d key-value pairs\n", i, len(output.Pairs))
}
}type PSBT struct {
Global PSBTMap
Inputs []PSBTMap
Outputs []PSBTMap
}
type PSBTMap struct {
Pairs []KV
}
type KV struct {
Key []byte
Value []byte
}Transaction types:
type Transaction struct {
Version uint32
Inputs []TxInput
Outputs []TxOutput
LockTime uint32
}
type TxInput struct {
PreviousOutput OutPoint
ScriptSig []byte
Sequence uint32
}
type TxOutput struct {
Value uint64
ScriptPubKey []byte
}
type OutPoint struct {
Hash [32]byte
Index uint32
}The validation system checks:
- Duplicate keys in PSBT maps
- Proper map structure
- PSBT magic bytes (
psbt\xff) - Correct CompactSize encodings
- Required fields for PSBT v0 and v2
🚧 Active Development
Current focus areas:
- improving validation
- extending PSBT utilities
- expanding test coverage
- PSBT serialization (encoding)
- PSBT combining
- Improved validation rules
- Comprehensive test suite
- Transaction signing
- PSBT finalization
- Hardware wallet compatibility
- CLI utilities
Contributions are welcome. This project values:
- spec-accurate implementations
- readable and well-structured code
- meaningful validation checks
- thorough testing
MIT License