diff --git a/ioctl/cmd/did/common.go b/ioctl/cmd/did/common.go index 38b7613637..17c3efd44a 100644 --- a/ioctl/cmd/did/common.go +++ b/ioctl/cmd/did/common.go @@ -43,6 +43,20 @@ type ( Signature PublicKey string `json:"publicKey"` } + + // ServiceAddRequest add service to DID request + ServiceAddRequest struct { + Signature + Tag string `json:"tag"` + Type string `json:"type"` + ServiceEndpoint string `json:"serviceEndpoint"` + } + + // ServiceRemoveRequest remove service from DID request + ServiceRemoveRequest struct { + Signature + Tag string `json:"tag"` + } ) // getPermit fetch DID permit from resolver diff --git a/ioctl/cmd/did/did.go b/ioctl/cmd/did/did.go index d3606c13b5..164795e9d2 100644 --- a/ioctl/cmd/did/did.go +++ b/ioctl/cmd/did/did.go @@ -38,6 +38,8 @@ func init() { DIDCmd.AddCommand(_didRegisterCmd) DIDCmd.AddCommand(_didGetCmd) DIDCmd.AddCommand(_didDeregisterCmd) + DIDCmd.AddCommand(_didServiceAddCmd) + DIDCmd.AddCommand(_didServiceRemoveCmd) DIDCmd.PersistentFlags().StringVar(&config.ReadConfig.Endpoint, "endpoint", config.ReadConfig.Endpoint, config.TranslateInLang(_flagEndpoint, config.UILanguage)) DIDCmd.PersistentFlags().BoolVar(&config.Insecure, "insecure", config.Insecure, config.TranslateInLang(_flagInsecure, config.UILanguage)) diff --git a/ioctl/cmd/did/didserviceadd.go b/ioctl/cmd/did/didserviceadd.go new file mode 100644 index 0000000000..5dea7285b7 --- /dev/null +++ b/ioctl/cmd/did/didserviceadd.go @@ -0,0 +1,60 @@ +package did + +import ( + "encoding/json" + + "github.com/spf13/cobra" + + "github.com/iotexproject/iotex-core/ioctl/cmd/action" + "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/output" +) + +// Multi-language support +var ( + _serviceaddCmdShorts = map[config.Language]string{ + config.English: "Add service to DID document using private key from wallet", + config.Chinese: "用钱包中的私钥向DID document添加服务", + } + _serviceaddCmdUses = map[config.Language]string{ + config.English: "serviceadd [-s SIGNER] RESOLVER_ENDPOINT TAG TYPE SERVICE_ENDPOINT", + config.Chinese: "serviceadd [-s 签署人] Resolver端点 标签 类型 服务端点", + } +) + +// _didServiceAddCmd represents service add command +var _didServiceAddCmd = &cobra.Command{ + Use: config.TranslateInLang(_serviceaddCmdUses, config.UILanguage), + Short: config.TranslateInLang(_serviceaddCmdShorts, config.UILanguage), + Args: cobra.ExactArgs(4), + RunE: func(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true + err := addService(args) + return output.PrintError(err) + }, +} + +func init() { + action.RegisterWriteCommand(_didServiceAddCmd) +} + +func addService(args []string) error { + endpoint := args[0] + + signature, _, addr, err := signPermit(endpoint) + if err != nil { + return err + } + + serviceReq := &ServiceAddRequest{ + Signature: *signature, + Tag: args[1], + Type: args[2], + ServiceEndpoint: args[3], + } + serviceBytes, err := json.Marshal(&serviceReq) + if err != nil { + return output.NewError(output.ConvertError, "failed to encode request", err) + } + return postToResolver(endpoint+"/did/"+addr+"/service", serviceBytes) +} diff --git a/ioctl/cmd/did/didserviceremove.go b/ioctl/cmd/did/didserviceremove.go new file mode 100644 index 0000000000..a15cdcf960 --- /dev/null +++ b/ioctl/cmd/did/didserviceremove.go @@ -0,0 +1,58 @@ +package did + +import ( + "encoding/json" + + "github.com/spf13/cobra" + + "github.com/iotexproject/iotex-core/ioctl/cmd/action" + "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/output" +) + +// Multi-language support +var ( + _serviceremoveCmdShorts = map[config.Language]string{ + config.English: "Remove service to DID document using private key from wallet", + config.Chinese: "用钱包中的私钥从DID document移除服务", + } + _serviceremoveCmdUses = map[config.Language]string{ + config.English: "serviceremove [-s SIGNER] RESOLVER_ENDPOINT TAG", + config.Chinese: "serviceremove [-s 签署人] Resolver端点 标签", + } +) + +// _didServiceRemoveCmd represents service remove command +var _didServiceRemoveCmd = &cobra.Command{ + Use: config.TranslateInLang(_serviceremoveCmdUses, config.UILanguage), + Short: config.TranslateInLang(_serviceremoveCmdShorts, config.UILanguage), + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true + err := removeService(args) + return output.PrintError(err) + }, +} + +func init() { + action.RegisterWriteCommand(_didServiceRemoveCmd) +} + +func removeService(args []string) error { + endpoint := args[0] + + signature, _, addr, err := signPermit(endpoint) + if err != nil { + return err + } + + serviceReq := &ServiceRemoveRequest{ + Signature: *signature, + Tag: args[1], + } + serviceBytes, err := json.Marshal(&serviceReq) + if err != nil { + return output.NewError(output.ConvertError, "failed to encode request", err) + } + return postToResolver(endpoint+"/did/"+addr+"/service/delete", serviceBytes) +}