From fce4488e85d9913ccc81a2e664759e71e630bbce Mon Sep 17 00:00:00 2001 From: vetclippy Date: Fri, 2 Oct 2026 17:36:39 +0800 Subject: [PATCH] fix(evm): decode web3_sha3 DATA before hashing Signed-off-by: vetclippy --- evm/jsonrpc/api_web3.go | 4 ++-- evm/jsonrpc/api_web3_test.go | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 evm/jsonrpc/api_web3_test.go diff --git a/evm/jsonrpc/api_web3.go b/evm/jsonrpc/api_web3.go index f5a4895066..9f3919aff0 100644 --- a/evm/jsonrpc/api_web3.go +++ b/evm/jsonrpc/api_web3.go @@ -22,6 +22,6 @@ func (a *APIWeb3) ClientVersion() string { } // Sha3 returns the keccak-256 hash of the passed-in input. -func (a *APIWeb3) Sha3(input string) hexutil.Bytes { - return crypto.Keccak256(hexutil.Bytes(input)) +func (a *APIWeb3) Sha3(input hexutil.Bytes) hexutil.Bytes { + return crypto.Keccak256(input) } diff --git a/evm/jsonrpc/api_web3_test.go b/evm/jsonrpc/api_web3_test.go new file mode 100644 index 0000000000..f287b1b0b7 --- /dev/null +++ b/evm/jsonrpc/api_web3_test.go @@ -0,0 +1,41 @@ +package jsonrpc_test + +import ( + "net/http/httptest" + "testing" + + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/crypto" + gethrpc "github.com/ethereum/go-ethereum/rpc" + "github.com/stretchr/testify/require" + + "github.com/NibiruChain/nibiru/v2/evm/jsonrpc" +) + +func TestAPIWeb3Sha3(t *testing.T) { + api := jsonrpc.NewImplWeb3API() + + got := api.Sha3(hexutil.Bytes([]byte("hello world"))) + want := hexutil.Bytes(hexutil.MustDecode("0x47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad")) + + require.Equal(t, want, got) +} + +func TestAPIWeb3Sha3JSONRPC(t *testing.T) { + server := gethrpc.NewServer() + require.NoError(t, server.RegisterName("web3", jsonrpc.NewImplWeb3API())) + defer server.Stop() + + httpServer := httptest.NewServer(server) + defer httpServer.Close() + + client, err := gethrpc.DialHTTP(httpServer.URL) + require.NoError(t, err) + defer client.Close() + + var got hexutil.Bytes + require.NoError(t, client.Call(&got, "web3_sha3", "0x68656c6c6f20776f726c64")) + + want := crypto.Keccak256([]byte("hello world")) + require.Equal(t, hexutil.Bytes(want), got) +}