Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove WIF and NEP2 support in --wallet argument #1789

Merged
merged 1 commit into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Changelog for NeoFS Node
### Fixed

### Removed

- Remove WIF and NEP2 support in `neofs-cli`'s --wallet flag (#1128)
### Updated

### Updating from v0.32.0
Expand Down
2 changes: 1 addition & 1 deletion cmd/neofs-cli/internal/commonflags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
WalletPath = "wallet"
WalletPathShorthand = "w"
WalletPathDefault = ""
WalletPathUsage = "WIF (NEP-2) string or path to the wallet or binary key"
WalletPathUsage = "path to the wallet or binary key"

Account = "address"
AccountShorthand = ""
Expand Down
26 changes: 7 additions & 19 deletions cmd/neofs-cli/internal/key/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func Test_getOrGenerate(t *testing.T) {
w, err := wallet.NewWallet(wallPath)
require.NoError(t, err)

badWallPath := filepath.Join(dir, "bad_wallet.json")
require.NoError(t, os.WriteFile(badWallPath, []byte("bad content"), os.ModePerm))

acc1, err := wallet.NewAccount()
require.NoError(t, err)
require.NoError(t, acc1.Encrypt("pass", keys.NEP2ScryptParams()))
Expand Down Expand Up @@ -55,7 +58,8 @@ func Test_getOrGenerate(t *testing.T) {
Writer: io.Discard,
}, "")

checkKeyError(t, filepath.Join(dir, "badfile"), ErrInvalidKey)
checkKeyError(t, filepath.Join(dir, "badfile"), ErrFs)
checkKeyError(t, badWallPath, ErrInvalidKey)

t.Run("wallet", func(t *testing.T) {
checkKeyError(t, wallPath, ErrInvalidPassword)
Expand All @@ -80,27 +84,11 @@ func Test_getOrGenerate(t *testing.T) {
})

t.Run("WIF", func(t *testing.T) {
checkKey(t, wifKey.WIF(), wifKey)
checkKeyError(t, wifKey.WIF(), ErrFs)
})

t.Run("NEP-2", func(t *testing.T) {
checkKeyError(t, nep2, ErrInvalidPassword)

in.WriteString("invalid\r")
checkKeyError(t, nep2, ErrInvalidPassword)

in.WriteString("pass\r")
checkKey(t, nep2, nep2Key)

t.Run("password from config", func(t *testing.T) {
viper.Set("password", "invalid")
in.WriteString("pass\r")
checkKeyError(t, nep2, ErrInvalidPassword)

viper.Set("password", "pass")
in.WriteString("invalid\r")
checkKey(t, nep2, nep2Key)
})
checkKeyError(t, nep2, ErrFs)
})

t.Run("raw key", func(t *testing.T) {
Expand Down
26 changes: 0 additions & 26 deletions cmd/neofs-cli/internal/key/nep2.go

This file was deleted.

49 changes: 12 additions & 37 deletions cmd/neofs-cli/internal/key/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ import (

var errCantGenerateKey = errors.New("can't generate new private key")

// Get returns private key from the following sources:
// 1. WIF
// 2. Raw binary key
acid-ant marked this conversation as resolved.
Show resolved Hide resolved
// 3. Wallet file
// 4. NEP-2 encrypted WIF.
// Get returns private key from wallet or binary file.
// 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.
// This function assumes that all flags were bind to viper in a `PersistentPreRun`.
func Get(cmd *cobra.Command) *ecdsa.PrivateKey {
pk, err := get()
Expand All @@ -32,26 +27,20 @@ func Get(cmd *cobra.Command) *ecdsa.PrivateKey {

func get() (*ecdsa.PrivateKey, error) {
keyDesc := viper.GetString(commonflags.WalletPath)
priv, err := keys.NewPrivateKeyFromWIF(keyDesc)
if err == nil {
return &priv.PrivateKey, nil
}

p, err := getKeyFromFile(keyDesc)
if err == nil {
return p, nil
}

w, err := wallet.NewWalletFromFile(keyDesc)
if err == nil {
return FromWallet(w, viper.GetString(commonflags.Account))
data, err := os.ReadFile(keyDesc)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrFs, err)
}

if len(keyDesc) == nep2Base58Length {
return FromNEP2(keyDesc)
priv, err := keys.NewPrivateKeyFromBytes(data)
if err != nil {
w, err := wallet.NewWalletFromFile(keyDesc)
if err == nil {
return FromWallet(w, viper.GetString(commonflags.Account))
}
return nil, fmt.Errorf("%w: %v", ErrInvalidKey, err)
}

return nil, ErrInvalidKey
return &priv.PrivateKey, nil
}

// GetOrGenerate is similar to get but generates a new key if commonflags.GenerateKey is set.
Expand All @@ -71,17 +60,3 @@ func getOrGenerate() (*ecdsa.PrivateKey, error) {
}
return get()
}

func getKeyFromFile(keyPath string) (*ecdsa.PrivateKey, error) {
data, err := os.ReadFile(keyPath)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInvalidKey, err)
}

priv, err := keys.NewPrivateKeyFromBytes(data)
if err != nil {
return nil, fmt.Errorf("%w: %v", ErrInvalidKey, err)
}

return &priv.PrivateKey, nil
}
3 changes: 2 additions & 1 deletion cmd/neofs-cli/internal/key/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import (

// Key-related errors.
var (
ErrInvalidKey = errors.New("provided key is incorrect")
ErrFs = errors.New("unable to read file from given path")
ErrInvalidKey = errors.New("provided key is incorrect, only wallet or binary key supported")
ErrInvalidAddress = errors.New("--address option must be specified and valid")
ErrInvalidPassword = errors.New("invalid password for the encrypted key")
)
Expand Down