forked from aergoio/aergo
/
keygen.go
142 lines (126 loc) · 3.17 KB
/
keygen.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
package cmd
import (
b64 "encoding/base64"
"encoding/json"
"fmt"
"os"
"github.com/aergoio/aergo/account/key"
"github.com/aergoio/aergo/types"
"github.com/btcsuite/btcd/btcec"
"github.com/libp2p/go-libp2p-crypto"
"github.com/libp2p/go-libp2p-peer"
"github.com/spf13/cobra"
)
type keyJson struct {
Address string `json:"address"`
PubKey string `json:"pubkey"`
PrivKey string `json:"privkey"`
Id string `json:"id"`
}
var (
genPubkey bool
genID bool
genJSON bool
password string
)
func init() {
//keygenCmd.Flags().StringVar(&prefix, "prefix", "nodekey", "prefix name of key file")
//keygenCmd.Flags().BoolVar(&genPubkey, "genpubkey", true, "also generate public key")
keygenCmd.Flags().BoolVar(&genJSON, "json", false, "output combined json object instead of generating files")
keygenCmd.Flags().StringVar(&password, "password", "", "password for encrypted private key in json file")
rootCmd.AddCommand(keygenCmd)
}
var keygenCmd = &cobra.Command{
Use: "keygen [flags] <prefix>",
Short: "Generate private key",
Run: func(cmd *cobra.Command, args []string) {
var err error
if genJSON {
err = generateKeyJson()
} else {
if len(args) < 1 {
fmt.Println("Failed: no prefix")
return
}
prefix := args[0]
if prefix == "" {
fmt.Printf("Failed: invalid prefix %s\n", prefix)
return
}
err = generateKeyFiles(prefix)
}
if err != nil {
fmt.Printf("Failed: %s\n", err.Error())
return
}
},
}
func generateKeyFiles(prefix string) error {
priv, pub, _ := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
pkFile := prefix + ".key"
pubFile := prefix + ".pub"
idFile := prefix + ".id"
// Write private key file
pkf, err := os.Create(pkFile)
if err != nil {
return err
}
pkBytes, err := priv.Bytes()
if err != nil {
return err
}
pkf.Write(pkBytes)
pkf.Sync()
// Write public key file
pubf, err := os.Create(pubFile)
if err != nil {
return err
}
pubBytes, err := pub.Bytes()
if err != nil {
return err
}
pubf.Write(pubBytes)
pubf.Sync()
// Write id file
idf, err := os.Create(idFile)
if err != nil {
return err
}
pid, _ := peer.IDFromPublicKey(pub)
idBytes := []byte(peer.IDB58Encode(pid))
idf.Write(idBytes)
idf.Sync()
fmt.Printf("Wrote files %s.{key,pub,id}.\n", prefix)
return nil
}
func generateKeyJson() error {
priv, pub, _ := crypto.GenerateKeyPair(crypto.Secp256k1, 256)
pkBytes, err := priv.Bytes()
pubBytes, err := pub.Bytes()
pid, _ := peer.IDFromPublicKey(pub)
if err != nil {
return err
}
if password == "" {
fmt.Printf("Warning: private key in json file encrypted with empty password. Use command line parameter --password.\n")
}
privKeyExport, err := key.EncryptKey(pkBytes, password)
if err != nil {
return err
}
_, pubkey := btcec.PrivKeyFromBytes(btcec.S256(), pkBytes)
address := key.GenerateAddress(pubkey.ToECDSA())
addressEncoded := types.EncodeAddress(address)
jsonMarshalled, err := json.MarshalIndent(keyJson{
Address: addressEncoded,
PubKey: b64.StdEncoding.EncodeToString(pubBytes),
PrivKey: types.EncodePrivKey(privKeyExport),
Id: peer.IDB58Encode(pid),
}, "", " ")
if err != nil {
return err
}
fmt.Printf(string(jsonMarshalled))
return nil
}