github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/privval/grpc/server.go (about)

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