forked from iotexproject/iotex-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
53 lines (45 loc) · 1.52 KB
/
generate.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
// Copyright (c) 2018 IoTeX
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.
package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"go.uber.org/zap"
"github.com/iotexproject/iotex-core/pkg/keypair"
"github.com/iotexproject/iotex-core/pkg/log"
)
// generateCmd represents the generate command
var generateCmd = &cobra.Command{
Use: "generate [# number]",
Short: "Generates n number of address key pairs.",
Long: `Generates n number of address key pairs.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(generate(args))
},
}
var _addrNum int
func generate(_ []string) string {
items := make([]string, _addrNum)
for i := 0; i < _addrNum; i++ {
private, err := keypair.GenerateKey()
if err != nil {
log.L().Fatal("failed to create key pair", zap.Error(err))
}
priKeyBytes := private.Bytes()
pubKeyBytes := private.PublicKey().Bytes()
items[i] = fmt.Sprintf(
"{\"PublicKey\": \"%x\", \"PrivateKey\": \"%x\"}",
pubKeyBytes,
priKeyBytes,
)
}
return "[" + strings.Join(items, ",") + "]"
}
func init() {
generateCmd.Flags().IntVarP(&_addrNum, "number", "n", 10, "number of addresses to be generated")
rootCmd.AddCommand(generateCmd)
}