github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/newcmd/account/accountverify.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 account 7 8 import ( 9 "fmt" 10 11 "github.com/iotexproject/go-pkgs/crypto" 12 "github.com/pkg/errors" 13 "github.com/spf13/cobra" 14 15 "github.com/iotexproject/iotex-core/ioctl" 16 "github.com/iotexproject/iotex-core/ioctl/config" 17 ) 18 19 // Multi-language support 20 var ( 21 _verifyCmdShorts = map[config.Language]string{ 22 config.English: "Verify IoTeX public key and address by private key", 23 config.Chinese: "用私钥验证IoTeX的公钥和地址", 24 } 25 _enterPrivateKey = map[config.Language]string{ 26 config.English: "Enter private key:", 27 config.Chinese: "输入私钥:", 28 } 29 _failToGetPrivateKey = map[config.Language]string{ 30 config.English: "failed to get private key", 31 config.Chinese: "获取私钥失败", 32 } 33 _failToCovertHexStringToPrivateKey = map[config.Language]string{ 34 config.English: "failed to covert hex string to private key", 35 config.Chinese: "十六进制字符串转换私钥失败", 36 } 37 ) 38 39 // NewAccountVerify represents the account verify command 40 func NewAccountVerify(client ioctl.Client) *cobra.Command { 41 short, _ := client.SelectTranslation(_verifyCmdShorts) 42 _enterPrivateKey, _ := client.SelectTranslation(_enterPrivateKey) 43 _failToGetPrivateKey, _ := client.SelectTranslation(_failToGetPrivateKey) 44 failToConvertPublicKeyIntoAddress, _ := client.SelectTranslation(_failToConvertPublicKeyIntoAddress) 45 _failToCovertHexStringToPrivateKey, _ := client.SelectTranslation(_failToCovertHexStringToPrivateKey) 46 47 return &cobra.Command{ 48 Use: "verify", 49 Short: short, 50 Args: cobra.ExactArgs(0), 51 RunE: func(cmd *cobra.Command, args []string) error { 52 cmd.SilenceUsage = true 53 54 cmd.Println(_enterPrivateKey) 55 privateKey, err := client.ReadSecret() 56 if err != nil { 57 return errors.Wrap(err, _failToGetPrivateKey) 58 } 59 priKey, err := crypto.HexStringToPrivateKey(privateKey) 60 if err != nil { 61 return errors.Wrap(err, _failToCovertHexStringToPrivateKey) 62 } 63 addr := priKey.PublicKey().Address() 64 if addr == nil { 65 return errors.New(failToConvertPublicKeyIntoAddress) 66 } 67 priKey.Zero() 68 cmd.Println(fmt.Sprintf("Address:\t%s\nPublic Key:\t%s", 69 addr.String(), 70 fmt.Sprintf("%x", priKey.PublicKey().Bytes())), 71 ) 72 return nil 73 }, 74 } 75 }