Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[action]: add unit test for eth tx decode verify #4291

Merged
merged 3 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
74 changes: 70 additions & 4 deletions action/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
package action

import (
"crypto/ecdsa"
"encoding/hex"
"math/big"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
ethercrypto "github.com/ethereum/go-ethereum/crypto"
iotexcrypto "github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-proto/golang/iotextypes"
"github.com/stretchr/testify/require"

"github.com/iotexproject/iotex-core/pkg/util/assertions"
. "github.com/iotexproject/iotex-core/pkg/util/assertions"
"github.com/iotexproject/iotex-core/pkg/version"
)

Expand Down Expand Up @@ -82,7 +86,7 @@ func TestBuildRewardingAction(t *testing.T) {
t.Run("Success", func(t *testing.T) {
method := _claimRewardingMethod
t.Run("ClaimRewarding", func(t *testing.T) {
inputs := assertions.MustNoErrorV(method.Inputs.Pack(big.NewInt(101), []byte("any"), ""))
inputs := MustNoErrorV(method.Inputs.Pack(big.NewInt(101), []byte("any"), ""))
elp, err := eb.BuildRewardingAction(types.NewTx(&types.LegacyTx{
Nonce: 1,
GasPrice: big.NewInt(10004),
Expand All @@ -102,7 +106,7 @@ func TestBuildRewardingAction(t *testing.T) {
eb := &EnvelopeBuilder{}
eb.SetChainID(4689)

inputs := assertions.MustNoErrorV(method.Inputs.Pack(big.NewInt(100), []byte("any"), ""))
inputs := MustNoErrorV(method.Inputs.Pack(big.NewInt(100), []byte("any"), ""))
to := common.HexToAddress("0xA576C141e5659137ddDa4223d209d4744b2106BE")
tx := types.NewTx(&types.LegacyTx{
Nonce: 0,
Expand All @@ -119,7 +123,7 @@ func TestBuildRewardingAction(t *testing.T) {
})
method = _depositRewardMethod
t.Run("DepositRewarding", func(t *testing.T) {
inputs := assertions.MustNoErrorV(method.Inputs.Pack(big.NewInt(102), []byte("any")))
inputs := MustNoErrorV(method.Inputs.Pack(big.NewInt(102), []byte("any")))
elp, err := eb.BuildRewardingAction(types.NewTx(&types.LegacyTx{
Nonce: 1,
GasPrice: big.NewInt(10004),
Expand All @@ -136,3 +140,65 @@ func TestBuildRewardingAction(t *testing.T) {
})
})
}

func ptr[V any](v V) *V { return &v }

func TestEthTxUtils(t *testing.T) {
r := require.New(t)
var (
skhex = "708361c6460c93f027df0823eb440f3270ee2937944f2de933456a3900d6dd1a"
sk1, _ = iotexcrypto.HexStringToPrivateKey(skhex)
sk2, _ = ethercrypto.HexToECDSA(skhex)
chainID = uint32(4689)
builder = (&Builder{}).
SetNonce(100).
SetGasLimit(21000).
SetGasPrice(big.NewInt(101))
)

pk1 := sk1.PublicKey()
iotexhexaddr := hex.EncodeToString(pk1.Bytes())
t.Log("iotex address:", pk1.Address().String())

pk2 := sk2.Public().(*ecdsa.PublicKey)
etherhexaddr := hex.EncodeToString(ethercrypto.FromECDSAPub(pk2))
t.Log("ether address:", ethercrypto.PubkeyToAddress(*pk2).String())

r.Equal(iotexhexaddr, etherhexaddr)

tx, _ := ptr((&ClaimFromRewardingFundBuilder{Builder: *builder}).
SetAddress("0xA576C141e5659137ddDa4223d209d4744b2106BE").
SetData([]byte("any")).
SetAmount(big.NewInt(1)).Build()).ToEthTx(chainID)

var (
signer1, _ = NewEthSigner(iotextypes.Encoding_ETHEREUM_EIP155, chainID)
sig1, _ = sk1.Sign(tx.Hash().Bytes())
signer2 = types.NewEIP2930Signer(big.NewInt(int64(chainID)))
sig2, _ = ethercrypto.Sign(tx.Hash().Bytes(), sk2)
)
r.Equal(signer1, signer2)
r.Equal(sig1, sig2)

tx1, _ := RawTxToSignedTx(tx, signer1, sig1)
tx2, _ := tx.WithSignature(signer2, sig2)
r.Equal(tx1.Hash(), tx2.Hash())

sender1, _ := signer1.Sender(tx1)
sender2, _ := signer2.Sender(tx2)
r.Equal(sender1, sender2)

// r.Equal(sender1.Bytes(), pk1.Address().Bytes())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete unused code?

// r.Equal(sender2.Bytes(), ethercrypto.PubkeyToAddress(*pk2).Bytes())

pk1recover, _ := iotexcrypto.RecoverPubkey(signer1.Hash(tx).Bytes(), sig1)
t.Log(hex.EncodeToString(pk1recover.Bytes()))
pk2recoverbytes, _ := ethercrypto.Ecrecover(signer2.Hash(tx).Bytes(), sig2)
t.Log(hex.EncodeToString(pk2recoverbytes))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete t.Log

r.Equal(pk1recover.Bytes(), pk2recoverbytes)
pk2recover, _ := ethercrypto.SigToPub(signer2.Hash(tx).Bytes(), sig2)

// r.Equal(pk1.Bytes(), pk1recover.Bytes())
// r.True(pk2.Equal(pk2recover))
r.Equal(ethercrypto.FromECDSAPub(pk2recover), pk2recoverbytes)
}
Loading
Loading