[#1379] neofs-cli: Bind key-related arguments to viper

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-05-18 13:38:45 +03:00 committed by fyrchik
parent a198a4858c
commit cd46a7478e
3 changed files with 14 additions and 7 deletions

View file

@ -7,16 +7,20 @@ import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/wallet"
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
"github.com/spf13/viper"
)
// Get returns private key from the followind sources:
// Get returns private key from the following sources:
// 1. WIF
// 2. Raw binary key
// 3. Wallet file
// 4. NEP-2 encrypted WIF.
// Ideally we want to touch file-system on the last step.
// However, asking for NEP-2 password seems to be confusing if we provide a wallet.
func Get(keyDesc string, address string) (*ecdsa.PrivateKey, error) {
// This function assumes that all flags were bind to viper in a `PersistentPreRun`.
func Get() (*ecdsa.PrivateKey, error) {
keyDesc := viper.GetString(commonflags.WalletPath)
priv, err := keys.NewPrivateKeyFromWIF(keyDesc)
if err == nil {
return &priv.PrivateKey, nil
@ -29,7 +33,7 @@ func Get(keyDesc string, address string) (*ecdsa.PrivateKey, error) {
w, err := wallet.NewWalletFromFile(keyDesc)
if err == nil {
return FromWallet(w, address)
return FromWallet(w, viper.GetString(commonflags.Account))
}
if len(keyDesc) == nep2Base58Length {

View file

@ -151,7 +151,7 @@ func getKey() (*ecdsa.PrivateKey, error) {
}
func getKeyNoGenerate() (*ecdsa.PrivateKey, error) {
return key.Get(viper.GetString(commonflags.WalletPath), viper.GetString(commonflags.Account))
return key.Get()
}
type clientWithKey interface {

View file

@ -12,6 +12,7 @@ import (
"github.com/nspcc-dev/neofs-sdk-go/owner"
"github.com/nspcc-dev/neofs-sdk-go/session"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
@ -26,6 +27,10 @@ var createCmd = &cobra.Command{
Use: "create",
Short: "Create session token",
RunE: createSession,
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
_ = viper.BindPFlag(commonflags.WalletPath, cmd.Flags().Lookup(commonflags.WalletPath))
_ = viper.BindPFlag(commonflags.Account, cmd.Flags().Lookup(commonflags.Account))
},
}
func init() {
@ -43,9 +48,7 @@ func init() {
}
func createSession(cmd *cobra.Command, _ []string) error {
walletPath, _ := cmd.Flags().GetString(commonflags.WalletPath)
accPath, _ := cmd.Flags().GetString(commonflags.Account)
privKey, err := key.Get(walletPath, accPath)
privKey, err := key.Get()
if err != nil {
return err
}