-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
163 lines (139 loc) · 4.22 KB
/
key.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package commands
import (
"os"
"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
"github.com/bytom-gm/crypto/sm2/chainkd"
"github.com/bytom-gm/util"
)
var createKeyCmd = &cobra.Command{
Use: "create-key <alias> <password>",
Short: "Create a key",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
var key = struct {
Alias string `json:"alias"`
Password string `json:"password"`
}{Alias: args[0], Password: args[1]}
data, exitCode := util.ClientCall("/create-key", &key)
if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
},
}
var deleteKeyCmd = &cobra.Command{
Use: "delete-key <xpub> <password>",
Short: "Delete a key",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
xpub := new(chainkd.XPub)
if err := xpub.UnmarshalText([]byte(args[0])); err != nil {
jww.ERROR.Println("delete-key:", err)
os.Exit(util.ErrLocalExe)
}
var key = struct {
Password string
XPub chainkd.XPub `json:"xpub"`
}{XPub: *xpub, Password: args[1]}
if _, exitCode := util.ClientCall("/delete-key", &key); exitCode != util.Success {
os.Exit(exitCode)
}
jww.FEEDBACK.Println("Successfully delete key")
},
}
var listKeysCmd = &cobra.Command{
Use: "list-keys",
Short: "List the existing keys",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
data, exitCode := util.ClientCall("/list-keys")
if exitCode != util.Success {
os.Exit(exitCode)
}
printJSONList(data)
},
}
var resetKeyPwdCmd = &cobra.Command{
Use: "reset-key-password <xpub> <old-password> <new-password>",
Short: "Reset key password",
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
xpub := new(chainkd.XPub)
if err := xpub.UnmarshalText([]byte(args[0])); err != nil {
jww.ERROR.Println("reset-key-password args not valid:", err)
os.Exit(util.ErrLocalExe)
}
ins := struct {
XPub chainkd.XPub `json:"xpub"`
OldPassword string `json:"old_password"`
NewPassword string `json:"new_password"`
}{XPub: *xpub, OldPassword: args[1], NewPassword: args[2]}
data, exitCode := util.ClientCall("/reset-key-password", &ins)
if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
},
}
var checkKeyPwdCmd = &cobra.Command{
Use: "check-key-password <xpub> <password>",
Short: "check key password",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
xpub := new(chainkd.XPub)
if err := xpub.UnmarshalText([]byte(args[0])); err != nil {
jww.ERROR.Println("check-key-password args not valid:", err)
os.Exit(util.ErrLocalExe)
}
ins := struct {
XPub chainkd.XPub `json:"xpub"`
Password string `json:"password"`
}{XPub: *xpub, Password: args[1]}
data, exitCode := util.ClientCall("/check-key-password", &ins)
if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
},
}
var signMsgCmd = &cobra.Command{
Use: "sign-message <address> <message> <password>",
Short: "sign message to generate signature",
Args: cobra.ExactArgs(3),
Run: func(cmd *cobra.Command, args []string) {
var req = struct {
Address string `json:"address"`
Message string `json:"message"`
Password string `json:"password"`
}{Address: args[0], Message: args[1], Password: args[2]}
data, exitCode := util.ClientCall("/sign-message", &req)
if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
},
}
var verifyMsgCmd = &cobra.Command{
Use: "verify-message <address> <xpub> <message> <signature>",
Short: "verify signature for specified message",
Args: cobra.ExactArgs(4),
Run: func(cmd *cobra.Command, args []string) {
xpub := chainkd.XPub{}
if err := xpub.UnmarshalText([]byte(args[1])); err != nil {
jww.ERROR.Println(err)
os.Exit(util.ErrLocalExe)
}
var req = struct {
Address string `json:"address"`
DerivedXPub chainkd.XPub `json:"derived_xpub"`
Message string `json:"message"`
Signature string `json:"signature"`
}{Address: args[0], DerivedXPub: xpub, Message: args[2], Signature: args[3]}
data, exitCode := util.ClientCall("/verify-message", &req)
if exitCode != util.Success {
os.Exit(exitCode)
}
printJSON(data)
},
}