Skip to content

Commit

Permalink
fix(evm): JSON encoding for the EIP55Addr struct was not following …
Browse files Browse the repository at this point in the history
…the Go conventions and needed to include double quotes around the hexadecimal string.
  • Loading branch information
Unique-Divine committed Jan 9, 2025
1 parent c9c56a9 commit 36e8d2f
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ ChainLink interface. Publish all precompiled contracts and ABIs on npm under
the `@nibiruchain/solidity` package.
- [#2151](https://github.com/NibiruChain/nibiru/pull/2151) - feat(evm): randao support for evm
- [#2152](https://github.com/NibiruChain/nibiru/pull/2152) - fix(precompile): consume gas for precompile calls regardless of error
- [#2154](https://github.com/NibiruChain/nibiru/pull/2154) - fix(evm):
JSON encoding for the `EIP55Addr` struct was not following the Go conventions and
needed to include double quotes around the hexadecimal string.

#### Nibiru EVM | Before Audit 2 - 2024-12-06

Expand Down
11 changes: 9 additions & 2 deletions eth/eip55.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package eth

import (
"encoding/json"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -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").
Expand All @@ -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
}
Expand Down
14 changes: 8 additions & 6 deletions eth/eip55_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package eth_test

import (
"fmt"
"strconv"
"strings"
"testing"

gethcommon "github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -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() {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
42 changes: 42 additions & 0 deletions x/evm/msg_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package evm_test

import (
"encoding/json"
"fmt"
"math"
"math/big"
Expand All @@ -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"
Expand Down Expand Up @@ -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)
}

0 comments on commit 36e8d2f

Please sign in to comment.