forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
582 lines (485 loc) · 13.6 KB
/
params.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package issuecredential
import (
"encoding/json"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/decorator"
)
// ProposeCredentialParams holds parameters for a credential proposal message.
type ProposeCredentialParams struct {
Type string
ID string
Comment string
Attachments []decorator.GenericAttachment
CredentialProposal PreviewCredential
Formats []Format
GoalCode string
CredentialPreview interface{}
InvitationID string
}
// AsV2 translates this credential proposal into an issue credential 2.0 proposal message.
func (p *ProposeCredentialParams) AsV2() *ProposeCredentialV2 {
return &ProposeCredentialV2{
Type: p.Type,
Comment: p.Comment,
CredentialProposal: p.CredentialProposal,
Formats: p.Formats,
FiltersAttach: decorator.GenericAttachmentsToV1(p.Attachments),
InvitationID: p.InvitationID,
}
}
// AsV3 translates this credential proposal into an issue credential 3.0 proposal message.
func (p *ProposeCredentialParams) AsV3() *ProposeCredentialV3 {
return &ProposeCredentialV3{
Type: p.Type,
ID: p.ID,
InvitationID: p.InvitationID,
Body: ProposeCredentialV3Body{
GoalCode: p.GoalCode,
Comment: p.Comment,
CredentialPreview: p.CredentialPreview,
},
Attachments: decorator.GenericAttachmentsToV2(p.Attachments),
}
}
// FromV2 initializes this credential proposal from an issue credential 2.0 proposal.
func (p *ProposeCredentialParams) FromV2(v2 *ProposeCredentialV2) {
p.Type = v2.Type
p.ID = ""
p.Comment = v2.Comment
p.Attachments = decorator.V1AttachmentsToGeneric(v2.FiltersAttach)
p.CredentialProposal = v2.CredentialProposal
p.Formats = v2.Formats
p.InvitationID = v2.InvitationID
p.GoalCode = ""
p.CredentialPreview = nil
}
// FromV3 initializes this credential proposal from an issue credential 3.0 proposal.
func (p *ProposeCredentialParams) FromV3(v3 *ProposeCredentialV3) {
p.Type = v3.Type
p.ID = v3.ID
p.Comment = v3.Body.Comment
p.Attachments = decorator.V2AttachmentsToGeneric(v3.Attachments)
p.CredentialProposal = PreviewCredential{}
p.Formats = nil
p.GoalCode = v3.Body.GoalCode
p.CredentialPreview = v3.Body.CredentialPreview
p.InvitationID = v3.InvitationID
}
// UnmarshalJSON implements json.Unmarshaler.
func (p *ProposeCredentialParams) UnmarshalJSON(b []byte) error {
raw := rawPropose{}
err := json.Unmarshal(b, &raw)
if err != nil {
return err
}
if raw.isV3() {
p.FromV3(&raw.ProposeCredentialV3)
} else {
p.FromV2(&raw.ProposeCredentialV2)
}
return nil
}
// FromDIDCommMsgMap implements service.MsgMapDecoder.
func (p *ProposeCredentialParams) FromDIDCommMsgMap(msgMap service.DIDCommMsgMap) error {
isV2, _ := service.IsDIDCommV2(&msgMap) // nolint:errcheck
if isV2 {
proposeV3 := &ProposeCredentialV3{}
err := msgMap.Decode(proposeV3)
if err != nil {
return err
}
p.FromV3(proposeV3)
} else {
proposeV2 := &ProposeCredentialV2{}
err := msgMap.Decode(proposeV2)
if err != nil {
return err
}
p.FromV2(proposeV2)
}
return nil
}
type rawPropose struct {
ProposeCredentialV2
ProposeCredentialV3
}
func (p *ProposeCredentialV2) notEmpty() bool {
return p.Type != "" ||
p.Comment != "" ||
p.CredentialProposal.Type != "" ||
len(p.CredentialProposal.Attributes) != 0 ||
len(p.Formats) != 0 ||
len(p.FiltersAttach) != 0
}
func (p *ProposeCredentialV3) notEmpty() bool {
return p.ID != "" ||
p.Type != "" ||
p.Body.GoalCode != "" ||
p.Body.Comment != "" ||
p.Body.CredentialPreview != nil ||
len(p.Attachments) != 0
}
func (r *rawPropose) isV3() bool {
if r.ProposeCredentialV2.notEmpty() {
return false
}
if r.ProposeCredentialV3.notEmpty() {
return true
}
return false
}
// OfferCredentialParams holds parameters for a credential offer message.
type OfferCredentialParams struct {
Type string
ID string
Comment string
Attachments []decorator.GenericAttachment
Formats []Format
GoalCode string
ReplacementID string
CredentialPreview interface{}
}
// AsV2 translates this credential offer into an issue credential 2.0 offer message.
func (p *OfferCredentialParams) AsV2() *OfferCredentialV2 {
preview, ok := p.CredentialPreview.(PreviewCredential)
if !ok {
preview = PreviewCredential{}
}
return &OfferCredentialV2{
Type: p.Type,
Comment: p.Comment,
CredentialPreview: preview,
Formats: p.Formats,
OffersAttach: decorator.GenericAttachmentsToV1(p.Attachments),
}
}
// AsV3 translates this credential offer into an issue credential 3.0 offer message.
func (p *OfferCredentialParams) AsV3() *OfferCredentialV3 {
return &OfferCredentialV3{
Type: p.Type,
ID: p.ID,
Body: OfferCredentialV3Body{
GoalCode: p.GoalCode,
Comment: p.Comment,
ReplacementID: p.ReplacementID,
CredentialPreview: p.CredentialPreview,
},
Attachments: decorator.GenericAttachmentsToV2(p.Attachments),
}
}
// FromV2 initializes this credential offer from an issue credential 2.0 offer message.
func (p *OfferCredentialParams) FromV2(v2 *OfferCredentialV2) {
p.Type = v2.Type
p.ID = ""
p.Comment = v2.Comment
p.Attachments = decorator.V1AttachmentsToGeneric(v2.OffersAttach)
p.CredentialPreview = v2.CredentialPreview
p.Formats = v2.Formats
p.GoalCode = ""
p.ReplacementID = ""
}
// FromV3 initializes this credential offer from an issue credential 3.0 offer message.
func (p *OfferCredentialParams) FromV3(v3 *OfferCredentialV3) {
p.Type = v3.Type
p.ID = v3.ID
p.Comment = v3.Body.Comment
p.Attachments = decorator.V2AttachmentsToGeneric(v3.Attachments)
p.CredentialPreview = v3.Body.CredentialPreview
p.Formats = nil
p.GoalCode = v3.Body.GoalCode
p.ReplacementID = v3.Body.ReplacementID
}
// UnmarshalJSON implements json.Unmarshaler.
func (p *OfferCredentialParams) UnmarshalJSON(b []byte) error {
raw := rawOffer{}
err := json.Unmarshal(b, &raw)
if err != nil {
return err
}
if raw.isV3() {
p.FromV3(&raw.OfferCredentialV3)
} else {
p.FromV2(&raw.OfferCredentialV2)
}
return nil
}
// FromDIDCommMsgMap implements service.MsgMapDecoder.
func (p *OfferCredentialParams) FromDIDCommMsgMap(msgMap service.DIDCommMsgMap) error {
isV2, _ := service.IsDIDCommV2(&msgMap) // nolint:errcheck
if isV2 {
msgV3 := &OfferCredentialV3{}
err := msgMap.Decode(msgV3)
if err != nil {
return err
}
p.FromV3(msgV3)
} else {
msgV2 := &OfferCredentialV2{}
err := msgMap.Decode(msgV2)
if err != nil {
return err
}
p.FromV2(msgV2)
}
return nil
}
type rawOffer struct {
OfferCredentialV2
OfferCredentialV3
}
func (o *OfferCredentialV2) notEmpty() bool {
return o.Type != "" ||
o.Comment != "" ||
o.CredentialPreview.Type != "" ||
len(o.Formats) != 0 ||
len(o.OffersAttach) != 0 ||
len(o.CredentialPreview.Attributes) != 0
}
func (o *OfferCredentialV3) notEmpty() bool {
return o.ID != "" ||
o.Type != "" ||
o.Body.Comment != "" ||
o.Body.GoalCode != "" ||
o.Body.ReplacementID != "" ||
o.Body.CredentialPreview != nil ||
len(o.Attachments) != 0
}
func (r *rawOffer) isV3() bool {
if r.OfferCredentialV2.notEmpty() {
return false
}
if r.OfferCredentialV3.notEmpty() {
return true
}
return false
}
// RequestCredentialParams holds parameters for a credential request message.
type RequestCredentialParams struct {
ID string
Type string
Comment string
Formats []Format
GoalCode string
Attachments []decorator.GenericAttachment
}
// AsV2 translates this credential request into an issue credential 2.0 request message.
func (p *RequestCredentialParams) AsV2() *RequestCredentialV2 {
return &RequestCredentialV2{
Type: p.Type,
Comment: p.Comment,
Formats: p.Formats,
RequestsAttach: decorator.GenericAttachmentsToV1(p.Attachments),
}
}
// AsV3 translates this credential request into an issue credential 3.0 request message.
func (p *RequestCredentialParams) AsV3() *RequestCredentialV3 {
return &RequestCredentialV3{
ID: p.ID,
Type: p.Type,
Body: RequestCredentialV3Body{
Comment: p.Comment,
GoalCode: p.GoalCode,
},
Attachments: decorator.GenericAttachmentsToV2(p.Attachments),
}
}
// FromV2 initializes this credential request from an issue credential 2.0 request message.
func (p *RequestCredentialParams) FromV2(v2 *RequestCredentialV2) {
p.ID = ""
p.Type = v2.Type
p.Comment = v2.Comment
p.Formats = v2.Formats
p.GoalCode = ""
p.Attachments = decorator.V1AttachmentsToGeneric(v2.RequestsAttach)
}
// FromV3 initialized this credential request from an issue credential 3.0 request message.
func (p *RequestCredentialParams) FromV3(v3 *RequestCredentialV3) {
p.ID = v3.ID
p.Type = v3.Type
p.Comment = v3.Body.Comment
p.Formats = nil
p.GoalCode = v3.Body.GoalCode
p.Attachments = decorator.V2AttachmentsToGeneric(v3.Attachments)
}
type rawRequest struct {
RequestCredentialV2
RequestCredentialV3
}
func (r *RequestCredentialV2) notEmpty() bool {
return r.Type != "" ||
r.Comment != "" ||
len(r.Formats) != 0 ||
len(r.RequestsAttach) != 0
}
func (r *RequestCredentialV3) notEmpty() bool {
return r.ID != "" ||
r.Type != "" ||
r.Body.GoalCode != "" ||
r.Body.Comment != "" ||
len(r.Attachments) != 0
}
func (r *rawRequest) isV3() bool {
if r.RequestCredentialV2.notEmpty() {
return false
}
if r.RequestCredentialV3.notEmpty() {
return true
}
return false
}
// UnmarshalJSON implements json.Unmarshaler.
func (p *RequestCredentialParams) UnmarshalJSON(b []byte) error {
raw := rawRequest{}
err := json.Unmarshal(b, &raw)
if err != nil {
return err
}
if raw.isV3() {
p.FromV3(&raw.RequestCredentialV3)
} else {
p.FromV2(&raw.RequestCredentialV2)
}
return nil
}
// FromDIDCommMsgMap implements service.MsgMapDecoder.
func (p *RequestCredentialParams) FromDIDCommMsgMap(msgMap service.DIDCommMsgMap) error {
isV2, _ := service.IsDIDCommV2(&msgMap) // nolint:errcheck
if isV2 {
msgV3 := &RequestCredentialV3{}
err := msgMap.Decode(msgV3)
if err != nil {
return err
}
p.FromV3(msgV3)
} else {
msgV2 := &RequestCredentialV2{}
err := msgMap.Decode(msgV2)
if err != nil {
return err
}
p.FromV2(msgV2)
}
return nil
}
// IssueCredentialParams holds parameters for a credential issuance message.
type IssueCredentialParams struct { // nolint: golint
Type string
ID string
Comment string
Formats []Format
Attachments []decorator.GenericAttachment
GoalCode string
ReplacementID string
WebRedirect *decorator.WebRedirect
}
// AsV2 translates this credential issuance into an issue credential 2.0 issuance message.
func (p *IssueCredentialParams) AsV2() *IssueCredentialV2 {
return &IssueCredentialV2{
Type: p.Type,
Comment: p.Comment,
Formats: p.Formats,
CredentialsAttach: decorator.GenericAttachmentsToV1(p.Attachments),
WebRedirect: p.WebRedirect,
}
}
// AsV3 translates this credential issuance into an issue credential 3.0 issuance message.
func (p *IssueCredentialParams) AsV3() *IssueCredentialV3 {
return &IssueCredentialV3{
ID: p.ID,
Type: p.Type,
Body: IssueCredentialV3Body{
GoalCode: p.GoalCode,
ReplacementID: p.ReplacementID,
Comment: p.Comment,
},
Attachments: decorator.GenericAttachmentsToV2(p.Attachments),
WebRedirect: p.WebRedirect,
}
}
// FromV2 initializes this credential issuance from an issue credential 2.0 issuance message.
func (p *IssueCredentialParams) FromV2(v2 *IssueCredentialV2) {
p.ID = ""
p.Type = v2.Type
p.Comment = v2.Comment
p.Formats = v2.Formats
p.GoalCode = ""
p.ReplacementID = ""
p.Attachments = decorator.V1AttachmentsToGeneric(v2.CredentialsAttach)
p.WebRedirect = v2.WebRedirect
}
// FromV3 initialized this credential issuance from an issue credential 3.0 issuance message.
func (p *IssueCredentialParams) FromV3(v3 *IssueCredentialV3) {
p.ID = v3.ID
p.Type = v3.Type
p.Comment = v3.Body.Comment
p.Formats = nil
p.GoalCode = v3.Body.GoalCode
p.ReplacementID = v3.Body.ReplacementID
p.Attachments = decorator.V2AttachmentsToGeneric(v3.Attachments)
p.WebRedirect = v3.WebRedirect
}
type rawIssuance struct {
IssueCredentialV2
IssueCredentialV3
}
func (o *IssueCredentialV2) notEmpty() bool {
return o.Type != "" ||
o.Comment != "" ||
len(o.Formats) != 0 ||
len(o.CredentialsAttach) != 0
}
func (o *IssueCredentialV3) notEmpty() bool {
return o.ID != "" ||
o.Type != "" ||
o.Body.Comment != "" ||
o.Body.GoalCode != "" ||
o.Body.ReplacementID != "" ||
len(o.Attachments) != 0
}
func (r *rawIssuance) isV3() bool {
if r.IssueCredentialV2.notEmpty() {
return false
}
if r.IssueCredentialV3.notEmpty() {
return true
}
return false
}
// UnmarshalJSON implements json.Unmarshaler.
func (p *IssueCredentialParams) UnmarshalJSON(b []byte) error {
raw := rawIssuance{}
err := json.Unmarshal(b, &raw)
if err != nil {
return err
}
if raw.isV3() {
p.FromV3(&raw.IssueCredentialV3)
} else {
p.FromV2(&raw.IssueCredentialV2)
}
return nil
}
// FromDIDCommMsgMap implements service.MsgMapDecoder.
func (p *IssueCredentialParams) FromDIDCommMsgMap(msgMap service.DIDCommMsgMap) error {
isV2, _ := service.IsDIDCommV2(&msgMap) // nolint:errcheck
if isV2 {
msgV3 := &IssueCredentialV3{}
err := msgMap.Decode(msgV3)
if err != nil {
return err
}
p.FromV3(msgV3)
} else {
msgV2 := &IssueCredentialV2{}
err := msgMap.Decode(msgV2)
if err != nil {
return err
}
p.FromV2(msgV2)
}
return nil
}