You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.7 KiB

  1. package grpc
  2. import (
  3. context "context"
  4. "google.golang.org/grpc/codes"
  5. "google.golang.org/grpc/status"
  6. "github.com/tendermint/tendermint/crypto"
  7. cryptoenc "github.com/tendermint/tendermint/crypto/encoding"
  8. "github.com/tendermint/tendermint/libs/log"
  9. privvalproto "github.com/tendermint/tendermint/proto/tendermint/privval"
  10. "github.com/tendermint/tendermint/types"
  11. )
  12. // SignerServer implements PrivValidatorAPIServer 9generated via protobuf services)
  13. // Handles remote validator connections that provide signing services
  14. type SignerServer struct {
  15. logger log.Logger
  16. chainID string
  17. privVal types.PrivValidator
  18. }
  19. func NewSignerServer(chainID string,
  20. privVal types.PrivValidator, log log.Logger) *SignerServer {
  21. return &SignerServer{
  22. logger: log,
  23. chainID: chainID,
  24. privVal: privVal,
  25. }
  26. }
  27. var _ privvalproto.PrivValidatorAPIServer = (*SignerServer)(nil)
  28. // PubKey receives a request for the pubkey
  29. // returns the pubkey on success and error on failure
  30. func (ss *SignerServer) GetPubKey(ctx context.Context, req *privvalproto.PubKeyRequest) (
  31. *privvalproto.PubKeyResponse, error) {
  32. var pubKey crypto.PubKey
  33. pubKey, err := ss.privVal.GetPubKey(ctx)
  34. if err != nil {
  35. return nil, status.Errorf(codes.NotFound, "error getting pubkey: %v", err)
  36. }
  37. pk, err := cryptoenc.PubKeyToProto(pubKey)
  38. if err != nil {
  39. return nil, status.Errorf(codes.Internal, "error transitioning pubkey to proto: %v", err)
  40. }
  41. ss.logger.Info("SignerServer: GetPubKey Success")
  42. return &privvalproto.PubKeyResponse{PubKey: pk}, nil
  43. }
  44. // SignVote receives a vote sign requests, attempts to sign it
  45. // returns SignedVoteResponse on success and error on failure
  46. func (ss *SignerServer) SignVote(ctx context.Context, req *privvalproto.SignVoteRequest) (
  47. *privvalproto.SignedVoteResponse, error) {
  48. vote := req.Vote
  49. err := ss.privVal.SignVote(ctx, req.ChainId, vote)
  50. if err != nil {
  51. return nil, status.Errorf(codes.InvalidArgument, "error signing vote: %v", err)
  52. }
  53. ss.logger.Info("SignerServer: SignVote Success", "height", req.Vote.Height)
  54. return &privvalproto.SignedVoteResponse{Vote: *vote}, nil
  55. }
  56. // SignProposal receives a proposal sign requests, attempts to sign it
  57. // returns SignedProposalResponse on success and error on failure
  58. func (ss *SignerServer) SignProposal(ctx context.Context, req *privvalproto.SignProposalRequest) (
  59. *privvalproto.SignedProposalResponse, error) {
  60. proposal := req.Proposal
  61. err := ss.privVal.SignProposal(ctx, req.ChainId, proposal)
  62. if err != nil {
  63. return nil, status.Errorf(codes.InvalidArgument, "error signing proposal: %v", err)
  64. }
  65. ss.logger.Info("SignerServer: SignProposal Success", "height", req.Proposal.Height)
  66. return &privvalproto.SignedProposalResponse{Proposal: *proposal}, nil
  67. }