-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Create and List Accounts Validator RPC (#7172)
* impl of accounts RPCs * create and list accounts impls * impl create account and list accounts * tests pass * imports * fix test
- Loading branch information
1 parent
366b98a
commit 0961fef
Showing
11 changed files
with
200 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
ptypes "github.com/gogo/protobuf/types" | ||
"github.com/pkg/errors" | ||
pb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2" | ||
"github.com/prysmaticlabs/prysm/shared/petnames" | ||
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2" | ||
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/derived" | ||
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/direct" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
// CreateAccount allows creation of a new account in a user's wallet via RPC. | ||
func (s *Server) CreateAccount(ctx context.Context, _ *ptypes.Empty) (*pb.CreateAccountResponse, error) { | ||
if !s.walletInitialized { | ||
return nil, status.Error(codes.FailedPrecondition, "Wallet not yet initialized") | ||
} | ||
var pubKey []byte | ||
var err error | ||
switch s.wallet.KeymanagerKind() { | ||
case v2keymanager.Remote: | ||
return nil, status.Error(codes.InvalidArgument, "Cannot create account for remote keymanager") | ||
case v2keymanager.Direct: | ||
km, ok := s.keymanager.(*direct.Keymanager) | ||
if !ok { | ||
return nil, status.Error(codes.InvalidArgument, "Not a direct keymanager") | ||
} | ||
// Create a new validator account using the specified keymanager. | ||
pubKey, err = km.CreateAccount(ctx) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not create account in wallet") | ||
} | ||
case v2keymanager.Derived: | ||
km, ok := s.keymanager.(*derived.Keymanager) | ||
if !ok { | ||
return nil, status.Error(codes.InvalidArgument, "Not a derived keymanager") | ||
} | ||
pubKey, err = km.CreateAccount(ctx, false /*logAccountInfo*/) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "could not create account in wallet") | ||
} | ||
} | ||
return &pb.CreateAccountResponse{ | ||
Account: &pb.Account{ | ||
ValidatingPublicKey: pubKey, | ||
AccountName: petnames.DeterministicName(pubKey, "-"), | ||
}, | ||
}, nil | ||
} | ||
|
||
// ListAccounts allows retrieval of validating keys and their petnames | ||
// for a user's wallet via RPC. | ||
func (s *Server) ListAccounts(ctx context.Context, req *pb.ListAccountsRequest) (*pb.ListAccountsResponse, error) { | ||
if !s.walletInitialized { | ||
return nil, status.Error(codes.FailedPrecondition, "Wallet not yet initialized") | ||
} | ||
keys, err := s.keymanager.FetchValidatingPublicKeys(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
accounts := make([]*pb.Account, len(keys)) | ||
for i := 0; i < len(keys); i++ { | ||
accounts[i] = &pb.Account{ | ||
ValidatingPublicKey: keys[i][:], | ||
AccountName: petnames.DeterministicName(keys[i][:], "-"), | ||
} | ||
if s.wallet.KeymanagerKind() == v2keymanager.Derived { | ||
accounts[i].DerivationPath = fmt.Sprintf(derived.ValidatingKeyDerivationPathTemplate, i) | ||
} | ||
} | ||
return &pb.ListAccountsResponse{ | ||
Accounts: accounts, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package rpc | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
ptypes "github.com/gogo/protobuf/types" | ||
pb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2" | ||
"github.com/prysmaticlabs/prysm/shared/testutil/assert" | ||
"github.com/prysmaticlabs/prysm/shared/testutil/require" | ||
v2 "github.com/prysmaticlabs/prysm/validator/accounts/v2" | ||
v2keymanager "github.com/prysmaticlabs/prysm/validator/keymanager/v2" | ||
"github.com/prysmaticlabs/prysm/validator/keymanager/v2/direct" | ||
) | ||
|
||
func TestServer_CreateAccount(t *testing.T) { | ||
ctx := context.Background() | ||
localWalletDir := setupWalletDir(t) | ||
defaultWalletPath = localWalletDir | ||
strongPass := "29384283xasjasd32%%&*@*#*" | ||
// We attempt to create the wallet. | ||
wallet, err := v2.CreateWalletWithKeymanager(ctx, &v2.CreateWalletConfig{ | ||
WalletCfg: &v2.WalletConfig{ | ||
WalletDir: defaultWalletPath, | ||
KeymanagerKind: v2keymanager.Direct, | ||
WalletPassword: strongPass, | ||
}, | ||
SkipMnemonicConfirm: true, | ||
}) | ||
require.NoError(t, err) | ||
km, err := wallet.InitializeKeymanager(ctx, true /* skip mnemonic confirm */) | ||
require.NoError(t, err) | ||
s := &Server{ | ||
keymanager: km, | ||
walletInitialized: true, | ||
wallet: wallet, | ||
} | ||
resp, err := s.CreateAccount(ctx, &ptypes.Empty{}) | ||
require.NoError(t, err) | ||
assert.NotNil(t, resp.Account.ValidatingPublicKey) | ||
} | ||
|
||
func TestServer_ListAccounts(t *testing.T) { | ||
ctx := context.Background() | ||
localWalletDir := setupWalletDir(t) | ||
defaultWalletPath = localWalletDir | ||
strongPass := "29384283xasjasd32%%&*@*#*" | ||
// We attempt to create the wallet. | ||
wallet, err := v2.CreateWalletWithKeymanager(ctx, &v2.CreateWalletConfig{ | ||
WalletCfg: &v2.WalletConfig{ | ||
WalletDir: defaultWalletPath, | ||
KeymanagerKind: v2keymanager.Direct, | ||
WalletPassword: strongPass, | ||
}, | ||
SkipMnemonicConfirm: true, | ||
}) | ||
require.NoError(t, err) | ||
km, err := wallet.InitializeKeymanager(ctx, true /* skip mnemonic confirm */) | ||
require.NoError(t, err) | ||
s := &Server{ | ||
keymanager: km, | ||
walletInitialized: true, | ||
wallet: wallet, | ||
} | ||
numAccounts := 5 | ||
keys := make([][]byte, numAccounts) | ||
for i := 0; i < numAccounts; i++ { | ||
key, err := km.(*direct.Keymanager).CreateAccount(ctx) | ||
require.NoError(t, err) | ||
keys[i] = key | ||
} | ||
resp, err := s.ListAccounts(ctx, &pb.ListAccountsRequest{}) | ||
require.NoError(t, err) | ||
require.Equal(t, len(resp.Accounts), numAccounts) | ||
for i := 0; i < numAccounts; i++ { | ||
assert.DeepEqual(t, resp.Accounts[i].ValidatingPublicKey, keys[i]) | ||
} | ||
} |
Oops, something went wrong.