Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ioctl] add did service command #3848

Merged
merged 2 commits into from
Apr 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions ioctl/cmd/did/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions ioctl/cmd/did/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
60 changes: 60 additions & 0 deletions ioctl/cmd/did/didserviceadd.go
Original file line number Diff line number Diff line change
@@ -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"
ququzone marked this conversation as resolved.
Show resolved Hide resolved
)

// 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)
}

ququzone marked this conversation as resolved.
Show resolved Hide resolved
return postToResolver(endpoint+"/did/"+addr+"/service", serviceBytes)
}
58 changes: 58 additions & 0 deletions ioctl/cmd/did/didserviceremove.go
Original file line number Diff line number Diff line change
@@ -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)
}

ququzone marked this conversation as resolved.
Show resolved Hide resolved
return postToResolver(endpoint+"/did/"+addr+"/service/delete", serviceBytes)
}