Skip to content

Commit

Permalink
fix: eip55 proto encoding using hex string
Browse files Browse the repository at this point in the history
  • Loading branch information
CalicoNino committed Dec 6, 2024
1 parent 43578e2 commit 3081cff
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 94 deletions.
26 changes: 11 additions & 15 deletions eth/eip55.go
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"
Expand Down Expand Up @@ -36,53 +35,50 @@ func NewEIP55AddrFromStr(input string) (EIP55Addr, error) {
// 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.Address.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
}

// 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()))
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}
fmt.Printf("Unmarshal data: %s\n", data)
addr, err := NewEIP55AddrFromStr(string(data))
if err != nil {
return err
}
*h = addr
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))
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()))
}
32 changes: 27 additions & 5 deletions eth/eip55_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,15 @@ func (s *EIP55AddrSuite) TestProtobufEncoding() {
}{
{
input: threeValidAddrs[0],
expectedJson: "\"0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed\"",
expectedJson: "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
},
{
input: threeValidAddrs[1],
expectedJson: "\"0xAe967917c465db8578ca9024c205720b1a3651A9\"",
expectedJson: "0xAe967917c465db8578ca9024c205720b1a3651A9",
},
{
input: threeValidAddrs[2],
expectedJson: "\"0x1111111111111111111112222222222223333323\"",
expectedJson: "0x1111111111111111111112222222222223333323",
},
} {
s.Run(strconv.Itoa(tcIdx), func() {
Expand All @@ -140,15 +140,16 @@ func (s *EIP55AddrSuite) TestProtobufEncoding() {

bz, err := tc.input.Marshal()
s.NoError(err)
s.Equal(tc.input.Bytes(), bz,
s.Equal(tc.expectedJson, string(bz),
"Marshaling to bytes gives different value than the test case specifies. test case #%d", tcIdx)

err = eip55Addr.Unmarshal(bz)
s.NoError(err)
s.Equal(tc.input.Address, eip55Addr.Address,
"Given -> Marshal -> Unmarshal returns a different value than the given when it should be an identity operation (no-op). test case #%d", tcIdx)

s.Equal(len(tc.input.Bytes()), tc.input.Size())
s.Equal(len([]byte(tc.input.Hex())), tc.input.Size())
s.Equal(len(tc.input.Hex()), tc.input.Size())
})
}
}
Expand All @@ -173,3 +174,24 @@ type EIP55AddrSuite struct {
func TestEIP55AddrSuite(t *testing.T) {
suite.Run(t, new(EIP55AddrSuite))
}

func (s *EIP55AddrSuite) TestStringEncoding() {
addrHex := "0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"
addr := new(eth.EIP55Addr)
err := addr.Unmarshal([]byte(addrHex))
s.NoError(err)
s.Equal(addrHex, addr.Address.Hex())

addrBytes, err := addr.Marshal()
s.NoError(err)
s.Equal(addrHex, string(addrBytes))

bz, err := addr.MarshalJSON()
s.NoError(err)
s.Equal(addrHex, string(bz))

addrb := new(eth.EIP55Addr)
err = addrb.UnmarshalJSON([]byte(addrHex))
s.NoError(err)
s.EqualValues(addrb, addr)
}
2 changes: 1 addition & 1 deletion proto/eth/evm/v1/evm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ option go_package = "github.com/NibiruChain/nibiru/v2/x/evm";
// "Coin" type in Golang.
message FunToken {
// Hexadecimal address of the ERC20 token to which the `FunToken` maps
bytes erc20_addr = 1 [
string erc20_addr = 1 [
(gogoproto.customtype) = "github.com/NibiruChain/nibiru/v2/eth.EIP55Addr",
(gogoproto.nullable) = false
];
Expand Down
147 changes: 74 additions & 73 deletions x/evm/evm.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions x/evm/keeper/funtoken_from_erc20_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func (s *FunTokenFromErc20Suite) TestCreateFunTokenFromERC20() {
})
s.Require().NoError(err)

fmt.Printf("deployResp.ContractAddr.Hex(): %s\n", deployResp.ContractAddr.Hex())

erc20Addr := eth.EIP55Addr{
Address: deployResp.ContractAddr,
}
Expand Down

0 comments on commit 3081cff

Please sign in to comment.