github.com/lazyledger/lazyledger-core@v0.35.0-dev.0.20210613111200-4c651f053571/privval/signer_requestHandler.go (about)

     1  package privval
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/lazyledger/lazyledger-core/crypto"
     7  	cryptoenc "github.com/lazyledger/lazyledger-core/crypto/encoding"
     8  	cryptoproto "github.com/lazyledger/lazyledger-core/proto/tendermint/crypto"
     9  	privvalproto "github.com/lazyledger/lazyledger-core/proto/tendermint/privval"
    10  	tmproto "github.com/lazyledger/lazyledger-core/proto/tendermint/types"
    11  	"github.com/lazyledger/lazyledger-core/types"
    12  )
    13  
    14  func DefaultValidationRequestHandler(
    15  	privVal types.PrivValidator,
    16  	req privvalproto.Message,
    17  	chainID string,
    18  ) (privvalproto.Message, error) {
    19  	var (
    20  		res privvalproto.Message
    21  		err error
    22  	)
    23  
    24  	switch r := req.Sum.(type) {
    25  	case *privvalproto.Message_PubKeyRequest:
    26  		if r.PubKeyRequest.GetChainId() != chainID {
    27  			res = mustWrapMsg(&privvalproto.PubKeyResponse{
    28  				PubKey: cryptoproto.PublicKey{}, Error: &privvalproto.RemoteSignerError{
    29  					Code: 0, Description: "unable to provide pubkey"}})
    30  			return res, fmt.Errorf("want chainID: %s, got chainID: %s", r.PubKeyRequest.GetChainId(), chainID)
    31  		}
    32  
    33  		var pubKey crypto.PubKey
    34  		pubKey, err = privVal.GetPubKey()
    35  		if err != nil {
    36  			return res, err
    37  		}
    38  		pk, err := cryptoenc.PubKeyToProto(pubKey)
    39  		if err != nil {
    40  			return res, err
    41  		}
    42  
    43  		if err != nil {
    44  			res = mustWrapMsg(&privvalproto.PubKeyResponse{
    45  				PubKey: cryptoproto.PublicKey{}, Error: &privvalproto.RemoteSignerError{Code: 0, Description: err.Error()}})
    46  		} else {
    47  			res = mustWrapMsg(&privvalproto.PubKeyResponse{PubKey: pk, Error: nil})
    48  		}
    49  
    50  	case *privvalproto.Message_SignVoteRequest:
    51  		if r.SignVoteRequest.ChainId != chainID {
    52  			res = mustWrapMsg(&privvalproto.SignedVoteResponse{
    53  				Vote: tmproto.Vote{}, Error: &privvalproto.RemoteSignerError{
    54  					Code: 0, Description: "unable to sign vote"}})
    55  			return res, fmt.Errorf("want chainID: %s, got chainID: %s", r.SignVoteRequest.GetChainId(), chainID)
    56  		}
    57  
    58  		vote := r.SignVoteRequest.Vote
    59  
    60  		err = privVal.SignVote(chainID, vote)
    61  		if err != nil {
    62  			res = mustWrapMsg(&privvalproto.SignedVoteResponse{
    63  				Vote: tmproto.Vote{}, Error: &privvalproto.RemoteSignerError{Code: 0, Description: err.Error()}})
    64  		} else {
    65  			res = mustWrapMsg(&privvalproto.SignedVoteResponse{Vote: *vote, Error: nil})
    66  		}
    67  
    68  	case *privvalproto.Message_SignProposalRequest:
    69  		if r.SignProposalRequest.GetChainId() != chainID {
    70  			res = mustWrapMsg(&privvalproto.SignedProposalResponse{
    71  				Proposal: tmproto.Proposal{}, Error: &privvalproto.RemoteSignerError{
    72  					Code:        0,
    73  					Description: "unable to sign proposal"}})
    74  			return res, fmt.Errorf("want chainID: %s, got chainID: %s", r.SignProposalRequest.GetChainId(), chainID)
    75  		}
    76  
    77  		proposal := r.SignProposalRequest.Proposal
    78  
    79  		err = privVal.SignProposal(chainID, proposal)
    80  		if err != nil {
    81  			res = mustWrapMsg(&privvalproto.SignedProposalResponse{
    82  				Proposal: tmproto.Proposal{}, Error: &privvalproto.RemoteSignerError{Code: 0, Description: err.Error()}})
    83  		} else {
    84  			res = mustWrapMsg(&privvalproto.SignedProposalResponse{Proposal: *proposal, Error: nil})
    85  		}
    86  	case *privvalproto.Message_PingRequest:
    87  		err, res = nil, mustWrapMsg(&privvalproto.PingResponse{})
    88  
    89  	default:
    90  		err = fmt.Errorf("unknown msg: %v", r)
    91  	}
    92  
    93  	return res, err
    94  }