From f285fe4735d750125634e2d0e2d73d239b360c5f Mon Sep 17 00:00:00 2001 From: Tanmay Date: Thu, 7 Mar 2024 12:39:03 -0500 Subject: [PATCH] add validate --- x/crosschain/types/cctx_utils.go | 77 ++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/x/crosschain/types/cctx_utils.go b/x/crosschain/types/cctx_utils.go index bf3357c709..3b9b824f9f 100644 --- a/x/crosschain/types/cctx_utils.go +++ b/x/crosschain/types/cctx_utils.go @@ -5,6 +5,8 @@ import ( "strconv" sdk "github.com/cosmos/cosmos-sdk/types" + ethcommon "github.com/ethereum/go-ethereum/common" + "github.com/zeta-chain/zetacore/common" observertypes "github.com/zeta-chain/zetacore/x/observer/types" ) @@ -46,6 +48,81 @@ func GetAllAuthzZetaclientTxTypes() []string { } } +func (m CrossChainTx) Validate() error { + if m.InboundTxParams == nil { + return fmt.Errorf("inbound tx params cannot be nil") + } + if m.OutboundTxParams == nil { + return fmt.Errorf("outbound tx params cannot be nil") + } + if m.CctxStatus == nil { + return fmt.Errorf("cctx status cannot be nil") + } + if len(m.OutboundTxParams) > 2 { + return fmt.Errorf("outbound tx params cannot be more than 2") + } + if len(m.Index) != 66 { + return ErrInvalidCCTXIndex + } + + return nil +} + +func (m InboundTxParams) Validate() error { + if m.Sender == "" { + return fmt.Errorf("sender cannot be empty") + } + if m.InboundTxObservedHash == "" { + return fmt.Errorf("inbound tx observed hash cannot be empty") + } + if len(m.InboundTxBallotIndex) != 66 { + return fmt.Errorf("inbound tx ballot index must be 66 characters") + } + if common.IsEthereumChain(m.SenderChainId) { + if !ethcommon.IsHexAddress(m.Sender) { + return fmt.Errorf("sender a valid ethereum address") + } + } + if m.Amount.IsNil() { + return fmt.Errorf("amount cannot be nil") + } + if common.IsBitcoinChain(m.SenderChainId) { + //if _, err := common.BitcoinAddressToPubKeyHash(m.Sender); err != nil { + // return fmt.Errorf("sender must be a valid bitcoin address") + //} + } + return nil +} + +func (m OutboundTxParams) Validate() error { + if m.Receiver == "" { + return fmt.Errorf("receiver cannot be empty") + } + if m.Amount.IsNil() { + return fmt.Errorf("amount cannot be nil") + } + if m.OutboundTxGasPrice == "" { + return fmt.Errorf("outbound tx gas price cannot be empty") + } + if m.GasLimit == 0 { + return fmt.Errorf("gas limit cannot be 0") + } + if m.ReceiverChainId == 0 { + return fmt.Errorf("receiver chain id cannot be 0") + } + if common.IsEthereumChain(m.ReceiverChainId) { + if !ethcommon.IsHexAddress(m.Receiver) { + return fmt.Errorf("receiver must be a valid ethereum address") + } + } + if common.IsBitcoinChain(m.ReceiverChainId) { + //if _, err := common.BitcoinAddressToPubKeyHash(m.Receiver); err != nil { + // return fmt.Errorf("receiver must be a valid bitcoin address") + //} + } + return nil +} + // GetGasPrice returns the gas price of the outbound tx func (m OutboundTxParams) GetGasPrice() (uint64, error) { gasPrice, err := strconv.ParseUint(m.OutboundTxGasPrice, 10, 64)