github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/did/didget.go (about) 1 // Copyright (c) 2022 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package did 7 8 import ( 9 "io" 10 "net/http" 11 "strings" 12 13 "github.com/spf13/cobra" 14 15 "github.com/iotexproject/iotex-core/ioctl/config" 16 "github.com/iotexproject/iotex-core/ioctl/output" 17 "github.com/iotexproject/iotex-core/pkg/util/addrutil" 18 ) 19 20 // Multi-language support 21 var ( 22 _getCmdUses = map[config.Language]string{ 23 config.English: "get (RESOLVER_ENDPOINT) ADDRESS", 24 config.Chinese: "get (Resolver端点) ADDRESS", 25 } 26 _getCmdShorts = map[config.Language]string{ 27 config.English: "Get get DID Document on IoTeX blockchain", 28 config.Chinese: "Get 在IoTeX链上获取相应DID的文档", 29 } 30 ) 31 32 // _didGetCmd represents the DID document for account 33 var _didGetCmd = &cobra.Command{ 34 Use: config.TranslateInLang(_getCmdUses, config.UILanguage), 35 Short: config.TranslateInLang(_getCmdShorts, config.UILanguage), 36 Args: cobra.ExactArgs(2), 37 RunE: func(cmd *cobra.Command, args []string) error { 38 cmd.SilenceUsage = true 39 return output.PrintError(get(args)) 40 }, 41 } 42 43 func get(args []string) (err error) { 44 endpoint := args[0] 45 address := args[1] 46 if strings.HasPrefix(address, "io") { 47 ethAddress, err := addrutil.IoAddrToEvmAddr(address) 48 if err != nil { 49 return output.NewError(output.AddressError, "", err) 50 } 51 address = ethAddress.String() 52 } 53 resp, err := http.Get(endpoint + "/did/" + address) 54 if err != nil { 55 return output.NewError(output.ConvertError, "failed to get request", err) 56 } 57 defer resp.Body.Close() 58 59 body, err := io.ReadAll(resp.Body) 60 if err != nil { 61 return output.NewError(output.ConvertError, "failed to read response", err) 62 } 63 output.PrintResult(string(body)) 64 return 65 }