Migrating btcsuite Projects to V2 Modules
GOLANG + BITCOIN SCRIPT — Stage 001, Lesson 04 of 12.
Concept
Major-version upgrades can change import paths, APIs and type identities. Two transaction types from different import paths are distinct Go types even if their fields look identical. Never assume all btcsuite packages share a release number.
Engineering practice
Inspect the upstream module declaration and available tags using go list -m -versions MODULE. Read migration notes, select compatible versions, update imports together, and test serialized bytes, transaction IDs and signature vectors before deployment. The example is a dependency-free byte-order regression fixture, not a completed V2 migration. No V2 compatibility is claimed in this stage.
Source example
1package main
2
3import (
4 "encoding/hex"
5 "errors"
6 "fmt"
7)
8
9func displayHash(b []byte) (string, error) {
10 if len(b) != 32 {
11 return "", errors.New("expected 32 bytes")
12 }
13 var out [32]byte
14 for i := range b {
15 out[31-i] = b[i]
16 }
17 return hex.EncodeToString(out[:]), nil
18}
19func main() { b := make([]byte, 32); b[0] = 1; v, e := displayHash(b); fmt.Println(v, e) }Run locally
From the 001-foundations directory:
go test ./06-chainhash
go run ./06-chainhash
Regression test
1package main
2
3import (
4 "testing"
5)
6
7func TestExample(t *testing.T) {
8 b := make([]byte, 32)
9 b[0] = 1
10 v, e := displayHash(b)
11 if e != nil || v[62:] != "01" || b[0] != 1 {
12 t.Fatal(v, e)
13 }
14 if _, e = displayHash(nil); e == nil {
15 t.Fatal("length accepted")
16 }
17}Exercise
Reverse a known 32-byte sequence twice and confirm that the input is unchanged.
Reference
https://github.com/btcsuite/btcd
Series navigation
https://thamer.org/learn/bitcoin-development-libraries-go
Previous lesson:
https://thamer.org/learn/bitcoin-development-go-modules
Next lesson:
https://thamer.org/learn/bitcoin-bytes-hex-go
