-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpresentation_cwt.go
84 lines (67 loc) · 1.82 KB
/
presentation_cwt.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package verifiable
import (
"fmt"
"github.com/fxamacker/cbor/v2"
"github.com/veraison/go-cose"
"github.com/trustbloc/vc-go/cwt"
"github.com/trustbloc/vc-go/jwt"
)
func newCWTPresClaims(vp *Presentation, audience []string, minimizeVP bool) (*CWTPresClaims, error) {
jwtClaims, err := newJWTPresClaims(vp, audience, minimizeVP)
if err != nil {
return nil, err
}
return &CWTPresClaims{
Claims: jwtClaims.Claims,
Presentation: jwtClaims.Presentation,
}, nil
}
type CWTPresClaims struct {
*jwt.Claims
Presentation rawPresentation `json:"vp,omitempty"`
}
func (c *CWTPresClaims) MarshalCWT(
signatureAlg cose.Algorithm,
signer cwt.ProofCreator,
keyID string,
) ([]byte, *cose.Sign1Message, error) {
return marshalCOSE(c, signatureAlg, signer, keyID)
}
// CreateCWTVP creates a CWT presentation from the given presentation.
func (vp *Presentation) CreateCWTVP(
aud []string,
signatureAlg cose.Algorithm,
signer cwt.ProofCreator,
keyID string,
minimizeVP bool,
) (*Presentation, error) {
cwtClaims, err := vp.CWTClaims(aud, minimizeVP)
if err != nil {
return nil, fmt.Errorf("failed to create CWT claims: %w", err)
}
cwtBytes, msg, err := cwtClaims.MarshalCWT(signatureAlg, signer, keyID)
if err != nil {
return nil, fmt.Errorf("failed to marshal CWT: %w", err)
}
var vpMap map[interface{}]interface{}
err = cbor.Unmarshal(msg.Payload, &vpMap)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal VP map: %w", err)
}
vp2 := vp.Clone()
vp2.CWT = &VpCWT{
Raw: cwtBytes,
Message: msg,
VPMap: convertToStringMap(vpMap),
}
vp2.JWT = ""
return vp2, nil
}
// IsCWT returns true is the presentation is CWT.
func (vp *Presentation) IsCWT() bool {
return vp.CWT != nil && len(vp.CWT.Raw) > 0
}