-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpc.go
53 lines (43 loc) · 1.51 KB
/
grpc.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
package transports
import (
"bytes"
"context"
"github.com/consensys/gnark-crypto/ecc"
"github.com/consensys/gnark/backend/witness"
gt "github.com/go-kit/kit/transport/grpc"
"github.com/smsunarto/daedalus/pkg/prover/endpoints"
"github.com/smsunarto/daedalus/pkg/prover/proto"
)
type GRPCServer struct {
generateProof gt.Handler
proto.UnimplementedProverServer
}
func NewGRPCServer(endpoints endpoints.Endpoints) proto.ProverServer {
return &GRPCServer{
generateProof: gt.NewServer(
endpoints.GenerateProofEndpoint,
decodeGenerateProofRequest,
encodeGenerateProofResponse,
),
}
}
func (s *GRPCServer) GenerateProof(ctx context.Context, req *proto.GenerateProofRequest) (*proto.GenerateProofResponse, error) {
_, resp, err := s.generateProof.ServeGRPC(ctx, req)
if err != nil {
return nil, err
}
return resp.(*proto.GenerateProofResponse), nil
}
func decodeGenerateProofRequest(_ context.Context, grpcReq interface{}) (interface{}, error) {
req := grpcReq.(*proto.GenerateProofRequest)
buf := bytes.NewBuffer(req.FullWitness)
fullWitness, _ := witness.New(ecc.BN254.ScalarField())
fullWitness.ReadFrom(buf)
return endpoints.GenerateProofRequest{FullWitness: fullWitness, CircuitName: req.CircuitName}, nil
}
func encodeGenerateProofResponse(_ context.Context, response interface{}) (interface{}, error) {
resp := response.(endpoints.GenerateProofResponse)
var resBuf bytes.Buffer
resp.Proof.WriteTo(&resBuf)
return &proto.GenerateProofResponse{Proof: resBuf.Bytes(), Err: resp.Err.Error()}, nil
}