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 all commits
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 {
if length > len(h) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure what this PR does sorry @SamMayWork

It seems like this is just a bytes manipulation function - surely that could just be done directly by the calling function?
Why is this a helper that is unique to the HexBytes0xPrefix type wrapper for String() formatting?

return h
}

return h[:length]
}

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

result = testStruct.H2.Truncate(32)
testStruct.H2 = result

result = testStruct.H3.Truncate(32)

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())
}
2 changes: 1 addition & 1 deletion pkg/ethtypes/hexinteger_test.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
Loading