Skip to content

Commit 9793268

Browse files
committed
Strip 0x prefix from hex-encoded fields in bob responses
Bob's bytesToHex returns hex strings with a 0x prefix (e.g. signatures, timelock, inputData). Our hex decode helpers now handle this gracefully.
1 parent 854df38 commit 9793268

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

network/bob/convert.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ package bob
33
import (
44
"encoding/hex"
55
"fmt"
6+
"strings"
67

78
"github.com/qubic/go-node-connector/types"
89
)
910

11+
// stripHexPrefix removes a "0x" or "0X" prefix if present.
12+
func stripHexPrefix(s string) string {
13+
return strings.TrimPrefix(strings.TrimPrefix(s, "0x"), "0X")
14+
}
15+
1016
// qubicHashToBytes32 converts a Qubic hash (60-char lowercase identity encoding)
1117
// to a [32]byte public key.
1218
func qubicHashToBytes32(qubicHash string) ([32]byte, error) {
@@ -34,13 +40,13 @@ func identityToBytes32(identity string) ([32]byte, error) {
3440
return pubKey, nil
3541
}
3642

37-
// hexToBytes32 converts a hex-encoded string to [32]byte.
43+
// hexToBytes32 converts a hex-encoded string (with optional 0x prefix) to [32]byte.
3844
func hexToBytes32(hexStr string) ([32]byte, error) {
3945
var result [32]byte
4046
if len(hexStr) == 0 {
4147
return result, nil
4248
}
43-
decoded, err := hex.DecodeString(hexStr)
49+
decoded, err := hex.DecodeString(stripHexPrefix(hexStr))
4450
if err != nil {
4551
return result, fmt.Errorf("decoding hex: %w", err)
4652
}
@@ -51,13 +57,13 @@ func hexToBytes32(hexStr string) ([32]byte, error) {
5157
return result, nil
5258
}
5359

54-
// hexToBytes64 converts a hex-encoded string to [64]byte (for signatures).
60+
// hexToBytes64 converts a hex-encoded string (with optional 0x prefix) to [64]byte (for signatures).
5561
func hexToBytes64(hexStr string) ([64]byte, error) {
5662
var result [64]byte
5763
if len(hexStr) == 0 {
5864
return result, nil
5965
}
60-
decoded, err := hex.DecodeString(hexStr)
66+
decoded, err := hex.DecodeString(stripHexPrefix(hexStr))
6167
if err != nil {
6268
return result, fmt.Errorf("decoding hex: %w", err)
6369
}
@@ -68,12 +74,12 @@ func hexToBytes64(hexStr string) ([64]byte, error) {
6874
return result, nil
6975
}
7076

71-
// hexToBytes converts a hex-encoded string to a byte slice.
77+
// hexToBytes converts a hex-encoded string (with optional 0x prefix) to a byte slice.
7278
func hexToBytes(hexStr string) ([]byte, error) {
7379
if len(hexStr) == 0 {
7480
return nil, nil
7581
}
76-
return hex.DecodeString(hexStr)
82+
return hex.DecodeString(stripHexPrefix(hexStr))
7783
}
7884

7985
// convertVote converts a bob vote JSON to types.QuorumTickVote.

0 commit comments

Comments
 (0)