OSDN Git Service

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