github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/account/accountverify.go (about) 1 // Copyright (c) 2019 IoTeX 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/spf13/cobra" 12 13 "github.com/iotexproject/go-pkgs/crypto" 14 "github.com/iotexproject/iotex-core/ioctl/config" 15 "github.com/iotexproject/iotex-core/ioctl/output" 16 "github.com/iotexproject/iotex-core/ioctl/util" 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 ) 26 var ( 27 // _accountVerifyCmd represents the account verify command 28 _accountVerifyCmd = &cobra.Command{ 29 Use: "verify", 30 Short: config.TranslateInLang(_verifyCmdShorts, config.UILanguage), 31 Args: cobra.ExactArgs(0), 32 RunE: func(cmd *cobra.Command, args []string) error { 33 cmd.SilenceUsage = true 34 err := accountVerify() 35 return output.PrintError(err) 36 }, 37 } 38 ) 39 40 type verifyMessage struct { 41 Address string `json:"address"` 42 PublicKey string `json:"publicKey"` 43 } 44 45 func accountVerify() error { 46 fmt.Println("Enter private key:") 47 privateKey, err := util.ReadSecretFromStdin() 48 if err != nil { 49 return output.NewError(output.InputError, "failed to get private key", err) 50 } 51 priKey, err := crypto.HexStringToPrivateKey(privateKey) 52 if err != nil { 53 return output.NewError(output.CryptoError, "failed to generate private key from hex string", err) 54 } 55 addr := priKey.PublicKey().Address() 56 if addr == nil { 57 return output.NewError(output.ConvertError, "failed to convert public key into address", nil) 58 } 59 message := verifyMessage{ 60 Address: addr.String(), 61 PublicKey: fmt.Sprintf("%x", priKey.PublicKey().Bytes()), 62 } 63 priKey.Zero() 64 fmt.Println(message.String()) 65 return nil 66 } 67 68 func (m *verifyMessage) String() string { 69 if output.Format == "" { 70 return fmt.Sprintf("Address:\t%s\nPublic Key:\t%s", m.Address, m.PublicKey) 71 } 72 return output.FormatString(output.Result, m) 73 }