@@ -18,17 +18,21 @@ package account
18
18
19
19
import (
20
20
"bufio"
21
+ "encoding/json"
21
22
"fmt"
22
- "github.com/Zilliqa/gozilliqa-sdk/util"
23
- "github.com/spf13/cobra"
24
23
"os"
25
24
"zli/core"
25
+
26
+ "github.com/Zilliqa/gozilliqa-sdk/util"
27
+ "github.com/spf13/cobra"
26
28
)
27
29
28
30
var number int64
31
+ var file string
29
32
30
33
func init () {
31
34
generateCmd .Flags ().Int64VarP (& number , "number" , "n" , 2 , "the number of generated keys" )
35
+ generateCmd .Flags ().StringVarP (& file , "file" , "f" , "generatedAccounts.json" , "name of the intended output file" )
32
36
AccountCmd .AddCommand (generateCmd )
33
37
}
34
38
@@ -37,33 +41,32 @@ var generateCmd = &cobra.Command{
37
41
Short : "Randomly generate some private keys" ,
38
42
Long : "Randomly generate some private keys" ,
39
43
Run : func (cmd * cobra.Command , args []string ) {
40
- if number % 2 != 0 {
41
- panic ("number should be even" )
42
- }
43
44
fmt .Println ("start to generate " , number , " accounts" )
44
- f , err := os .Create ("./testAccounts.txt" )
45
+ file , err := os .Create (file )
45
46
if err != nil {
46
47
panic (err )
47
48
}
48
- defer f .Close ()
49
+ defer file .Close ()
49
50
50
51
keys , err := core .GeneratePrivateKeys (number )
51
52
if err != nil {
52
53
panic (err )
53
54
}
54
55
55
- i := 0
56
- w := bufio .NewWriter (f )
57
-
58
- for i + 1 < len (keys ) {
59
- k1 := keys [i ]
60
- k2 := keys [i + 1 ]
61
- line := fmt .Sprintf ("%s %s" , util .EncodeHex (k1 [:]), util .EncodeHex (k2 [:]))
62
- _ , err := fmt .Fprintln (w , line )
56
+ accounts := []core.Account {}
57
+ for _ , key := range keys {
58
+ account , err := core .NewAccount (util .EncodeHex (key [:]))
63
59
if err != nil {
64
60
panic (err .Error ())
65
61
}
66
- i += 2
62
+ accounts = append (accounts , * account )
63
+ }
64
+
65
+ w := bufio .NewWriter (file )
66
+ encoder := json .NewEncoder (w )
67
+ encoder .SetIndent ("" , " " )
68
+ if err := encoder .Encode (& accounts ); err != nil {
69
+ panic (err .Error ())
67
70
}
68
71
69
72
err = w .Flush ()
0 commit comments