Skip to content

Commit 673b2e7

Browse files
committed
Moved WriteKeys to utils
1 parent 26ef27f commit 673b2e7

3 files changed

Lines changed: 46 additions & 30 deletions

File tree

cmd/keys.go

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,19 @@ import (
44
"bufio"
55
"fmt"
66
"path/filepath"
7-
"text/tabwriter"
87

98
"github.com/cosmos/cosmos-sdk/client/flags"
109
"github.com/cosmos/cosmos-sdk/client/input"
11-
"github.com/cosmos/cosmos-sdk/crypto/hd"
10+
cryptohd "github.com/cosmos/cosmos-sdk/crypto/hd"
1211
"github.com/cosmos/cosmos-sdk/crypto/keyring"
1312
sdk "github.com/cosmos/cosmos-sdk/types"
1413
"github.com/cosmos/go-bip39"
1514
"github.com/pkg/errors"
16-
hubtypes "github.com/sentinel-official/hub/types"
1715
"github.com/spf13/cobra"
1816
"github.com/spf13/viper"
1917

2018
"github.com/sentinel-official/dvpn-node/types"
19+
"github.com/sentinel-official/dvpn-node/utils"
2120
)
2221

2322
func KeysCmd() *cobra.Command {
@@ -121,27 +120,20 @@ func keysAdd() *cobra.Command {
121120
}
122121

123122
var (
124-
hdPath = hd.CreateHDPath(sdk.GetConfig().GetCoinType(), account, index)
125-
algorithms, _ = kr.SupportedAlgorithms()
123+
coinType = sdk.GetConfig().GetCoinType()
124+
path = cryptohd.CreateHDPath(coinType, account, index)
126125
)
127126

128-
algorithm, err := keyring.NewSigningAlgoFromString(string(hd.Secp256k1Type), algorithms)
127+
key, err := kr.NewAccount(name, mnemonic, "", path.String(), cryptohd.Secp256k1)
129128
if err != nil {
130129
return err
131130
}
132131

133-
key, err := kr.NewAccount(name, mnemonic, "", hdPath.String(), algorithm)
134-
if err != nil {
135-
return err
136-
}
137-
138-
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "operator: %s\n", key.GetAddress())
139-
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "address: %s\n", hubtypes.NodeAddress(key.GetAddress()))
140-
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "\n")
141132
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "**Important** write this mnemonic phrase in a safe place\n")
142133
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "%s\n", mnemonic)
134+
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "\n")
143135

144-
return nil
136+
return utils.WriteKeys(cmd.OutOrStdout(), key)
145137
},
146138
}
147139

@@ -202,10 +194,7 @@ func keysShow() *cobra.Command {
202194
return err
203195
}
204196

205-
fmt.Printf("operator: %s\n", key.GetAddress())
206-
fmt.Printf("address: %s\n", hubtypes.NodeAddress(key.GetAddress()))
207-
208-
return nil
197+
return utils.WriteKeys(cmd.OutOrStdout(), key)
209198
},
210199
}
211200

@@ -257,16 +246,7 @@ func keysList() *cobra.Command {
257246
return err
258247
}
259248

260-
w := tabwriter.NewWriter(cmd.OutOrStdout(), 1, 1, 1, ' ', 0)
261-
for _, key := range keys {
262-
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\n",
263-
key.GetName(),
264-
key.GetAddress(),
265-
hubtypes.NodeAddress(key.GetAddress().Bytes()),
266-
)
267-
}
268-
269-
return w.Flush()
249+
return utils.WriteKeys(cmd.OutOrStdout(), keys...)
270250
},
271251
}
272252

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ require (
88
github.com/cosmos/go-bip39 v1.0.0
99
github.com/gin-contrib/cors v1.4.0
1010
github.com/gin-gonic/gin v1.9.0
11-
github.com/go-kit/kit v0.12.0
1211
github.com/pkg/errors v0.9.1
1312
github.com/rs/zerolog v1.29.0
1413
github.com/sentinel-official/hub v0.10.1

utils/tabwriter.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"text/tabwriter"
7+
8+
"github.com/cosmos/cosmos-sdk/crypto/keyring"
9+
hubtypes "github.com/sentinel-official/hub/types"
10+
)
11+
12+
func WriteKeys(w io.Writer, keys ...keyring.Info) error {
13+
tw := tabwriter.NewWriter(w, 1, 1, 1, ' ', 0)
14+
if _, err := fmt.Fprintf(
15+
tw, "%s\t%s\t%s\n",
16+
"Name", "Address", "Operator",
17+
); err != nil {
18+
return err
19+
}
20+
21+
for i := 0; i < len(keys); i++ {
22+
var (
23+
name = keys[i].GetName()
24+
address = hubtypes.NodeAddress(keys[i].GetAddress().Bytes()).String()
25+
operator = keys[i].GetAddress().String()
26+
)
27+
28+
if _, err := fmt.Fprintf(
29+
tw, "%s\t%s\t%s\n",
30+
name, address, operator,
31+
); err != nil {
32+
return err
33+
}
34+
}
35+
36+
return tw.Flush()
37+
}

0 commit comments

Comments
 (0)