From c8ac66ece1e5ed701914b8b164bb1aeb229f4f69 Mon Sep 17 00:00:00 2001 From: ququzone Date: Wed, 19 Apr 2023 23:36:52 +0800 Subject: [PATCH 1/2] add did service command --- ioctl/cmd/did/common.go | 14 ++++++++ ioctl/cmd/did/did.go | 2 ++ ioctl/cmd/did/didserviceadd.go | 60 +++++++++++++++++++++++++++++++ ioctl/cmd/did/didserviceremove.go | 58 ++++++++++++++++++++++++++++++ 4 files changed, 134 insertions(+) create mode 100644 ioctl/cmd/did/didserviceadd.go create mode 100644 ioctl/cmd/did/didserviceremove.go 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..e59efb6e47 --- /dev/null +++ b/ioctl/cmd/did/didserviceadd.go @@ -0,0 +1,60 @@ +package did + +import ( + "encoding/json" + + "github.com/iotexproject/iotex-core/ioctl/cmd/action" + "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/output" + "github.com/spf13/cobra" +) + +// 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..a1455f4a00 --- /dev/null +++ b/ioctl/cmd/did/didserviceremove.go @@ -0,0 +1,58 @@ +package did + +import ( + "encoding/json" + + "github.com/iotexproject/iotex-core/ioctl/cmd/action" + "github.com/iotexproject/iotex-core/ioctl/config" + "github.com/iotexproject/iotex-core/ioctl/output" + "github.com/spf13/cobra" +) + +// 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) +} From 74cbacdf8535ccfe36d7657cdf709df84d2ffa5d Mon Sep 17 00:00:00 2001 From: ququzone Date: Thu, 20 Apr 2023 10:14:57 +0800 Subject: [PATCH 2/2] optimize code style --- ioctl/cmd/did/didserviceadd.go | 4 ++-- ioctl/cmd/did/didserviceremove.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/ioctl/cmd/did/didserviceadd.go b/ioctl/cmd/did/didserviceadd.go index e59efb6e47..5dea7285b7 100644 --- a/ioctl/cmd/did/didserviceadd.go +++ b/ioctl/cmd/did/didserviceadd.go @@ -3,10 +3,11 @@ 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" - "github.com/spf13/cobra" ) // Multi-language support @@ -55,6 +56,5 @@ func addService(args []string) error { 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 index a1455f4a00..a15cdcf960 100644 --- a/ioctl/cmd/did/didserviceremove.go +++ b/ioctl/cmd/did/didserviceremove.go @@ -3,10 +3,11 @@ 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" - "github.com/spf13/cobra" ) // Multi-language support @@ -53,6 +54,5 @@ func removeService(args []string) error { if err != nil { return output.NewError(output.ConvertError, "failed to encode request", err) } - return postToResolver(endpoint+"/did/"+addr+"/service/delete", serviceBytes) }