2020-09-03 15:11:17 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-09-30 19:03:54 +00:00
|
|
|
"encoding/hex"
|
2020-09-03 15:11:17 +00:00
|
|
|
"encoding/json"
|
2020-09-24 20:37:59 +00:00
|
|
|
"io/ioutil"
|
2020-09-03 15:11:17 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
ptypes "github.com/gogo/protobuf/types"
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/rand"
|
|
|
|
v2 "github.com/prysmaticlabs/prysm/validator/accounts/v2"
|
2020-09-17 01:34:42 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/accounts/v2/wallet"
|
2020-09-03 15:11:17 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/flags"
|
|
|
|
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2"
|
2020-09-21 19:44:43 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/derived"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/direct"
|
2020-09-03 15:11:17 +00:00
|
|
|
"github.com/tyler-smith/go-bip39"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
var defaultWalletPath = filepath.Join(flags.DefaultValidatorDir(), flags.WalletDefaultDirName)
|
|
|
|
|
2020-09-30 14:13:37 +00:00
|
|
|
const (
|
|
|
|
checkExistsErrMsg = "Could not check if wallet exists"
|
|
|
|
checkValidityErrMsg = "Could not check if wallet is valid"
|
|
|
|
noWalletMsg = "No wallet found at path"
|
|
|
|
invalidWalletMsg = "Directory does not contain a valid wallet"
|
|
|
|
)
|
|
|
|
|
2020-09-24 20:37:59 +00:00
|
|
|
// HasWallet checks if a user has created a wallet before as well as whether or not
|
|
|
|
// they have used the web UI before to set a wallet password.
|
|
|
|
func (s *Server) HasWallet(ctx context.Context, _ *ptypes.Empty) (*pb.HasWalletResponse, error) {
|
2020-09-30 14:13:37 +00:00
|
|
|
exists, err := wallet.Exists(defaultWalletPath)
|
|
|
|
if err != nil {
|
2020-10-02 21:35:28 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "Could not check if wallet exists: %v", err)
|
2020-09-30 14:13:37 +00:00
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return &pb.HasWalletResponse{
|
|
|
|
WalletExists: false,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-09-24 20:37:59 +00:00
|
|
|
return &pb.HasWalletResponse{
|
|
|
|
WalletExists: true,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-09-03 15:11:17 +00:00
|
|
|
// CreateWallet via an API request, allowing a user to save a new
|
|
|
|
// derived, direct, or remote wallet.
|
2020-10-07 02:15:54 +00:00
|
|
|
func (s *Server) CreateWallet(ctx context.Context, req *pb.CreateWalletRequest) (*pb.CreateWalletResponse, error) {
|
2020-09-30 14:13:37 +00:00
|
|
|
// Currently defaultWalletPath is used as the wallet directory and req's WalletPath is ignored for simplicity
|
|
|
|
exists, err := wallet.Exists(defaultWalletPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not check for existing wallet: %v", err)
|
|
|
|
}
|
|
|
|
if exists {
|
|
|
|
return nil, status.Error(codes.AlreadyExists, "A wallet already exists at this location.")
|
|
|
|
}
|
2020-09-03 15:11:17 +00:00
|
|
|
switch req.Keymanager {
|
2020-09-24 20:37:59 +00:00
|
|
|
case pb.KeymanagerKind_DIRECT:
|
2020-09-03 15:11:17 +00:00
|
|
|
// Needs to unmarshal the keystores from the requests.
|
|
|
|
if req.KeystoresImported == nil || len(req.KeystoresImported) < 1 {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "No keystores included for import")
|
|
|
|
}
|
|
|
|
keystores := make([]*v2keymanager.Keystore, len(req.KeystoresImported))
|
|
|
|
for i := 0; i < len(req.KeystoresImported); i++ {
|
|
|
|
encoded := req.KeystoresImported[i]
|
|
|
|
keystore := &v2keymanager.Keystore{}
|
|
|
|
if err := json.Unmarshal([]byte(encoded), &keystore); err != nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "Not a valid EIP-2335 keystore JSON file: %v", err)
|
|
|
|
}
|
|
|
|
keystores[i] = keystore
|
|
|
|
}
|
2020-09-17 01:34:42 +00:00
|
|
|
w, err := v2.CreateWalletWithKeymanager(ctx, &v2.CreateWalletConfig{
|
|
|
|
WalletCfg: &wallet.Config{
|
2020-09-03 15:11:17 +00:00
|
|
|
WalletDir: defaultWalletPath,
|
|
|
|
KeymanagerKind: v2keymanager.Direct,
|
|
|
|
WalletPassword: req.WalletPassword,
|
|
|
|
},
|
|
|
|
SkipMnemonicConfirm: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Import the uploaded accounts.
|
|
|
|
if err := v2.ImportAccounts(ctx, &v2.ImportAccountsConfig{
|
2020-09-17 01:34:42 +00:00
|
|
|
Wallet: w,
|
2020-09-03 15:11:17 +00:00
|
|
|
Keystores: keystores,
|
|
|
|
AccountPassword: req.KeystoresPassword,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-22 14:49:07 +00:00
|
|
|
if err := s.initializeWallet(ctx, &wallet.Config{
|
|
|
|
WalletDir: defaultWalletPath,
|
|
|
|
KeymanagerKind: v2keymanager.Direct,
|
|
|
|
WalletPassword: req.WalletPassword,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-07 02:15:54 +00:00
|
|
|
return &pb.CreateWalletResponse{
|
|
|
|
Wallet: &pb.WalletResponse{
|
|
|
|
WalletPath: defaultWalletPath,
|
|
|
|
KeymanagerKind: pb.KeymanagerKind_DIRECT,
|
|
|
|
},
|
2020-09-03 15:11:17 +00:00
|
|
|
}, nil
|
2020-09-24 20:37:59 +00:00
|
|
|
case pb.KeymanagerKind_DERIVED:
|
2020-09-03 15:11:17 +00:00
|
|
|
if req.NumAccounts < 1 {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "Must create at least 1 validator account")
|
|
|
|
}
|
|
|
|
if req.Mnemonic == "" {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "Must include mnemonic in request")
|
|
|
|
}
|
2020-10-07 02:15:54 +00:00
|
|
|
_, depositData, err := v2.RecoverWallet(ctx, &v2.RecoverWalletConfig{
|
2020-09-03 15:11:17 +00:00
|
|
|
WalletDir: defaultWalletPath,
|
|
|
|
WalletPassword: req.WalletPassword,
|
|
|
|
Mnemonic: req.Mnemonic,
|
|
|
|
NumAccounts: int64(req.NumAccounts),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-22 14:49:07 +00:00
|
|
|
if err := s.initializeWallet(ctx, &wallet.Config{
|
|
|
|
WalletDir: defaultWalletPath,
|
|
|
|
KeymanagerKind: v2keymanager.Direct,
|
|
|
|
WalletPassword: req.WalletPassword,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-07 02:15:54 +00:00
|
|
|
|
|
|
|
depositDataList := make([]*pb.DepositDataResponse_DepositData, len(depositData))
|
|
|
|
for i, item := range depositData {
|
|
|
|
data, err := v2.DepositDataJSON(item)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
depositDataList[i] = &pb.DepositDataResponse_DepositData{
|
|
|
|
Data: data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &pb.CreateWalletResponse{
|
|
|
|
Wallet: &pb.WalletResponse{
|
|
|
|
WalletPath: defaultWalletPath,
|
|
|
|
KeymanagerKind: pb.KeymanagerKind_DERIVED,
|
|
|
|
},
|
|
|
|
AccountsCreated: &pb.DepositDataResponse{
|
|
|
|
DepositDataList: depositDataList,
|
|
|
|
},
|
2020-09-03 15:11:17 +00:00
|
|
|
}, nil
|
2020-09-24 20:37:59 +00:00
|
|
|
case pb.KeymanagerKind_REMOTE:
|
2020-09-03 15:11:17 +00:00
|
|
|
return nil, status.Error(codes.Unimplemented, "Remote keymanager not yet supported")
|
|
|
|
default:
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "Keymanager type %T not yet supported", req.Keymanager)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// EditConfig allows the user to edit their wallet's keymanageropts.
|
|
|
|
func (s *Server) EditConfig(ctx context.Context, req *pb.EditWalletConfigRequest) (*pb.WalletResponse, error) {
|
|
|
|
return nil, status.Error(codes.Unimplemented, "Unimplemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
// WalletConfig returns the wallet's configuration. If no wallet exists, we return an empty response.
|
|
|
|
func (s *Server) WalletConfig(ctx context.Context, _ *ptypes.Empty) (*pb.WalletResponse, error) {
|
2020-09-30 14:13:37 +00:00
|
|
|
exists, err := wallet.Exists(defaultWalletPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, checkExistsErrMsg)
|
|
|
|
}
|
|
|
|
if !exists {
|
2020-09-03 15:11:17 +00:00
|
|
|
// If no wallet is found, we simply return an empty response.
|
|
|
|
return &pb.WalletResponse{}, nil
|
|
|
|
}
|
2020-09-30 14:13:37 +00:00
|
|
|
valid, err := wallet.IsValid(defaultWalletPath)
|
|
|
|
if err == wallet.ErrNoWalletFound {
|
|
|
|
return &pb.WalletResponse{}, nil
|
|
|
|
}
|
2020-09-03 15:11:17 +00:00
|
|
|
if err != nil {
|
2020-09-30 14:13:37 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, checkValidityErrMsg)
|
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
return nil, status.Errorf(codes.FailedPrecondition, invalidWalletMsg)
|
2020-09-03 15:11:17 +00:00
|
|
|
}
|
2020-09-30 14:13:37 +00:00
|
|
|
|
2020-09-24 20:37:59 +00:00
|
|
|
if s.wallet == nil || s.keymanager == nil {
|
|
|
|
// If no wallet is found, we simply return an empty response.
|
|
|
|
return &pb.WalletResponse{}, nil
|
|
|
|
}
|
|
|
|
var keymanagerKind pb.KeymanagerKind
|
|
|
|
switch s.wallet.KeymanagerKind() {
|
|
|
|
case v2keymanager.Derived:
|
|
|
|
keymanagerKind = pb.KeymanagerKind_DERIVED
|
|
|
|
case v2keymanager.Direct:
|
|
|
|
keymanagerKind = pb.KeymanagerKind_DIRECT
|
|
|
|
case v2keymanager.Remote:
|
|
|
|
keymanagerKind = pb.KeymanagerKind_REMOTE
|
|
|
|
}
|
|
|
|
f, err := s.wallet.ReadKeymanagerConfigFromDisk(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not read keymanager config from disk: %v", err)
|
|
|
|
}
|
|
|
|
encoded, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not parse keymanager config: %v", err)
|
|
|
|
}
|
|
|
|
var config map[string]string
|
|
|
|
if err := json.Unmarshal(encoded, &config); err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not JSON unmarshal keymanager config: %v", err)
|
|
|
|
}
|
2020-09-03 15:11:17 +00:00
|
|
|
return &pb.WalletResponse{
|
|
|
|
WalletPath: defaultWalletPath,
|
2020-09-24 20:37:59 +00:00
|
|
|
KeymanagerKind: keymanagerKind,
|
|
|
|
KeymanagerConfig: config,
|
2020-09-03 15:11:17 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateMnemonic creates a new, random bip39 mnemonic phrase.
|
|
|
|
func (s *Server) GenerateMnemonic(ctx context.Context, _ *ptypes.Empty) (*pb.GenerateMnemonicResponse, error) {
|
|
|
|
mnemonicRandomness := make([]byte, 32)
|
|
|
|
if _, err := rand.NewGenerator().Read(mnemonicRandomness); err != nil {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.FailedPrecondition,
|
|
|
|
"Could not initialize mnemonic source of randomness: %v",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
mnemonic, err := bip39.NewMnemonic(mnemonicRandomness)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not generate wallet seed: %v", err)
|
|
|
|
}
|
|
|
|
return &pb.GenerateMnemonicResponse{
|
|
|
|
Mnemonic: mnemonic,
|
|
|
|
}, nil
|
|
|
|
}
|
2020-09-21 19:44:43 +00:00
|
|
|
|
|
|
|
// ChangePassword allows changing a wallet password via the API as
|
|
|
|
// an authenticated method.
|
|
|
|
func (s *Server) ChangePassword(ctx context.Context, req *pb.ChangePasswordRequest) (*ptypes.Empty, error) {
|
2020-09-30 14:13:37 +00:00
|
|
|
exists, err := wallet.Exists(defaultWalletPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, checkExistsErrMsg)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return nil, status.Errorf(codes.FailedPrecondition, noWalletMsg)
|
|
|
|
}
|
|
|
|
valid, err := wallet.IsValid(defaultWalletPath)
|
|
|
|
if err == wallet.ErrNoWalletFound {
|
|
|
|
return nil, status.Errorf(codes.FailedPrecondition, noWalletMsg)
|
2020-09-21 19:44:43 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2020-09-30 14:13:37 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, checkValidityErrMsg)
|
|
|
|
}
|
|
|
|
if !valid {
|
|
|
|
return nil, status.Errorf(codes.FailedPrecondition, invalidWalletMsg)
|
2020-09-21 19:44:43 +00:00
|
|
|
}
|
|
|
|
if req.Password == "" {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "Password cannot be empty")
|
|
|
|
}
|
|
|
|
if req.Password != req.PasswordConfirmation {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "Password does not match confirmation")
|
|
|
|
}
|
|
|
|
switch s.wallet.KeymanagerKind() {
|
|
|
|
case v2keymanager.Direct:
|
|
|
|
km, ok := s.keymanager.(*direct.Keymanager)
|
|
|
|
if !ok {
|
|
|
|
return nil, status.Error(codes.FailedPrecondition, "Not a valid direct keymanager")
|
|
|
|
}
|
|
|
|
s.wallet.SetPassword(req.Password)
|
2020-09-24 16:47:13 +00:00
|
|
|
if err := s.wallet.SaveHashedPassword(ctx); err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not save hashed password: %v", err)
|
|
|
|
}
|
2020-09-21 19:44:43 +00:00
|
|
|
if err := km.RefreshWalletPassword(ctx); err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not refresh wallet password: %v", err)
|
|
|
|
}
|
|
|
|
case v2keymanager.Derived:
|
|
|
|
km, ok := s.keymanager.(*derived.Keymanager)
|
|
|
|
if !ok {
|
|
|
|
return nil, status.Error(codes.FailedPrecondition, "Not a valid derived keymanager")
|
|
|
|
}
|
|
|
|
s.wallet.SetPassword(req.Password)
|
2020-09-24 16:47:13 +00:00
|
|
|
if err := s.wallet.SaveHashedPassword(ctx); err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not save hashed password: %v", err)
|
|
|
|
}
|
2020-09-21 19:44:43 +00:00
|
|
|
if err := km.RefreshWalletPassword(ctx); err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "Could not refresh wallet password: %v", err)
|
|
|
|
}
|
|
|
|
case v2keymanager.Remote:
|
|
|
|
return nil, status.Error(codes.Internal, "Cannot change password for remote keymanager")
|
|
|
|
}
|
|
|
|
return &ptypes.Empty{}, nil
|
|
|
|
}
|
2020-09-29 01:58:14 +00:00
|
|
|
|
2020-09-30 19:03:54 +00:00
|
|
|
// ImportKeystores allows importing new keystores via RPC into the wallet
|
|
|
|
// which will be decrypted using the specified password .
|
2020-09-29 01:58:14 +00:00
|
|
|
func (s *Server) ImportKeystores(
|
|
|
|
ctx context.Context, req *pb.ImportKeystoresRequest,
|
|
|
|
) (*pb.ImportKeystoresResponse, error) {
|
2020-09-30 19:03:54 +00:00
|
|
|
if s.wallet == nil {
|
|
|
|
return nil, status.Error(codes.FailedPrecondition, "No wallet initialized")
|
|
|
|
}
|
|
|
|
if s.wallet.KeymanagerKind() != v2keymanager.Direct {
|
|
|
|
return nil, status.Error(codes.FailedPrecondition, "Only Non-HD wallets can import keystores")
|
|
|
|
}
|
|
|
|
if req.KeystoresPassword == "" {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "Password required for keystores")
|
|
|
|
}
|
|
|
|
// Needs to unmarshal the keystores from the requests.
|
|
|
|
if req.KeystoresImported == nil || len(req.KeystoresImported) < 1 {
|
|
|
|
return nil, status.Error(codes.InvalidArgument, "No keystores included for import")
|
|
|
|
}
|
|
|
|
keystores := make([]*v2keymanager.Keystore, len(req.KeystoresImported))
|
|
|
|
importedPubKeys := make([][]byte, len(req.KeystoresImported))
|
|
|
|
for i := 0; i < len(req.KeystoresImported); i++ {
|
|
|
|
encoded := req.KeystoresImported[i]
|
|
|
|
keystore := &v2keymanager.Keystore{}
|
|
|
|
if err := json.Unmarshal([]byte(encoded), &keystore); err != nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "Not a valid EIP-2335 keystore JSON file: %v", err)
|
|
|
|
}
|
|
|
|
keystores[i] = keystore
|
|
|
|
pubKey, err := hex.DecodeString(keystore.Pubkey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "Not a valid BLS public key in keystore file: %v", err)
|
|
|
|
}
|
|
|
|
importedPubKeys[i] = pubKey
|
|
|
|
}
|
|
|
|
// Import the uploaded accounts.
|
|
|
|
if err := v2.ImportAccounts(ctx, &v2.ImportAccountsConfig{
|
|
|
|
Wallet: s.wallet,
|
|
|
|
Keystores: keystores,
|
|
|
|
AccountPassword: req.KeystoresPassword,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &pb.ImportKeystoresResponse{
|
|
|
|
ImportedPublicKeys: importedPubKeys,
|
|
|
|
}, nil
|
2020-09-29 01:58:14 +00:00
|
|
|
}
|