Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow for truncation of 0x prefix hashes #61

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion pkg/ethtypes/hexbytes.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright © 2022 Kaleido, Inc.
// Copyright © 2024 Kaleido, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand Down Expand Up @@ -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)
}
Copy link
Contributor

@Chengxuan Chengxuan Apr 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a common behaviour that truncates throws an error when the length is smaller than the truncate size.

It seems the truncate size conveys the constraint of minSize implicitly, may worth thinking making that explicit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree - changed the logic here to return the original hex bytes if the length of the bytes is less than the desired length


return h[:length], nil
}

func NewHexBytes0xPrefix(s string) (HexBytes0xPrefix, error) {
h, err := hex.DecodeString(strings.TrimPrefix(s, "0x"))
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions pkg/ethtypes/hexbytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Loading