9 "github.com/spf13/cobra"
10 jww "github.com/spf13/jwalterweatherman"
12 "github.com/bytom/api"
13 "github.com/bytom/crypto/ed25519/chainkd"
14 "github.com/bytom/util"
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) {
23 Alias string `json:"alias"`
24 Password string `json:"password"`
25 }{Alias: args[0], Password: args[1]}
27 data, exitCode := util.ClientCall("/create-key", &key)
28 if exitCode != util.Success {
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)
49 XPub chainkd.XPub `json:"xpubs"`
50 }{XPub: *xpub, Password: args[1]}
52 if _, exitCode := util.ClientCall("/delete-key", &key); exitCode != util.Success {
55 jww.FEEDBACK.Println("Successfully delete key")
59 var listKeysCmd = &cobra.Command{
61 Short: "List the existing keys",
63 Run: func(cmd *cobra.Command, args []string) {
64 data, exitCode := util.ClientCall("/list-keys")
65 if exitCode != util.Success {
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)
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]}
90 if _, exitCode := util.ClientCall("/reset-key-password", &ins); exitCode != util.Success {
93 jww.FEEDBACK.Println("Successfully reset key password")
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) {
107 xpub := new(chainkd.XPub)
108 rawPub, err := hex.DecodeString(args[0])
110 jww.ERROR.Println("error: export-private-key args not vaild", err)
112 copy(xpub[:], rawPub)
115 key.Password = args[1]
117 data, exitCode := util.ClientCall("/export-private-key", &key)
118 if exitCode != util.Success {
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)
138 jww.ERROR.Println("params index wrong")
142 data, exitCode := util.ClientCall("/import-private-key", ¶ms)
143 if exitCode != util.Success {
150 var importKeyProgressCmd = &cobra.Command{
151 Use: "import-key-progress",
152 Short: "Get import private key progress info",
154 Run: func(cmd *cobra.Command, args []string) {
155 data, exitCode := util.ClientCall("/import-key-progress")
156 if exitCode != util.Success {
159 fmt.Println("data:", data)