2020-09-04 00:58:36 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
pb "github.com/prysmaticlabs/prysm/proto/validator/accounts/v2"
|
2020-10-03 22:26:39 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/cmd"
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/pagination"
|
2020-09-04 00:58:36 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/petnames"
|
2020-10-15 22:31:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager"
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/keymanager/derived"
|
2020-09-04 00:58:36 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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")
|
|
|
|
}
|
2020-10-03 22:26:39 +00:00
|
|
|
if int(req.PageSize) > cmd.Get().MaxRPCPageSize {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "Requested page size %d can not be greater than max size %d",
|
|
|
|
req.PageSize, cmd.Get().MaxRPCPageSize)
|
|
|
|
}
|
2020-09-04 00:58:36 +00:00
|
|
|
keys, err := s.keymanager.FetchValidatingPublicKeys(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-20 01:22:36 +00:00
|
|
|
accs := make([]*pb.Account, len(keys))
|
2020-09-04 00:58:36 +00:00
|
|
|
for i := 0; i < len(keys); i++ {
|
2020-10-20 01:22:36 +00:00
|
|
|
accs[i] = &pb.Account{
|
2020-09-04 00:58:36 +00:00
|
|
|
ValidatingPublicKey: keys[i][:],
|
|
|
|
AccountName: petnames.DeterministicName(keys[i][:], "-"),
|
|
|
|
}
|
2020-10-15 22:31:52 +00:00
|
|
|
if s.wallet.KeymanagerKind() == keymanager.Derived {
|
2020-10-20 01:22:36 +00:00
|
|
|
accs[i].DerivationPath = fmt.Sprintf(derived.ValidatingKeyDerivationPathTemplate, i)
|
2020-09-04 00:58:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-03 22:26:39 +00:00
|
|
|
if req.All {
|
|
|
|
return &pb.ListAccountsResponse{
|
2020-10-20 01:22:36 +00:00
|
|
|
Accounts: accs,
|
2020-10-03 22:26:39 +00:00
|
|
|
TotalSize: int32(len(keys)),
|
|
|
|
NextPageToken: "",
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
start, end, nextPageToken, err := pagination.StartAndEndPage(req.PageToken, int(req.PageSize), len(keys))
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(
|
|
|
|
codes.Internal,
|
|
|
|
"Could not paginate results: %v",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
2020-09-04 00:58:36 +00:00
|
|
|
return &pb.ListAccountsResponse{
|
2020-10-20 01:22:36 +00:00
|
|
|
Accounts: accs[start:end],
|
2020-10-03 22:26:39 +00:00
|
|
|
TotalSize: int32(len(keys)),
|
|
|
|
NextPageToken: nextPageToken,
|
2020-09-04 00:58:36 +00:00
|
|
|
}, nil
|
|
|
|
}
|