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)