Skip to content

refactor: Go 1.25 modernization, dedup, and test suite overhaul - #354

Merged
mrz1836 merged 5 commits into
masterfrom
go125-modernization
Aug 22, 2026
Merged

mrz1836 merged 5 commits into
masterfrom
go125-modernization

Conversation

@mrz1836

@mrz1836 mrz1836 commented Aug 22, 2026

Copy link
Copy Markdown
Member

What Changed

A Go 1.25 modernization and deduplication pass. The public API, wire formats, and behavior are unchanged.

Production internals (behavior-preserving):

  • Extracted shared logic into unexported helpers: privateKeyFromHex (3 call sites), ECIES deriveKeys, collapsed the identical HD-key string wrappers, and looped GetPublicKeysForPath's twin blocks.
  • Reused hash.Sha256d for the address double-SHA256 (removed the hand-rolled crypto/sha256 version).
  • Adopted modern idioms: slices.Backward, for range n, for b.Loop() in benchmarks, and s == "" for string emptiness.
  • Tidied transaction.go: merged redundant fee guards and hoisted OP_RETURN prefix constants.

Tests:

  • Converted the legacy anonymous-table + if/else/t.Fatalf tests to t.Run named subtests with t.Parallel() and testify require/assert.
  • Centralized duplicated constants into test_constants_test.go and added reusable fixtures/helpers (newTestUtxo, newTestOpReturns, mustTestPrivKey, mustHDKey).
  • Added coverage for DefaultStandardFee, GenerateSharedKeyPair, and the A25 accessors, plus fuzz targets for TxFromHex and DecodeWIF.

Security/tooling:

  • Refreshed .gitleaksignore: the test refactor relocated test-vector lines, so the existing file:line fingerprints no longer matched. Regenerated the allowlist against the current tree. All entries are documentation/test fixtures — not real secrets.

Docs: README feature list now documents a few previously-omitted public functions (Address from PubKey, Validate a Base58 Address, PubKey from Signature, Create Tx with Change using WIF).

Why It Was Necessary

The library is already on Go 1.25 but carried pre-1.22 idioms, hand-rolled implementations that duplicated stdlib/SDK functionality, and two eras of test style. This pass removes duplication (a maintenance hazard), aligns the code with modern Go, and makes the test suite uniform, parallel, and better-covered — without touching the widely-imported public API.

Testing Performed

  • magex test / magex test:race — pass
  • magex lint (golangci-lint, 62 linters + go vet) — 0 issues
  • staticcheck ./..., gofumpt -l . — clean
  • go-pre-commit run --all-files — pass, including gitleaks
  • gitleaks detect in both --no-git and git-log modes — no leaks found
  • Wire-format known-answer vectors (ECIES/WIF) and all address/sign/verify vectors remain green
  • Fuzz targets smoke-tested; example programs still build and run

Impact / Risk

Low. No exported signature, name, or behavior changes — a drop-in for all downstream consumers. Error strings are unchanged (error wrapping intentionally not introduced). Crypto-internal refactors are guarded by the existing byte-for-byte compatibility vectors.

Notifications

Dedup shared logic into unexported helpers (privateKeyFromHex, ecies deriveKeys,
HD-key wrappers, GetPublicKeysForPath loop), reuse hash.Sha256d for the address
double-SHA256, adopt slices.Backward and for-range loops, tidy fee guards, and
add OP_RETURN prefix constants. Public API and wire formats unchanged.
Convert legacy if/else tables to t.Run named subtests with t.Parallel() and
testify, centralize shared constants, add reusable fixtures/helpers, and add
tests for DefaultStandardFee, GenerateSharedKeyPair, A25 accessors plus fuzz
targets for TxFromHex and DecodeWIF.
The test-suite modernization moved test-vector lines, so the existing
file:line fingerprints in .gitleaksignore no longer matched. Regenerated the
allowlist against the current tree so all known test fixtures (not real
secrets) are covered. Verified clean in both no-git and git-log scan modes.
Copilot AI lite review requested due to automatic review settings August 22, 2026 17:29
@github-actions github-actions Bot added refactor Any significant refactoring test Unit tests, mocking, integration testing labels Aug 22, 2026
@github-actions github-actions Bot added the size/XL Very large change (>500 lines) label Aug 22, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR modernizes the Go codebase for Go 1.25, deduplicates internal implementations, and overhauls the test suite toward parallel subtests, shared fixtures, and added fuzz/coverage targets—while aiming to preserve the public API and runtime behavior.

Changes:

  • Refactors internals to reuse shared helpers/stdlib equivalents (e.g., ECIES key derivation helper, address double-SHA via hash.Sha256d, transaction cleanup).
  • Standardizes tests to t.Run subtests with t.Parallel() plus shared test fixtures/helpers and new fuzz targets.
  • Updates docs and tooling metadata (README feature list, .gitleaksignore fingerprints).

