github.com/ChainSafe/chainbridge-core@v1.4.2/chains/evm/cli/centrifuge/get-hash.go (about)

     1  package centrifuge
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	callsUtil "github.com/ChainSafe/chainbridge-core/chains/evm/calls"
     8  	"github.com/ChainSafe/chainbridge-core/chains/evm/calls/contracts/centrifuge"
     9  	"github.com/ChainSafe/chainbridge-core/chains/evm/calls/evmtransaction"
    10  	"github.com/ChainSafe/chainbridge-core/chains/evm/cli/initialize"
    11  	"github.com/ChainSafe/chainbridge-core/util"
    12  
    13  	"github.com/ChainSafe/chainbridge-core/chains/evm/cli/flags"
    14  	"github.com/ChainSafe/chainbridge-core/chains/evm/cli/logger"
    15  	"github.com/ethereum/go-ethereum/common"
    16  	"github.com/rs/zerolog/log"
    17  	"github.com/spf13/cobra"
    18  )
    19  
    20  var getHashCmd = &cobra.Command{
    21  	Use:   "get-hash",
    22  	Short: "Returns the status of whether a given hash exists in an asset store",
    23  	Long:  "The get-hash subcommand checks the _assetsStored map on a Centrifuge asset store contract to determine whether the asset hash exists or not",
    24  	PreRun: func(cmd *cobra.Command, args []string) {
    25  		logger.LoggerMetadata(cmd.Name(), cmd.Flags())
    26  	},
    27  	PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
    28  		return util.CallPersistentPreRun(cmd, args)
    29  	},
    30  	RunE: func(cmd *cobra.Command, args []string) error {
    31  		c, err := initialize.InitializeClient(url, senderKeyPair)
    32  		if err != nil {
    33  			return err
    34  		}
    35  		t, err := initialize.InitializeTransactor(gasPrice, evmtransaction.NewTransaction, c, prepare)
    36  		if err != nil {
    37  			return err
    38  		}
    39  		return GetHashCmd(cmd, args, centrifuge.NewAssetStoreContract(c, StoreAddr, t))
    40  	},
    41  	Args: func(cmd *cobra.Command, args []string) error {
    42  		err := ValidateGetHashFlags(cmd, args)
    43  		if err != nil {
    44  			return err
    45  		}
    46  
    47  		err = ProcessGetHashFlags(cmd, args)
    48  		if err != nil {
    49  			return err
    50  		}
    51  
    52  		return nil
    53  	},
    54  }
    55  
    56  func init() {
    57  	BindGetHashFlags(getHashCmd)
    58  }
    59  
    60  func BindGetHashFlags(cmd *cobra.Command) {
    61  	cmd.Flags().StringVar(&Hash, "hash", "", "A hash to lookup")
    62  	cmd.Flags().StringVar(&Address, "address", "", "Centrifuge asset store contract address")
    63  	flags.MarkFlagsAsRequired(cmd, "hash", "address")
    64  }
    65  
    66  func ValidateGetHashFlags(cmd *cobra.Command, args []string) error {
    67  	if !common.IsHexAddress(Address) {
    68  		return errors.New("invalid Centrifuge asset store address")
    69  	}
    70  
    71  	return nil
    72  }
    73  
    74  func ProcessGetHashFlags(cmd *cobra.Command, args []string) error {
    75  	StoreAddr = common.HexToAddress(Address)
    76  	ByteHash = callsUtil.SliceTo32Bytes([]byte(Hash))
    77  
    78  	return nil
    79  }
    80  
    81  func GetHashCmd(cmd *cobra.Command, args []string, contract *centrifuge.AssetStoreContract) error {
    82  	isAssetStored, err := contract.IsCentrifugeAssetStored(ByteHash)
    83  	if err != nil {
    84  		log.Error().Err(fmt.Errorf("checking if asset stored failed: %w", err))
    85  		return err
    86  	}
    87  
    88  	log.Info().Msgf("The hash '%s' exists: %t", Hash, isAssetStored)
    89  	return nil
    90  }