github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/did/didregister.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  	"math/big"
    11  
    12  	"github.com/pkg/errors"
    13  	"github.com/spf13/cobra"
    14  
    15  	"github.com/iotexproject/iotex-core/ioctl"
    16  	"github.com/iotexproject/iotex-core/ioctl/config"
    17  	"github.com/iotexproject/iotex-core/ioctl/newcmd/action"
    18  )
    19  
    20  // Multi-language support
    21  var (
    22  	_registerCmdUses = map[config.Language]string{
    23  		config.English: "register (CONTRACT_ADDRESS|ALIAS) hash uri [-s SIGNER] [-n NONCE] [-l GAS_LIMIT] [-p GAS_PRICE] [-P PASSWORD] [-y]",
    24  		config.Chinese: "register (合约地址|别名) hash uri [-s 签署人] [-n NONCE] [-l GAS限制] [-P GAS价格] [-P 密码] [-y]",
    25  	}
    26  	_registerCmdShorts = map[config.Language]string{
    27  		config.English: "Register DID on IoTeX blockchain",
    28  		config.Chinese: "在IoTeX链上注册DID",
    29  	}
    30  )
    31  
    32  // NewDidRegisterCmd represents the did register command
    33  func NewDidRegisterCmd(client ioctl.Client) *cobra.Command {
    34  	use, _ := client.SelectTranslation(_registerCmdUses)
    35  	short, _ := client.SelectTranslation(_registerCmdShorts)
    36  
    37  	cmd := &cobra.Command{
    38  		Use:   use,
    39  		Short: short,
    40  		Args:  cobra.ExactArgs(3),
    41  		RunE: func(cmd *cobra.Command, args []string) error {
    42  			cmd.SilenceUsage = true
    43  			contract, err := client.Address(args[0])
    44  			if err != nil {
    45  				return errors.Wrap(err, "failed to get contract address")
    46  			}
    47  			bytecode, err := encode(_registerDIDName, args[1], args[2])
    48  			if err != nil {
    49  				return errors.Wrap(err, "failed to decode data")
    50  			}
    51  			gasPrice, signer, password, nonce, gasLimit, assumeYes, err := action.GetWriteCommandFlag(cmd)
    52  			if err != nil {
    53  				return err
    54  			}
    55  			return action.Execute(client, cmd, contract, big.NewInt(0), bytecode, gasPrice, signer, password, nonce, gasLimit, assumeYes)
    56  		},
    57  	}
    58  	action.RegisterWriteCommand(client, cmd)
    59  	return cmd
    60  }
    61  
    62  func encode(method, didHash, uri string) ([]byte, error) {
    63  	hashSlice, err := hex.DecodeString(didHash)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  	var hashArray [32]byte
    68  	copy(hashArray[:], hashSlice)
    69  	return _didABI.Pack(method, hashArray, []byte(uri))
    70  }