forked from OKEAMAH/exchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.go
More file actions
202 lines (176 loc) · 6.1 KB
/
export.go
File metadata and controls
202 lines (176 loc) · 6.1 KB
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
package client
import (
"bufio"
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/ethereum/go-ethereum/common/hexutil"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/okex/exchain/app/crypto/ethkeystore"
"github.com/okex/exchain/app/crypto/hd"
"github.com/okex/exchain/libs/cosmos-sdk/client/flags"
"github.com/okex/exchain/libs/cosmos-sdk/client/input"
"github.com/okex/exchain/libs/cosmos-sdk/crypto/keys"
sdk "github.com/okex/exchain/libs/cosmos-sdk/types"
)
// UnsafeExportEthKeyCommand exports a key with the given name as a private key in hex format.
func UnsafeExportEthKeyCommand() *cobra.Command {
return &cobra.Command{
Use: "unsafe-export-eth-key [name]",
Short: "**UNSAFE** Export an Ethereum private key",
Long: `**UNSAFE** Export an Ethereum private key unencrypted to use in dev tooling`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
kb, err := keys.NewKeyring(
sdk.KeyringServiceName(),
viper.GetString(flags.FlagKeyringBackend),
viper.GetString(flags.FlagHome),
inBuf,
hd.EthSecp256k1Options()...,
)
if err != nil {
return err
}
decryptPassword := ""
conf := true
keyringBackend := viper.GetString(flags.FlagKeyringBackend)
switch keyringBackend {
case keys.BackendFile:
decryptPassword, err = input.GetPassword(
"**WARNING this is an unsafe way to export your unencrypted private key**\nEnter key password:",
inBuf)
case keys.BackendOS:
conf, err = input.GetConfirmation(
"**WARNING** this is an unsafe way to export your unencrypted private key, are you sure?",
inBuf)
}
if err != nil || !conf {
return err
}
// Exports private key from keybase using password
privKey, err := kb.ExportPrivateKeyObject(args[0], decryptPassword)
if err != nil {
return err
}
// Converts tendermint key to ethereum key
ethKey, err := ethkeystore.EncodeTmKeyToEthKey(privKey)
if err != nil {
return fmt.Errorf("invalid private key type, must be Ethereum key: %T", privKey)
}
// Formats key for output
privB := ethcrypto.FromECDSA(ethKey)
keyS := strings.ToLower(hexutil.Encode(privB)[2:])
fmt.Println(keyS)
return nil
},
}
}
// ExportEthCompCommand exports a key with the given name as a keystore file.
func ExportEthCompCommand() *cobra.Command {
return &cobra.Command{
Use: "export-eth-comp [name] [dir]",
Short: "Export an Ethereum private keystore directory (This command has been deprecated,please use 'exchaincli keys export-eth-keystore')",
Hidden: true,
Long: `Export an Ethereum private keystore file encrypted to use in eth client import.
The parameters of scrypt encryption algorithm is StandardScryptN and StandardScryptN`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
return fmt.Errorf("This command has been deprecated,please use 'exchaincli keys export-eth-keystore'")
},
}
}
// ExportEthKeyStoreCommand exports a key with the given name as a keystore file.
func ExportEthKeyStoreCommand() *cobra.Command {
return &cobra.Command{
Use: "export-eth-keystore [name] [dir]",
Short: "Export an Ethereum private keystore directory",
Long: `Export an Ethereum private keystore file encrypted to use in eth client import.
The parameters of scrypt encryption algorithm is StandardScryptN and StandardScryptN`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
accountName := args[0]
dir := args[1]
kb, err := keys.NewKeyring(
sdk.KeyringServiceName(),
viper.GetString(flags.FlagKeyringBackend),
viper.GetString(flags.FlagHome),
inBuf,
hd.EthSecp256k1Options()...,
)
if err != nil {
return err
}
decryptPassword := ""
conf := true
keyringBackend := viper.GetString(flags.FlagKeyringBackend)
switch keyringBackend {
case keys.BackendFile:
decryptPassword, err = input.GetPassword(
"Enter passphrase to decrypt your key:",
inBuf)
case keys.BackendOS:
conf, err = input.GetConfirmation(
"Decrypt your key by os passphrase. Are you sure?",
inBuf)
}
if err != nil || !conf {
return err
}
// Get keystore password
encryptPassword, err := input.GetPassword("Enter passphrase to encrypt the exported keystore file:", inBuf)
if err != nil {
return err
}
// exports private key from keybase using password
privKey, err := kb.ExportPrivateKeyObject(accountName, decryptPassword)
if err != nil {
return err
}
// Exports private key from keybase using password
fileName, err := ethkeystore.CreateKeystoreByTmKey(privKey, dir, encryptPassword)
if err != nil {
return err
}
fmt.Printf("The keystore has exported to: %s \n", fileName)
return nil
},
}
}
// ImportEthKeyStoreCommand import keystore file.
func ImportEthKeyStoreCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "import-eth-keystore [name] [file]",
Short: "Import an Ethereum private keystore",
Long: `Import an Ethereum private keystore file.`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
algo, err := cmd.Flags().GetString(flagKeyAlgo)
if err != nil {
return err
}
inBuf := bufio.NewReader(cmd.InOrStdin())
accountName := args[0]
file := args[1]
// Get keystore password
decryptPassword, err := input.GetPassword("Enter passphrase to decrypt keystore file:", inBuf)
if err != nil {
return err
}
privkeyArmor, err := ethkeystore.ImportKeyStoreFile(decryptPassword, decryptPassword, file, keys.SigningAlgo(algo))
if err != nil {
return err
}
buf := bufio.NewReader(cmd.InOrStdin())
kb, err := keys.NewKeyring(sdk.KeyringServiceName(), viper.GetString(flags.FlagKeyringBackend), viper.GetString(flags.FlagHome), buf)
if err != nil {
return err
}
return kb.ImportPrivKey(accountName, privkeyArmor, decryptPassword)
},
}
cmd.Flags().String(flagKeyAlgo, string(hd.EthSecp256k1), "Key signing algorithm to generate keys for")
return cmd
}