Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions evm/jsonrpc/api_web3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
41 changes: 41 additions & 0 deletions evm/jsonrpc/api_web3_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading