github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/did/didgethash.go (about)

     1  // Copyright (c) 2022 IoTeX Foundation
     2  // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
     3  // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
     4  // This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
     5  
     6  package did
     7  
     8  import (
     9  	"encoding/hex"
    10  
    11  	"github.com/pkg/errors"
    12  	"github.com/spf13/cobra"
    13  
    14  	"github.com/iotexproject/iotex-core/ioctl"
    15  	"github.com/iotexproject/iotex-core/ioctl/config"
    16  	"github.com/iotexproject/iotex-core/ioctl/newcmd/action"
    17  	"github.com/iotexproject/iotex-core/ioctl/newcmd/alias"
    18  	"github.com/iotexproject/iotex-core/ioctl/util"
    19  )
    20  
    21  // Multi-language support
    22  var (
    23  	_getHashCmdUses = map[config.Language]string{
    24  		config.English: "gethash (CONTRACT_ADDRESS|ALIAS) DID",
    25  		config.Chinese: "gethash (合约地址|别名) DID",
    26  	}
    27  	_getHashCmdShorts = map[config.Language]string{
    28  		config.English: "Get DID doc's hash on IoTeX blockchain",
    29  		config.Chinese: "在IoTeX链上获取相应DID的doc hash",
    30  	}
    31  )
    32  
    33  // NewDidGetHashCmd represents the did get hash command
    34  func NewDidGetHashCmd(client ioctl.Client) *cobra.Command {
    35  	use, _ := client.SelectTranslation(_getHashCmdUses)
    36  	short, _ := client.SelectTranslation(_getHashCmdShorts)
    37  
    38  	cmd := &cobra.Command{
    39  		Use:   use,
    40  		Short: short,
    41  		Args:  cobra.ExactArgs(2),
    42  		RunE: func(cmd *cobra.Command, args []string) error {
    43  			cmd.SilenceUsage = true
    44  			addr, err := alias.IOAddress(client, args[0])
    45  			if err != nil {
    46  				return errors.Wrap(err, "failed to get contract address")
    47  			}
    48  			bytecode, err := _didABI.Pack(_getHashName, []byte(args[1]))
    49  			if err != nil {
    50  				return errors.Wrap(err, "invalid bytecode")
    51  			}
    52  			result, err := action.Read(client, addr, "0", bytecode, action.SignerFlagDefault, action.GasLimitFlagDefault)
    53  			if err != nil {
    54  				return errors.Wrap(err, "failed to read contract")
    55  			}
    56  			ret, err := hex.DecodeString(result)
    57  			if err != nil {
    58  				return errors.Wrap(err, "failed to decode contract")
    59  			}
    60  			res, err := _didABI.Unpack(_getHashName, ret)
    61  			if err != nil {
    62  				return errors.New("DID does not exist")
    63  			}
    64  			out, err := util.To32Bytes(res[0])
    65  			if err != nil {
    66  				return errors.Wrap(err, "failed to convert hash to bytes")
    67  			}
    68  			cmd.Println(hex.EncodeToString(out[:]))
    69  			return nil
    70  		},
    71  	}
    72  	return cmd
    73  }