2022-05-24 08:22:23 +00:00
|
|
|
package key
|
2022-01-19 10:56:10 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-03-18 11:12:58 +00:00
|
|
|
"io"
|
2022-01-19 10:56:10 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-cli/internal/commonflags"
|
2022-01-19 10:56:10 +00:00
|
|
|
"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-12-27 09:36:30 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-01-19 10:56:10 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/term"
|
|
|
|
)
|
|
|
|
|
2022-12-27 09:36:30 +00:00
|
|
|
var testCmd = &cobra.Command{
|
|
|
|
Use: "test",
|
|
|
|
Short: "test",
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {},
|
|
|
|
}
|
|
|
|
|
2022-05-24 08:22:23 +00:00
|
|
|
func Test_getOrGenerate(t *testing.T) {
|
2024-08-21 14:08:44 +00:00
|
|
|
t.Cleanup(viper.Reset)
|
|
|
|
|
2022-01-19 10:56:10 +00:00
|
|
|
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)
|
|
|
|
|
2022-09-16 06:48:12 +00:00
|
|
|
badWallPath := filepath.Join(dir, "bad_wallet.json")
|
|
|
|
require.NoError(t, os.WriteFile(badWallPath, []byte("bad content"), os.ModePerm))
|
|
|
|
|
2022-01-19 10:56:10 +00:00
|
|
|
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())
|
|
|
|
|
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-09-16 06:48:12 +00:00
|
|
|
checkKeyError(t, filepath.Join(dir, "badfile"), ErrFs)
|
|
|
|
checkKeyError(t, badWallPath, ErrInvalidKey)
|
2022-01-19 10:56:10 +00:00
|
|
|
|
|
|
|
t.Run("wallet", func(t *testing.T) {
|
2022-05-24 08:22:23 +00:00
|
|
|
checkKeyError(t, wallPath, ErrInvalidPassword)
|
2022-01-19 10:56:10 +00:00
|
|
|
|
|
|
|
in.WriteString("invalid\r")
|
2022-05-24 08:22:23 +00:00
|
|
|
checkKeyError(t, wallPath, ErrInvalidPassword)
|
2022-01-19 10:56:10 +00:00
|
|
|
|
|
|
|
in.WriteString("pass\r")
|
|
|
|
checkKey(t, wallPath, acc2.PrivateKey()) // default account
|
|
|
|
|
2022-05-18 09:22:02 +00:00
|
|
|
viper.Set(commonflags.Account, acc1.Address)
|
2022-01-19 10:56:10 +00:00
|
|
|
in.WriteString("pass\r")
|
|
|
|
checkKey(t, wallPath, acc1.PrivateKey())
|
|
|
|
|
2022-05-18 09:22:02 +00:00
|
|
|
viper.Set(commonflags.Account, "not an address")
|
2022-05-24 08:22:23 +00:00
|
|
|
checkKeyError(t, wallPath, ErrInvalidAddress)
|
2022-01-19 10:56:10 +00:00
|
|
|
|
|
|
|
acc, err := wallet.NewAccount()
|
|
|
|
require.NoError(t, err)
|
2022-05-18 09:22:02 +00:00
|
|
|
viper.Set(commonflags.Account, acc.Address)
|
2022-05-24 08:22:23 +00:00
|
|
|
checkKeyError(t, wallPath, ErrInvalidAddress)
|
2022-01-19 10:56:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("WIF", func(t *testing.T) {
|
2022-09-16 06:48:12 +00:00
|
|
|
checkKeyError(t, wifKey.WIF(), ErrFs)
|
2022-01-19 10:56:10 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NEP-2", func(t *testing.T) {
|
2022-09-16 06:48:12 +00:00
|
|
|
checkKeyError(t, nep2, ErrFs)
|
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) {
|
2022-05-18 09:22:02 +00:00
|
|
|
viper.Set(commonflags.GenerateKey, true)
|
2022-12-27 09:36:30 +00:00
|
|
|
actual, err := getOrGenerate(testCmd)
|
2022-01-19 10:56:10 +00:00
|
|
|
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) {
|
2022-05-18 09:22:02 +00:00
|
|
|
viper.Set(commonflags.WalletPath, desc)
|
2022-12-27 09:36:30 +00:00
|
|
|
_, actualErr := getOrGenerate(testCmd)
|
2022-04-29 10:22:29 +00:00
|
|
|
require.ErrorIs(t, actualErr, err)
|
2022-01-19 10:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkKey(t *testing.T, desc string, expected *keys.PrivateKey) {
|
2022-05-18 09:22:02 +00:00
|
|
|
viper.Set(commonflags.WalletPath, desc)
|
2022-12-27 09:36:30 +00:00
|
|
|
actual, err := getOrGenerate(testCmd)
|
2022-01-19 10:56:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, &expected.PrivateKey, actual)
|
|
|
|
}
|