OSDN Git Service

772086a65511395fb31972d6004e620898763fc4
[bytom/bytom.git] / cmd / bytomcli / commands / account.go
1 package commands
2
3 import (
4         "os"
5         "strings"
6         "time"
7
8         "github.com/spf13/cobra"
9         jww "github.com/spf13/jwalterweatherman"
10
11         "github.com/bytom/crypto/ed25519/chainkd"
12 )
13
14 func init() {
15         createAccountCmd.PersistentFlags().IntVarP(&accountQuorum, "quorom", "q", 1, "quorum must be greater than 0 and less than or equal to the number of signers")
16         createAccountCmd.PersistentFlags().StringVarP(&accountToken, "access", "a", "", "access token")
17         createAccountCmd.PersistentFlags().StringVarP(&accountTags, "tags", "t", "", "tags")
18
19         updateAccountTagsCmd.PersistentFlags().StringVarP(&accountUpdateTags, "tags", "t", "", "tags to add, delete or update")
20
21         listAccountsCmd.PersistentFlags().StringVar(&accountID, "id", "", "ID of account")
22
23         listUnspentOutputsCmd.PersistentFlags().StringVar(&outputID, "id", "", "ID of unspent output")
24 }
25
26 var (
27         accountID         = ""
28         accountQuorum     = 1
29         accountToken      = ""
30         accountTags       = ""
31         accountUpdateTags = ""
32         outputID          = ""
33 )
34
35 var createAccountCmd = &cobra.Command{
36         Use:   "create-account <alias> <xpub(s)>",
37         Short: "Create an account",
38         Args:  cobra.MinimumNArgs(2),
39         Run: func(cmd *cobra.Command, args []string) {
40                 ins := accountIns{}
41
42                 for _, x := range args[1:] {
43                         xpub := chainkd.XPub{}
44                         if err := xpub.UnmarshalText([]byte(x)); err != nil {
45                                 jww.ERROR.Println(err)
46                                 os.Exit(ErrLocalExe)
47                         }
48                         ins.RootXPubs = append(ins.RootXPubs, xpub)
49                 }
50
51                 ins.Quorum = accountQuorum
52                 ins.Alias = args[0]
53                 if len(accountTags) != 0 {
54                         tags := strings.Split(accountTags, ":")
55                         if len(tags) != 2 {
56                                 jww.ERROR.Println("Invalid tags")
57                                 os.Exit(ErrLocalExe)
58                         }
59                         ins.Tags = map[string]interface{}{tags[0]: tags[1]}
60                 }
61
62                 ins.AccessToken = accountToken
63
64                 data, exitCode := clientCall("/create-account", &ins)
65                 if exitCode != Success {
66                         os.Exit(exitCode)
67                 }
68
69                 printJSON(data)
70         },
71 }
72
73 var listAccountsCmd = &cobra.Command{
74         Use:   "list-accounts",
75         Short: "List the existing accounts",
76         Args:  cobra.NoArgs,
77         Run: func(cmd *cobra.Command, args []string) {
78                 filter := struct {
79                         ID string `json:"id"`
80                 }{ID: accountID}
81
82                 data, exitCode := clientCall("/list-accounts", &filter)
83                 if exitCode != Success {
84                         os.Exit(exitCode)
85                 }
86
87                 printJSONList(data)
88         },
89 }
90
91 var deleteAccountCmd = &cobra.Command{
92         Use:   "delete-account <accountID|alias>",
93         Short: "Delete the existing account",
94         Args:  cobra.ExactArgs(1),
95         Run: func(cmd *cobra.Command, args []string) {
96                 accountInfo := &struct {
97                         AccountInfo string `json:"account_info"`
98                 }{AccountInfo: args[0]}
99
100                 if _, exitCode := clientCall("/delete-account", accountInfo); exitCode != Success {
101                         os.Exit(exitCode)
102                 }
103
104                 jww.FEEDBACK.Println("Successfully delete account")
105         },
106 }
107
108 var updateAccountTagsCmd = &cobra.Command{
109         Use:   "update-account-tags <accountID|alias>",
110         Short: "Update the account tags",
111         Args:  cobra.ExactArgs(1),
112         PreRun: func(cmd *cobra.Command, args []string) {
113                 cmd.MarkFlagRequired("tags")
114         },
115         Run: func(cmd *cobra.Command, args []string) {
116                 var updateTag = struct {
117                         AccountInfo string                 `json:"account_info"`
118                         Tags        map[string]interface{} `json:"tags"`
119                 }{}
120
121                 if len(accountUpdateTags) != 0 {
122                         tags := strings.Split(accountUpdateTags, ":")
123                         if len(tags) != 2 {
124                                 jww.ERROR.Println("Invalid tags")
125                                 os.Exit(ErrLocalExe)
126                         }
127                         updateTag.Tags = map[string]interface{}{tags[0]: tags[1]}
128                 }
129
130                 updateTag.AccountInfo = args[0]
131
132                 if _, exitCode := clientCall("/update-account-tags", &updateTag); exitCode != Success {
133                         os.Exit(exitCode)
134                 }
135
136                 jww.FEEDBACK.Println("Successfully update account tags")
137         },
138 }
139
140 var createAccountReceiverCmd = &cobra.Command{
141         Use:   "create-account-receiver <accountID | alias>",
142         Short: "Create an account receiver control program",
143         Args:  cobra.ExactArgs(1),
144         Run: func(cmd *cobra.Command, args []string) {
145                 var ins = struct {
146                         AccountInfo string    `json:"account_info"`
147                         ExpiresAt   time.Time `json:"expires_at,omitempty"`
148                 }{AccountInfo: args[0]}
149
150                 data, exitCode := clientCall("/create-account-receiver", &ins)
151                 if exitCode != Success {
152                         os.Exit(exitCode)
153                 }
154
155                 printJSON(data)
156         },
157 }
158
159 var createAccountAddressCmd = &cobra.Command{
160         Use:   "create-account-address <accountID | alias>",
161         Short: "Create an account address",
162         Args:  cobra.ExactArgs(1),
163         Run: func(cmd *cobra.Command, args []string) {
164                 var ins = struct {
165                         AccountInfo string    `json:"account_info"`
166                         ExpiresAt   time.Time `json:"expires_at,omitempty"`
167                 }{AccountInfo: args[0]}
168
169                 data, exitCode := clientCall("/create-account-address", &ins)
170                 if exitCode != Success {
171                         os.Exit(exitCode)
172                 }
173
174                 printJSON(data)
175         },
176 }
177
178 var listBalancesCmd = &cobra.Command{
179         Use:   "list-balances",
180         Short: "List the accounts balances",
181         Args:  cobra.NoArgs,
182         Run: func(cmd *cobra.Command, args []string) {
183                 data, exitCode := clientCall("/list-balances")
184                 if exitCode != Success {
185                         os.Exit(exitCode)
186                 }
187
188                 printJSONList(data)
189         },
190 }
191
192 var listUnspentOutputsCmd = &cobra.Command{
193         Use:   "list-unspent-outputs",
194         Short: "List the accounts unspent outputs",
195         Args:  cobra.NoArgs,
196         Run: func(cmd *cobra.Command, args []string) {
197                 filter := struct {
198                         ID string `json:"id"`
199                 }{ID: outputID}
200
201                 data, exitCode := clientCall("/list-unspent-outputs", &filter)
202                 if exitCode != Success {
203                         os.Exit(exitCode)
204                 }
205
206                 printJSONList(data)
207         },
208 }