From e654fb9ef55bb5ac052c01f8bb9f63505f073043 Mon Sep 17 00:00:00 2001 From: SamMayWork Date: Thu, 7 Mar 2024 12:10:35 +0000 Subject: [PATCH 1/3] fix: allow for bytes to be truncated down Signed-off-by: SamMayWork --- pkg/ethtypes/hexbytes.go | 10 ++++++++- pkg/ethtypes/hexbytes_test.go | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/pkg/ethtypes/hexbytes.go b/pkg/ethtypes/hexbytes.go index 8b7ac2ef..2fb63643 100644 --- a/pkg/ethtypes/hexbytes.go +++ b/pkg/ethtypes/hexbytes.go @@ -1,4 +1,4 @@ -// Copyright © 2022 Kaleido, Inc. +// Copyright © 2024 Kaleido, Inc. // // SPDX-License-Identifier: Apache-2.0 // @@ -62,6 +62,14 @@ func (h HexBytes0xPrefix) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf(`"%s"`, h.String())), nil } +func (h HexBytes0xPrefix) Truncate(length int) (HexBytes0xPrefix, error) { + if length > len(h) { + return nil, fmt.Errorf("truncation length %d larger than length of hex bytes", length) + } + + return h[:length], nil +} + func NewHexBytes0xPrefix(s string) (HexBytes0xPrefix, error) { h, err := hex.DecodeString(strings.TrimPrefix(s, "0x")) if err != nil { diff --git a/pkg/ethtypes/hexbytes_test.go b/pkg/ethtypes/hexbytes_test.go index a3f939ce..a9fb70e8 100644 --- a/pkg/ethtypes/hexbytes_test.go +++ b/pkg/ethtypes/hexbytes_test.go @@ -90,3 +90,41 @@ func TestHexByteConstructors(t *testing.T) { MustNewHexBytes0xPrefix("!wrong") }) } + +func TestHexBytesTruncation(t *testing.T) { + testStruct := struct { + H1 HexBytes0xPrefix `json:"h1"` + H2 HexBytes0xPrefix `json:"h2"` + H3 HexBytes0xPrefix `json:"h3"` + H4 HexBytes0xPrefix `json:"h4"` + }{} + + testData := `{ + "h1": "0x34d2e4fef9de99a2688148de11174741d1399992f261dcd6ff019211a91eda748dcef26338e992f4df60b8dee3fdd28a", + "h2": "0x34d2e4fef9de99a2688148de11174741d1399992f261dcd6ff019211a91eda74", + "h3": "0x34d2e4fef9", + "h4": "0x34d2e4fef9de99a2688148de11174741d1399992f261dcd6ff019211a91eda748dcef26338e992" + }` + + err := json.Unmarshal([]byte(testData), &testStruct) + assert.NoError(t, err) + + result, err := testStruct.H1.Truncate(32) + assert.NoError(t, err) + testStruct.H1 = result + + result, err = testStruct.H2.Truncate(32) + assert.NoError(t, err) + testStruct.H2 = result + + result, err = testStruct.H3.Truncate(32) + assert.Error(t, err) + + result, err = testStruct.H4.Truncate(32) + assert.NoError(t, err) + testStruct.H4 = result + + assert.Equal(t, "0x34d2e4fef9de99a2688148de11174741d1399992f261dcd6ff019211a91eda74", testStruct.H1.String()) + assert.Equal(t, "0x34d2e4fef9de99a2688148de11174741d1399992f261dcd6ff019211a91eda74", testStruct.H2.String()) + assert.Equal(t, "0x34d2e4fef9de99a2688148de11174741d1399992f261dcd6ff019211a91eda74", testStruct.H4.String()) +} From bf9149fc271488b45c2542909b3266f4c35a1d4a Mon Sep 17 00:00:00 2001 From: SamMayWork Date: Fri, 10 May 2024 11:34:23 +0100 Subject: [PATCH 2/3] fix: remove truncation error Signed-off-by: SamMayWork --- pkg/ethtypes/hexbytes.go | 8 ++++---- pkg/ethtypes/hexbytes_test.go | 13 +++++-------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/pkg/ethtypes/hexbytes.go b/pkg/ethtypes/hexbytes.go index 2fb63643..adad8046 100644 --- a/pkg/ethtypes/hexbytes.go +++ b/pkg/ethtypes/hexbytes.go @@ -62,12 +62,12 @@ func (h HexBytes0xPrefix) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf(`"%s"`, h.String())), nil } -func (h HexBytes0xPrefix) Truncate(length int) (HexBytes0xPrefix, error) { +func (h HexBytes0xPrefix) Truncate(length int) (HexBytes0xPrefix) { if length > len(h) { - return nil, fmt.Errorf("truncation length %d larger than length of hex bytes", length) + return h } - - return h[:length], nil + + return h[:length] } func NewHexBytes0xPrefix(s string) (HexBytes0xPrefix, error) { diff --git a/pkg/ethtypes/hexbytes_test.go b/pkg/ethtypes/hexbytes_test.go index a9fb70e8..5c406bdf 100644 --- a/pkg/ethtypes/hexbytes_test.go +++ b/pkg/ethtypes/hexbytes_test.go @@ -109,22 +109,19 @@ func TestHexBytesTruncation(t *testing.T) { err := json.Unmarshal([]byte(testData), &testStruct) assert.NoError(t, err) - result, err := testStruct.H1.Truncate(32) - assert.NoError(t, err) + result := testStruct.H1.Truncate(32) testStruct.H1 = result - result, err = testStruct.H2.Truncate(32) - assert.NoError(t, err) + result = testStruct.H2.Truncate(32) testStruct.H2 = result - result, err = testStruct.H3.Truncate(32) - assert.Error(t, err) + result = testStruct.H3.Truncate(32) - result, err = testStruct.H4.Truncate(32) - assert.NoError(t, err) + result = testStruct.H4.Truncate(32) testStruct.H4 = result assert.Equal(t, "0x34d2e4fef9de99a2688148de11174741d1399992f261dcd6ff019211a91eda74", testStruct.H1.String()) assert.Equal(t, "0x34d2e4fef9de99a2688148de11174741d1399992f261dcd6ff019211a91eda74", testStruct.H2.String()) + assert.Equal(t, "0x34d2e4fef9", testStruct.H3.String()) assert.Equal(t, "0x34d2e4fef9de99a2688148de11174741d1399992f261dcd6ff019211a91eda74", testStruct.H4.String()) } From 2b549f38b4fc8903636ba1b70f77007b519e441c Mon Sep 17 00:00:00 2001 From: SamMayWork Date: Fri, 10 May 2024 11:39:10 +0100 Subject: [PATCH 3/3] style: update copyright messages Signed-off-by: SamMayWork --- pkg/ethtypes/hexbytes.go | 4 ++-- pkg/ethtypes/hexinteger_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/ethtypes/hexbytes.go b/pkg/ethtypes/hexbytes.go index adad8046..c2d8fbfc 100644 --- a/pkg/ethtypes/hexbytes.go +++ b/pkg/ethtypes/hexbytes.go @@ -62,11 +62,11 @@ func (h HexBytes0xPrefix) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf(`"%s"`, h.String())), nil } -func (h HexBytes0xPrefix) Truncate(length int) (HexBytes0xPrefix) { +func (h HexBytes0xPrefix) Truncate(length int) HexBytes0xPrefix { if length > len(h) { return h } - + return h[:length] } diff --git a/pkg/ethtypes/hexinteger_test.go b/pkg/ethtypes/hexinteger_test.go index 097f976a..973fc107 100644 --- a/pkg/ethtypes/hexinteger_test.go +++ b/pkg/ethtypes/hexinteger_test.go @@ -1,4 +1,4 @@ -// Copyright © 2022 Kaleido, Inc. +// Copyright © 2024 Kaleido, Inc. // // SPDX-License-Identifier: Apache-2.0 //