2022-01-19 10:56:10 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2022-03-18 11:12:58 +00:00
|
|
|
"io"
|
2022-01-19 10:56:10 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/cli/input"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
2022-03-28 10:23:45 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/key"
|
2022-01-19 10:56:10 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/term"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Test_getKey(t *testing.T) {
|
|
|
|
dir := t.TempDir()
|
|
|
|
|
2022-02-02 13:28:08 +00:00
|
|
|
wallPath := filepath.Join(dir, "wallet.json")
|
2022-01-19 10:56:10 +00:00
|
|
|
w, err := wallet.NewWallet(wallPath)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
acc1, err := wallet.NewAccount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, acc1.Encrypt("pass", keys.NEP2ScryptParams()))
|
|
|
|
w.AddAccount(acc1)
|
|
|
|
|
|
|
|
acc2, err := wallet.NewAccount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, acc2.Encrypt("pass", keys.NEP2ScryptParams()))
|
|
|
|
acc2.Default = true
|
|
|
|
w.AddAccount(acc2)
|
|
|
|
require.NoError(t, w.Save())
|
|
|
|
w.Close()
|
|
|
|
|
2022-02-02 13:28:08 +00:00
|
|
|
keyPath := filepath.Join(dir, "binary.key")
|
2022-01-19 10:56:10 +00:00
|
|
|
rawKey, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
2022-03-18 11:12:58 +00:00
|
|
|
require.NoError(t, os.WriteFile(keyPath, rawKey.Bytes(), os.ModePerm))
|
2022-01-19 10:56:10 +00:00
|
|
|
|
|
|
|
wifKey, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
nep2Key, err := keys.NewPrivateKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
nep2, err := keys.NEP2Encrypt(nep2Key, "pass", keys.NEP2ScryptParams())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
in := bytes.NewBuffer(nil)
|
|
|
|
input.Terminal = term.NewTerminal(input.ReadWriter{
|
|
|
|
Reader: in,
|
2022-03-18 11:12:58 +00:00
|
|
|
Writer: io.Discard,
|
2022-01-19 10:56:10 +00:00
|
|
|
}, "")
|
|
|
|
|
2022-03-28 10:23:45 +00:00
|
|
|
checkKeyError(t, filepath.Join(dir, "badfile"), key.ErrInvalidKey)
|
2022-01-19 10:56:10 +00:00
|
|
|
|
|
|
|
t.Run("wallet", func(t *testing.T) {
|
2022-03-28 10:23:45 +00:00
|
|
|
checkKeyError(t, wallPath, key.ErrInvalidPassword)
|
2022-01-19 10:56:10 +00:00
|
|
|
|
|
|
|
in.WriteString("invalid\r")
|
2022-03-28 10:23:45 +00:00
|
|
|
checkKeyError(t, wallPath, key.ErrInvalidPassword)
|
2022-01-19 10:56:10 +00:00
|
|
|
|
|
|
|
in.WriteString("pass\r")
|
|
|
|
checkKey(t, wallPath, acc2.PrivateKey()) // default account
|
|
|
|
|
|
|
|
viper.Set(address, acc1.Address)
|
|
|
|
in.WriteString("pass\r")
|
|
|
|
checkKey(t, wallPath, acc1.PrivateKey())
|
|
|
|
|
|
|
|
viper.Set(address, "not an address")
|
2022-03-28 10:23:45 +00:00
|
|
|
checkKeyError(t, wallPath, key.ErrInvalidAddress)
|
2022-01-19 10:56:10 +00:00
|
|
|
|
|
|
|
acc, err := wallet.NewAccount()
|
|
|
|
require.NoError(t, err)
|
|
|
|
viper.Set(address, acc.Address)
|
2022-03-28 10:23:45 +00:00
|
|
|
checkKeyError(t, wallPath, key.ErrInvalidAddress)
|
2022-01-19 10:56:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("WIF", func(t *testing.T) {
|
|
|
|
checkKey(t, wifKey.WIF(), wifKey)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NEP-2", func(t *testing.T) {
|
2022-03-28 10:23:45 +00:00
|
|
|
checkKeyError(t, nep2, key.ErrInvalidPassword)
|
2022-01-19 10:56:10 +00:00
|
|
|
|
|
|
|
in.WriteString("invalid\r")
|
2022-03-28 10:23:45 +00:00
|
|
|
checkKeyError(t, nep2, key.ErrInvalidPassword)
|
2022-01-19 10:56:10 +00:00
|
|
|
|
|
|
|
in.WriteString("pass\r")
|
|
|
|
checkKey(t, nep2, nep2Key)
|
2022-01-26 07:50:32 +00:00
|
|
|
|
|
|
|
t.Run("password from config", func(t *testing.T) {
|
2022-03-28 10:23:45 +00:00
|
|
|
viper.Set("password", "invalid")
|
2022-01-26 07:50:32 +00:00
|
|
|
in.WriteString("pass\r")
|
2022-03-28 10:23:45 +00:00
|
|
|
checkKeyError(t, nep2, key.ErrInvalidPassword)
|
2022-01-26 07:50:32 +00:00
|
|
|
|
2022-03-28 10:23:45 +00:00
|
|
|
viper.Set("password", "pass")
|
2022-01-26 07:50:32 +00:00
|
|
|
in.WriteString("invalid\r")
|
|
|
|
checkKey(t, nep2, nep2Key)
|
|
|
|
})
|
2022-01-19 10:56:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("raw key", func(t *testing.T) {
|
|
|
|
checkKey(t, keyPath, rawKey)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("generate", func(t *testing.T) {
|
|
|
|
viper.Set(generateKey, true)
|
|
|
|
actual, err := getKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotNil(t, actual)
|
|
|
|
for _, p := range []*keys.PrivateKey{nep2Key, rawKey, wifKey, acc1.PrivateKey(), acc2.PrivateKey()} {
|
|
|
|
require.NotEqual(t, p, actual, "expected new key to be generated")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkKeyError(t *testing.T, desc string, err error) {
|
|
|
|
viper.Set(walletPath, desc)
|
|
|
|
_, actualErr := getKey()
|
|
|
|
require.True(t, errors.Is(actualErr, err), "got: %v", actualErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkKey(t *testing.T, desc string, expected *keys.PrivateKey) {
|
|
|
|
viper.Set(walletPath, desc)
|
|
|
|
actual, err := getKey()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &expected.PrivateKey, actual)
|
|
|
|
}
|