Skip to content

Commit

Permalink
Moved WriteKeys to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Mar 14, 2023
1 parent 26ef27f commit 673b2e7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
38 changes: 9 additions & 29 deletions cmd/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import (
"bufio"
"fmt"
"path/filepath"
"text/tabwriter"

"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/input"
"github.com/cosmos/cosmos-sdk/crypto/hd"
cryptohd "github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/go-bip39"
"github.com/pkg/errors"
hubtypes "github.com/sentinel-official/hub/types"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/sentinel-official/dvpn-node/types"
"github.com/sentinel-official/dvpn-node/utils"
)

func KeysCmd() *cobra.Command {
Expand Down Expand Up @@ -121,27 +120,20 @@ func keysAdd() *cobra.Command {
}

var (
hdPath = hd.CreateHDPath(sdk.GetConfig().GetCoinType(), account, index)
algorithms, _ = kr.SupportedAlgorithms()
coinType = sdk.GetConfig().GetCoinType()
path = cryptohd.CreateHDPath(coinType, account, index)
)

algorithm, err := keyring.NewSigningAlgoFromString(string(hd.Secp256k1Type), algorithms)
key, err := kr.NewAccount(name, mnemonic, "", path.String(), cryptohd.Secp256k1)
if err != nil {
return err
}

key, err := kr.NewAccount(name, mnemonic, "", hdPath.String(), algorithm)
if err != nil {
return err
}

_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "operator: %s\n", key.GetAddress())
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "address: %s\n", hubtypes.NodeAddress(key.GetAddress()))
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "\n")
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "**Important** write this mnemonic phrase in a safe place\n")
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "%s\n", mnemonic)
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "\n")

return nil
return utils.WriteKeys(cmd.OutOrStdout(), key)
},
}

Expand Down Expand Up @@ -202,10 +194,7 @@ func keysShow() *cobra.Command {
return err
}

fmt.Printf("operator: %s\n", key.GetAddress())
fmt.Printf("address: %s\n", hubtypes.NodeAddress(key.GetAddress()))

return nil
return utils.WriteKeys(cmd.OutOrStdout(), key)
},
}

Expand Down Expand Up @@ -257,16 +246,7 @@ func keysList() *cobra.Command {
return err
}

w := tabwriter.NewWriter(cmd.OutOrStdout(), 1, 1, 1, ' ', 0)
for _, key := range keys {
_, _ = fmt.Fprintf(w, "%s\t%s\t%s\n",
key.GetName(),
key.GetAddress(),
hubtypes.NodeAddress(key.GetAddress().Bytes()),
)
}

return w.Flush()
return utils.WriteKeys(cmd.OutOrStdout(), keys...)
},
}

Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ require (
github.com/cosmos/go-bip39 v1.0.0
github.com/gin-contrib/cors v1.4.0
github.com/gin-gonic/gin v1.9.0
github.com/go-kit/kit v0.12.0
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.29.0
github.com/sentinel-official/hub v0.10.1
Expand Down
37 changes: 37 additions & 0 deletions utils/tabwriter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package utils

import (
"fmt"
"io"
"text/tabwriter"

"github.com/cosmos/cosmos-sdk/crypto/keyring"
hubtypes "github.com/sentinel-official/hub/types"
)

func WriteKeys(w io.Writer, keys ...keyring.Info) error {
tw := tabwriter.NewWriter(w, 1, 1, 1, ' ', 0)
if _, err := fmt.Fprintf(
tw, "%s\t%s\t%s\n",
"Name", "Address", "Operator",
); err != nil {
return err
}

for i := 0; i < len(keys); i++ {
var (
name = keys[i].GetName()
address = hubtypes.NodeAddress(keys[i].GetAddress().Bytes()).String()
operator = keys[i].GetAddress().String()
)

if _, err := fmt.Fprintf(
tw, "%s\t%s\t%s\n",
name, address, operator,
); err != nil {
return err
}
}

return tw.Flush()
}

0 comments on commit 673b2e7

Please sign in to comment.