[#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 {