OSDN Git Service

update master (#487)
[bytom/bytom.git] / cmd / bytomcli / commands / key.go
1 package commands
2
3 import (
4         "encoding/hex"
5         "fmt"
6         "os"
7         "strconv"
8
9         "github.com/spf13/cobra"
10         jww "github.com/spf13/jwalterweatherman"
11
12         "github.com/bytom/api"
13         "github.com/bytom/crypto/ed25519/chainkd"
14         "github.com/bytom/util"
15 )
16
17 var createKeyCmd = &cobra.Command{
18         Use:   "create-key <alias> <password>",
19         Short: "Create a key",
20         Args:  cobra.ExactArgs(2),
21         Run: func(cmd *cobra.Command, args []string) {
22                 var key = struct {
23                         Alias    string `json:"alias"`
24                         Password string `json:"password"`
25                 }{Alias: args[0], Password: args[1]}
26
27                 data, exitCode := util.ClientCall("/create-key", &key)
28                 if exitCode != util.Success {
29                         os.Exit(exitCode)
30                 }
31
32                 printJSON(data)
33         },
34 }
35
36 var deleteKeyCmd = &cobra.Command{
37         Use:   "delete-key <xpub> <password>",
38         Short: "Delete a key",
39         Args:  cobra.ExactArgs(2),
40         Run: func(cmd *cobra.Command, args []string) {
41                 xpub := new(chainkd.XPub)
42                 if err := xpub.UnmarshalText([]byte(args[0])); err != nil {
43                         jww.ERROR.Println("delete-key:", err)
44                         os.Exit(util.ErrLocalExe)
45                 }
46
47                 var key = struct {
48                         Password string
49                         XPub     chainkd.XPub `json:"xpubs"`
50                 }{XPub: *xpub, Password: args[1]}
51
52                 if _, exitCode := util.ClientCall("/delete-key", &key); exitCode != util.Success {
53                         os.Exit(exitCode)
54                 }
55                 jww.FEEDBACK.Println("Successfully delete key")
56         },
57 }
58
59 var listKeysCmd = &cobra.Command{
60         Use:   "list-keys",
61         Short: "List the existing keys",
62         Args:  cobra.NoArgs,
63         Run: func(cmd *cobra.Command, args []string) {
64                 data, exitCode := util.ClientCall("/list-keys")
65                 if exitCode != util.Success {
66                         os.Exit(exitCode)
67                 }
68
69                 printJSONList(data)
70         },
71 }
72
73 var resetKeyPwdCmd = &cobra.Command{
74         Use:   "reset-key-password <xpub> <old-password> <new-password>",
75         Short: "Delete a key",
76         Args:  cobra.ExactArgs(3),
77         Run: func(cmd *cobra.Command, args []string) {
78                 xpub := new(chainkd.XPub)
79                 if err := xpub.UnmarshalText([]byte(args[0])); err != nil {
80                         jww.ERROR.Println("reset-key-password args not vaild:", err)
81                         os.Exit(util.ErrLocalExe)
82                 }
83
84                 ins := struct {
85                         XPub        chainkd.XPub `json:"xpub"`
86                         OldPassword string       `json:"old_password"`
87                         NewPassword string       `json:"new_password"`
88                 }{XPub: *xpub, OldPassword: args[1], NewPassword: args[2]}
89
90                 if _, exitCode := util.ClientCall("/reset-key-password", &ins); exitCode != util.Success {
91                         os.Exit(exitCode)
92                 }
93                 jww.FEEDBACK.Println("Successfully reset key password")
94         },
95 }
96
97 var exportPrivateCmd = &cobra.Command{
98         Use:   "export-private-key <xpub> <password>",
99         Short: "Export the private key",
100         Args:  cobra.ExactArgs(2),
101         Run: func(cmd *cobra.Command, args []string) {
102                 type Key struct {
103                         Password string
104                         XPub     chainkd.XPub
105                 }
106                 var key Key
107                 xpub := new(chainkd.XPub)
108                 rawPub, err := hex.DecodeString(args[0])
109                 if err != nil {
110                         jww.ERROR.Println("error: export-private-key args not vaild", err)
111                 }
112                 copy(xpub[:], rawPub)
113
114                 key.XPub = *xpub
115                 key.Password = args[1]
116
117                 data, exitCode := util.ClientCall("/export-private-key", &key)
118                 if exitCode != util.Success {
119                         os.Exit(exitCode)
120                 }
121
122                 printJSON(data)
123         },
124 }
125
126 var importPrivateCmd = &cobra.Command{
127         Use:   "import-private-key <key-alias> <private key> <index> <password> <account-alias>",
128         Short: "Import the private key",
129         Args:  cobra.ExactArgs(5),
130         Run: func(cmd *cobra.Command, args []string) {
131                 var params api.KeyImportParams
132                 params.KeyAlias = args[0]
133                 params.XPrv = args[1]
134                 params.Password = args[3]
135                 params.AccountAlias = args[4]
136                 index, err := strconv.ParseUint(args[2], 10, 64)
137                 if err != nil {
138                         jww.ERROR.Println("params index wrong")
139                 }
140                 params.Index = index
141
142                 data, exitCode := util.ClientCall("/import-private-key", &params)
143                 if exitCode != util.Success {
144                         os.Exit(exitCode)
145                 }
146                 printJSON(data)
147         },
148 }
149
150 var importKeyProgressCmd = &cobra.Command{
151         Use:   "import-key-progress",
152         Short: "Get import private key progress info",
153         Args:  cobra.NoArgs,
154         Run: func(cmd *cobra.Command, args []string) {
155                 data, exitCode := util.ClientCall("/import-key-progress")
156                 if exitCode != util.Success {
157                         os.Exit(exitCode)
158                 }
159                 fmt.Println("data:", data)
160         },
161 }