2019-01-31 11:57:57 +00:00
|
|
|
package accounts
|
|
|
|
|
|
|
|
import (
|
2020-05-18 21:35:17 +00:00
|
|
|
"encoding/hex"
|
2020-05-06 05:55:25 +00:00
|
|
|
"flag"
|
2020-05-01 15:12:26 +00:00
|
|
|
"fmt"
|
2019-03-29 22:26:41 +00:00
|
|
|
"io/ioutil"
|
2019-01-31 11:57:57 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/keystore"
|
2019-02-23 06:06:20 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2019-01-31 11:57:57 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-05-06 05:55:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/flags"
|
|
|
|
"gopkg.in/urfave/cli.v2"
|
2019-01-31 11:57:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewValidatorAccount_AccountExists(t *testing.T) {
|
2019-02-13 23:49:06 +00:00
|
|
|
directory := testutil.TempDir() + "/testkeystore"
|
2020-04-14 16:41:09 +00:00
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(directory); err != nil {
|
2020-05-01 15:12:26 +00:00
|
|
|
t.Logf("Could not remove directory: %v", err)
|
2020-04-14 16:41:09 +00:00
|
|
|
}
|
|
|
|
}()
|
2020-01-07 23:45:29 +00:00
|
|
|
validatorKey, err := keystore.NewKey()
|
2019-01-31 11:57:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create new key: %v", err)
|
|
|
|
}
|
|
|
|
ks := keystore.NewKeystore(directory)
|
2019-02-13 23:49:06 +00:00
|
|
|
if err := ks.StoreKey(directory+params.BeaconConfig().ValidatorPrivkeyFileName, validatorKey, ""); err != nil {
|
2019-01-31 11:57:57 +00:00
|
|
|
t.Fatalf("Unable to store key %v", err)
|
|
|
|
}
|
2019-03-29 22:26:41 +00:00
|
|
|
if err := NewValidatorAccount(directory, ""); err != nil {
|
|
|
|
t.Errorf("Should support multiple keys: %v", err)
|
|
|
|
}
|
2020-04-14 16:41:09 +00:00
|
|
|
files, err := ioutil.ReadDir(directory)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-03-29 22:26:41 +00:00
|
|
|
if len(files) != 3 {
|
2019-04-18 17:23:38 +00:00
|
|
|
t.Errorf("multiple validators were not created only %v files in directory", len(files))
|
2019-03-29 22:26:41 +00:00
|
|
|
for _, f := range files {
|
|
|
|
t.Errorf("%v\n", f.Name())
|
|
|
|
}
|
2019-01-31 11:57:57 +00:00
|
|
|
}
|
2020-05-01 15:12:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewValidatorAccount_CreateValidatorAccount(t *testing.T) {
|
|
|
|
directory := "foobar"
|
|
|
|
_, _, err := CreateValidatorAccount(directory, "foobar")
|
|
|
|
wantErrString := fmt.Sprintf("path %q does not exist", directory)
|
|
|
|
if err == nil || err.Error() != wantErrString {
|
|
|
|
t.Errorf("expected error not thrown, want: %v, got: %v", wantErrString, err)
|
2019-01-31 11:57:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-06 05:55:25 +00:00
|
|
|
|
2020-05-06 23:50:19 +00:00
|
|
|
func TestHandleEmptyFlags_FlagsSet(t *testing.T) {
|
2020-05-06 05:55:25 +00:00
|
|
|
passedPath := "~/path/given"
|
|
|
|
passedPassword := "password"
|
|
|
|
|
|
|
|
app := &cli.App{}
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.String(flags.KeystorePathFlag.Name, passedPath, "set keystore path")
|
|
|
|
set.String(flags.PasswordFlag.Name, passedPassword, "set keystore password")
|
|
|
|
ctx := cli.NewContext(app, set, nil)
|
2020-05-18 21:35:17 +00:00
|
|
|
path, passphrase, err := HandleEmptyKeystoreFlags(ctx, false)
|
2020-05-06 05:55:25 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if passedPath != path {
|
|
|
|
t.Fatalf("Expected set path to be unchanged, expected %s, received %s", passedPath, path)
|
|
|
|
}
|
|
|
|
if passedPassword != passphrase {
|
|
|
|
t.Fatalf("Expected set password to be unchanged, expected %s, received %s", passedPassword, passphrase)
|
|
|
|
}
|
|
|
|
}
|
2020-05-18 21:35:17 +00:00
|
|
|
|
|
|
|
func TestChangePassword_KeyEncryptedWithNewPassword(t *testing.T) {
|
|
|
|
directory := testutil.TempDir() + "/testkeystore"
|
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(directory); err != nil {
|
|
|
|
t.Logf("Could not remove directory: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
oldPassword := "old"
|
|
|
|
newPassword := "new"
|
|
|
|
|
|
|
|
validatorKey, err := keystore.NewKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create new key: %v", err)
|
|
|
|
}
|
|
|
|
ks := keystore.NewKeystore(directory)
|
|
|
|
if err := ks.StoreKey(directory+params.BeaconConfig().ValidatorPrivkeyFileName, validatorKey, oldPassword); err != nil {
|
|
|
|
t.Fatalf("Unable to store key %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ChangePassword(directory, oldPassword, newPassword); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys, err := DecryptKeysFromKeystore(directory, params.BeaconConfig().ValidatorPrivkeyFileName, newPassword)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, ok := keys[hex.EncodeToString(validatorKey.PublicKey.Marshal())]; !ok {
|
|
|
|
t.Error("Key not encrypted using the new password")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangePassword_KeyNotMatchingOldPasswordNotEncryptedWithNewPassword(t *testing.T) {
|
|
|
|
directory := testutil.TempDir() + "/testkeystore"
|
|
|
|
defer func() {
|
|
|
|
if err := os.RemoveAll(directory); err != nil {
|
|
|
|
t.Logf("Could not remove directory: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
oldPassword := "old"
|
|
|
|
newPassword := "new"
|
|
|
|
|
|
|
|
validatorKey, err := keystore.NewKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Cannot create new key: %v", err)
|
|
|
|
}
|
|
|
|
ks := keystore.NewKeystore(directory)
|
|
|
|
if err := ks.StoreKey(directory+params.BeaconConfig().ValidatorPrivkeyFileName, validatorKey, "notmatching"); err != nil {
|
|
|
|
t.Fatalf("Unable to store key %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ChangePassword(directory, oldPassword, newPassword); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys, err := DecryptKeysFromKeystore(directory, params.BeaconConfig().ValidatorPrivkeyFileName, newPassword)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if _, ok := keys[hex.EncodeToString(validatorKey.PublicKey.Marshal())]; ok {
|
|
|
|
t.Error("Key incorrectly encrypted using the new password")
|
|
|
|
}
|
|
|
|
}
|