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