-
Notifications
You must be signed in to change notification settings - Fork 193
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
fix: Use canonical hexadecimal strings for Eip155 address encoding #2120
Changes from all commits
20cc19e
43578e2
3081cff
d1747ab
7c06ddd
aeacfbc
9aa15f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,7 +1,6 @@ | ||||||||||||||||||||||
package eth | ||||||||||||||||||||||
|
||||||||||||||||||||||
import ( | ||||||||||||||||||||||
"encoding/json" | ||||||||||||||||||||||
"fmt" | ||||||||||||||||||||||
|
||||||||||||||||||||||
sdk "github.com/cosmos/cosmos-sdk/types" | ||||||||||||||||||||||
|
@@ -36,53 +35,49 @@ | |||||||||||||||||||||
// Marshal implements the gogo proto custom type interface. | ||||||||||||||||||||||
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md | ||||||||||||||||||||||
func (h EIP55Addr) Marshal() ([]byte, error) { | ||||||||||||||||||||||
return h.Bytes(), nil | ||||||||||||||||||||||
return []byte(h.Address.Hex()), nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// MarshalJSON returns the [EIP55Addr] as JSON bytes. | ||||||||||||||||||||||
// Implements the gogo proto custom type interface. | ||||||||||||||||||||||
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md | ||||||||||||||||||||||
func (h EIP55Addr) MarshalJSON() ([]byte, error) { | ||||||||||||||||||||||
return json.Marshal(h.String()) | ||||||||||||||||||||||
return []byte(h.String()), nil | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Invalid JSON Encoding in Directly returning Proposed fix: func (h EIP55Addr) MarshalJSON() ([]byte, error) {
- return []byte(h.String()), nil
+ return json.Marshal(h.String())
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// MarshalTo serializes a EIP55Addr directly into a pre-allocated byte slice ("data"). | ||||||||||||||||||||||
// MarshalTo implements the gogo proto custom type interface. | ||||||||||||||||||||||
// Implements the gogo proto custom type interface. | ||||||||||||||||||||||
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md | ||||||||||||||||||||||
func (h *EIP55Addr) MarshalTo(data []byte) (n int, err error) { | ||||||||||||||||||||||
copy(data, h.Bytes()) | ||||||||||||||||||||||
copy(data, []byte(h.Address.Hex())) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect Serialization in The Suggested fix: func (h *EIP55Addr) MarshalTo(data []byte) (n int, err error) {
- copy(data, []byte(h.Address.Hex()))
+ copy(data, h.Address.Bytes())
return h.Size(), nil
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||
return h.Size(), nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// Unmarshal implements the gogo proto custom type interface. | ||||||||||||||||||||||
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md | ||||||||||||||||||||||
func (h *EIP55Addr) Unmarshal(data []byte) error { | ||||||||||||||||||||||
addr := gethcommon.BytesToAddress(data) | ||||||||||||||||||||||
*h = EIP55Addr{Address: addr} | ||||||||||||||||||||||
addr, err := NewEIP55AddrFromStr(string(data)) | ||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||
return err | ||||||||||||||||||||||
} | ||||||||||||||||||||||
*h = addr | ||||||||||||||||||||||
Comment on lines
+60
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect Deserialization in The Suggested fix: func (h *EIP55Addr) Unmarshal(data []byte) error {
- addr, err := NewEIP55AddrFromStr(string(data))
- if err != nil {
- return err
- }
- *h = addr
+ if len(data) != gethcommon.AddressLength {
+ return fmt.Errorf("invalid EIP55Addr length: %d", len(data))
+ }
+ h.Address = gethcommon.BytesToAddress(data)
return nil
} 📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||
return nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// UnmarshalJSON implements the gogo proto custom type interface. | ||||||||||||||||||||||
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md | ||||||||||||||||||||||
func (h *EIP55Addr) UnmarshalJSON(bz []byte) error { | ||||||||||||||||||||||
text := new(string) | ||||||||||||||||||||||
if err := json.Unmarshal(bz, text); err != nil { | ||||||||||||||||||||||
return err | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
addr, err := NewEIP55AddrFromStr(*text) | ||||||||||||||||||||||
addr, err := NewEIP55AddrFromStr(string(bz)) | ||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Incorrect JSON Decoding in The Proposed fix: func (h *EIP55Addr) UnmarshalJSON(bz []byte) error {
- addr, err := NewEIP55AddrFromStr(string(bz))
+ var addrStr string
+ if err := json.Unmarshal(bz, &addrStr); err != nil {
+ return err
+ }
+ addr, err := NewEIP55AddrFromStr(addrStr)
if err != nil {
return err
}
*h = addr
return nil
}
|
||||||||||||||||||||||
if err != nil { | ||||||||||||||||||||||
return err | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
*h = addr | ||||||||||||||||||||||
|
||||||||||||||||||||||
return nil | ||||||||||||||||||||||
} | ||||||||||||||||||||||
|
||||||||||||||||||||||
// Size implements the gogo proto custom type interface. | ||||||||||||||||||||||
// Ref: https://github.com/cosmos/gogoproto/blob/v1.5.0/custom_types.md | ||||||||||||||||||||||
func (h EIP55Addr) Size() int { | ||||||||||||||||||||||
return len(h.Bytes()) | ||||||||||||||||||||||
return len([]byte(h.Address.Hex())) | ||||||||||||||||||||||
CalicoNino marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||
} |
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.
Incorrect Protobuf Serialization Format in
Marshal
MethodThe
Marshal
method should return the raw bytes of the address to conform with protobuf serialization expectations. Returning the hexadecimal string representation ([]byte(h.Address.Hex())
) may lead to serialization issues.Suggested fix:
📝 Committable suggestion