Skip to content

Commit

Permalink
[action] remove DecodeRawTx() (iotexproject#4235)
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinxie authored Apr 16, 2024
1 parent da2517d commit cfcf281
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 134 deletions.
8 changes: 4 additions & 4 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
/config/ @CoderZhi @dustinxie @Liuhaai @envestcc @millken
/p2p/ @CoderZhi @dustinxie @Liuhaai @envestcc
/action/ @CoderZhi @dustinxie @Liuhaai @envestcc
/blockchain/ @CoderZhi @dustinxie @Liuhaai
/blockindex/ @CoderZhi @dustinxie @Liuhaai
/crypto/ @CoderZhi @dustinxie @Liuhaai
/db/ @CoderZhi @dustinxie @Liuhaai
/blockchain/ @CoderZhi @dustinxie @Liuhaai @envestcc
/blockindex/ @CoderZhi @dustinxie @Liuhaai @envestcc
/crypto/ @CoderZhi @dustinxie @Liuhaai @envestcc
/db/ @CoderZhi @dustinxie @Liuhaai @envestcc
/dispatcher/ @CoderZhi @dustinxie @Liuhaai @envestcc
/pkg/ @CoderZhi @dustinxie @Liuhaai
/server/ @CoderZhi @dustinxie @Liuhaai
Expand Down
35 changes: 0 additions & 35 deletions action/rlp_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
Expand Down Expand Up @@ -45,40 +44,6 @@ func RawTxToSignedTx(rawTx *types.Transaction, signer types.Signer, sig []byte)
return signedTx, nil
}

// DecodeRawTx decodes raw data string into eth tx
func DecodeRawTx(rawData string, chainID uint32) (tx *types.Transaction, sig []byte, pubkey crypto.PublicKey, err error) {
//remove Hex prefix and decode string to byte
rawData = strings.Replace(rawData, "0x", "", -1)
rawData = strings.Replace(rawData, "0X", "", -1)
var dataInString []byte
dataInString, err = hex.DecodeString(rawData)
if err != nil {
return
}

// decode raw data into rlp tx
tx = &types.Transaction{}
err = rlp.DecodeBytes(dataInString, tx)
if err != nil {
return
}

// extract signature and recover pubkey
v, r, s := tx.RawSignatureValues()
recID := uint32(v.Int64()) - 2*chainID - 8
sig = make([]byte, 65)
rSize := len(r.Bytes())
copy(sig[32-rSize:32], r.Bytes())
sSize := len(s.Bytes())
copy(sig[64-sSize:], s.Bytes())
sig[64] = byte(recID)

// recover public key
rawHash := types.NewEIP155Signer(big.NewInt(int64(chainID))).Hash(tx)
pubkey, err = crypto.RecoverPubkey(rawHash[:], sig)
return
}

