-
Notifications
You must be signed in to change notification settings - Fork 2
/
core_utils.go
295 lines (257 loc) · 7.03 KB
/
core_utils.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
package verifiable
import (
"fmt"
"strings"
"github.com/iden3/go-schema-processor/v2/merklize"
"github.com/iden3/go-schema-processor/v2/utils"
"github.com/piprate/json-gold/ld"
"github.com/pkg/errors"
)
const (
credentialSubjectKey = "credentialSubject"
//nolint:gosec // G101: this is not a hardcoded credential
credentialSubjectFullKey = "https://www.w3.org/2018/credentials#credentialSubject"
//nolint:gosec // G101: this is not a hardcoded credential
verifiableCredentialFullKey = "https://www.w3.org/2018/credentials#VerifiableCredential"
typeFullKey = "@type"
contextFullKey = "@context"
serializationFullKey = "iden3_serialization"
)
// CoreClaimOptions is params for core claim parsing
type CoreClaimOptions struct {
RevNonce uint64 `json:"revNonce"`
Version uint32 `json:"version"`
SubjectPosition string `json:"subjectPosition"`
MerklizedRootPosition string `json:"merklizedRootPosition"`
Updatable bool `json:"updatable"`
MerklizerOpts []merklize.MerklizeOption
}
func findCredentialType(mz *merklize.Merklizer) (string, error) {
opts := mz.Options()
// try to look into credentialSubject.@type to get type of credentials
path1, err := opts.NewPath(credentialSubjectFullKey, typeFullKey)
if err == nil {
var e any
e, err = mz.RawValue(path1)
if err == nil {
tp, ok := e.(string)
if ok {
return tp, nil
}
}
}
// if type of credentials not found in credentialSubject.@type, loop at
// top level @types if it contains two elements: type we are looking for
// and "VerifiableCredential" type.
path2, err := opts.NewPath(typeFullKey)
if err != nil {
return "", err
}
e, err := mz.RawValue(path2)
if err != nil {
return "", err
}
eArr, ok := e.([]any)
if !ok {
return "", fmt.Errorf("top level @type expected to be an array")
}
topLevelTypes, err := toStringSlice(eArr)
if err != nil {
return "", err
}
if len(topLevelTypes) != 2 {
return "", fmt.Errorf("top level @type expected to be of length 2")
}
switch verifiableCredentialFullKey {
case topLevelTypes[0]:
return topLevelTypes[1], nil
case topLevelTypes[1]:
return topLevelTypes[0], nil
default:
return "", fmt.Errorf(
"@type(s) are expected to contain VerifiableCredential type")
}
}
func toStringSlice(in []any) ([]string, error) {
out := make([]string, len(in))
for i, v := range in {
s, ok := v.(string)
if !ok {
return nil, fmt.Errorf("element #%v is not a string", i)
}
out[i] = s
}
return out, nil
}
// parsedSlots is struct that represents iden3 claim specification
type parsedSlots struct {
IndexA, IndexB []byte
ValueA, ValueB []byte
}
// parseSlots converts payload to claim slots using provided schema
func parseSlots(mz *merklize.Merklizer,
credential W3CCredential,
credentialType string) (parsedSlots, bool, error) {
slots := parsedSlots{
IndexA: make([]byte, 32),
IndexB: make([]byte, 32),
ValueA: make([]byte, 32),
ValueB: make([]byte, 32),
}
jsonLDOpts := mz.Options().JSONLDOptions()
serAttr, err := getSerializationAttr(credential, jsonLDOpts,
credentialType)
if err != nil {
return slots, false, err
}
if serAttr == "" {
return slots, false, nil
}
sPaths, err := ParseSerializationAttr(serAttr)
if err != nil {
return slots, true, err
}
if sPaths.isEmpty() {
return slots, true, nil
}
err = fillSlot(slots.IndexA, mz, sPaths.IndexAPath)
if err != nil {
return slots, true, err
}
err = fillSlot(slots.IndexB, mz, sPaths.IndexBPath)
if err != nil {
return slots, true, err
}
err = fillSlot(slots.ValueA, mz, sPaths.ValueAPath)
if err != nil {
return slots, true, err
}
err = fillSlot(slots.ValueB, mz, sPaths.ValueBPath)
if err != nil {
return slots, true, err
}
return slots, true, nil
}
// Get `iden3_serialization` attr definition from context document either using
// type name like DeliverAddressMultiTestForked or by type id like
// urn:uuid:ac2ede19-b3b9-454d-b1a9-a7b3d5763100.
func getSerializationAttr(credential W3CCredential,
opts *ld.JsonLdOptions, tp string) (string, error) {
ldCtx, err := ld.NewContext(nil, opts).Parse(anySlice(credential.Context))
if err != nil {
return "", err
}
return GetSerializationAttrFromParsedContext(ldCtx, tp)
}
// convert from the slice of concrete type to the slice of interface{}
func anySlice[T any](in []T) []any {
if in == nil {
return nil
}
s := make([]any, len(in))
for i := range in {
s[i] = in[i]
}
return s
}
func GetSerializationAttrFromParsedContext(ldCtx *ld.Context,
tp string) (string, error) {
termDef, ok := ldCtx.AsMap()["termDefinitions"]
if !ok {
return "", errors.New("types now found in context")
}
termDefM, ok := termDef.(map[string]any)
if !ok {
return "", errors.New("terms definitions is not of correct type")
}
for typeName, typeDef := range termDefM {
typeDefM, ok := typeDef.(map[string]any)
if !ok {
// not a type
continue
}
typeCtx, ok := typeDefM[contextFullKey]
if !ok {
// not a type
continue
}
typeCtxM, ok := typeCtx.(map[string]any)
if !ok {
return "", errors.New("type @context is not of correct type")
}
typeID, _ := typeDefM["@id"].(string)
if typeName != tp && typeID != tp {
continue
}
serStr, _ := typeCtxM[serializationFullKey].(string)
return serStr, nil
}
return "", nil
}
type slotsPaths struct {
IndexAPath string
IndexBPath string
ValueAPath string
ValueBPath string
}
func ParseSerializationAttr(serAttr string) (slotsPaths, error) {
prefix := "iden3:v1:"
if !strings.HasPrefix(serAttr, prefix) {
return slotsPaths{},
errors.New("serialization attribute does not have correct prefix")
}
parts := strings.Split(serAttr[len(prefix):], "&")
if len(parts) > 4 {
return slotsPaths{},
errors.New("serialization attribute has too many parts")
}
var paths slotsPaths
for _, part := range parts {
kv := strings.Split(part, "=")
if len(kv) != 2 {
return slotsPaths{}, errors.New(
"serialization attribute part does not have correct format")
}
switch kv[0] {
case "slotIndexA":
paths.IndexAPath = kv[1]
case "slotIndexB":
paths.IndexBPath = kv[1]
case "slotValueA":
paths.ValueAPath = kv[1]
case "slotValueB":
paths.ValueBPath = kv[1]
default:
return slotsPaths{},
errors.New("unknown serialization attribute slot")
}
}
return paths, nil
}
func (p slotsPaths) isEmpty() bool {
return p.IndexAPath == "" && p.IndexBPath == "" &&
p.ValueAPath == "" && p.ValueBPath == ""
}
func fillSlot(slotData []byte, mz *merklize.Merklizer, path string) error {
if path == "" {
return nil
}
path = credentialSubjectKey + "." + path
p, err := mz.ResolveDocPath(path)
if err != nil {
return errors.Wrapf(err, "field not found in credential %s", path)
}
entry, err := mz.Entry(p)
if errors.Is(err, merklize.ErrorEntryNotFound) {
return errors.Wrapf(err, "field not found in credential %s", path)
} else if err != nil {
return err
}
intVal, err := entry.ValueMtEntry()
if err != nil {
return err
}
bytesVal := utils.SwapEndianness(intVal.Bytes())
copy(slotData, bytesVal)
return nil
}