forked from strangelove-ventures/horcrux
/
remote_cosigner.go
142 lines (126 loc) · 3.51 KB
/
remote_cosigner.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package signer
import (
"context"
"fmt"
"net/url"
"time"
cometcrypto "github.com/cometbft/cometbft/crypto"
"github.com/google/uuid"
"github.com/strangelove-ventures/horcrux/signer/proto"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
var _ Cosigner = &RemoteCosigner{}
// RemoteCosigner uses CosignerGRPC to request signing from a remote cosigner
type RemoteCosigner struct {
id int
address string
client proto.CosignerClient
}
// NewRemoteCosigner returns a newly initialized RemoteCosigner
func NewRemoteCosigner(id int, address string) (*RemoteCosigner, error) {
client, err := getGRPCClient(address)
if err != nil {
return nil, err
}
cosigner := &RemoteCosigner{
id: id,
address: address,
client: client,
}
return cosigner, nil
}
// GetID returns the ID of the remote cosigner
// Implements the cosigner interface
func (cosigner *RemoteCosigner) GetID() int {
return cosigner.id
}
// GetAddress returns the P2P URL of the remote cosigner
// Implements the cosigner interface
func (cosigner *RemoteCosigner) GetAddress() string {
return cosigner.address
}
// GetPubKey returns public key of the validator.
// Implements Cosigner interface
func (cosigner *RemoteCosigner) GetPubKey(_ string) (cometcrypto.PubKey, error) {
return nil, fmt.Errorf("unexpected call to RemoteCosigner.GetPubKey")
}
// VerifySignature validates a signed payload against the public key.
// Implements Cosigner interface
func (cosigner *RemoteCosigner) VerifySignature(_ string, _, _ []byte) bool {
return false
}
func getGRPCClient(address string) (proto.CosignerClient, error) {
var grpcAddress string
url, err := url.Parse(address)
if err != nil {
grpcAddress = address
} else {
grpcAddress = url.Host
}
conn, err := grpc.Dial(grpcAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
}
return proto.NewCosignerClient(conn), nil
}
// Implements the cosigner interface
func (cosigner *RemoteCosigner) GetNonces(
ctx context.Context,
uuids []uuid.UUID,
) (CosignerUUIDNoncesMultiple, error) {
us := make([][]byte, len(uuids))
for i, u := range uuids {
us[i] = make([]byte, 16)
copy(us[i], u[:])
}
res, err := cosigner.client.GetNonces(ctx, &proto.GetNoncesRequest{
Uuids: us,
})
if err != nil {
return nil, err
}
out := make(CosignerUUIDNoncesMultiple, len(res.Nonces))
for i, nonces := range res.Nonces {
out[i] = &CosignerUUIDNonces{
UUID: uuid.UUID(nonces.Uuid),
Nonces: CosignerNoncesFromProto(nonces.Nonces),
}
}
return out, nil
}
// Implements the cosigner interface
func (cosigner *RemoteCosigner) SetNoncesAndSign(
ctx context.Context,
req CosignerSetNoncesAndSignRequest) (*CosignerSignResponse, error) {
res, err := cosigner.client.SetNoncesAndSign(ctx, &proto.SetNoncesAndSignRequest{
Uuid: req.Nonces.UUID[:],
ChainID: req.ChainID,
Nonces: req.Nonces.Nonces.toProto(),
Hrst: req.HRST.toProto(),
SignBytes: req.SignBytes,
})
if err != nil {
return nil, err
}
return &CosignerSignResponse{
NoncePublic: res.GetNoncePublic(),
Timestamp: time.Unix(0, res.GetTimestamp()),
Signature: res.GetSignature(),
}, nil
}
func (cosigner *RemoteCosigner) Sign(
ctx context.Context,
req CosignerSignBlockRequest,
) (*CosignerSignBlockResponse, error) {
res, err := cosigner.client.SignBlock(ctx, &proto.SignBlockRequest{
ChainID: req.ChainID,
Block: req.Block.ToProto(),
})
if err != nil {
return nil, err
}
return &CosignerSignBlockResponse{
Signature: res.GetSignature(),
}, nil
}