OSDN Git Service

Merge pull request #41 from Bytom/dev
[bytom/vapor.git] / cmd / vaporcli / commands / key.go
1 package commands
2
3 import (
4         "os"
5
6         "github.com/spf13/cobra"
7         jww "github.com/spf13/jwalterweatherman"
8
9         "github.com/vapor/crypto/ed25519/chainkd"
10         "github.com/vapor/util"
11 )
12
13 var createKeyCmd = &cobra.Command{
14         Use:   "create-key <alias> <password>",
15         Short: "Create a key",
16         Args:  cobra.ExactArgs(2),
17         Run: func(cmd *cobra.Command, args []string) {
18                 var key = struct {
19                         Alias    string `json:"alias"`
20                         Password string `json:"password"`
21                 }{Alias: args[0], Password: args[1]}
22
23                 data, exitCode := util.ClientCall("/create-key", &key)
24                 if exitCode != util.Success {
25                         os.Exit(exitCode)
26                 }
27
28                 printJSON(data)
29         },
30 }
31
32 var deleteKeyCmd = &cobra.Command{
33         Use:   "delete-key <xpub> <password>",
34         Short: "Delete a key",
35         Args:  cobra.ExactArgs(2),
36         Run: func(cmd *cobra.Command, args []string) {
37                 xpub := new(chainkd.XPub)
38                 if err := xpub.UnmarshalText([]byte(args[0])); err != nil {
39                         jww.ERROR.Println("delete-key:", err)
40                         os.Exit(util.ErrLocalExe)
41                 }
42
43                 var key = struct {
44                         Password string
45                         XPub     chainkd.XPub `json:"xpub"`
46                 }{XPub: *xpub, Password: args[1]}
47
48                 if _, exitCode := util.ClientCall("/delete-key", &key); exitCode != util.Success {
49                         os.Exit(exitCode)
50                 }
51                 jww.FEEDBACK.Println("Successfully delete key")
52         },
53 }
54
55 var listKeysCmd = &cobra.Command{
56         Use:   "list-keys",
57         Short: "List the existing keys",
58         Args:  cobra.NoArgs,
59         Run: func(cmd *cobra.Command, args []string) {
60                 data, exitCode := util.ClientCall("/list-keys")
61                 if exitCode != util.Success {
62                         os.Exit(exitCode)
63                 }
64
65                 printJSONList(data)
66         },
67 }
68
69 var resetKeyPwdCmd = &cobra.Command{
70         Use:   "reset-key-password <xpub> <old-password> <new-password>",
71         Short: "Reset key password",
72         Args:  cobra.ExactArgs(3),
73         Run: func(cmd *cobra.Command, args []string) {
74                 xpub := new(chainkd.XPub)
75                 if err := xpub.UnmarshalText([]byte(args[0])); err != nil {
76                         jww.ERROR.Println("reset-key-password args not valid:", err)
77                         os.Exit(util.ErrLocalExe)
78                 }
79
80                 ins := struct {
81                         XPub        chainkd.XPub `json:"xpub"`
82                         OldPassword string       `json:"old_password"`
83                         NewPassword string       `json:"new_password"`
84                 }{XPub: *xpub, OldPassword: args[1], NewPassword: args[2]}
85
86                 data, exitCode := util.ClientCall("/reset-key-password", &ins)
87                 if exitCode != util.Success {
88                         os.Exit(exitCode)
89                 }
90
91                 printJSON(data)
92         },
93 }
94
95 var checkKeyPwdCmd = &cobra.Command{
96         Use:   "check-key-password <xpub> <password>",
97         Short: "check key password",
98         Args:  cobra.ExactArgs(2),
99         Run: func(cmd *cobra.Command, args []string) {
100                 xpub := new(chainkd.XPub)
101                 if err := xpub.UnmarshalText([]byte(args[0])); err != nil {
102                         jww.ERROR.Println("check-key-password args not valid:", err)
103                         os.Exit(util.ErrLocalExe)
104                 }
105
106                 ins := struct {
107                         XPub     chainkd.XPub `json:"xpub"`
108                         Password string       `json:"password"`
109                 }{XPub: *xpub, Password: args[1]}
110
111                 data, exitCode := util.ClientCall("/check-key-password", &ins)
112                 if exitCode != util.Success {
113                         os.Exit(exitCode)
114                 }
115
116                 printJSON(data)
117         },
118 }
119
120 var signMsgCmd = &cobra.Command{
121         Use:   "sign-message <address> <message> <password>",
122         Short: "sign message to generate signature",
123         Args:  cobra.ExactArgs(3),
124         Run: func(cmd *cobra.Command, args []string) {
125                 var req = struct {
126                         Address  string `json:"address"`
127                         Message  string `json:"message"`
128                         Password string `json:"password"`
129                 }{Address: args[0], Message: args[1], Password: args[2]}
130
131                 data, exitCode := util.ClientCall("/sign-message", &req)
132                 if exitCode != util.Success {
133                         os.Exit(exitCode)
134                 }
135                 printJSON(data)
136         },
137 }
138
139 var verifyMsgCmd = &cobra.Command{
140         Use:   "verify-message <address> <xpub> <message> <signature>",
141         Short: "verify signature for specified message",
142         Args:  cobra.ExactArgs(4),
143         Run: func(cmd *cobra.Command, args []string) {
144                 xpub := chainkd.XPub{}
145                 if err := xpub.UnmarshalText([]byte(args[1])); err != nil {
146                         jww.ERROR.Println(err)
147                         os.Exit(util.ErrLocalExe)
148                 }
149
150                 var req = struct {
151                         Address     string       `json:"address"`
152                         DerivedXPub chainkd.XPub `json:"derived_xpub"`
153                         Message     string       `json:"message"`
154                         Signature   string       `json:"signature"`
155                 }{Address: args[0], DerivedXPub: xpub, Message: args[2], Signature: args[3]}
156
157                 data, exitCode := util.ClientCall("/verify-message", &req)
158                 if exitCode != util.Success {
159                         os.Exit(exitCode)
160                 }
161                 printJSON(data)
162         },
163 }