Skip to content

Commit

Permalink
Merge c8ac66e into a65ee2b
Browse files Browse the repository at this point in the history
  • Loading branch information
ququzone authored Apr 19, 2023
2 parents a65ee2b + c8ac66e commit 5263ba4
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
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"
)

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

return postToResolver(endpoint+"/did/"+addr+"/service/delete", serviceBytes)
}

0 comments on commit 5263ba4

Please sign in to comment.