Reviewed changes

Copilot reviewed 32 out of 32 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
wif.go Uses modern for range <int> loop form in padding helper.
wif_test.go Removes in-file WIF constants in favor of shared test constants.
wif_fuzz_test.go Adds fuzz target for DecodeWIF.
verify_test.go Converts table tests to named parallel subtests; modernizes benchmarks.
transaction.go Adds OP_RETURN prefix constants, uses slices.Backward, tightens fee clamping logic.
transaction_test.go Converts tests/benchmarks to subtests + helpers; reduces duplication.
transaction_fuzz_test.go Adds fuzz target for TxFromHex.
transaction_creation_test.go Expands extended transaction tests; introduces more parallel subtests and helper usage.
testhelpers_test.go Introduces shared test helpers (newTestUtxo, newTestOpReturns, mustTestPrivKey, mustHDKey).
test_constants_test.go Centralizes frequently reused test constants and case-name strings.
sign.go Updates empty-string checks to idiomatic == "".
sign_verify_extended_test.go Adds t.Parallel() to many subtests.
sign_test.go Refactors to require-based assertions and parallel subtests.
script.go Updates empty-string checks to idiomatic == "".
script_test.go Refactors to named parallel subtests with testify assertions.
README.md Documents additional public functions in the feature list.
pubkey.go Updates empty-string checks to idiomatic == "".
pubkey_test.go Refactors to named parallel subtests; reuses shared constants.
private_key.go Deduplicates hex private-key parsing via privateKeyFromHex; updates empty checks.
private_key_test.go Refactors to named parallel subtests; reuses shared constants; modernizes benchmarks.
private_key_fuzz_test.go Updates fuzz seeds to use shared WIF constants.
misc_functions_test.go Adds t.Parallel() to many subtests and reuses helper functions.
hd_key.go Deduplicates path pubkey derivation; routes xpub parsing through GenerateHDKeyFromString.
hd_key_test.go Refactors many tests to named parallel subtests and updated fixtures.
encryption.go Reuses existing primitives (EncryptShared) and simplifies private-key parsing call path.
encryption_test.go Refactors to named parallel subtests with stronger assertions.
ecies.go Extracts shared key-derivation logic into helper deriveKeys.
ecies_test.go Adds parallel subtests and shared case-name usage.
coverage_gaps_test.go Adds coverage for fee defaults, shared keypair symmetry, and A25 accessors.
address.go Replaces hand-rolled double-SHA256 with hash.Sha256d; modernizes empty checks.
address_test.go Refactors to named parallel subtests with testify assertions.
.gitleaksignore Updates allowlist fingerprints to match relocated/edited test vectors.
Suppressed comments (2)

hd_key_test.go:288

  • Same issue here: the table entry uses XOR (^) instead of exponentiation, so the "2^31+1" / "2^32-1" values do not match the test name intent and won’t cover boundary derivation cases. Use bit shifts / max constants (e.g., (uint32(1)<<31)+1 and math.MaxUint32).
		{"chain 10 num 10", validKey, 10, 10, false, false},
		{"chain 100 num 100", validKey, 100, 100, false, false},
		{"chain 2^31+1 num 2^32-1", validKey, 2 ^ 31 + 1, 2 ^ 32 - 1, false, false},
		{"chain 1<<8 num 1<<8", validKey, 1 << 8, 1 << 8, false, false},

hd_key_test.go:401

  • This test-case uses XOR (^) in "2 ^ 31 + 1", which evaluates to 30, not (1<<31)+1. If the intent is to exercise a high-bit child index, switch to (uint32(1)<<31)+1 (or a similar boundary) to match the name and improve coverage.
		{"num 10", validKey, 10, false, false},
		{"num 100", validKey, 100, false, false},
		{"num 2^31+1", validKey, 2 ^ 31 + 1, false, false},
		{"num 1<<8", validKey, 1 << 8, false, false},
		{"num 1<<9", validKey, 1 << 9, false, false},

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread transaction.go Resolved
Comment thread hd_key_test.go Resolved
The chain/num values used 2 ^ 31 + 1 and 2 ^ 32 - 1, which are XOR (30 and 33)
in Go, not the intended 2^31+1 / 2^32-1 boundaries. Use shift expressions
(1<<31 + 1, 1<<32 - 1) to match the case names and actually exercise high-bit
hardened derivation, consistent with the sibling 1<<N cases.
@mrz1836
mrz1836 merged commit 7021151 into master Aug 22, 2026
24 checks passed
@github-actions
github-actions Bot deleted the go125-modernization branch August 22, 2026 18:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactor Any significant refactoring size/XL Very large change (>500 lines) test Unit tests, mocking, integration testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants