Skip to content

Commit

Permalink
improve pre-EIP155 check (#2327)
Browse files Browse the repository at this point in the history
  • Loading branch information
tclemos authored Aug 18, 2023
1 parent a1476c9 commit 181d16f
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
5 changes: 5 additions & 0 deletions pool/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ func (p *Pool) validateTx(ctx context.Context, poolTx Transaction) error {
return ErrTxTypeNotSupported
}

// check Pre EIP155 txs signature
if txChainID == 0 && !state.IsPreEIP155Tx(poolTx.Transaction) {
return ErrInvalidSender
}

// gets tx sender for validations
from, err := state.GetSender(poolTx.Transaction)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,12 +924,12 @@ func Test_TryAddIncompatibleTxs(t *testing.T) {
require.NoError(t, err)

chainIdOver64Bits := big.NewInt(0).SetUint64(math.MaxUint64)
chainIdOver64Bits = chainIdOver64Bits.Add(chainIdOver64Bits, big.NewInt(1))
chainIdOver64Bits = chainIdOver64Bits.Add(chainIdOver64Bits, big.NewInt(2))
authChainIdOver64Bits, err := bind.NewKeyedTransactorWithChainID(privateKey, chainIdOver64Bits)
require.NoError(t, err)

bigIntOver256Bits, _ := big.NewInt(0).SetString(encoding.MaxUint256StrNumber, encoding.Base10)
bigIntOver256Bits = bigIntOver256Bits.Add(bigIntOver256Bits, big.NewInt(1))
bigIntOver256Bits = bigIntOver256Bits.Add(bigIntOver256Bits, big.NewInt(2))

testCases := []testCase{
{
Expand Down
9 changes: 8 additions & 1 deletion state/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func prepareRPLTxData(tx types.Transaction) ([]byte, error) {
data,
}

if tx.ChainId().Uint64() > 0 {
if !IsPreEIP155Tx(tx) {
rlpFieldsToEncode = append(rlpFieldsToEncode, chainID)
rlpFieldsToEncode = append(rlpFieldsToEncode, uint(0))
rlpFieldsToEncode = append(rlpFieldsToEncode, uint(0))
Expand Down Expand Up @@ -338,6 +338,13 @@ func toPostgresInterval(duration string) (string, error) {
return fmt.Sprintf("%s %s", duration[:len(duration)-1], pgUnit), nil
}

// IsPreEIP155Tx checks if the tx is a tx that has a chainID as zero and
// V field is either 27 or 28
func IsPreEIP155Tx(tx types.Transaction) bool {
v, _, _ := tx.RawSignatureValues()
return tx.ChainId().Uint64() == 0 && (v.Uint64() == 27 || v.Uint64() == 28)
}

// CheckLogOrder checks the order of the logs. The order should be incremental
func CheckLogOrder(logs []*types.Log) bool {
logsAux := make([]*types.Log, len(logs))
Expand Down
66 changes: 66 additions & 0 deletions test/e2e/preEIP155_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package e2e

import (
"context"
"crypto/ecdsa"
"math/big"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -88,3 +90,67 @@ func TestPreEIP155Tx(t *testing.T) {
}
}
}

func TestFakeEIP155With_V_As35(t *testing.T) {
if testing.Short() {
t.Skip()
}

var err error
err = operations.Teardown()
require.NoError(t, err)

defer func() {
require.NoError(t, operations.Teardown())
}()

ctx := context.Background()
opsCfg := operations.GetDefaultOperationsConfig()
opsMan, err := operations.NewManager(ctx, opsCfg)
require.NoError(t, err)
err = opsMan.Setup()
require.NoError(t, err)

for _, network := range networks {
log.Debugf(network.Name)
client := operations.MustGetClient(network.URL)

privateKey, err := crypto.HexToECDSA("ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80")
require.NoError(t, err)
publicKey := privateKey.Public()
publicKeyECDSA, _ := publicKey.(*ecdsa.PublicKey)
fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
require.NoError(t, err)

toAddress := common.HexToAddress("0x1234")
tx := &types.LegacyTx{
Nonce: nonce,
To: &toAddress,
Value: big.NewInt(0),
Gas: uint64(21000),

GasPrice: big.NewInt(10000000000000),
Data: nil,
}

// set the chainID to 0 to fake a pre EIP155 tx
signer := types.NewEIP155Signer(big.NewInt(0))

// sign tx
h := signer.Hash(types.NewTx(tx))
sig, err := crypto.Sign(h[:], privateKey)
require.NoError(t, err)
r, s, _, err := signer.SignatureValues(types.NewTx(tx), sig)
require.NoError(t, err)

// set the value V of the signature to 35
tx.V = big.NewInt(35)
tx.R = r
tx.S = s

signedTx := types.NewTx(tx)
err = client.SendTransaction(context.Background(), signedTx)
require.Equal(t, "invalid sender", err.Error())
}
}

0 comments on commit 181d16f

Please sign in to comment.