-
Notifications
You must be signed in to change notification settings - Fork 1
/
keyManager.go
170 lines (144 loc) · 4.4 KB
/
keyManager.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
package hsm
import (
"crypto/ecdsa"
crand "crypto/rand"
"fmt"
chain33Address "github.com/33cn/chain33/common/address"
"github.com/ethereum/go-ethereum/common/math"
"github.com/ethereum/go-ethereum/crypto"
chain33Common "github.com/33cn/chain33/common"
"github.com/btcsuite/btcd/btcec"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
)
func KeyManageCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "key",
Short: "generate secp256k1 private key, show public key and address",
}
cmd.AddCommand(
chain33Cmd(),
ethereumCmd(),
)
return cmd
}
func chain33Cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "chain33",
Short: "generate secp256k1 private key or show info for chain33",
}
cmd.AddCommand(
generareChain33KeyCmd(),
showChain33pubCmd(),
)
return cmd
}
func generareChain33KeyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "new",
Short: "create a private key for chain33",
Run: generareChain33Key,
}
return cmd
}
func generareChain33Key(cmd *cobra.Command, _ []string) {
privateKey, err := btcec.NewPrivateKey(btcec.S256())
if nil != err {
fmt.Println("Failed to generate private key for chain33" + err.Error())
return
}
fmt.Println(common.Bytes2Hex(privateKey.Serialize()))
}
func showChain33pubCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "show",
Short: "show public key(inluding uncompressed and compressed, chain33 address)",
Run: showChain33Key,
}
addShowKeyFlags(cmd)
return cmd
}
func showChain33Key(cmd *cobra.Command, _ []string) {
key, _ := cmd.Flags().GetString("key")
privateKeySlice, err := chain33Common.FromHex(key)
if nil != err {
fmt.Println("convert string error due to:" + err.Error())
return
}
if len(privateKeySlice) != 32 {
fmt.Println("invalid priv key length", len(privateKeySlice))
return
}
_, pubKey := btcec.PrivKeyFromBytes(btcec.S256(), privateKeySlice)
uncompressedKey := pubKey.SerializeUncompressed()
compressedKey := pubKey.SerializeCompressed()
fmt.Println("The uncompressed pub key = "+common.Bytes2Hex(uncompressedKey), "with length=", len(uncompressedKey))
fmt.Println("The compressed pub key = "+common.Bytes2Hex(compressedKey), "with length=", len(compressedKey))
fmt.Println("Chain33 address = " + chain33Address.PubKeyToAddr(compressedKey))
}
////////////////////////////////////////////////////////
func ethereumCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "ethereum",
Short: "generate secp256k1 private key or show info for ethereum",
}
cmd.AddCommand(
generareEthereumKeyCmd(),
showEtheremKeyCmd(),
)
return cmd
}
func showEtheremKeyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "show",
Short: "show public key(inluding uncompressed and compressed, ethereum address)",
Run: showEtheremKey,
}
addShowKeyFlags(cmd)
return cmd
}
func addShowKeyFlags(cmd *cobra.Command) {
cmd.Flags().StringP("key", "k", "", "private key")
_ = cmd.MarkFlagRequired("key")
}
func showEtheremKey(cmd *cobra.Command, _ []string) {
key, _ := cmd.Flags().GetString("key")
privateKeySlice, err := chain33Common.FromHex(key)
if nil != err {
fmt.Println("convert string error due to:" + err.Error())
return
}
if len(privateKeySlice) != 32 {
fmt.Println("invalid priv key length", len(privateKeySlice))
return
}
privateKey, err := crypto.ToECDSA(privateKeySlice)
if nil != err {
fmt.Println("Failed ToECDSA due to " + err.Error())
return
}
_, pubKey := btcec.PrivKeyFromBytes(crypto.S256(), privateKeySlice)
uncompressedKey := pubKey.SerializeUncompressed()
compressedKey := pubKey.SerializeCompressed()
address := crypto.PubkeyToAddress(privateKey.PublicKey)
fmt.Println("The uncompressed pub key = "+common.Bytes2Hex(uncompressedKey), "with length=", len(uncompressedKey))
fmt.Println("The compressed pub key = "+common.Bytes2Hex(compressedKey), "with length=", len(compressedKey))
fmt.Println("Address = " + address.String())
}
func generareEthereumKeyCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "new",
Short: "create a private key for ethereum",
Run: generareEthereumKey,
}
return cmd
}
func generareEthereumKey(cmd *cobra.Command, _ []string) {
privateKeyECDSA, err := ecdsa.GenerateKey(crypto.S256(), crand.Reader)
if err != nil {
fmt.Println("Failed to generate private key for ethereum due to:" + err.Error())
return
}
privateKeyBytes := math.PaddedBigBytes(privateKeyECDSA.D, 32)
fmt.Println(common.Bytes2Hex(privateKeyBytes))
}