This repository has been archived by the owner on Mar 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
/
event.go
718 lines (608 loc) · 22.4 KB
/
event.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package rfc0593
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/google/uuid"
"github.com/hyperledger/aries-framework-go/pkg/crypto"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/common/service"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/decorator"
"github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/issuecredential"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/jsonld"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/signer"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/suite"
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
"github.com/hyperledger/aries-framework-go/pkg/kms"
"github.com/hyperledger/aries-framework-go/pkg/vdr/fingerprint"
"github.com/hyperledger/aries-framework-go/spi/storage"
)
const (
// ProofVCDetailFormat is the attachment format used in the proposal, offer, and request message attachments.
ProofVCDetailFormat = "aries/ld-proof-vc-detail@v1.0"
// ProofVCFormat is the attachment format used in the issue-credential message attachment.
ProofVCFormat = "aries/ld-proof-vc@v1.0"
// StoreName is the name of the transient store used by AutoExecute.
StoreName = "RFC0593TransientStore"
mediaTypeJSON = "application/json"
mediaTypeJSONLD = "application/ld+json"
)
// ErrRFC0593NotApplicable indicates RFC0593 does not apply to the message being handled because
// it does not contain an attachment with the proof format identifiers.
//
// See also: ProofVCDetailFormat, ProofVCFormat.
var ErrRFC0593NotApplicable = errors.New("RFC0593 is not applicable")
// CredentialSpec is the attachment payload in messages conforming to the RFC0593 format.
type CredentialSpec struct {
Template json.RawMessage `json:"credential"`
Options *CredentialSpecOptions `json:"options"`
}
// CredentialSpecOptions are the options for issuance of the credential.
// TODO support CredentialStatus.
type CredentialSpecOptions struct {
ProofPurpose string `json:"proofPurpose"`
Created string `json:"created"`
Domain string `json:"domain"`
Challenge string `json:"challenge"`
Status *CredentialStatus `json:"credentialStatus"`
ProofType string `json:"proofType"`
}
// CredentialStatus is the requested status for the credential.
type CredentialStatus struct {
Type string `json:"type"`
}
// AutoExecute will automatically execute the issue-credential V2 protocol using ReplayProposal, ReplayOffer, and
// IssueCredential by handling the associated actions if they contain RFC0593 attachments.
// Other actions are passed through to 'next'.
//
// Usage:
// client := issuecredential.Client = ...
// events = make(chan service.DIDCommAction)
// err := client.RegisterActionEvent(events)
// if err != nil {
// panic(err)
// }
// var p Provider = ...
// next := make(chan service.DIDCommAction)
// go AutoExecute(p, next)(events)
// for event := range next {
// // handle events from issue-credential that do not conform to RFC0593
// }
//
// Note: use the protocol Middleware if the protocol needs to be started with a request-credential message.
//
// See also: service.AutoExecuteActionEvent.
func AutoExecute(p Provider, next chan service.DIDCommAction) func(chan service.DIDCommAction) { // nolint:funlen
return func(events chan service.DIDCommAction) {
// TODO make AutoExecute return an error if the store cannot be opened?
db, storeErr := p.ProtocolStateStorageProvider().OpenStore(StoreName)
for event := range events {
if storeErr != nil {
event.Stop(fmt.Errorf("rfc0593: failed to open transient store: %w", storeErr))
continue
}
var (
arg interface{}
options *CredentialSpecOptions
err error
)
switch event.Message.Type() {
case issuecredential.ProposeCredentialMsgTypeV2:
arg, options, err = ReplayProposal(p, event.Message)
err = saveOptionsIfNoError(err, db, event.Message, options)
case issuecredential.OfferCredentialMsgTypeV2:
arg, options, err = ReplayOffer(p, event.Message)
err = saveOptionsIfNoError(err, db, event.Message, options)
case issuecredential.RequestCredentialMsgTypeV2:
arg, options, err = IssueCredential(p, event.Message)
err = saveOptionsIfNoError(err, db, event.Message, options)
case issuecredential.IssueCredentialMsgTypeV2:
// TODO credential issued to us. We have middleware that automatically saves the credentials.
// Should this package ensure it's saved?
// Should we ensure issued VC is up to spec?
options, err = fetchCredentialSpecOptions(db, event.Message)
if err != nil {
err = fmt.Errorf("failed to fetch credential spec options to validate credential: %w", err)
break
}
arg, err = VerifyCredential(p, options, uuid.New().String(), event.Message)
err = deleteOptionsIfNoError(err, db, event.Message)
default:
next <- event
continue
}
if errors.Is(err, ErrRFC0593NotApplicable) {
next <- event
continue
}
if err != nil {
event.Stop(fmt.Errorf("rfc0593: %w", err))
continue
}
event.Continue(arg)
}
}
}
// ReplayProposal replays the inbound proposed CredentialSpec as an outbound offer that can be sent back to the
// original sender.
//
// Usage:
// var p JSONLDDocumentLoaderProvider = ...
// client := issuecredential.Client = ...
// var events chan service.DIDCommAction = ...
// err := client.RegisterActionEvent(events)
// if err != nil {
// panic(err)
// }
// for event := range events {
// if event.Message.Type() == issuecredential.ProposeCredentialMsgType {
// arg, options, err := ReplayProposal(p, event.Message)
// if errors.Is(err, ErrRFC0593NotApplicable) {
// // inspect and handle the event yourself
// arg, err = handleEvent(event)
// }
//
// if err != nil {
// event.Stop(err)
// }
//
// // inspect options
//
// event.Continue(arg)
// }
// }
func ReplayProposal(p JSONLDDocumentLoaderProvider,
msg service.DIDCommMsg) (interface{}, *CredentialSpecOptions, error) {
proposal := &issuecredential.ProposeCredentialV2{}
err := msg.Decode(proposal)
if err != nil {
return nil, nil, fmt.Errorf("failed to decode msg type %s: %w", msg.Type(), err)
}
payload, err := GetCredentialSpec(p, proposal.Formats, proposal.FiltersAttach)
if err != nil {
return nil, nil, fmt.Errorf("failed to extract payload for msg type %s: %w", msg.Type(), err)
}
attachID := uuid.New().String()
return issuecredential.WithOfferCredentialV2(&issuecredential.OfferCredentialV2{
Type: issuecredential.OfferCredentialMsgTypeV2,
Comment: fmt.Sprintf("response to msg id: %s", msg.ID()),
Formats: []issuecredential.Format{{
AttachID: attachID,
Format: ProofVCDetailFormat,
}},
OffersAttach: []decorator.Attachment{{
ID: attachID,
MimeType: mediaTypeJSON,
Data: decorator.AttachmentData{
JSON: payload,
},
}},
}), payload.Options, nil
}
// ReplayOffer replays the inbound offered CredentialSpec as an outbound request that can be sent back to the
// original sender.
//
// Usage:
// var p JSONLDDocumentLoaderProvider = ...
// client := issuecredential.Client = ...
// var events chan service.DIDCommAction = ...
// err := client.RegisterActionEvent(events)
// if err != nil {
// panic(err)
// }
// for event := range events {
// if event.Message.Type() == issuecredential.OfferCredentialMsgType {
// arg, options, err := ReplayOffer(p, event.Message)
// if errors.Is(err, ErrRFC0593NotApplicable) {
// // inspect and handle the event yourself
// arg, err = handleEvent(event)
// }
//
// if err != nil {
// event.Stop(err)
// }
//
// // inspect options
//
// event.Continue(arg)
// }
// }
func ReplayOffer(p JSONLDDocumentLoaderProvider, msg service.DIDCommMsg) (interface{}, *CredentialSpecOptions, error) {
offer := &issuecredential.OfferCredentialV2{}
err := msg.Decode(offer)
if err != nil {
return nil, nil, fmt.Errorf("failed to decode msg type %s: %w", msg.Type(), err)
}
payload, err := GetCredentialSpec(p, offer.Formats, offer.OffersAttach)
if err != nil {
return nil, nil, fmt.Errorf("failed to extract payoad for msg type %s: %w", msg.Type(), err)
}
attachID := uuid.New().String()
return issuecredential.WithRequestCredentialV2(&issuecredential.RequestCredentialV2{
Type: issuecredential.RequestCredentialMsgTypeV2,
Comment: fmt.Sprintf("response to msg id: %s", msg.ID()),
Formats: []issuecredential.Format{{
AttachID: attachID,
Format: ProofVCDetailFormat,
}},
RequestsAttach: []decorator.Attachment{{
ID: attachID,
MimeType: mediaTypeJSON,
Data: decorator.AttachmentData{
JSON: payload,
},
}},
}), payload.Options, nil
}
// IssueCredential attaches an LD proof to the template VC in the inbound request message and attaches the
// verifiable credential to an outbound issue-credential message.
//
// Usage:
// var p Provider = ...
// client := issuecredential.Client = ...
// var events chan service.DIDCommAction = ...
// err := client.RegisterActionEvent(events)
// if err != nil {
// panic(err)
// }
// for event := range events {
// if event.Message.Type() == issuecredential.RequestCredentialMsgType {
// arg, options, err := IssueCredential(p, event.Message)
// if errors.Is(err, ErrRFC0593NotApplicable) {
// // inspect and handle the event yourself
// arg, err = handleEvent(event)
// }
//
// if err != nil {
// event.Stop(err)
// }
//
// // inspect options
//
// event.Continue(arg)
// }
// }
func IssueCredential(p Provider, msg service.DIDCommMsg) (interface{}, *CredentialSpecOptions, error) {
request := &issuecredential.RequestCredentialV2{}
err := msg.Decode(request)
if err != nil {
return nil, nil, fmt.Errorf("failed to decode msg type %s: %w", msg.Type(), err)
}
payload, err := GetCredentialSpec(p, request.Formats, request.RequestsAttach)
if err != nil {
return nil, nil, fmt.Errorf("failed to get payload for msg type %s: %w", msg.Type(), err)
}
ic, err := CreateIssueCredentialMsg(p, payload)
if err != nil {
return nil, nil, fmt.Errorf("failed to create issue-credential msg: %w", err)
}
ic.Comment = fmt.Sprintf("response to request with id %s", msg.ID())
return issuecredential.WithIssueCredentialV2(ic), payload.Options, nil
}
// CreateIssueCredentialMsg creates an issue-credential message using the credential spec.
func CreateIssueCredentialMsg(p Provider, spec *CredentialSpec) (*issuecredential.IssueCredentialV2, error) {
vc, err := verifiable.ParseCredential(
spec.Template,
verifiable.WithDisabledProofCheck(), // no proof is expected in this credential
verifiable.WithJSONLDDocumentLoader(p.JSONLDDocumentLoader()),
)
if err != nil {
return nil, fmt.Errorf("failed to parse vc: %w", err)
}
ctx, err := ldProofContext(p, spec.Options)
if err != nil {
return nil, fmt.Errorf("failed to determine the LD context required to add a proof: %w", err)
}
err = vc.AddLinkedDataProof(ctx, jsonld.WithDocumentLoader(p.JSONLDDocumentLoader()))
if err != nil {
return nil, fmt.Errorf("failed to add LD proof: %w", err)
}
attachID := uuid.New().String()
return &issuecredential.IssueCredentialV2{
Type: issuecredential.IssueCredentialMsgTypeV2,
Formats: []issuecredential.Format{{
AttachID: attachID,
Format: ProofVCFormat,
}},
CredentialsAttach: []decorator.Attachment{{
ID: attachID,
MimeType: mediaTypeJSONLD,
Data: decorator.AttachmentData{
JSON: vc,
},
}},
}, nil
}
// VerifyCredential verifies the credential received in an RFC0593 issue-credential message.
//
// The credential is validated to ensure it complies with the given CredentialSpecOptions.
//
// The credential will then be saved with the given name.
//
// Usage:
// var p Provider = ...
// client := issuecredential.Client = ...
// var events chan service.DIDCommAction = ...
// err := client.RegisterActionEvent(events)
// if err != nil {
// panic(err)
// }
// var options *CredentialSpecOptions
// for event := range events {
// switch event.Message.Type() {
// case issuecredential.OfferCredentialMsgType:
// arg, opts, err := ReplayOffer(p, event.Message)
// if err != nil {
// event.Stop(err)
// }
//
// options = opts
// event.Continue(arg)
// case issuecredential.IssueCredentialMsgType:
// arg, err := VerifyCredential(p, options, "my_vc", event.Message)
// if errors.Is(err, ErrRFC0593NotApplicable) {
// // inspect and handle the event yourself
// arg, err = handleEvent(event)
// }
//
// if err != nil {
// event.Stop(err)
// }
//
// event.Continue(arg)
// }
// }
func VerifyCredential(p Provider,
options *CredentialSpecOptions, name string, msg service.DIDCommMsg) (interface{}, error) {
issueCredential := &issuecredential.IssueCredentialV2{}
err := msg.Decode(issueCredential)
if err != nil {
return nil, fmt.Errorf("failed to decode msg type %s: %w", msg.Type(), err)
}
attachment, err := FindAttachment(ProofVCFormat, issueCredential.Formats, issueCredential.CredentialsAttach)
if err != nil {
return nil, fmt.Errorf("failed to fetch attachment with format %s: %w", ProofVCFormat, err)
}
raw, err := attachment.Data.Fetch()
if err != nil {
return nil, fmt.Errorf("failed to fetch the attachment's contents: %w", err)
}
vc, err := verifiable.ParseCredential(
raw,
verifiable.WithJSONLDDocumentLoader(p.JSONLDDocumentLoader()),
verifiable.WithPublicKeyFetcher(verifiable.NewVDRKeyResolver(p.VDRegistry()).PublicKeyFetcher()),
)
if err != nil {
return nil, fmt.Errorf("failed to parse vc: %w", err)
}
err = validateCredentialRequestVC(vc)
if err != nil {
return nil, fmt.Errorf("invalid credential: %w", err)
}
err = ValidateVCMatchesSpecOptions(vc, options)
if err != nil {
return nil, fmt.Errorf("invalid credential: %w", err)
}
return issuecredential.WithFriendlyNames(name), nil
}
func ldProofContext(p Provider, options *CredentialSpecOptions) (*verifiable.LinkedDataProofContext, error) {
now := time.Now()
ctx := &verifiable.LinkedDataProofContext{
SignatureType: options.ProofType,
Purpose: "assertionMethod",
Created: &now,
Challenge: options.Challenge,
Domain: options.Domain,
}
ss, spec, verMethod, err := signatureSuite(p, options.ProofType)
if err != nil {
return nil, fmt.Errorf("failed to init a signature suite: %w", err)
}
ctx.Suite = ss
ctx.VerificationMethod = verMethod
ctx.SignatureRepresentation = spec.SignatureRepresentation // TODO RFC does not specify representation
if options.ProofPurpose != "" {
ctx.Purpose = options.ProofPurpose
}
if options.Created != "" {
// TODO spec does not specify format for `created`
created, err := time.Parse(time.RFC3339, options.Created)
if err != nil {
return nil, fmt.Errorf("failed to parse `created`: %w", err)
}
ctx.Created = &created
}
return ctx, nil
}
func signatureSuite(p Provider, proofType string) (signer.SignatureSuite, *SignatureSuiteSpec, string, error) {
spec, supported := DefaultSignatureSuiteSpecs[proofType]
if !supported {
return nil, nil, "", fmt.Errorf("unsupported proof type: %s", proofType)
}
keyID, kh, err := p.KMS().Create(spec.KeyType)
if err != nil {
return nil, nil, "", fmt.Errorf("failed to create a new signing key: %w", err)
}
keyBytes, _, err := p.KMS().ExportPubKeyBytes(keyID)
if err != nil {
return nil, nil, "", fmt.Errorf("failed to export signing key bytes: %w", err)
}
_, verMethod := fingerprint.CreateDIDKeyByCode(spec.KeyMultiCodec, keyBytes)
suiteSigner := spec.Signer(p, kh)
return spec.Suite(suite.WithSigner(suiteSigner)), &spec, verMethod, nil
}
// GetCredentialSpec extracts the CredentialSpec from the formats and attachments.
func GetCredentialSpec(p JSONLDDocumentLoaderProvider,
formats []issuecredential.Format, attachments []decorator.Attachment) (*CredentialSpec, error) {
attachment, err := FindAttachment(ProofVCDetailFormat, formats, attachments)
if err != nil {
return nil, fmt.Errorf("failed to find attachment of type %s: %w", ProofVCDetailFormat, err)
}
payload := &CredentialSpec{}
err = unmarshalAttachmentContents(attachment, payload)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal attachment contents: %w", err)
}
err = validateCredentialRequestOptions(payload)
if err != nil {
return nil, fmt.Errorf("bad request: invalid options: %w", err)
}
vc, err := verifiable.ParseCredential(
payload.Template,
verifiable.WithDisabledProofCheck(), // no proof is expected in this credential
verifiable.WithJSONLDDocumentLoader(p.JSONLDDocumentLoader()),
)
if err != nil {
return nil, fmt.Errorf("bad request: unable to parse vc: %w", err)
}
err = validateCredentialRequestVC(vc)
if err != nil {
return nil, fmt.Errorf("bad request: invalid vc: %w", err)
}
return payload, nil
}
// FindAttachment returns the attachment corresponding to the RFC0593 format entry.
func FindAttachment(formatType string,
formats []issuecredential.Format, attachments []decorator.Attachment) (*decorator.Attachment, error) {
// TODO not documented in the RFC but the intent of having `format` and `requests~attach` be an array
// is not to enable "bulk issuance" (issuance of multiple vcs), but to requests a single credential
// using different request formats.
// TODO clarify precedence of different enabled middlewares if request has multiple attachment formats
var attachID string
for i := range formats {
if formats[i].Format == formatType {
attachID = formats[i].AttachID
break
}
}
if attachID == "" {
return nil, ErrRFC0593NotApplicable
}
for i := range attachments {
if attachments[i].ID == attachID {
return &attachments[i], nil
}
}
return nil, fmt.Errorf(
"format with attachID=%s indicates support for %s for no attachment with that ID was found",
attachID, formatType,
)
}
func unmarshalAttachmentContents(a *decorator.Attachment, v interface{}) error {
contents, err := a.Data.Fetch()
if err != nil {
return fmt.Errorf("failed to fetch attachment contents: %w", err)
}
return json.Unmarshal(contents, v)
}
// TODO this should be configurable.
func validateCredentialRequestOptions(_ *CredentialSpec) error {
// TODO validatations (eg. valid proofPurpose, created, credentialStatus, proofType)
return nil
}
// TODO this should be configurable.
func validateCredentialRequestVC(_ *verifiable.Credential) error {
// TODO validate claims in credential template
return nil
}
// ValidateVCMatchesSpecOptions ensures the vc matches the spec.
func ValidateVCMatchesSpecOptions(vc *verifiable.Credential, options *CredentialSpecOptions) error { // nolint:gocyclo
if len(vc.Proofs) == 0 {
return errors.New("vc is missing a proof")
}
// TODO which proof?
proof := vc.Proofs[0]
if !reflect.DeepEqual(options.ProofType, proof["type"]) {
return fmt.Errorf("expected proofType %s but got %s", options.ProofType, proof["type"])
}
if !reflect.DeepEqual(options.Domain, proof["domain"]) {
return fmt.Errorf("expected domain %s but got %s", options.Domain, proof["domain"])
}
if !reflect.DeepEqual(options.Challenge, proof["challenge"]) {
return fmt.Errorf("expected challenge %s but got %s", options.Challenge, proof["challenge"])
}
if options.ProofPurpose != "" && !reflect.DeepEqual(options.ProofPurpose, proof["proofPurpose"]) {
return fmt.Errorf("expected proofPurpose %s but got %s", options.ProofPurpose, proof["proofPurpose"])
}
if options.Status != nil {
if vc.Status == nil {
return fmt.Errorf("expected credentialStatus of type %s but VC does not have any", options.Status.Type)
}
if options.Status.Type != vc.Status.Type {
return fmt.Errorf("expected credentialStatus of type %s but got %s", options.Status.Type, vc.Status.Type)
}
}
if options.Created == "" {
return fmt.Errorf("missing 'created' on proof") // RFC: default current system time it unspecified in options
}
if options.Created != "" && !reflect.DeepEqual(options.Created, proof["created"]) {
return fmt.Errorf("expected proof.created %s but got %s", options.Created, proof["created"])
}
return nil
}
func saveOptionsIfNoError(err error, s storage.Store, msg service.DIDCommMsg, options *CredentialSpecOptions) error {
if err != nil {
return err
}
thid, err := msg.ThreadID()
if err != nil {
return fmt.Errorf("failed to get message's threadID: %w", err)
}
raw, err := json.Marshal(options)
if err != nil {
return fmt.Errorf("failed to marshal options: %w", err)
}
return s.Put(thid, raw)
}
func fetchCredentialSpecOptions(s storage.Store, msg service.DIDCommMsg) (*CredentialSpecOptions, error) {
thid, err := msg.ThreadID()
if err != nil {
return nil, fmt.Errorf("failed to get message's threadID: %w", err)
}
raw, err := s.Get(thid)
if err != nil {
return nil, fmt.Errorf("failed to fetch options from store with threadID %s: %w", thid, err)
}
options := &CredentialSpecOptions{}
return options, json.Unmarshal(raw, options)
}
func deleteOptionsIfNoError(err error, s storage.Store, msg service.DIDCommMsg) error {
if err != nil {
return err
}
thid, err := msg.ThreadID()
if err != nil {
return fmt.Errorf("failed to get message's threadID: %w", err)
}
return s.Delete(thid)
}
type bbsSigner struct {
km kms.KeyManager
cr crypto.Crypto
kh interface{}
}
func newBBSSigner(km kms.KeyManager, cr crypto.Crypto, keyHandle interface{}) *bbsSigner {
return &bbsSigner{km: km, cr: cr, kh: keyHandle}
}
func (s *bbsSigner) Sign(data []byte) ([]byte, error) {
return s.cr.SignMulti(s.textToLines(string(data)), s.kh)
}
func (s *bbsSigner) Alg() string {
return "Bls12381G2Key2020"
}
func (s *bbsSigner) textToLines(txt string) [][]byte {
lines := strings.Split(txt, "\n")
linesBytes := make([][]byte, 0, len(lines))
for i := range lines {
if strings.TrimSpace(lines[i]) != "" {
linesBytes = append(linesBytes, []byte(lines[i]))
}
}
return linesBytes
}