// NewEthSigner returns the proper signer for Eth-compatible tx
func NewEthSigner(txType iotextypes.Encoding, chainID uint32) (types.Signer, error) {
switch txType {
Expand Down
73 changes: 0 additions & 73 deletions action/rlp_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/go-pkgs/hash"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
Expand Down Expand Up @@ -89,68 +88,6 @@ func TestGenerateRlp(t *testing.T) {
}
}

func TestRlpDecodeVerify(t *testing.T) {
require := require.New(t)

oldTests := rlpTests[:len(rlpTests)-1]
for _, v := range oldTests {
// decode received RLP tx
tx, sig, pubkey, err := DecodeRawTx(v.raw, _evmNetworkID)
require.NoError(err)
require.EqualValues(types.LegacyTxType, tx.Type())
require.True(tx.Protected())
require.EqualValues(v.chainID, tx.ChainId().Uint64())
require.Equal(v.pubkey, pubkey.HexString())
require.Equal(v.pkhash, hex.EncodeToString(pubkey.Hash()))

// convert to our Execution
pb := &iotextypes.Action{
Encoding: iotextypes.Encoding_ETHEREUM_EIP155,
}
pb.Core = convertToNativeProto(tx, v.actType)
pb.SenderPubKey = pubkey.Bytes()
pb.Signature = sig

// send on wire
bs, err := proto.Marshal(pb)
require.NoError(err)

// receive from API
proto.Unmarshal(bs, pb)
selp := &SealedEnvelope{}
require.NoError(selp.loadProto(pb, _evmNetworkID))
act, ok := selp.Action().(EthCompatibleAction)
require.True(ok)
rlpTx, err := act.ToEthTx(_evmNetworkID)
require.NoError(err)

// verify against original tx
require.Equal(v.nonce, rlpTx.Nonce())
require.Equal(v.price, rlpTx.GasPrice().String())
require.Equal(v.limit, rlpTx.Gas())
if v.to == "" {
require.Nil(rlpTx.To())
} else {
require.Equal(v.to, rlpTx.To().Hex())
}
require.Equal(v.amount, rlpTx.Value().String())
require.Equal(v.dataLen, len(rlpTx.Data()))
h, err := selp.Hash()
require.NoError(err)
require.Equal(v.hash, hex.EncodeToString(h[:]))
require.Equal(pubkey, selp.SrcPubkey())
require.True(bytes.Equal(sig, selp.signature))
raw, err := selp.envelopeHash()
require.NoError(err)
signer, err := NewEthSigner(iotextypes.Encoding_ETHEREUM_RLP, _evmNetworkID)
require.NoError(err)
rawHash := signer.Hash(tx)
require.True(bytes.Equal(rawHash[:], raw[:]))
require.NotEqual(raw, h)
require.NoError(selp.VerifySignature())
}
}

var (
// deterministic deployment: https://github.com/Arachnid/deterministic-deployment-proxy
// see example at https://etherscan.io/tx/0xeddf9e61fb9d8f5111840daef55e5fde0041f5702856532cdbb5a02998033d26
Expand Down Expand Up @@ -594,13 +531,3 @@ func TestIssue3944(t *testing.T) {
r.Equal("9415", v.String()) // this is the correct V value corresponding to chainID = 4690
r.Equal(hash, tx1.Hash().Hex())
}

func TestBackwardComp(t *testing.T) {
r := require.New(t)
for _, chainID := range []uint32{_evmNetworkID, _evmNetworkID + 1, 31337, 0} {
_, _, _, err := DecodeRawTx(deterministicDeploymentTx, chainID)
r.Equal(crypto.ErrInvalidKey, err)
_, _, _, err = DecodeRawTx(accessListTx, chainID)
r.ErrorContains(err, "typed transaction too short")
}
}
29 changes: 10 additions & 19 deletions api/web3server.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,25 +465,16 @@ func (svr *web3Handler) sendRawTransaction(in *gjson.Result) (interface{}, error
pubkey crypto.PublicKey
err error
)
if g := cs.Genesis(); g.IsSumatra(cs.TipHeight()) {
tx, err = action.DecodeEtherTx(dataStr.String())
if err != nil {
return nil, err
}
if tx.Protected() && tx.ChainId().Uint64() != uint64(cs.EVMNetworkID()) {
return nil, errors.Wrapf(errInvalidEvmChainID, "expect chainID = %d, got %d", cs.EVMNetworkID(), tx.ChainId().Uint64())
}
encoding, sig, pubkey, err = action.ExtractTypeSigPubkey(tx)
if err != nil {
return nil, err
}
} else {
tx, sig, pubkey, err = action.DecodeRawTx(dataStr.String(), cs.EVMNetworkID())
if err != nil {
return nil, err
}
// before Sumatra height, all tx are EIP-155 format
encoding = iotextypes.Encoding_ETHEREUM_EIP155
tx, err = action.DecodeEtherTx(dataStr.String())
if err != nil {
return nil, err
}
if tx.Protected() && tx.ChainId().Uint64() != uint64(cs.EVMNetworkID()) {
return nil, errors.Wrapf(errInvalidEvmChainID, "expect chainID = %d, got %d", cs.EVMNetworkID(), tx.ChainId().Uint64())
}
encoding, sig, pubkey, err = action.ExtractTypeSigPubkey(tx)
if err != nil {
return nil, err
}
elp, err := svr.ethTxToEnvelope(tx)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions api/web3server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/iotexproject/iotex-core/action"
apitypes "github.com/iotexproject/iotex-core/api/types"
"github.com/iotexproject/iotex-core/blockchain/block"
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/test/identityset"
"github.com/iotexproject/iotex-core/test/mock/mock_apicoreservice"
mock_apitypes "github.com/iotexproject/iotex-core/test/mock/mock_apiresponder"
Expand Down Expand Up @@ -434,8 +433,6 @@ func TestSendRawTransaction(t *testing.T) {
defer ctrl.Finish()
core := mock_apicoreservice.NewMockCoreService(ctrl)
web3svr := &web3Handler{core, nil, _defaultBatchRequestLimit}
core.EXPECT().Genesis().Return(genesis.Default)
core.EXPECT().TipHeight().Return(uint64(0))
core.EXPECT().EVMNetworkID().Return(uint32(1))
core.EXPECT().ChainID().Return(uint32(1))
core.EXPECT().Account(gomock.Any()).Return(&iotextypes.AccountMeta{IsContract: true}, nil, nil)
Expand Down

0 comments on commit cfcf281

Please sign in to comment.