-
Notifications
You must be signed in to change notification settings - Fork 13
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
Conversation
Signed-off-by: SamMayWork <sam.may@kaleido.io>
pkg/ethtypes/hexbytes.go
Outdated
@@ -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) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
Signed-off-by: SamMayWork <sam.may@kaleido.io>
Signed-off-by: SamMayWork <sam.may@kaleido.io>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@SamMayWork - could do with a little explanation of why firefly-signer
common utility functions are the right place for []byte
truncation functions?
@@ -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 { |
There was a problem hiding this comment.
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?
Not required. |
Pre-Reqs: hyperledger/firefly-evmconnect#118
Hedera uses 48 bytes hashes for their blocks, for compatibility with FireFly we need to truncate down to 32 bytes - this is an entirely supported feature of the JSON RPC layer for Hedera, this PR adds the function for us to be able to do this.