Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gRPC create #19

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ require (
github.com/swaggo/swag v1.16.3
github.com/uber/h3-go/v4 v4.1.1
go.uber.org/mock v0.4.0
golang.org/x/sync v0.8.0
google.golang.org/grpc v1.65.0
google.golang.org/protobuf v1.34.2
)
Expand Down
58 changes: 22 additions & 36 deletions internal/controllers/rpc/vinvc_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,42 @@ import (
"context"
"encoding/json"
"fmt"
"sync"

"github.com/DIMO-Network/attestation-api/pkg/grpc"
"github.com/DIMO-Network/attestation-api/pkg/verifiable"
"golang.org/x/sync/errgroup"
)

type Server struct {
ctrl vinCtrl
repo vinRepo
}

type vinCtrl interface {
createVIN(ctx context.Context, tokenId uint32, force bool) error
GetOrCreateVC(ctx context.Context, tokenID uint32, force bool) error
}

func (s *Server) BatchCreateVINVC(ctx context.Context, req *grpc.BatchCreateVINVCRequest) (*grpc.BatchCreateVINVCResponse, error) {
group, egCtx := errgroup.WithContext(ctx)
group.SetLimit(25)
resp := grpc.BatchCreateVINVCResponse{}
var mtx sync.Mutex
for _, tokenId := range req.GetTokenIds() {
group.Go(func() error {
result := &grpc.VINVCResult{
TokenId: tokenId,
}

mtx.Lock()
resp.Results = append(resp.Results, result)
mtx.Unlock()

err := s.ctrl.createVIN(egCtx, tokenId, req.GetForce())
if err != nil {
errStr := fmt.Sprintf("failed to create VC: %s", err.Error())
result.Error = &errStr
return nil
}
var vc verifiable.VerificationControlDocument
rawVC, err := json.Marshal(vc)
if err != nil {
errStr := fmt.Sprintf("failed to marashal VC: %s", err.Error())
result.Error = &errStr
return nil
}
result.RawVC = string(rawVC)
return nil
})
type vinRepo interface {
GetLatestVINVC(ctx context.Context, vehicleTokenID uint32) (*verifiable.Credential, error)
}

func (s *Server) EnsureVinVc(ctx context.Context, req *grpc.EnsureVinVcRequest) (*grpc.EnsureVinVcResponse, error) {
err := s.ctrl.GetOrCreateVC(ctx, req.TokenId, req.Force)
if err != nil {
return nil, err
}
err := group.Wait()
return &grpc.EnsureVinVcResponse{}, nil
}

func (s *Server) GetVinVcLatest(ctx context.Context, req *grpc.GetLatestVinVcRequest) (*grpc.GetLatestVinVcResponse, error) {
cred, err := s.repo.GetLatestVINVC(ctx, req.TokenId)
if err != nil {
return nil, err
}
return &resp, nil

raw, err := json.Marshal(cred)
if err != nil {
return nil, fmt.Errorf("failed to marshal VIN VC: %w", err)
}

return &grpc.GetLatestVinVcResponse{RawVc: string(raw)}, nil
}
Loading
Loading