Skip to content

BIP 276 issue #286

Description

@F1r3Hydr4nt

BIP276 Decoding Fails with Hex Values and Incorrect Field Assignment

Bug Description

The BIP276 implementation has multiple critical bugs in the decoding logic:

  1. Regex pattern uses \d{2} for hex fields, failing on values with letters (A-F)
  2. Parsing uses decimal base instead of hex base for network/version fields
  3. Network and version fields are assigned to wrong struct members
  4. Regex doesn't allow empty data in BIP276 strings

Steps to Reproduce

  1. Create a BIP276 script with network=255 (0xFF) or version=170 (0xAA)
  2. Encode the script using EncodeBIP276()
  3. Try to decode the resulting string using DecodeBIP276()
  4. 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:

  1. 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}
  2. Lines 76-85 (before fix): Used strconv.Atoi() for hex values

    • Should use strconv.ParseInt(s, 16, 64)
  3. 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)
  4. 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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions