-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
116 lines (99 loc) · 2.86 KB
/
keys.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
package client
import (
"bufio"
"encoding/hex"
"fmt"
"github.com/evoblockchain/evochain/libs/tendermint/p2p"
"io"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/client/flags"
clientkeys "github.com/evoblockchain/evochain/libs/cosmos-sdk/client/keys"
"github.com/evoblockchain/evochain/libs/cosmos-sdk/crypto/keys"
sdk "github.com/evoblockchain/evochain/libs/cosmos-sdk/types"
"github.com/evoblockchain/evochain/app/crypto/hd"
)
const (
flagDryRun = "dry-run"
)
// KeyCommands registers a sub-tree of commands to interact with
// local private key storage.
func KeyCommands() *cobra.Command {
cmd := &cobra.Command{
Use: "keys",
Short: "Add or view local private keys",
Long: `Keys allows you to manage your local keystore for tendermint.
These keys may be in any format supported by go-crypto and can be
used by light-clients, full nodes, or any other application that
needs to sign with a private key.`,
}
// support adding Ethereum supported keys
addCmd := clientkeys.AddKeyCommand()
// update the default signing algorithm value to "eth_secp256k1"
algoFlag := addCmd.Flag("algo")
algoFlag.DefValue = string(hd.EthSecp256k1)
err := algoFlag.Value.Set(string(hd.EthSecp256k1))
if err != nil {
panic(err)
}
addCmd.RunE = runAddCmd
cmd.AddCommand(
clientkeys.MnemonicKeyCommand(),
addCmd,
clientkeys.ExportKeyCommand(),
clientkeys.ImportKeyCommand(),
clientkeys.ListKeysCmd(),
clientkeys.ShowKeysCmd(),
flags.LineBreak,
clientkeys.DeleteKeyCommand(),
clientkeys.ParseKeyStringCommand(),
clientkeys.MigrateCommand(),
flags.LineBreak,
UnsafeExportEthKeyCommand(),
ExportEthCompCommand(),
extractNodeKey(),
)
return cmd
}
func runAddCmd(cmd *cobra.Command, args []string) error {
inBuf := bufio.NewReader(cmd.InOrStdin())
kb, err := getKeybase(viper.GetBool(flagDryRun), inBuf)
if err != nil {
return err
}
return clientkeys.RunAddCmd(cmd, args, kb, inBuf)
}
func getKeybase(transient bool, buf io.Reader) (keys.Keybase, error) {
if transient {
return keys.NewInMemory(
hd.EthSecp256k1Options()...,
), nil
}
return keys.NewKeyring(
sdk.KeyringServiceName(),
viper.GetString(flags.FlagKeyringBackend),
viper.GetString(flags.FlagHome),
buf,
hd.EthSecp256k1Options()...,
)
}
func extractNodeKey() *cobra.Command {
cmd := &cobra.Command{
Use: "extract-node-key [filename] ",
Short: "extract current node key or from specificed file",
RunE: func(cmd *cobra.Command, args []string) error {
var filename string
if len(args) >= 1 {
filename = args[0]
}
nodekey, err := p2p.LoadNodeKey(filename)
if err != nil {
return err
}
//fmt.Printf("base64: %s\n", base64.StdEncoding.EncodeToString(nodekey.PubKey().Bytes()))
fmt.Printf("hex: %s\n", hex.EncodeToString(nodekey.PubKey().Bytes()))
return nil
},
}
return cmd
}