From 990bf628ddfb6c96d4a9fad0bc3ba036d50cb9c2 Mon Sep 17 00:00:00 2001 From: Darren Kelly Date: Fri, 25 Oct 2024 10:38:18 +0100 Subject: [PATCH 1/2] ehancement(Bytes32): apply left hand padding in UnmarshalJSON --- thor/bytes32.go | 5 +++++ thor/bytes32_test.go | 14 +++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/thor/bytes32.go b/thor/bytes32.go index 109c4635a..3ecec00d0 100644 --- a/thor/bytes32.go +++ b/thor/bytes32.go @@ -55,6 +55,11 @@ func (b *Bytes32) UnmarshalJSON(data []byte) error { if err := json.Unmarshal(data, &hex); err != nil { return err } + hex = strings.TrimPrefix(hex, "0x") + // apply left padding + for i := len(hex); i < 32*2; i++ { + hex = "0" + hex + } parsed, err := ParseBytes32(hex) if err != nil { return err diff --git a/thor/bytes32_test.go b/thor/bytes32_test.go index 6da73f4f4..0d96b4944 100644 --- a/thor/bytes32_test.go +++ b/thor/bytes32_test.go @@ -11,7 +11,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestMarshalUnmarshal(t *testing.T) { +func TestBytes32_UnmarshalJSON(t *testing.T) { // Example hex string representing the value 100 originalHex := `"0x00000000000000000000000000000000000000000000000000006d6173746572"` // Note the enclosing double quotes for valid JSON string @@ -50,3 +50,15 @@ func TestMarshalUnmarshal(t *testing.T) { assert.NoError(t, err, "Marshaling should not produce an error") assert.Equal(t, `"0x0000000000000000000000000000000000000000000000000000000000000000"`, string(j)) } + +func TestBytes32_UnmarshalJSON_ShouldApplyPadding(t *testing.T) { + // Example hex string representing the value 100 + originalHex := `"0x01"` + + // Unmarshal JSON into HexOrDecimal256 + var unmarshaledValue Bytes32 + err := unmarshaledValue.UnmarshalJSON([]byte(originalHex)) + assert.NoError(t, err) + + assert.Equal(t, MustParseBytes32("0x0000000000000000000000000000000000000000000000000000000000000001"), unmarshaledValue) +} From 5cdf75b6c693b4194561ff568a54629cab3147b3 Mon Sep 17 00:00:00 2001 From: Darren Kelly Date: Fri, 25 Oct 2024 10:44:32 +0100 Subject: [PATCH 2/2] refactor(Bytes32): apply left padding --- thor/bytes32.go | 22 ++++++++-------------- thor/bytes32_test.go | 13 +++++-------- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/thor/bytes32.go b/thor/bytes32.go index 3ecec00d0..371ca7611 100644 --- a/thor/bytes32.go +++ b/thor/bytes32.go @@ -8,7 +8,6 @@ package thor import ( "encoding/hex" "encoding/json" - "errors" "fmt" "strings" @@ -55,11 +54,6 @@ func (b *Bytes32) UnmarshalJSON(data []byte) error { if err := json.Unmarshal(data, &hex); err != nil { return err } - hex = strings.TrimPrefix(hex, "0x") - // apply left padding - for i := len(hex); i < 32*2; i++ { - hex = "0" + hex - } parsed, err := ParseBytes32(hex) if err != nil { return err @@ -70,14 +64,14 @@ func (b *Bytes32) UnmarshalJSON(data []byte) error { // ParseBytes32 convert string presented into Bytes32 type func ParseBytes32(s string) (Bytes32, error) { - if len(s) == 32*2 { - } else if len(s) == 32*2+2 { - if strings.ToLower(s[:2]) != "0x" { - return Bytes32{}, errors.New("invalid prefix") - } - s = s[2:] - } else { - return Bytes32{}, errors.New("invalid length") + s = strings.TrimPrefix(s, "0x") + // if the string is too long, return error + if len(s) > 32*2 { + return Bytes32{}, fmt.Errorf("invalid length %d", len(s)) + } + // if the string is too short, add padding + if len(s) < 32*2 { + s = strings.Repeat("0", 32*2-len(s)) + s } var b Bytes32 diff --git a/thor/bytes32_test.go b/thor/bytes32_test.go index 0d96b4944..8c439daa6 100644 --- a/thor/bytes32_test.go +++ b/thor/bytes32_test.go @@ -51,14 +51,11 @@ func TestBytes32_UnmarshalJSON(t *testing.T) { assert.Equal(t, `"0x0000000000000000000000000000000000000000000000000000000000000000"`, string(j)) } -func TestBytes32_UnmarshalJSON_ShouldApplyPadding(t *testing.T) { +func TestParseBytes32(t *testing.T) { // Example hex string representing the value 100 - originalHex := `"0x01"` - - // Unmarshal JSON into HexOrDecimal256 - var unmarshaledValue Bytes32 - err := unmarshaledValue.UnmarshalJSON([]byte(originalHex)) + expected := MustParseBytes32("0x0000000000000000000000006d95e6dca01d109882fe1726a2fb9865fa41e7aa") + trimmed := "0x6d95e6dca01d109882fe1726a2fb9865fa41e7aa" + parsed, err := ParseBytes32(trimmed) assert.NoError(t, err) - - assert.Equal(t, MustParseBytes32("0x0000000000000000000000000000000000000000000000000000000000000001"), unmarshaledValue) + assert.Equal(t, expected, parsed) }