From 32e218d9d58d567421538c665d4bb1d4b20e54e4 Mon Sep 17 00:00:00 2001 From: kevinssgh Date: Wed, 28 Feb 2024 16:20:13 -0500 Subject: [PATCH] update mocks and test files --- zetaclient/bitcoin/bitcoin_client_test.go | 6 +- zetaclient/evm/evm_signer.go | 47 +++++-- zetaclient/evm/evm_signer_test.go | 97 +++++++++++++ zetaclient/evm/inbounds_test.go | 4 +- zetaclient/testdata/cctx/cctx_56_68270.json | 48 +++++++ zetaclient/testutils/mock.go | 72 ---------- zetaclient/testutils/mock/mock_core_bridge.go | 131 ++++++++++++++++++ zetaclient/testutils/mock/mock_evm_rpc.go | 98 +++++++++++++ zetaclient/testutils/mock/mock_tss_signer.go | 83 +++++++++++ 9 files changed, 496 insertions(+), 90 deletions(-) create mode 100644 zetaclient/evm/evm_signer_test.go create mode 100644 zetaclient/testdata/cctx/cctx_56_68270.json delete mode 100644 zetaclient/testutils/mock.go create mode 100644 zetaclient/testutils/mock/mock_core_bridge.go create mode 100644 zetaclient/testutils/mock/mock_evm_rpc.go create mode 100644 zetaclient/testutils/mock/mock_tss_signer.go diff --git a/zetaclient/bitcoin/bitcoin_client_test.go b/zetaclient/bitcoin/bitcoin_client_test.go index b9a29db816..14ff165c7d 100644 --- a/zetaclient/bitcoin/bitcoin_client_test.go +++ b/zetaclient/bitcoin/bitcoin_client_test.go @@ -9,6 +9,8 @@ import ( "sync" "testing" + "github.com/zeta-chain/zetacore/zetaclient/testutils/mock" + "github.com/btcsuite/btcd/blockchain" "github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/chaincfg" @@ -24,8 +26,8 @@ import ( func MockBTCClientMainnet() *BTCChainClient { return &BTCChainClient{ chain: common.BtcMainnetChain(), - zetaClient: testutils.MockCoreBridge(), - Tss: testutils.NewMockTSSMainnet(), + zetaClient: mock.NewZetaCoreBridge(), + Tss: mock.NewTSSMainnet(), } } diff --git a/zetaclient/evm/evm_signer.go b/zetaclient/evm/evm_signer.go index 9bd571582f..f30d397b4b 100644 --- a/zetaclient/evm/evm_signer.go +++ b/zetaclient/evm/evm_signer.go @@ -13,6 +13,8 @@ import ( "sync" "time" + "github.com/zeta-chain/zetacore/zetaclient/testutils/mock" + sdk "github.com/cosmos/cosmos-sdk/types" clientcommon "github.com/zeta-chain/zetacore/zetaclient/common" @@ -92,16 +94,10 @@ func NewEVMSigner( loggers clientcommon.ClientLogger, ts *metrics.TelemetryServer, ) (*Signer, error) { - client, err := ethclient.Dial(endpoint) + client, chainID, ethSigner, err := getEVMRPC(endpoint) if err != nil { return nil, err } - - chainID, err := client.ChainID(context.TODO()) - if err != nil { - return nil, err - } - ethSigner := ethtypes.LatestSignerForChainID(chainID) connectorABI, err := abi.JSON(strings.NewReader(abiString)) if err != nil { return nil, err @@ -339,7 +335,7 @@ func (signer *Signer) SignCommandTx(txData *TransactionData) (*ethtypes.Transact return nil, fmt.Errorf("SignCommandTx: unknown command %s", txData.cmd) } -func setChainAndSender(cctx *types.CrossChainTx, logger zerolog.Logger, txData *TransactionData) bool { +func SetChainAndSender(cctx *types.CrossChainTx, logger zerolog.Logger, txData *TransactionData) bool { if cctx.CctxStatus.Status == types.CctxStatus_PendingRevert { txData.to = ethcommon.HexToAddress(cctx.InboundTxParams.Sender) txData.toChainID = big.NewInt(cctx.InboundTxParams.SenderChainId) //common.GetChainFromChainID(cctx.InboundTxParams.SenderChainId) @@ -354,7 +350,7 @@ func setChainAndSender(cctx *types.CrossChainTx, logger zerolog.Logger, txData * return false } -func setupGas( +func SetupGas( cctx *types.CrossChainTx, logger zerolog.Logger, client interfaces.EVMRPCClient, @@ -392,7 +388,7 @@ func setupGas( return nil } -func setTransactionData( +func SetTransactionData( cctx *types.CrossChainTx, evmClient *ChainClient, evmRPC interfaces.EVMRPCClient, @@ -407,7 +403,7 @@ func setTransactionData( txData.srcChainID = big.NewInt(cctx.InboundTxParams.SenderChainId) txData.asset = ethcommon.HexToAddress(cctx.InboundTxParams.Asset) - skipTx := setChainAndSender(cctx, logger, txData) + skipTx := SetChainAndSender(cctx, logger, txData) if skipTx { return true, nil } @@ -429,7 +425,7 @@ func setTransactionData( } // Set up gas limit and gas price - err = setupGas(cctx, logger, evmRPC, toChain, txData) + err = SetupGas(cctx, logger, evmRPC, toChain, txData) if err != nil { return true, err } @@ -499,7 +495,7 @@ func (signer *Signer) TryProcessOutTx( // Setup Transaction input txData := TransactionData{} txData.height = height - skipTx, err := setTransactionData(cctx, evmClient, signer.client, logger, zetaBridge, &txData) + skipTx, err := SetTransactionData(cctx, evmClient, signer.client, logger, zetaBridge, &txData) if err != nil { logger.Error().Msg(err.Error()) return @@ -577,10 +573,10 @@ func (signer *Signer) TryProcessOutTx( } // Broadcast Signed Tx - signer.broadcastOutTx(tx, cctx, logger, myID, zetaBridge, &txData) + signer.BroadcastOutTx(tx, cctx, logger, myID, zetaBridge, &txData) } -func (signer *Signer) broadcastOutTx( +func (signer *Signer) BroadcastOutTx( tx *ethtypes.Transaction, cctx *types.CrossChainTx, logger zerolog.Logger, @@ -770,6 +766,27 @@ func (signer *Signer) SignWhitelistTx( return tx, nil } +func getEVMRPC(endpoint string) (interfaces.EVMRPCClient, *big.Int, ethtypes.Signer, error) { + if endpoint == mock.EVMRPCEnabled { + chainID := big.NewInt(common.BscMainnetChain().ChainId) + ethSigner := ethtypes.NewEIP155Signer(chainID) + client := mock.EvmClient{} + return client, chainID, ethSigner, nil + } + + client, err := ethclient.Dial(endpoint) + if err != nil { + return nil, nil, nil, err + } + + chainID, err := client.ChainID(context.TODO()) + if err != nil { + return nil, nil, nil, err + } + ethSigner := ethtypes.LatestSignerForChainID(chainID) + return client, chainID, ethSigner, nil +} + func roundUpToNearestGwei(gasPrice *big.Int) *big.Int { oneGwei := big.NewInt(1_000_000_000) // 1 Gwei mod := new(big.Int) diff --git a/zetaclient/evm/evm_signer_test.go b/zetaclient/evm/evm_signer_test.go new file mode 100644 index 0000000000..83ec275928 --- /dev/null +++ b/zetaclient/evm/evm_signer_test.go @@ -0,0 +1,97 @@ +package evm + +import ( + "path" + "testing" + + ethcommon "github.com/ethereum/go-ethereum/common" + "github.com/rs/zerolog" + "github.com/stretchr/testify/require" + corecommon "github.com/zeta-chain/zetacore/common" + "github.com/zeta-chain/zetacore/x/crosschain/types" + crosschaintypes "github.com/zeta-chain/zetacore/x/crosschain/types" + "github.com/zeta-chain/zetacore/zetaclient/common" + "github.com/zeta-chain/zetacore/zetaclient/config" + "github.com/zeta-chain/zetacore/zetaclient/metrics" + "github.com/zeta-chain/zetacore/zetaclient/outtxprocessor" + "github.com/zeta-chain/zetacore/zetaclient/testutils" + "github.com/zeta-chain/zetacore/zetaclient/testutils/mock" +) + +const ( + // Dummy addresses as they are just used as transaction data to be signed + ConnectorAddress = "0x00000000219ab540356cbb839cbe05303d7705fa" + ERC20CustodyAddress = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" +) + +func getNewEvmSigner() (*Signer, error) { + mpiAddress := ethcommon.HexToAddress(ConnectorAddress) + erc20CustodyAddress := ethcommon.HexToAddress(ERC20CustodyAddress) + logger := common.ClientLogger{} + ts := &metrics.TelemetryServer{} + return NewEVMSigner( + corecommon.BscMainnetChain(), + mock.EVMRPCEnabled, + mock.NewTSSMainnet(), + config.GetConnectorABI(), + config.GetERC20CustodyABI(), + mpiAddress, + erc20CustodyAddress, + logger, + ts) +} + +func getNewOutTxProcessor() *outtxprocessor.Processor { + logger := zerolog.Logger{} + return outtxprocessor.NewOutTxProcessorManager(logger) +} + +func getCCTX() (*types.CrossChainTx, error) { + var cctx crosschaintypes.CrossChainTx + err := testutils.LoadObjectFromJSONFile(&cctx, path.Join("../", testutils.TestDataPathCctx, "cctx_56_68270.json")) + return &cctx, err +} + +func TestSigner_TryProcessOutTx(t *testing.T) { + evmSigner, err := getNewEvmSigner() + require.NoError(t, err) + + cctx, err := getCCTX() + require.NoError(t, err) + + processorManager := getNewOutTxProcessor() + + mockChainClient := &ChainClient{ + chain: corecommon.BscMainnetChain(), + zetaClient: mock.NewZetaCoreBridge(), + Tss: mock.NewTSSMainnet(), + } + evmSigner.TryProcessOutTx(cctx, processorManager, "123", mockChainClient, mock.NewZetaCoreBridge(), 123) +} + +func TestSigner_SignOutboundTx(t *testing.T) { +} + +func TestSigner_SignRevertTx(t *testing.T) { +} + +func TestSigner_SignWithdrawTx(t *testing.T) { +} + +func TestSigner_SignCommandTx(t *testing.T) { +} + +func TestSigner_SignERC20WithdrawTx(t *testing.T) { +} + +func TestSigner_BroadcastOutTx(t *testing.T) { +} + +func TestSigner_SetChainAndSender(t *testing.T) { +} + +func TestSigner_SetupGas(t *testing.T) { +} + +func TestSigner_SetTransactionData(t *testing.T) { +} diff --git a/zetaclient/evm/inbounds_test.go b/zetaclient/evm/inbounds_test.go index 84b24dd200..ad9c3c60dc 100644 --- a/zetaclient/evm/inbounds_test.go +++ b/zetaclient/evm/inbounds_test.go @@ -4,6 +4,8 @@ import ( "path" "testing" + "github.com/zeta-chain/zetacore/zetaclient/testutils/mock" + ethcommon "github.com/ethereum/go-ethereum/common" ethtypes "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethclient" @@ -18,7 +20,7 @@ import ( func MockEVMClient(chain common.Chain) *ChainClient { return &ChainClient{ chain: chain, - zetaClient: testutils.MockCoreBridge(), + zetaClient: mock.NewZetaCoreBridge(), } } diff --git a/zetaclient/testdata/cctx/cctx_56_68270.json b/zetaclient/testdata/cctx/cctx_56_68270.json new file mode 100644 index 0000000000..1fe6789231 --- /dev/null +++ b/zetaclient/testdata/cctx/cctx_56_68270.json @@ -0,0 +1,48 @@ +{ + "CrossChainTx": [ + { + "creator": "", + "index": "0x541b570182950809f9b9077861a0fc7038af9a14ce8af4e151a83adfa308c7a9", + "zeta_fees": "0", + "relayed_message": "", + "cctx_status": { + "status": "PendingOutbound", + "status_message": "", + "lastUpdate_timestamp": "1709145057" + }, + "inbound_tx_params": { + "sender": "0xd91b507F2A3e2D4A32d0C86Ac19FEAD2D461008D", + "sender_chain_id": "7000", + "tx_origin": "0xb0C04e07A301927672A8A7a874DB6930576C90B8", + "coin_type": "Gas", + "asset": "", + "amount": "657177295293237048", + "inbound_tx_observed_hash": "0x093f4ca4c1884df0fd9dd59b75979342ded29d3c9b6861644287a2e1417b9a39", + "inbound_tx_observed_external_height": "1960153", + "inbound_tx_ballot_index": "0x541b570182950809f9b9077861a0fc7038af9a14ce8af4e151a83adfa308c7a9", + "inbound_tx_finalized_zeta_height": "0", + "tx_finalization_status": "NotFinalized" + }, + "outbound_tx_params": [ + { + "receiver": "0xb0C04e07A301927672A8A7a874DB6930576C90B8", + "receiver_chainId": "56", + "coin_type": "Gas", + "amount": "657177295293237048", + "outbound_tx_tss_nonce": "68270", + "outbound_tx_gas_limit": "21000", + "outbound_tx_gas_price": "6000000000", + "outbound_tx_hash": "", + "outbound_tx_ballot_index": "", + "outbound_tx_observed_external_height": "0", + "outbound_tx_gas_used": "0", + "outbound_tx_effective_gas_price": "0", + "outbound_tx_effective_gas_limit": "0", + "tss_pubkey": "zetapub1addwnpepqtadxdyt037h86z60nl98t6zk56mw5zpnm79tsmvspln3hgt5phdc79kvfc", + "tx_finalization_status": "NotFinalized" + } + ] + } + ], + "totalPending": "1" +} \ No newline at end of file diff --git a/zetaclient/testutils/mock.go b/zetaclient/testutils/mock.go deleted file mode 100644 index 92c0f6d1b2..0000000000 --- a/zetaclient/testutils/mock.go +++ /dev/null @@ -1,72 +0,0 @@ -package testutils - -import ( - "github.com/btcsuite/btcutil" - "github.com/cosmos/cosmos-sdk/types" - ethcommon "github.com/ethereum/go-ethereum/common" - "github.com/zeta-chain/zetacore/common" - "github.com/zeta-chain/zetacore/zetaclient/interfaces" - "github.com/zeta-chain/zetacore/zetaclient/keys" - "github.com/zeta-chain/zetacore/zetaclient/zetabridge" -) - -var _ interfaces.TSSSigner = (*MockTSS)(nil) - -// MockTSS is a mock of TSS signer for testing -type MockTSS struct { - evmAddress string - btcAddress string -} - -func NewMockTSS(evmAddress string, btcAddress string) *MockTSS { - return &MockTSS{ - evmAddress: evmAddress, - btcAddress: btcAddress, - } -} - -func NewMockTSSMainnet() *MockTSS { - return NewMockTSS(TSSAddressEVMMainnet, TSSAddressBTCMainnet) -} - -func NewMockTSSAthens3() *MockTSS { - return NewMockTSS(TSSAddressEVMAthens3, TSSAddressBTCAthens3) -} - -func (s *MockTSS) Sign(_ []byte, _ uint64, _ uint64, _ *common.Chain, _ string) ([65]byte, error) { - return [65]byte{}, nil -} - -func (s *MockTSS) Pubkey() []byte { - return []byte{} -} - -func (s *MockTSS) EVMAddress() ethcommon.Address { - return ethcommon.HexToAddress(s.evmAddress) -} - -func (s *MockTSS) BTCAddress() string { - return s.btcAddress -} - -func (s *MockTSS) BTCAddressWitnessPubkeyHash() *btcutil.AddressWitnessPubKeyHash { - return nil -} - -func (s *MockTSS) PubKeyCompressedBytes() []byte { - return []byte{} -} - -func MockCoreBridge() *zetabridge.ZetaCoreBridge { - bridge, err := zetabridge.NewZetaCoreBridge( - &keys.Keys{OperatorAddress: types.AccAddress{}}, - "127.0.0.1", - "", - "zetachain_7000-1", - false, - nil) - if err != nil { - panic(err) - } - return bridge -} diff --git a/zetaclient/testutils/mock/mock_core_bridge.go b/zetaclient/testutils/mock/mock_core_bridge.go new file mode 100644 index 0000000000..fc5e9ca236 --- /dev/null +++ b/zetaclient/testutils/mock/mock_core_bridge.go @@ -0,0 +1,131 @@ +package mock + +import ( + "math/big" + + "cosmossdk.io/math" + "github.com/rs/zerolog" + "github.com/zeta-chain/go-tss/blame" + "github.com/zeta-chain/zetacore/common" + cctxtypes "github.com/zeta-chain/zetacore/x/crosschain/types" + observerTypes "github.com/zeta-chain/zetacore/x/observer/types" + "github.com/zeta-chain/zetacore/zetaclient/interfaces" + "github.com/zeta-chain/zetacore/zetaclient/keys" + "github.com/zeta-chain/zetacore/zetaclient/testutils" +) + +var _ interfaces.ZetaCoreBridger = &ZetaCoreBridge{} + +type ZetaCoreBridge struct { + zetaChain common.Chain +} + +func (z ZetaCoreBridge) PostVoteInbound(_, _ uint64, _ *cctxtypes.MsgVoteOnObservedInboundTx) (string, string, error) { + return "", "", nil +} + +func (z ZetaCoreBridge) PostVoteOutbound(_ string, _ string, _ uint64, _ uint64, _ *big.Int, _ uint64, _ *big.Int, _ common.ReceiveStatus, _ common.Chain, _ uint64, _ common.CoinType) (string, string, error) { + return "", "", nil +} + +func (z ZetaCoreBridge) PostGasPrice(_ common.Chain, _ uint64, _ string, _ uint64) (string, error) { + return "", nil +} + +func (z ZetaCoreBridge) PostAddBlockHeader(_ int64, _ []byte, _ int64, _ common.HeaderData) (string, error) { + return "", nil +} + +func (z ZetaCoreBridge) GetBlockHeaderStateByChain(_ int64) (observerTypes.QueryGetBlockHeaderStateResponse, error) { + return observerTypes.QueryGetBlockHeaderStateResponse{}, nil +} + +func (z ZetaCoreBridge) PostBlameData(_ *blame.Blame, _ int64, _ string) (string, error) { + return "", nil +} + +func (z ZetaCoreBridge) AddTxHashToOutTxTracker(_ int64, _ uint64, _ string, _ *common.Proof, _ string, _ int64) (string, error) { + return "", nil +} + +func (z ZetaCoreBridge) GetKeys() *keys.Keys { + return &keys.Keys{} +} + +func (z ZetaCoreBridge) GetBlockHeight() (int64, error) { + return 0, nil +} + +func (z ZetaCoreBridge) GetZetaBlockHeight() (int64, error) { + return 0, nil +} + +func (z ZetaCoreBridge) GetLastBlockHeightByChain(_ common.Chain) (*cctxtypes.LastBlockHeight, error) { + return &cctxtypes.LastBlockHeight{}, nil +} + +func (z ZetaCoreBridge) ListPendingCctx(_ int64) ([]*cctxtypes.CrossChainTx, uint64, error) { + return []*cctxtypes.CrossChainTx{}, 0, nil +} + +func (z ZetaCoreBridge) GetPendingNoncesByChain(_ int64) (observerTypes.PendingNonces, error) { + return observerTypes.PendingNonces{}, nil +} + +func (z ZetaCoreBridge) GetCctxByNonce(_ int64, _ uint64) (*cctxtypes.CrossChainTx, error) { + return &cctxtypes.CrossChainTx{}, nil +} + +func (z ZetaCoreBridge) GetOutTxTracker(_ common.Chain, _ uint64) (*cctxtypes.OutTxTracker, error) { + return &cctxtypes.OutTxTracker{}, nil +} + +func (z ZetaCoreBridge) GetAllOutTxTrackerByChain(_ int64, _ interfaces.Order) ([]cctxtypes.OutTxTracker, error) { + return []cctxtypes.OutTxTracker{}, nil +} + +func (z ZetaCoreBridge) GetCrosschainFlags() (observerTypes.CrosschainFlags, error) { + return observerTypes.CrosschainFlags{}, nil +} + +func (z ZetaCoreBridge) GetObserverList() ([]string, error) { + return []string{}, nil +} + +func (z ZetaCoreBridge) GetKeyGen() (*observerTypes.Keygen, error) { + return &observerTypes.Keygen{}, nil +} + +func (z ZetaCoreBridge) GetBtcTssAddress(_ int64) (string, error) { + return testutils.TSSAddressBTCMainnet, nil +} + +func (z ZetaCoreBridge) GetInboundTrackersForChain(_ int64) ([]cctxtypes.InTxTracker, error) { + return []cctxtypes.InTxTracker{}, nil +} + +func (z ZetaCoreBridge) GetLogger() *zerolog.Logger { + return nil +} + +func (z ZetaCoreBridge) ZetaChain() common.Chain { + return z.zetaChain +} + +func (z ZetaCoreBridge) Pause() { +} + +func (z ZetaCoreBridge) Unpause() { +} + +func (z ZetaCoreBridge) GetZetaHotKeyBalance() (math.Int, error) { + return math.NewInt(0), nil +} + +func NewZetaCoreBridge() *ZetaCoreBridge { + zetaChain, err := common.ZetaChainFromChainID("zetachain_7000-1") + if err != nil { + panic(err) + } + return &ZetaCoreBridge{zetaChain: zetaChain} +} diff --git a/zetaclient/testutils/mock/mock_evm_rpc.go b/zetaclient/testutils/mock/mock_evm_rpc.go new file mode 100644 index 0000000000..c48afcc046 --- /dev/null +++ b/zetaclient/testutils/mock/mock_evm_rpc.go @@ -0,0 +1,98 @@ +package mock + +import ( + "math/big" + + "github.com/ethereum/go-ethereum" + ethcommon "github.com/ethereum/go-ethereum/common" + ethtypes "github.com/ethereum/go-ethereum/core/types" + "github.com/zeta-chain/zetacore/zetaclient/interfaces" + "golang.org/x/net/context" +) + +const EVMRPCEnabled = "MockEVMRPCEnabled" + +// Subscription interface +var _ ethereum.Subscription = subscription{} + +type subscription struct { +} + +func (s subscription) Unsubscribe() { +} + +func (s subscription) Err() <-chan error { + return nil +} + +// EvmClient interface +var _ interfaces.EVMRPCClient = EvmClient{} + +type EvmClient struct { +} + +func (e EvmClient) SubscribeFilterLogs(_ context.Context, _ ethereum.FilterQuery, _ chan<- ethtypes.Log) (ethereum.Subscription, error) { + return subscription{}, nil +} + +func (e EvmClient) CodeAt(_ context.Context, _ ethcommon.Address, _ *big.Int) ([]byte, error) { + return []byte{}, nil +} + +func (e EvmClient) CallContract(_ context.Context, _ ethereum.CallMsg, _ *big.Int) ([]byte, error) { + return []byte{}, nil +} + +func (e EvmClient) HeaderByNumber(_ context.Context, _ *big.Int) (*ethtypes.Header, error) { + return ðtypes.Header{}, nil +} + +func (e EvmClient) PendingCodeAt(_ context.Context, _ ethcommon.Address) ([]byte, error) { + return []byte{}, nil +} + +func (e EvmClient) PendingNonceAt(_ context.Context, _ ethcommon.Address) (uint64, error) { + return 0, nil +} + +func (e EvmClient) SuggestGasPrice(_ context.Context) (*big.Int, error) { + return big.NewInt(0), nil +} + +func (e EvmClient) SuggestGasTipCap(_ context.Context) (*big.Int, error) { + return big.NewInt(0), nil +} + +func (e EvmClient) EstimateGas(_ context.Context, _ ethereum.CallMsg) (gas uint64, err error) { + gas = 0 + err = nil + return +} + +func (e EvmClient) SendTransaction(_ context.Context, _ *ethtypes.Transaction) error { + return nil +} + +func (e EvmClient) FilterLogs(_ context.Context, _ ethereum.FilterQuery) ([]ethtypes.Log, error) { + return []ethtypes.Log{}, nil +} + +func (e EvmClient) BlockNumber(_ context.Context) (uint64, error) { + return 0, nil +} + +func (e EvmClient) BlockByNumber(_ context.Context, _ *big.Int) (*ethtypes.Block, error) { + return ðtypes.Block{}, nil +} + +func (e EvmClient) TransactionByHash(_ context.Context, _ ethcommon.Hash) (tx *ethtypes.Transaction, isPending bool, err error) { + return ðtypes.Transaction{}, false, nil +} + +func (e EvmClient) TransactionReceipt(_ context.Context, _ ethcommon.Hash) (*ethtypes.Receipt, error) { + return ðtypes.Receipt{}, nil +} + +func (e EvmClient) TransactionSender(_ context.Context, _ *ethtypes.Transaction, _ ethcommon.Hash, _ uint) (ethcommon.Address, error) { + return ethcommon.Address{}, nil +} diff --git a/zetaclient/testutils/mock/mock_tss_signer.go b/zetaclient/testutils/mock/mock_tss_signer.go new file mode 100644 index 0000000000..6e047321e9 --- /dev/null +++ b/zetaclient/testutils/mock/mock_tss_signer.go @@ -0,0 +1,83 @@ +package mock + +import ( + "crypto/ecdsa" + "fmt" + + "github.com/btcsuite/btcutil" + ethcommon "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + "github.com/zeta-chain/zetacore/common" + "github.com/zeta-chain/zetacore/zetaclient/interfaces" + "github.com/zeta-chain/zetacore/zetaclient/testutils" +) + +const ( + TestPrivateKey = "401c22d98e2b63138afba629f70bc7dea9f299f973b257b484d1ae4c73bf54e4" +) + +var _ interfaces.TSSSigner = (*TSS)(nil) + +// TSS is a mock of TSS signer for testing +type TSS struct { + evmAddress string + btcAddress string +} + +func NewMockTSS(evmAddress string, btcAddress string) *TSS { + return &TSS{ + evmAddress: evmAddress, + btcAddress: btcAddress, + } +} + +func NewTSSMainnet() *TSS { + return NewMockTSS(testutils.TSSAddressEVMMainnet, testutils.TSSAddressBTCMainnet) +} + +func NewTSSAthens3() *TSS { + return NewMockTSS(testutils.TSSAddressEVMAthens3, testutils.TSSAddressBTCAthens3) +} + +// Sign uses test key unrelated to any tss key in production +func (s *TSS) Sign(data []byte, _ uint64, _ uint64, _ *common.Chain, _ string) ([65]byte, error) { + privateKey, err := crypto.HexToECDSA(TestPrivateKey) + if err != nil { + return [65]byte{}, err + } + signature, err := crypto.Sign(data, privateKey) + if err != nil { + return [65]byte{}, err + } + var sigbyte [65]byte + _ = copy(sigbyte[:], signature[:65]) + + return sigbyte, nil +} + +// Pubkey uses the hardcoded private test key to generate the public key in bytes +func (s *TSS) Pubkey() []byte { + privateKey, _ := crypto.HexToECDSA(TestPrivateKey) + publicKey := privateKey.Public() + publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) + if !ok { + fmt.Println("error casting public key to ECDSA") + } + return crypto.FromECDSAPub(publicKeyECDSA) +} + +func (s *TSS) EVMAddress() ethcommon.Address { + return ethcommon.HexToAddress(s.evmAddress) +} + +func (s *TSS) BTCAddress() string { + return s.btcAddress +} + +func (s *TSS) BTCAddressWitnessPubkeyHash() *btcutil.AddressWitnessPubKeyHash { + return nil +} + +func (s *TSS) PubKeyCompressedBytes() []byte { + return []byte{} +}