Skip to content

Commit

Permalink
refactor: remove unused eth_signTypedData endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
MikkySnow committed Oct 8, 2024
1 parent 49c0031 commit 84a2ed0
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 95 deletions.
3 changes: 0 additions & 3 deletions rpc/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/signer/core/apitypes"

rpctypes "swisstronik/rpc/types"
"swisstronik/server/config"
ethermint "swisstronik/types"
Expand Down Expand Up @@ -75,7 +73,6 @@ type EVMBackend interface {
// Sign Tx
Sign(address common.Address, data hexutil.Bytes) (hexutil.Bytes, error)
SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error)
SignTypedData(address common.Address, typedData apitypes.TypedData) (hexutil.Bytes, error)

// Blocks Info
BlockNumber() (hexutil.Uint64, error)
Expand Down
34 changes: 1 addition & 33 deletions rpc/backend/sign_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/signer/core/apitypes"

evmtypes "swisstronik/x/evm/types"
)

Expand Down Expand Up @@ -126,7 +124,7 @@ func (b *Backend) SendTransaction(args evmtypes.TransactionArgs) (common.Hash, e
// Sign signs the provided data using the private key of address via Geth's signature standard.
func (b *Backend) Sign(address common.Address, data hexutil.Bytes) (hexutil.Bytes, error) {
if !b.cfg.JSONRPC.UnsafeEthEndpointsEnabled {
return nil, fmt.Errorf("eth_sendTransaction not enabled")
return nil, fmt.Errorf("eth_sign not enabled")
}

from := sdk.AccAddress(address.Bytes())
Expand All @@ -147,33 +145,3 @@ func (b *Backend) Sign(address common.Address, data hexutil.Bytes) (hexutil.Byte
signature[crypto.RecoveryIDOffset] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
return signature, nil
}

// SignTypedData signs EIP-712 conformant typed data
func (b *Backend) SignTypedData(address common.Address, typedData apitypes.TypedData) (hexutil.Bytes, error) {
if !b.cfg.JSONRPC.UnsafeEthEndpointsEnabled {
return nil, fmt.Errorf("eth_sendTransaction not enabled")
}

from := sdk.AccAddress(address.Bytes())

_, err := b.clientCtx.Keyring.KeyByAddress(from)
if err != nil {
b.logger.Error("failed to find key in keyring", "address", address.String())
return nil, fmt.Errorf("%s; %s", keystore.ErrNoMatch, err.Error())
}

sigHash, _, err := apitypes.TypedDataAndHash(typedData)
if err != nil {
return nil, err
}

// Sign the requested hash with the wallet
signature, _, err := b.clientCtx.Keyring.SignByAddress(from, sigHash)
if err != nil {
b.logger.Error("keyring.SignByAddress failed", "address", address.Hex())
return nil, err
}

signature[crypto.RecoveryIDOffset] += 27 // Transform V from 0/1 to 27/28 according to the yellow paper
return signature, nil
}
50 changes: 0 additions & 50 deletions rpc/backend/sign_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
goethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
"google.golang.org/grpc/metadata"

"swisstronik/crypto/ethsecp256k1"
Expand Down Expand Up @@ -227,52 +226,3 @@ func (suite *BackendTestSuite) TestSign() {
})
}
}

func (suite *BackendTestSuite) TestSignTypedData() {
from, priv := tests.RandomEthAddressWithPrivateKey()
testCases := []struct {
name string
registerMock func()
fromAddr common.Address
inputTypedData apitypes.TypedData
expPass bool
}{
{
"fail - can't find key in Keyring",
func() {},
from,
apitypes.TypedData{},
false,
},
{
"fail - empty TypeData",
func() {
armor := crypto.EncryptArmorPrivKey(priv, "", "eth_secp256k1")
err := suite.backend.clientCtx.Keyring.ImportPrivKey("test_key", armor, "")
suite.Require().NoError(err)
},
from,
apitypes.TypedData{},
false,
},
}

for _, tc := range testCases {
suite.Run(fmt.Sprintf("case %s", tc.name), func() {
suite.SetupTest() // reset test and queries
tc.registerMock()

responseBz, err := suite.backend.SignTypedData(tc.fromAddr, tc.inputTypedData)

if tc.expPass {
sigHash, _, err := apitypes.TypedDataAndHash(tc.inputTypedData)
signature, _, err := suite.backend.clientCtx.Keyring.SignByAddress((sdk.AccAddress)(from.Bytes()), sigHash)
signature[goethcrypto.RecoveryIDOffset] += 27
suite.Require().NoError(err)
suite.Require().Equal((hexutil.Bytes)(signature), responseBz)
} else {
suite.Require().Error(err)
}
})
}
}
9 changes: 0 additions & 9 deletions rpc/namespaces/ethereum/eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package eth
import (
"context"

"github.com/ethereum/go-ethereum/signer/core/apitypes"

"github.com/ethereum/go-ethereum/rpc"

"github.com/cometbft/cometbft/libs/log"
Expand Down Expand Up @@ -113,7 +111,6 @@ type EthereumAPI interface {
Coinbase() (string, error)
Sign(address common.Address, data hexutil.Bytes) (hexutil.Bytes, error)
GetTransactionLogs(txHash common.Hash) ([]*ethtypes.Log, error)
SignTypedData(address common.Address, typedData apitypes.TypedData) (hexutil.Bytes, error)
FillTransaction(args evmtypes.TransactionArgs) (*rpctypes.SignTransactionResult, error)
Resend(ctx context.Context, args evmtypes.TransactionArgs, gasPrice *hexutil.Big, gasLimit *hexutil.Uint64) (common.Hash, error)
GetPendingTransactions() ([]*rpctypes.RPCTransaction, error)
Expand Down Expand Up @@ -454,12 +451,6 @@ func (e *PublicAPI) GetTransactionLogs(txHash common.Hash) ([]*ethtypes.Log, err
return backend.TxLogsFromEvents(resBlockResult.TxsResults[res.TxIndex].Events, int(res.MsgIndex))
}

// SignTypedData signs EIP-712 conformant typed data
func (e *PublicAPI) SignTypedData(address common.Address, typedData apitypes.TypedData) (hexutil.Bytes, error) {
e.logger.Debug("eth_signTypedData", "address", address.Hex(), "data", typedData)
return e.backend.SignTypedData(address, typedData)
}

// FillTransaction fills the defaults (nonce, gas, gasPrice or 1559 fields)
// on a given unsigned transaction, and returns it to the caller for further
// processing (signing + broadcast).
Expand Down

0 comments on commit 84a2ed0

Please sign in to comment.