This repository has been archived by the owner on May 17, 2024. It is now read-only.
generated from findy-network/findy-template-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
api_present_proof_service.go
165 lines (140 loc) · 5.83 KB
/
api_present_proof_service.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Aries Agent Test Harness Backchannel API
*
* This page documents the backchannel API the test harness uses to communicate with agents under tests. For more information checkout the [Aries Interoperability Information](http://aries-interop.info) page.
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package openapi
import (
"context"
"log"
"net/http"
"github.com/findy-network/findy-agent-backchannel/agent"
)
func getProofStatus(verifier bool, state agent.PresentProofState) PresentProofState {
res := PRESENT_PROOF_DONE
switch state {
case agent.StateProofProposal:
res = PRESENT_PROOF_PROPOSAL_RECEIVED
if !verifier {
res = PRESENT_PROOF_PROPOSAL_SENT
}
case agent.StateProofRequest:
res = PRESENT_PROOF_REQUEST_SENT
if !verifier {
res = PRESENT_PROOF_REQUEST_RECEIVED
}
case agent.StateProofPresentation:
res = PRESENT_PROOF_PRESENTATION_RECEIVED
if !verifier {
res = PRESENT_PROOF_PRESENTATION_SENT
}
case agent.StateProofDone:
res = PRESENT_PROOF_DONE
}
return res
}
// PresentProofApiService is a service that implements the logic for the PresentProofApiServicer
// This service should implement the business logic for every endpoint for the PresentProofApi API.
// Include any external packages or services that will be required by this service.
type PresentProofApiService struct {
a *agent.Agent
}
// NewPresentProofApiService creates a default api service
func NewPresentProofApiService(a *agent.Agent) PresentProofApiServicer {
return &PresentProofApiService{a: a}
}
// PresentProofGetByThreadId - Get presentation exchange record by thread id
func (s *PresentProofApiService) PresentProofGetByThreadId(ctx context.Context, presentationExchangeThreadId string) (ImplResponse, error) {
verifier, state, err := s.a.GetProof(presentationExchangeThreadId)
if err != nil {
return Response(http.StatusNotFound, nil), err
}
res := getProofStatus(verifier, state)
log.Println("PresentProofGetByThreadId Proof status ", res, presentationExchangeThreadId)
return Response(200, PresentProofOperationResponse{
State: res,
ThreadId: presentationExchangeThreadId,
}), nil
}
// PresentProofSendPresentation - Send presentation
func (s *PresentProofApiService) PresentProofSendPresentation(ctx context.Context, presentProofSendPresentationRequest PresentProofSendPresentationRequest) (ImplResponse, error) {
threadId, err := s.a.SendProofPresentation(presentProofSendPresentationRequest.Id)
if err == nil {
return Response(200, PresentProofOperationResponse{State: PRESENT_PROOF_PRESENTATION_SENT, ThreadId: threadId}), nil
}
return Response(http.StatusInternalServerError, nil), err
}
// PresentProofSendProposal - Send presentation proposal
func (s *PresentProofApiService) PresentProofSendProposal(ctx context.Context, presentProofSendProposalRequest PresentProofSendProposalRequest) (ImplResponse, error) {
connectionID := presentProofSendProposalRequest.Data.ConnectionId
proposalAttributes := presentProofSendProposalRequest.Data.PresentationProposal.Attributes
attributes := make([]*agent.ProofAttribute, 0)
for _, attr := range proposalAttributes {
attributes = append(attributes, &agent.ProofAttribute{
Name: attr.Name,
CredDefID: attr.CredDefId,
})
}
threadId, err := s.a.ProposeProof(connectionID, attributes)
if err == nil {
return Response(200, PresentProofOperationResponse{State: PRESENT_PROOF_PROPOSAL_SENT, ThreadId: threadId}), nil
}
return Response(http.StatusInternalServerError, nil), err
}
// PresentProofSendRequest - Send presentation request
func (s *PresentProofApiService) PresentProofSendRequest(ctx context.Context, presentProofSendRequestRequest PresentProofSendRequestRequest) (r ImplResponse, err error) {
connectionID := presentProofSendRequestRequest.Data.ConnectionId
proposalAttributes := presentProofSendRequestRequest.Data.PresentationRequest.ProofRequest.Data.RequestedAttributes
attributes := make([]*agent.ProofAttribute, 0)
for key, value := range proposalAttributes {
if value.Name != "" {
attributes = append(attributes, &agent.ProofAttribute{
Name: value.Name,
ID: key,
})
} else {
for _, name := range value.Names {
attributes = append(attributes, &agent.ProofAttribute{
Name: name,
ID: key,
})
}
}
}
proposalPredicates := presentProofSendRequestRequest.Data.PresentationRequest.ProofRequest.Data.RequestedPredicates
predicates := make([]*agent.ProofPredicate, 0)
for key, value := range proposalPredicates {
valueMap := value.(map[string]interface{})
predicates = append(predicates, &agent.ProofPredicate{
ID: key,
Name: valueMap["name"].(string),
PType: valueMap["p_type"].(string),
PValue: int64(valueMap["p_value"].(float64)), // TODO: check does indy support float or int
})
}
threadId, err := s.a.RequestProof(connectionID, attributes, predicates)
if err == nil {
return Response(200, PresentProofOperationResponse{State: PRESENT_PROOF_REQUEST_SENT, ThreadId: threadId}), nil
}
return Response(http.StatusInternalServerError, nil), err
}
// PresentProofVerifyPresentation - Verify presentation
func (s *PresentProofApiService) PresentProofVerifyPresentation(ctx context.Context, presentProofVerifyPresentationRequest PresentProofVerifyPresentationRequest) (ImplResponse, error) {
err := s.a.VerifyPresentation(presentProofVerifyPresentationRequest.Id)
if err != nil {
return Response(http.StatusInternalServerError, nil), err
}
verifier, state, err := s.a.GetProof(presentProofVerifyPresentationRequest.Id)
if err != nil {
return Response(http.StatusNotFound, nil), err
}
res := getProofStatus(verifier, state)
log.Println("PresentProofVerifyPresentation Proof status ", res, presentProofVerifyPresentationRequest.Id)
return Response(200, PresentProofOperationResponse{
State: res,
ThreadId: presentProofVerifyPresentationRequest.Id,
}), nil
}