From 8fcf6053a2146d26167c15118d895c6fdd7c30fc Mon Sep 17 00:00:00 2001 From: Unique-Divine Date: Thu, 9 Jan 2025 02:14:46 -0600 Subject: [PATCH] fix(evm): JSON encoding for the `EIP55Addr` struct was not following the Go conventions and needed to include double quotes around the hexadecimal string. --- eth/eip55.go | 11 +++++++++-- eth/eip55_test.go | 14 ++++++++------ x/evm/msg_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/eth/eip55.go b/eth/eip55.go index b4e66a4d0..36f781d24 100644 --- a/eth/eip55.go +++ b/eth/eip55.go @@ -1,6 +1,7 @@ package eth import ( + "encoding/json" "fmt" sdk "github.com/cosmos/cosmos-sdk/types" @@ -42,7 +43,7 @@ func (h EIP55Addr) Marshal() ([]byte, error) { // 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 []byte(h.String()), nil + return json.Marshal(h.String()) } // MarshalTo serializes a EIP55Addr directly into a pre-allocated byte slice ("data"). @@ -68,7 +69,13 @@ func (h *EIP55Addr) Unmarshal(data []byte) error { // 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 { - addr, err := NewEIP55AddrFromStr(string(bz)) + var addrStr string + if err := json.Unmarshal(bz, &addrStr); err != nil { + return fmt.Errorf( + "EIP55AddrError: UnmarhsalJSON had invalid input %s: %w", bz, err, + ) + } + addr, err := NewEIP55AddrFromStr(addrStr) if err != nil { return err } diff --git a/eth/eip55_test.go b/eth/eip55_test.go index d927d9447..a2970e5e2 100644 --- a/eth/eip55_test.go +++ b/eth/eip55_test.go @@ -1,7 +1,9 @@ package eth_test import ( + "fmt" "strconv" + "strings" "testing" gethcommon "github.com/ethereum/go-ethereum/common" @@ -116,15 +118,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() { @@ -140,7 +142,7 @@ func (s *EIP55AddrSuite) TestProtobufEncoding() { bz, err := tc.input.Marshal() s.NoError(err) - s.Equal(tc.expectedJson, string(bz), + s.Equal(strings.Trim(tc.expectedJson, `"`), string(bz), "Marshaling to bytes gives different value than the test case specifies. test case #%d", tcIdx) err = eip55Addr.Unmarshal(bz) @@ -188,10 +190,10 @@ func (s *EIP55AddrSuite) TestStringEncoding() { bz, err := addr.MarshalJSON() s.NoError(err) - s.Equal(addrHex, string(bz)) + s.Equal(fmt.Sprintf(`"%s"`, addrHex), string(bz)) addrb := new(eth.EIP55Addr) - err = addrb.UnmarshalJSON([]byte(addrHex)) + err = addrb.UnmarshalJSON([]byte(fmt.Sprintf(`"%s"`, addrHex))) s.NoError(err) s.EqualValues(addrb, addr) } diff --git a/x/evm/msg_test.go b/x/evm/msg_test.go index e51c2b9a3..37bfb5dac 100644 --- a/x/evm/msg_test.go +++ b/x/evm/msg_test.go @@ -1,6 +1,7 @@ package evm_test import ( + "encoding/json" "fmt" "math" "math/big" @@ -19,6 +20,7 @@ import ( "github.com/stretchr/testify/suite" "github.com/NibiruChain/nibiru/v2/app" + "github.com/NibiruChain/nibiru/v2/eth" "github.com/NibiruChain/nibiru/v2/eth/crypto/ethsecp256k1" "github.com/NibiruChain/nibiru/v2/eth/encoding" "github.com/NibiruChain/nibiru/v2/x/evm" @@ -980,3 +982,43 @@ func (s *MsgsSuite) TestTransactionLogsEncodeDecode() { s.Nil(decodeErr) s.Equal(txLogs, txLogsEncodedDecoded) } + +func (s *MsgsSuite) TestMarshalJSON() { + addrHex := "0x1111111111111111122222222222222222222222" + { + jsonBz, err := json.Marshal(addrHex) + s.NoError(err) + eip55Addr := new(eth.EIP55Addr) + s.Require().NoError(eip55Addr.UnmarshalJSON(jsonBz)) + s.Require().Equal(addrHex, eip55Addr.Hex()) + } + + sender := "nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl" + { + fromErc20, err := eth.NewEIP55AddrFromStr(addrHex) + s.NoError(err) + goType := evm.MsgCreateFunToken{ + FromErc20: &fromErc20, + Sender: sender, + } + + outJsonBz, err := json.Marshal(goType) + s.Require().NoError(err) + + var outGoType evm.MsgCreateFunToken + err = json.Unmarshal(outJsonBz, &outGoType) + s.NoError(err) + s.Equal(goType, outGoType) + } + + var goType evm.MsgCreateFunToken + err := json.Unmarshal([]byte(` + { + "from_erc20": "0x1111111111111111122222222222222222222222", + "sender": "nibi1zaavvzxez0elundtn32qnk9lkm8kmcsz44g7xl" + } + `), &goType) + s.NoError(err) + s.Equal(addrHex, goType.FromErc20.Hex()) + s.Equal(sender, goType.Sender) +}