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