-
Notifications
You must be signed in to change notification settings - Fork 320
/
accountsign.go
73 lines (64 loc) · 2.28 KB
/
accountsign.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
// Copyright (c) 2019 IoTeX Foundation
// 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/iotex-core/ioctl/config"
"github.com/iotexproject/iotex-core/ioctl/output"
"github.com/iotexproject/iotex-core/ioctl/util"
)
var signer string
// Multi-language support
var (
signCmdShorts = map[config.Language]string{
config.English: "Sign message with private key from wallet",
config.Chinese: "用钱包中的私钥对信息签名",
}
signCmdUses = map[config.Language]string{
config.English: "sign MESSAGE [-s SIGNER]",
config.Chinese: "sign 信息 [-s 签署人]",
}
flagSignerUsages = map[config.Language]string{
config.English: "choose a signing account",
config.Chinese: "选择一个签名账户",
}
)
// accountSignCmd represents the account sign command
var accountSignCmd = &cobra.Command{
Use: config.TranslateInLang(signCmdUses, config.UILanguage),
Short: config.TranslateInLang(signCmdShorts, config.UILanguage),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
err := accountSign(args[0])
return output.PrintError(err)
},
}
func init() {
accountSignCmd.Flags().StringVarP(&signer, "signer", "s", "", config.TranslateInLang(flagSignerUsages, config.UILanguage))
}
func accountSign(msg string) error {
addr := signer
if !util.AliasIsHdwalletKey(signer) {
var err error
addr, err = util.GetAddress(signer)
if err != nil {
return output.NewError(output.InputError, "failed to get signer addr", err)
}
}
fmt.Printf("Enter password #%s:\n", addr)
password, err := util.ReadSecretFromStdin()
if err != nil {
return output.NewError(output.InputError, "failed to get password", err)
}
signedMessage, err := Sign(addr, password, msg)
if err != nil {
return output.NewError(output.KeystoreError, "failed to sign message", err)
}
output.PrintResult(signedMessage)
return nil
}