BIP276 Decoding Fails with Hex Values and Incorrect Field Assignment
Bug Description
The BIP276 implementation has multiple critical bugs in the decoding logic:
- Regex pattern uses
\d{2} for hex fields, failing on values with letters (A-F)
- Parsing uses decimal base instead of hex base for network/version fields
- Network and version fields are assigned to wrong struct members
- Regex doesn't allow empty data in BIP276 strings
Steps to Reproduce
- Create a BIP276 script with network=255 (0xFF) or version=170 (0xAA)
- Encode the script using
EncodeBIP276()
- Try to decode the resulting string using
DecodeBIP276()
- Observe that decoding fails with regex mismatch or incorrect field values
Example code:
script := BIP276{
Prefix: "test",
Version: 170, // 0xAA
Network: 255, // 0xFF
Data: []byte("hello"),
}
encoded := EncodeBIP276(script)
// encoded = "test:ffaa68656c6c6f..."
decoded, err := DecodeBIP276(encoded)
// This should fail or return wrong values
Expected Behavior
- Encoding should produce valid BIP276 strings
- Decoding should correctly parse network/version as hex values
- Fields should be assigned correctly (network=255, version=170)
Actual Behavior
- Regex rejects strings with hex digits A-F (fails on "ff", "aa", etc.)
- If regex somehow matches, parsing fails because
strconv.Atoi() can't parse hex
- If parsing worked, fields would be swapped (network gets version value, vice versa)
Stack Traces or Screenshots
panic: invalid literal for int() with base 10: 'ff'
Environment
- Go version: 1.x
- @bsv/go-sdk version: current main branch
- OS: All
Additional Information
The bugs were in go-sdk/script/bip276.go:
-
Line 42 (before fix): regexp.MustCompile(^(.+?):(\d{2})(\d{2})([0-9A-Fa-f]+)([0-9A-Fa-f]{8})$)
- Should use
[0-9A-Fa-f]{2} for hex digits, not \d{2}
-
Lines 76-85 (before fix): Used strconv.Atoi() for hex values
- Should use
strconv.ParseInt(s, 16, 64)
-
Lines 76-85 (before fix): Wrong field assignment order
version, err := strconv.Atoi(res[2]) (should be network)
network, err := strconv.Atoi(res[3]) (should be version)
-
Line 42 (before fix): Data part used + instead of *
- Should allow empty data with
* instead of +
Root Cause: BIP276 network and version fields are encoded as 2-digit hex values (00-FF), but the implementation treated them as decimal digits.
Impact: Any BIP276 strings with hex values containing A-F letters cannot be decoded, breaking interoperability with the Python SDK and BIP276 specification compliance.
Proposed Solution
// Fix regex to accept hex digits and allow empty data
var validBIP276 = regexp.MustCompile(`^(.+?):([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]*)([0-9A-Fa-f]{8})$`)
// Fix parsing to use hex base
network, err := strconv.ParseInt(res[2], 16, 64)
s.Network = int(network)
version, err := strconv.ParseInt(res[3], 16, 64)
s.Version = int(version)
Priority: High - Breaking bug affecting BIP276 functionality
Type: Bug Fix
BIP276 Decoding Fails with Hex Values and Incorrect Field Assignment
Bug Description
The BIP276 implementation has multiple critical bugs in the decoding logic:
\d{2}for hex fields, failing on values with letters (A-F)Steps to Reproduce
EncodeBIP276()DecodeBIP276()Example code:
Expected Behavior
Actual Behavior
strconv.Atoi()can't parse hexStack Traces or Screenshots
Environment
Additional Information
The bugs were in
go-sdk/script/bip276.go:Line 42 (before fix):
regexp.MustCompile(^(.+?):(\d{2})(\d{2})([0-9A-Fa-f]+)([0-9A-Fa-f]{8})$)[0-9A-Fa-f]{2}for hex digits, not\d{2}Lines 76-85 (before fix): Used
strconv.Atoi()for hex valuesstrconv.ParseInt(s, 16, 64)Lines 76-85 (before fix): Wrong field assignment order
version, err := strconv.Atoi(res[2])(should be network)network, err := strconv.Atoi(res[3])(should be version)Line 42 (before fix): Data part used
+instead of**instead of+Root Cause: BIP276 network and version fields are encoded as 2-digit hex values (00-FF), but the implementation treated them as decimal digits.
Impact: Any BIP276 strings with hex values containing A-F letters cannot be decoded, breaking interoperability with the Python SDK and BIP276 specification compliance.
Proposed Solution
Priority: High - Breaking bug affecting BIP276 functionality
Type: Bug Fix