github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/did/didderegister.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/json"
    10  
    11  	"github.com/spf13/cobra"
    12  
    13  	"github.com/iotexproject/iotex-core/ioctl/cmd/action"
    14  	"github.com/iotexproject/iotex-core/ioctl/config"
    15  	"github.com/iotexproject/iotex-core/ioctl/output"
    16  )
    17  
    18  // Multi-language support
    19  var (
    20  	_deregisterCmdUses = map[config.Language]string{
    21  		config.English: "deregister (RESOLVER_ENDPOINT) [-s SIGNER]",
    22  		config.Chinese: "deregister (Resolver端点) [-s 签署人]",
    23  	}
    24  	_deregisterCmdShorts = map[config.Language]string{
    25  		config.English: "Deregister DID on IoTeX blockchain",
    26  		config.Chinese: "Deregister 在IoTeX链上注销DID",
    27  	}
    28  )
    29  
    30  // _didDeregisterCmd represents the contract invoke deregister command
    31  var _didDeregisterCmd = &cobra.Command{
    32  	Use:   config.TranslateInLang(_deregisterCmdUses, config.UILanguage),
    33  	Short: config.TranslateInLang(_deregisterCmdShorts, config.UILanguage),
    34  	Args:  cobra.ExactArgs(1),
    35  	RunE: func(cmd *cobra.Command, args []string) error {
    36  		cmd.SilenceUsage = true
    37  		err := deregisterDID(args)
    38  		return output.PrintError(err)
    39  	},
    40  }
    41  
    42  func init() {
    43  	action.RegisterWriteCommand(_didDeregisterCmd)
    44  }
    45  
    46  func deregisterDID(args []string) (err error) {
    47  	endpoint := args[0]
    48  
    49  	signature, _, addr, err := signPermit(endpoint)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	deleteBytes, err := json.Marshal(&signature)
    55  	if err != nil {
    56  		return output.NewError(output.ConvertError, "failed to encode request", err)
    57  	}
    58  
    59  	return postToResolver(endpoint+"/did/"+addr+"/delete", deleteBytes)
    60  }