forked from fbsobreira/gotron-sdk
-
Notifications
You must be signed in to change notification settings - Fork 2
/
keys.go
248 lines (226 loc) · 6.58 KB
/
keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package cmd
import (
"bufio"
"fmt"
"os"
"github.com/fatih/color"
"github.com/syr2bill/gotron-sdk/pkg/account"
c "github.com/syr2bill/gotron-sdk/pkg/common"
"github.com/spf13/cobra"
"github.com/syr2bill/gotron-sdk/pkg/ledger"
"github.com/syr2bill/gotron-sdk/pkg/mnemonic"
"github.com/syr2bill/gotron-sdk/pkg/store"
"github.com/tyler-smith/go-bip39"
)
const (
seedPhraseWarning = "**Important** write this seed phrase in a safe place, " +
"it is the only way to recover your account if you ever forget your password\n\n"
)
var (
quietImport bool
recoverFromMnemonic bool
passphrase string
blsFilePath string
blsShardID uint32
blsCount uint32
ppPrompt = fmt.Sprintf(
"prompt for passphrase, otherwise use default passphrase: \"`%s`\"", c.DefaultPassphrase,
)
)
func keysSub() []*cobra.Command {
cmdList := &cobra.Command{
Use: "list",
Short: "List all the local accounts",
RunE: func(cmd *cobra.Command, args []string) error {
if useLedgerWallet {
ledger.ProcessAddressCommand()
return nil
}
store.DescribeLocalAccounts()
return nil
},
}
cmdLocation := &cobra.Command{
Use: "location",
Short: "Show where `tronctl` keeps accounts & their keys",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println(store.DefaultLocation())
return nil
},
}
cmdAdd := &cobra.Command{
Use: "add <ACCOUNT_NAME>",
Short: "Create a new keystore key",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if store.DoesNamedAccountExist(args[0]) {
return fmt.Errorf("account %s already exists", args[0])
}
passphrase, err := getPassphraseWithConfirm()
if err != nil {
return err
}
acc := account.Creation{
Name: args[0],
Passphrase: passphrase,
}
if err := account.CreateNewLocalAccount(&acc); err != nil {
return err
}
if !recoverFromMnemonic {
color.Red(seedPhraseWarning)
fmt.Println(acc.Mnemonic)
}
addr, _ := store.AddressFromAccountName(acc.Name)
fmt.Printf("Tron Address: %s\n", addr)
return nil
},
}
cmdRemove := &cobra.Command{
Use: "remove <ACCOUNT_NAME>",
Short: "Remove a key from the keystore",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if err := account.RemoveAccount(args[0]); err != nil {
return err
}
return nil
},
}
cmdMnemonic := &cobra.Command{
Use: "mnemonic",
Short: "Compute the bip39 mnemonic for some input entropy",
RunE: func(cmd *cobra.Command, args []string) error {
fmt.Println(mnemonic.Generate())
return nil
},
}
cmdRecoverMnemonic := &cobra.Command{
Use: "recover-from-mnemonic [ACCOUNT_NAME]",
Short: "Recover account from mnemonic",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if store.DoesNamedAccountExist(args[0]) {
return fmt.Errorf("account %s already exists", args[0])
}
passphrase, err := getPassphraseWithConfirm()
if err != nil {
return err
}
acc := account.Creation{
Name: args[0],
Passphrase: passphrase,
}
fmt.Println("Enter mnemonic to recover keys from")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
m := scanner.Text()
if !bip39.IsMnemonicValid(m) {
return fmt.Errorf("invalid mnemonic given")
}
fmt.Println("Enter mnemonic password [optional]")
scanner.Scan()
p := scanner.Text()
acc.Mnemonic = m
acc.MnemonicPassphrase = p
if err := account.CreateNewLocalAccount(&acc); err != nil {
return err
}
fmt.Println("Successfully recovered account from mnemonic!")
addr, _ := store.AddressFromAccountName(acc.Name)
fmt.Printf("Tron Address: %s\n", addr)
return nil
},
}
cmdImportKS := &cobra.Command{
Use: "import-ks <KEYSTORE_FILE_PATH> [ACCOUNT_NAME]",
Args: cobra.RangeArgs(1, 2),
Short: "Import an existing keystore key",
RunE: func(cmd *cobra.Command, args []string) error {
userName := ""
if len(args) == 2 {
userName = args[1]
}
passphrase, err := getPassphrase()
if err != nil {
return err
}
name, err := account.ImportKeyStore(args[0], userName, passphrase)
if !quietImport && err == nil {
fmt.Printf("Imported keystore given account alias of `%s`\n", name)
addr, _ := store.AddressFromAccountName(name)
fmt.Printf("Tron Address: %s\n", addr)
}
return err
},
}
cmdImportKS.Flags().BoolVar(&quietImport, "quiet", false, "do not print out imported account name")
cmdImportPK := &cobra.Command{
Use: "import-private-key <secp256k1_PRIVATE_KEY> [ACCOUNT_NAME]",
Short: "Import an existing keystore key (only accept secp256k1 private keys)",
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
userName := ""
if len(args) == 2 {
userName = args[1]
}
passphrase, err := getPassphrase()
if err != nil {
return err
}
name, err := account.ImportFromPrivateKey(args[0], userName, passphrase)
if !quietImport && err == nil {
fmt.Printf("Imported keystore given account alias of `%s`\n", name)
addr, _ := store.AddressFromAccountName(name)
fmt.Printf("Tron Address: %s\n", addr)
}
return err
},
}
cmdImportPK.Flags().BoolVar(&quietImport, "quiet", false, "do not print out imported account name")
cmdExportPK := &cobra.Command{
Use: "export-private-key <ACCOUNT_ADDRESS>",
Short: "Export the secp256k1 private key",
Args: cobra.ExactArgs(1),
PreRunE: validateAddress,
RunE: func(cmd *cobra.Command, args []string) error {
passphrase, err := getPassphrase()
if err != nil {
return err
}
return account.ExportPrivateKey(addr.address, passphrase)
},
}
cmdExportKS := &cobra.Command{
Use: "export-ks <ACCOUNT_ADDRESS> <OUTPUT_DIRECTORY>",
Short: "Export the keystore file contents",
Args: cobra.ExactArgs(2),
PreRunE: validateAddress,
RunE: func(cmd *cobra.Command, args []string) error {
passphrase, err := getPassphrase()
if err != nil {
return err
}
file, e := account.ExportKeystore(addr.address, args[1], passphrase)
if file != "" {
fmt.Println("Exported keystore to", file)
}
return e
},
}
return []*cobra.Command{cmdList, cmdLocation, cmdAdd, cmdRemove, cmdMnemonic, cmdRecoverMnemonic, cmdImportKS, cmdImportPK,
cmdExportKS, cmdExportPK}
}
func init() {
cmdKeys := &cobra.Command{
Use: "keys",
Short: "Add or view local private keys",
Long: "Manage your local keys",
RunE: func(cmd *cobra.Command, args []string) error {
cmd.Help()
return nil
},
}
cmdKeys.AddCommand(keysSub()...)
RootCmd.AddCommand(cmdKeys)
}