github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/did/didserviceadd.go (about) 1 package did 2 3 import ( 4 "encoding/json" 5 6 "github.com/spf13/cobra" 7 8 "github.com/iotexproject/iotex-core/ioctl/cmd/action" 9 "github.com/iotexproject/iotex-core/ioctl/config" 10 "github.com/iotexproject/iotex-core/ioctl/output" 11 ) 12 13 // Multi-language support 14 var ( 15 _serviceaddCmdShorts = map[config.Language]string{ 16 config.English: "Add service to DID document using private key from wallet", 17 config.Chinese: "用钱包中的私钥向DID document添加服务", 18 } 19 _serviceaddCmdUses = map[config.Language]string{ 20 config.English: "serviceadd [-s SIGNER] RESOLVER_ENDPOINT TAG TYPE SERVICE_ENDPOINT", 21 config.Chinese: "serviceadd [-s 签署人] Resolver端点 标签 类型 服务端点", 22 } 23 ) 24 25 // _didServiceAddCmd represents service add command 26 var _didServiceAddCmd = &cobra.Command{ 27 Use: config.TranslateInLang(_serviceaddCmdUses, config.UILanguage), 28 Short: config.TranslateInLang(_serviceaddCmdShorts, config.UILanguage), 29 Args: cobra.ExactArgs(4), 30 RunE: func(cmd *cobra.Command, args []string) error { 31 cmd.SilenceUsage = true 32 err := addService(args) 33 return output.PrintError(err) 34 }, 35 } 36 37 func init() { 38 action.RegisterWriteCommand(_didServiceAddCmd) 39 } 40 41 func addService(args []string) error { 42 endpoint := args[0] 43 44 signature, _, addr, err := signPermit(endpoint) 45 if err != nil { 46 return err 47 } 48 49 serviceReq := &ServiceAddRequest{ 50 Signature: *signature, 51 Tag: args[1], 52 Type: args[2], 53 ServiceEndpoint: args[3], 54 } 55 serviceBytes, err := json.Marshal(&serviceReq) 56 if err != nil { 57 return output.NewError(output.ConvertError, "failed to encode request", err) 58 } 59 return postToResolver(endpoint+"/did/"+addr+"/service", serviceBytes) 60 }