Skip to content

Commit

Permalink
Fix Change Wallet Password Logic (#7324)
Browse files Browse the repository at this point in the history
* fix change password logic
* gaz
* Merge refs/heads/master into change-password-fix
* Merge refs/heads/master into change-password-fix
* Merge refs/heads/master into change-password-fix
* Merge refs/heads/master into change-password-fix
* Merge refs/heads/master into change-password-fix
* Merge refs/heads/master into change-password-fix
* Merge refs/heads/master into change-password-fix
* Merge refs/heads/master into change-password-fix
* Merge refs/heads/master into change-password-fix
* Merge refs/heads/master into change-password-fix
  • Loading branch information
rauljordan authored Sep 24, 2020
1 parent 1bc0cc7 commit 76a3070
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 45 deletions.
1 change: 0 additions & 1 deletion validator/keymanager/v2/direct/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ go_library(
"//shared/interop:go_default_library",
"//shared/params:go_default_library",
"//shared/petnames:go_default_library",
"//shared/promptutil:go_default_library",
"//validator/accounts/v2/iface:go_default_library",
"//validator/keymanager/v2:go_default_library",
"@com_github_fsnotify_fsnotify//:go_default_library",
Expand Down
44 changes: 1 addition & 43 deletions validator/keymanager/v2/direct/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package direct
import (
"bytes"
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand All @@ -23,7 +22,6 @@ import (
"github.com/prysmaticlabs/prysm/shared/interop"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/petnames"
"github.com/prysmaticlabs/prysm/shared/promptutil"
"github.com/prysmaticlabs/prysm/validator/accounts/v2/iface"
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -385,13 +383,7 @@ func (dr *Keymanager) initializeAccountKeystore(ctx context.Context) error {
decryptor := keystorev4.New()
enc, err := decryptor.Decrypt(keystoreFile.Crypto, password)
if err != nil && strings.Contains(err.Error(), "invalid checksum") {
// If the password fails for an individual account, we ask the user to input
// that individual account's password until it succeeds.
enc, password, err = askUntilPasswordConfirms(decryptor, keystoreFile)
if err != nil {
return errors.Wrap(err, "could not confirm password via prompt")
}
dr.wallet.SetPassword(password) // Write the correct password to the wallet.
return errors.Wrap(err, "wrong password for wallet entered")
} else if err != nil {
return errors.Wrap(err, "could not decrypt keystore")
}
Expand Down Expand Up @@ -474,37 +466,3 @@ func (dr *Keymanager) createAccountsKeystore(
Name: encryptor.Name(),
}, nil
}

func askUntilPasswordConfirms(
decryptor *keystorev4.Encryptor, keystore *v2keymanager.Keystore,
) ([]byte, string, error) {
au := aurora.NewAurora(true)
// Loop asking for the password until the user enters it correctly.
var secretKey []byte
var password string
var err error
publicKey, err := hex.DecodeString(keystore.Pubkey)
if err != nil {
return nil, "", errors.Wrap(err, "could not decode public key")
}
formattedPublicKey := fmt.Sprintf("%#x", bytesutil.Trunc(publicKey))
for {
password, err = promptutil.PasswordPrompt(
fmt.Sprintf("\nPlease try again, incorrect password for account %s", au.BrightGreen(formattedPublicKey)),
promptutil.NotEmpty,
)
if err != nil {
return nil, "", fmt.Errorf("could not read account password: %v", err)
}
secretKey, err = decryptor.Decrypt(keystore.Crypto, password)
if err != nil && strings.Contains(err.Error(), "invalid checksum") {
fmt.Print(au.Red("Incorrect password entered, please try again"))
continue
}
if err != nil {
return nil, "", err
}
break
}
return secretKey, password, nil
}
6 changes: 5 additions & 1 deletion validator/rpc/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"

"github.com/dgrijalva/jwt-go"
Expand Down Expand Up @@ -67,7 +68,10 @@ func (s *Server) Login(ctx context.Context, req *pb.AuthRequest) (*pb.AuthRespon
WalletDir: defaultWalletPath,
WalletPassword: req.Password,
}); err != nil {
return nil, status.Error(codes.Internal, "Could not initialize wallet")
if strings.Contains(err.Error(), "invalid checksum") {
return nil, status.Error(codes.Unauthenticated, "Incorrect password")
}
return nil, status.Errorf(codes.Internal, "Could not initialize wallet: %v", err)
}
return s.sendAuthResponse()
}
Expand Down
6 changes: 6 additions & 0 deletions validator/rpc/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ func (s *Server) ChangePassword(ctx context.Context, req *pb.ChangePasswordReque
return nil, status.Error(codes.FailedPrecondition, "Not a valid direct keymanager")
}
s.wallet.SetPassword(req.Password)
if err := s.wallet.SaveHashedPassword(ctx); err != nil {
return nil, status.Errorf(codes.Internal, "Could not save hashed password: %v", err)
}
if err := km.RefreshWalletPassword(ctx); err != nil {
return nil, status.Errorf(codes.Internal, "Could not refresh wallet password: %v", err)
}
Expand All @@ -174,6 +177,9 @@ func (s *Server) ChangePassword(ctx context.Context, req *pb.ChangePasswordReque
return nil, status.Error(codes.FailedPrecondition, "Not a valid derived keymanager")
}
s.wallet.SetPassword(req.Password)
if err := s.wallet.SaveHashedPassword(ctx); err != nil {
return nil, status.Errorf(codes.Internal, "Could not save hashed password: %v", err)
}
if err := km.RefreshWalletPassword(ctx); err != nil {
return nil, status.Errorf(codes.Internal, "Could not refresh wallet password: %v", err)
}
Expand Down

0 comments on commit 76a3070

Please sign in to comment.