mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 21:07:18 +00:00
2a1a0ce43f
* Created merge command * Key merging * Merge branch 'master' into db-manage-validators * flag comments * fixed compilation errors * added comment to Merge function * Tested that merging copies keys * Created command to change password for keys * Merge branch 'master' into change-validator-password * Merge branch 'master' into change-validator-password * cleanup after code review * Merge remote-tracking branch 'rkapka/change-validator-password' into change-validator-password * Merge branch 'master' into change-validator-password * Merge branch 'master' into change-validator-password * Merge branch 'master' into change-validator-password * Merge branch 'master' into change-validator-password
144 lines
4.2 KiB
Go
144 lines
4.2 KiB
Go
package accounts
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"flag"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/keystore"
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
|
"github.com/prysmaticlabs/prysm/validator/flags"
|
|
"gopkg.in/urfave/cli.v2"
|
|
)
|
|
|
|
func TestNewValidatorAccount_AccountExists(t *testing.T) {
|
|
directory := testutil.TempDir() + "/testkeystore"
|
|
defer func() {
|
|
if err := os.RemoveAll(directory); err != nil {
|
|
t.Logf("Could not remove directory: %v", err)
|
|
}
|
|
}()
|
|
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, ""); err != nil {
|
|
t.Fatalf("Unable to store key %v", err)
|
|
}
|
|
if err := NewValidatorAccount(directory, ""); err != nil {
|
|
t.Errorf("Should support multiple keys: %v", err)
|
|
}
|
|
files, err := ioutil.ReadDir(directory)
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if len(files) != 3 {
|
|
t.Errorf("multiple validators were not created only %v files in directory", len(files))
|
|
for _, f := range files {
|
|
t.Errorf("%v\n", f.Name())
|
|
}
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
|
|
func TestHandleEmptyFlags_FlagsSet(t *testing.T) {
|
|
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)
|
|
path, passphrase, err := HandleEmptyKeystoreFlags(ctx, false)
|
|
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)
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|