github.com/number571/tendermint@v0.34.11-gost/privval/grpc/client.go (about)

     1  package grpc
     2  
     3  import (
     4  	"context"
     5  
     6  	grpc "google.golang.org/grpc"
     7  	"google.golang.org/grpc/status"
     8  
     9  	"github.com/number571/tendermint/crypto"
    10  	cryptoenc "github.com/number571/tendermint/crypto/encoding"
    11  	"github.com/number571/tendermint/libs/log"
    12  	privvalproto "github.com/number571/tendermint/proto/tendermint/privval"
    13  	tmproto "github.com/number571/tendermint/proto/tendermint/types"
    14  	"github.com/number571/tendermint/types"
    15  )
    16  
    17  // SignerClient implements PrivValidator.
    18  // Handles remote validator connections that provide signing services
    19  type SignerClient struct {
    20  	logger log.Logger
    21  
    22  	client  privvalproto.PrivValidatorAPIClient
    23  	conn    *grpc.ClientConn
    24  	chainID string
    25  }
    26  
    27  var _ types.PrivValidator = (*SignerClient)(nil)
    28  
    29  // NewSignerClient returns an instance of SignerClient.
    30  // it will start the endpoint (if not already started)
    31  func NewSignerClient(conn *grpc.ClientConn,
    32  	chainID string, log log.Logger) (*SignerClient, error) {
    33  
    34  	sc := &SignerClient{
    35  		logger:  log,
    36  		chainID: chainID,
    37  		client:  privvalproto.NewPrivValidatorAPIClient(conn), // Create the Private Validator Client
    38  	}
    39  
    40  	return sc, nil
    41  }
    42  
    43  // Close closes the underlying connection
    44  func (sc *SignerClient) Close() error {
    45  	sc.logger.Info("Stopping service")
    46  	if sc.conn != nil {
    47  		return sc.conn.Close()
    48  	}
    49  	return nil
    50  }
    51  
    52  //--------------------------------------------------------
    53  // Implement PrivValidator
    54  
    55  // GetPubKey retrieves a public key from a remote signer
    56  // returns an error if client is not able to provide the key
    57  func (sc *SignerClient) GetPubKey(ctx context.Context) (crypto.PubKey, error) {
    58  	resp, err := sc.client.GetPubKey(ctx, &privvalproto.PubKeyRequest{ChainId: sc.chainID})
    59  	if err != nil {
    60  		errStatus, _ := status.FromError(err)
    61  		sc.logger.Error("SignerClient::GetPubKey", "err", errStatus.Message())
    62  		return nil, errStatus.Err()
    63  	}
    64  
    65  	pk, err := cryptoenc.PubKeyFromProto(resp.PubKey)
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	return pk, nil
    71  }
    72  
    73  // SignVote requests a remote signer to sign a vote
    74  func (sc *SignerClient) SignVote(ctx context.Context, chainID string, vote *tmproto.Vote) error {
    75  	resp, err := sc.client.SignVote(ctx, &privvalproto.SignVoteRequest{ChainId: sc.chainID, Vote: vote})
    76  	if err != nil {
    77  		errStatus, _ := status.FromError(err)
    78  		sc.logger.Error("Client SignVote", "err", errStatus.Message())
    79  		return errStatus.Err()
    80  	}
    81  
    82  	*vote = resp.Vote
    83  
    84  	return nil
    85  }
    86  
    87  // SignProposal requests a remote signer to sign a proposal
    88  func (sc *SignerClient) SignProposal(ctx context.Context, chainID string, proposal *tmproto.Proposal) error {
    89  	resp, err := sc.client.SignProposal(
    90  		ctx, &privvalproto.SignProposalRequest{ChainId: chainID, Proposal: proposal})
    91  
    92  	if err != nil {
    93  		errStatus, _ := status.FromError(err)
    94  		sc.logger.Error("SignerClient::SignProposal", "err", errStatus.Message())
    95  		return errStatus.Err()
    96  	}
    97  
    98  	*proposal = resp.Proposal
    99  
   100  	return nil
   101  }