-
Notifications
You must be signed in to change notification settings - Fork 4
/
keys.go
executable file
·188 lines (167 loc) · 5.01 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
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
package main
import (
"bufio"
"fmt"
"github.com/cosmos/go-bip39"
"github.com/spf13/cobra"
sdkerrors "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/hd"
odin "github.com/ODIN-PROTOCOL/odin-core/app"
)
const (
flagAccount = "account"
flagIndex = "index"
flagRecover = "recover"
)
const (
EntropySize = 256
)
// KeysCmd defines the list of commands to manage faucet keybase.
func KeysCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "keys",
Aliases: []string{"k"},
Short: "Manage key held by the oracle process",
}
cmd.AddCommand(faucet.keysAddCmd())
cmd.AddCommand(faucet.keysDeleteCmd())
cmd.AddCommand(faucet.keysListCmd())
cmd.AddCommand(faucet.keysShowCmd())
return cmd
}
// keysAddCmd adds new key to the keybase.
func (f *Faucet) keysAddCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "add [name]",
Aliases: []string{"a"},
Short: "Add a new key to the keychain",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
account, err := cmd.Flags().GetUint32(flagAccount)
if err != nil {
return sdkerrors.Wrap(err, "failed to parse the account flag")
}
index, err := cmd.Flags().GetUint32(flagIndex)
if err != nil {
return sdkerrors.Wrap(err, "failed to parse the index flag")
}
rec, err := cmd.Flags().GetBool(flagRecover)
if err != nil {
return sdkerrors.Wrap(err, "failed to parse recover flag")
}
var mnemonic string
if rec {
inBuf := bufio.NewReader(cmd.InOrStdin())
mnemonic, err = input.GetString("Enter your bip39 mnemonic", inBuf)
if err != nil {
return sdkerrors.Wrap(err, "failed to parse the mnemonic")
}
} else {
seed, err := bip39.NewEntropy(EntropySize)
if err != nil {
return sdkerrors.Wrap(err, "failed to create a new entropy")
}
mnemonic, err = bip39.NewMnemonic(seed)
if err != nil {
return sdkerrors.Wrap(err, "failed to create a new mnemonic")
}
fmt.Printf("Mnemonic: %s\n", mnemonic)
}
hdPath := hd.CreateHDPath(odin.Bip44CoinType, account, index)
record, err := f.keybase.NewAccount(args[0], mnemonic, "", hdPath.String(), hd.Secp256k1)
if err != nil {
return sdkerrors.Wrap(err, "failed to create a new keybase account")
}
address, info_error := record.GetAddress()
if info_error != nil {
return sdkerrors.Wrap(err, "failed to retrieve address from new keybase account")
}
fmt.Printf("Address: %s\n", address.String())
return nil
},
}
cmd.Flags().Bool(flagRecover, false, "Provide seed phrase to recover existing key instead of creating")
cmd.Flags().Uint32(flagAccount, 0, "Account number for HD derivation")
cmd.Flags().Uint32(flagIndex, 0, "Address index number for HD derivation")
return cmd
}
// keysDeleteCmd removes key form the keybase by the given key.
func (f *Faucet) keysDeleteCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "delete [name]",
Aliases: []string{"d"},
Short: "Delete a key from the keychain",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
_, err := f.keybase.Key(name)
if err != nil {
return sdkerrors.Wrap(err, "failed to get by the given key")
}
inBuf := bufio.NewReader(cmd.InOrStdin())
confirmInput, err := input.GetString("Key will be deleted. Continue?[y/N]", inBuf)
if err != nil {
return sdkerrors.Wrap(err, "failed to read the input")
}
if confirmInput != "y" {
fmt.Println("Cancel")
return nil
}
if err := f.keybase.Delete(name); err != nil {
return sdkerrors.Wrap(err, "failed to delete by the given key")
}
fmt.Printf("Deleted key: %s\n", name)
return nil
},
}
return cmd
}
// keysListCmd prints all values from the keybase.
func (f *Faucet) keysListCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"l"},
Short: "List all the keys in the keychain",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
keys, err := f.keybase.List()
if err != nil {
return sdkerrors.Wrap(err, "failed to retrieve the keys list")
}
for _, key := range keys {
addr, error := key.GetAddress()
if error != nil {
fmt.Printf("failed to retrieve address from key: %s", key.Name)
continue
}
fmt.Printf("%s => %s\n", key.Name, addr.String())
}
return nil
},
}
return cmd
}
// keysShowCmd prints value by the given key.
func (f *Faucet) keysShowCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "show [name]",
Aliases: []string{"s"},
Short: "Show address from name in the keychain",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
name := args[0]
key, err := f.keybase.Key(name)
if err != nil {
return sdkerrors.Wrap(err, "failed to get by the given key")
}
keyAddress, err := key.GetAddress()
if err != nil {
return sdkerrors.Wrap(err, "failed to get key address")
}
fmt.Println(keyAddress.String())
return nil
},
}
return cmd
}