This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
processor.go
331 lines (293 loc) · 10.6 KB
/
processor.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Package presentproof is Aries protocol processor for present proof protocol.
package presentproof
import (
"encoding/gob"
"strconv"
"github.com/findy-network/findy-agent/agent/comm"
"github.com/findy-network/findy-agent/agent/didcomm"
"github.com/findy-network/findy-agent/agent/pltype"
"github.com/findy-network/findy-agent/agent/prot"
"github.com/findy-network/findy-agent/agent/psm"
"github.com/findy-network/findy-agent/agent/utils"
"github.com/findy-network/findy-agent/protocol/presentproof/data"
"github.com/findy-network/findy-agent/protocol/presentproof/prover"
"github.com/findy-network/findy-agent/protocol/presentproof/verifier"
"github.com/findy-network/findy-agent/std/presentproof"
"github.com/findy-network/findy-common-go/dto"
pb "github.com/findy-network/findy-common-go/grpc/agency/v1"
"github.com/findy-network/findy-wrapper-go/anoncreds"
"github.com/golang/glog"
"github.com/lainio/err2"
"github.com/lainio/err2/assert"
"github.com/lainio/err2/try"
)
type taskPresentProof struct {
comm.TaskBase
Comment string
ProofAttrs []didcomm.ProofAttribute
ProofPredicates []didcomm.ProofPredicate
}
type continuatorFunc func(ca comm.Receiver, im didcomm.Msg)
var presentProofProcessor = comm.ProtProc{
Creator: createPresentProofTask,
Starter: startProofProtocol,
Continuator: continueProtocol,
Handlers: map[string]comm.HandlerFunc{
pltype.HandlerPresentProofPropose: verifier.HandleProposePresentation,
pltype.HandlerPresentProofRequest: prover.HandleRequestPresentation,
pltype.HandlerPresentProofPresentation: verifier.HandlePresentation,
pltype.HandlerPresentProofACK: handleProofACK,
pltype.HandlerPresentProofNACK: handleProofNACK,
},
FillStatus: fillPresentProofStatus,
}
func init() {
gob.Register(&taskPresentProof{})
prot.AddCreator(pltype.ProtocolPresentProof, presentProofProcessor)
prot.AddStarter(pltype.CAProofPropose, presentProofProcessor)
prot.AddStarter(pltype.CAProofRequest, presentProofProcessor)
prot.AddContinuator(pltype.CAContinuePresentProofProtocol, presentProofProcessor)
prot.AddStatusProvider(pltype.ProtocolPresentProof, presentProofProcessor)
comm.Proc.Add(pltype.ProtocolPresentProof, presentProofProcessor)
}
func createPresentProofTask(header *comm.TaskHeader, protocol *pb.Protocol) (t comm.Task, err error) {
defer err2.Handle(&err, "createIssueCredentialTask")
var proofAttrs []didcomm.ProofAttribute
var proofPredicates []didcomm.ProofPredicate
if protocol != nil {
proof := protocol.GetPresentProof()
assert.That(proof != nil, "present proof data missing")
assert.That(
protocol.GetRole() == pb.Protocol_INITIATOR || protocol.GetRole() == pb.Protocol_ADDRESSEE,
"role is needed for proof protocol")
// attributes - mandatory
if proof.GetAttributesJSON() != "" {
dto.FromJSONStr(proof.GetAttributesJSON(), &proofAttrs)
glog.V(3).Infoln("set proof attrs from json:", proof.GetAttributesJSON())
} else {
assert.That(proof.GetAttributes() != nil, "present proof attributes data missing")
proofAttrs = make([]didcomm.ProofAttribute, len(proof.GetAttributes().GetAttributes()))
for i, attribute := range proof.GetAttributes().GetAttributes() {
proofAttrs[i] = didcomm.ProofAttribute{
ID: attribute.ID,
Name: attribute.Name,
CredDefID: attribute.CredDefID,
}
}
glog.V(3).Infoln("set proof from attrs")
}
// predicates - optional
if proof.GetPredicatesJSON() != "" {
dto.FromJSONStr(proof.GetPredicatesJSON(), &proofPredicates)
glog.V(3).Infoln("set proof predicates from json:", proof.GetPredicatesJSON())
} else if proof.GetPredicates() != nil {
proofPredicates = make([]didcomm.ProofPredicate, len(proof.GetPredicates().GetPredicates()))
for i, predicate := range proof.GetPredicates().GetPredicates() {
proofPredicates[i] = didcomm.ProofPredicate{
ID: predicate.ID,
Name: predicate.Name,
PType: predicate.PType,
PValue: predicate.PValue,
}
}
glog.V(3).Infoln("set proof from predicates")
}
glog.V(1).Infof(
"Create task for PresentProof with connection id %s, role %s",
header.ConnID,
protocol.GetRole().String(),
)
}
return &taskPresentProof{
TaskBase: comm.TaskBase{TaskHeader: *header},
ProofAttrs: proofAttrs,
ProofPredicates: proofPredicates,
}, nil
}
func generateProofRequest(proofTask *taskPresentProof) *anoncreds.ProofRequest {
reqAttrs := make(map[string]anoncreds.AttrInfo)
for index, attr := range proofTask.ProofAttrs {
restrictions := make([]anoncreds.Filter, 0)
if attr.CredDefID != "" {
restrictions = append(restrictions, anoncreds.Filter{CredDefID: attr.CredDefID})
}
id := "attr_referent_" + strconv.Itoa(index+1)
if attr.ID != "" {
id = attr.ID
}
reqAttrs[id] = anoncreds.AttrInfo{
Name: attr.Name,
Restrictions: restrictions,
}
}
reqPredicates := make(map[string]anoncreds.PredicateInfo)
if proofTask.ProofPredicates != nil {
for index, predicate := range proofTask.ProofPredicates {
// TODO: restrictions
id := "predicate_" + strconv.Itoa(index+1)
if predicate.ID != "" {
id = predicate.ID
}
reqPredicates[id] = anoncreds.PredicateInfo{
Name: predicate.Name,
PType: predicate.PType,
PValue: int(predicate.PValue),
}
}
}
return &anoncreds.ProofRequest{
Name: "ProofReq",
Version: "0.1",
Nonce: utils.NewNonceStr(),
RequestedAttributes: reqAttrs,
RequestedPredicates: reqPredicates,
}
}
func startProofProtocol(ca comm.Receiver, t comm.Task) {
defer err2.Catch()
proofTask, ok := t.(*taskPresentProof)
assert.That(ok)
switch t.Type() {
case pltype.CAProofPropose: // ----- prover will start -----
try.To(prot.StartPSM(prot.Initial{
SendNext: pltype.PresentProofPropose,
WaitingNext: pltype.PresentProofRequest,
Ca: ca,
T: t,
Setup: func(key psm.StateKey, msg didcomm.MessageHdr) error {
// Note!! StartPSM() sends certain Task fields to other end
// as PL.Message like msg.ID, .SubMsg, .Info
attrs := make([]presentproof.Attribute, len(proofTask.ProofAttrs))
for i, attr := range proofTask.ProofAttrs {
attrs[i] = presentproof.Attribute{
Name: attr.Name,
CredDefID: attr.CredDefID,
}
}
pp := presentproof.NewPreviewWithAttributes(attrs)
propose := msg.FieldObj().(*presentproof.Propose)
propose.PresentationProposal = pp
propose.Comment = proofTask.Comment
rep := &data.PresentProofRep{
StateKey: key,
WeProposed: true,
}
return psm.AddRep(rep)
},
}))
case pltype.CAProofRequest: // ----- verifier will start -----
try.To(prot.StartPSM(prot.Initial{
SendNext: pltype.PresentProofRequest,
WaitingNext: pltype.PresentProofPresentation,
Ca: ca,
T: t,
Setup: func(key psm.StateKey, msg didcomm.MessageHdr) error {
// We are started by verifier aka SA, Proof Request comes
// as startup argument, no need to call SA API to get it.
// Notice that Proof Req has Nonce, we use the same one for
// the protocol. UPDATE! After use of Aries message format,
// we cannot share same Nonce with the proof and messages
// here. StartPSM() sends certain Task fields to other end
// as PL.Message
proofRequest := generateProofRequest(proofTask)
// get proof req from task came in
proofReqStr := dto.ToJSON(proofRequest)
// set proof req to outgoing request message
req := msg.FieldObj().(*presentproof.Request)
req.RequestPresentations = presentproof.NewRequestPresentation(
pltype.LibindyRequestPresentationID, []byte(proofReqStr))
// create Rep and save it for PSM to run protocol
rep := &data.PresentProofRep{
StateKey: key,
// Verifier cannot provide this..
ProofReq: proofReqStr, // .. but it gives this one.
}
return psm.AddRep(rep)
},
}))
default:
glog.Error("unsupported protocol start api type ")
}
}
func continueProtocol(ca comm.Receiver, im didcomm.Msg) {
defer err2.Catch()
assert.That(im.Thread().ID != "", "continue present proof, packet thread ID missing")
var continuators = map[string]continuatorFunc{
pltype.SAPresentProofAcceptPropose: verifier.ContinueProposePresentation,
pltype.SAPresentProofAcceptValues: verifier.ContinueHandlePresentation,
pltype.CANotifyUserAction: prover.UserActionProofPresentation,
}
key := &psm.StateKey{
DID: ca.WDID(),
Nonce: im.Thread().ID,
}
state := try.To1(psm.GetPSM(*key))
assert.That(state != nil, "continue present proof, task not found")
proofTask := state.LastState().T
continuator, ok := continuators[proofTask.UserActionType()]
if !ok {
glog.Info(string(im.JSON()))
s := "no continuator in present proof processor"
glog.Error(s)
panic(s)
}
continuator(ca, im)
}
// handleProofACK is a protocol handler func at PROVER side.
// Even the inner handler does nothing, the execution of PSM transition
// terminates the state machine which triggers the notification system to the EA
// side.
func handleProofACK(packet comm.Packet) (err error) {
return prot.ExecPSM(prot.Transition{
Packet: packet,
SendNext: pltype.Terminate,
WaitingNext: pltype.Terminate,
InOut: func(_ string, _, _ didcomm.MessageHdr) (ack bool, err error) {
defer err2.Handle(&err, "proof ACK handler")
return true, nil
},
})
}
// handleProofNACK is a protocol handler func at PROVER (for this version) side.
// Even the inner handler does nothing, the execution of PSM transition
// terminates the state machine which triggers the notification system to the EA
// side.
func handleProofNACK(packet comm.Packet) (err error) {
return prot.ExecPSM(prot.Transition{
Packet: packet,
SendNext: pltype.Terminate,
WaitingNext: pltype.Terminate,
InOut: func(_ string, _, _ didcomm.MessageHdr) (ack bool, err error) {
defer err2.Handle(&err, "proof NACK handler")
return false, nil
},
})
}
func fillPresentProofStatus(workerDID string, taskID string, ps *pb.ProtocolStatus) *pb.ProtocolStatus {
defer err2.Catch(err2.Err(func(err error) {
glog.Error("Failed to fill present proof status: ", err)
}))
assert.That(ps != nil)
status := ps
proofRep := try.To1(data.GetPresentProofRep(psm.StateKey{
DID: workerDID,
Nonce: taskID,
}))
attrs := make([]*pb.Protocol_Proof_Attribute, 0, len(proofRep.Attributes))
for _, attr := range proofRep.Attributes {
a := &pb.Protocol_Proof_Attribute{
Name: attr.Name,
CredDefID: attr.CredDefID,
Value: attr.Value,
}
attrs = append(attrs, a)
}
status.Status = &pb.ProtocolStatus_PresentProof{
PresentProof: &pb.ProtocolStatus_PresentProofStatus{
Proof: &pb.Protocol_Proof{
Attributes: attrs,
},
},
}
return status
}