-
Notifications
You must be signed in to change notification settings - Fork 320
/
accountverify.go
80 lines (71 loc) · 2.46 KB
/
accountverify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2019 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package account
import (
"fmt"
"github.com/spf13/cobra"
"github.com/iotexproject/go-pkgs/crypto"
"github.com/iotexproject/iotex-address/address"
"github.com/iotexproject/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/ioctl/util"
)
// Multi-language support
var (
verifyCmdShorts = map[config.Language]string{
config.English: "Verify IoTeX public key and address by private key",
config.Chinese: "用私钥验证IoTeX的公钥和地址",
}
verifyCmdUses = map[config.Language]string{
config.English: "verify",
config.Chinese: "verify 验证",
}
)
var (
// accountVerifyCmd represents the account verify command
accountVerifyCmd = &cobra.Command{
Use: config.TranslateInLang(verifyCmdUses, config.UILanguage),
Short: config.TranslateInLang(verifyCmdShorts, config.UILanguage),
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := accountVerify()
return output.PrintError(err)
},
}
)
type verifyMessage struct {
Address string `json:"address"`
PublicKey string `json:"publicKey"`
}
func accountVerify() error {
fmt.Println("Enter private key:")
privateKey, err := util.ReadSecretFromStdin()
if err != nil {
return output.NewError(output.InputError, "failed to get private key", err)
}
priKey, err := crypto.HexStringToPrivateKey(privateKey)
if err != nil {
return output.NewError(output.CryptoError, "failed to generate private key from hex string", err)
}
addr, err := address.FromBytes(priKey.PublicKey().Hash())
if err != nil {
return output.NewError(output.ConvertError, "failed to convert public key into address", err)
}
message := verifyMessage{
Address: addr.String(),
PublicKey: fmt.Sprintf("%x", priKey.PublicKey().Bytes()),
}
priKey.Zero()
fmt.Println(message.String())
return nil
}
func (m *verifyMessage) String() string {
if output.Format == "" {
return fmt.Sprintf("Address:\t%s\nPublic Key:\t%s", m.Address, m.PublicKey)
}
return output.FormatString(output.Result, m)
}