forked from TBD54566975/ssi-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
430 lines (374 loc) · 10.4 KB
/
helpers.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
package util
import (
_ "embed"
"errors"
"fmt"
"os"
"reflect"
"strings"
"time"
"github.com/go-playground/validator/v10"
"github.com/goccy/go-json"
"github.com/hyperledger/aries-framework-go/pkg/doc/signature/jsonld"
"github.com/piprate/json-gold/ld"
)
type LDProcessor struct {
*ld.JsonLdProcessor
*ld.JsonLdOptions
}
func NewValidator() *validator.Validate {
return validator.New()
}
func IsValidStruct(data any) error {
if t := reflect.TypeOf(data).Kind(); t != reflect.Struct {
return fmt.Errorf("provided data is not of Kind struct: %+v", data)
}
return NewValidator().Struct(data)
}
//go:embed known_contexts/w3c_2018_credentials_v1.json
var w3c2018CredentialsV1 string
//go:embed known_contexts/w3c_2018_credentials_examples_v1.json
var w3c2018CredentialsExamplesV1 string
//go:embed known_contexts/w3c_ns_did_v1.json
var w3cNamespaceDIDV1 string
//go:embed known_contexts/w3c_vc_di_bbs_contexts_v1.json
var w3cVCDIBBSV1 string
//go:embed known_contexts/w3c_jws_2020_v1.json
var w3cJWS2020V1 string
//go:embed known_contexts/w3id_security_v1.json
var w3idSecurityV1 string
//go:embed known_contexts/w3id_security_v2.json
var w3idSecurityV2 string
//go:embed known_contexts/w3id_citizenship_v1.json
var w3idCitizenshipV1 string
//go:embed known_contexts/w3_ns_odrl.json
var w3NamespaceODRL string
func NewLDProcessor() (*LDProcessor, error) {
// JSON LD processing
proc := ld.NewJsonLdProcessor()
// Initialize a new doc loader with caching capability
// LDProcessor is expected to be re-used for multiple json-ld operations
docLoader, err := NewLDDocumentLoader()
if err != nil {
return nil, err
}
options := ld.NewJsonLdOptions("")
options.Format = "application/n-quads"
options.Algorithm = "URDNA2015"
options.ProcessingMode = ld.JsonLd_1_1
options.ProduceGeneralizedRdf = true
options.DocumentLoader = docLoader
return &LDProcessor{
JsonLdProcessor: proc,
JsonLdOptions: options,
}, nil
}
func NewLDDocumentLoader() (*ld.CachingDocumentLoader, error) {
rfcDocLoader := ld.NewRFC7324CachingDocumentLoader(nil)
docLoader := ld.NewCachingDocumentLoader(rfcDocLoader)
// We cache the contexts we know we'll use over and over.
if err := preloadContext(docLoader, w3c2018CredentialsV1, "https://www.w3.org/2018/credentials/v1"); err != nil {
return nil, err
}
if err := preloadContext(docLoader, w3c2018CredentialsExamplesV1, "https://www.w3.org/2018/credentials/examples/v1"); err != nil {
return nil, err
}
if err := preloadContext(docLoader, w3cNamespaceDIDV1, "https://www.w3.org/ns/did/v1"); err != nil {
return nil, err
}
if err := preloadContext(docLoader, w3cVCDIBBSV1, "https://w3c.github.io/vc-di-bbs/contexts/v1"); err != nil {
return nil, err
}
if err := preloadContext(docLoader, w3cJWS2020V1, "https://w3id.org/security/suites/jws-2020/v1"); err != nil {
return nil, err
}
if err := preloadContext(docLoader, w3idSecurityV1, "https://w3id.org/security/v1"); err != nil {
return nil, err
}
if err := preloadContext(docLoader, w3idSecurityV2, "https://w3id.org/security/v2"); err != nil {
return nil, err
}
if err := preloadContext(docLoader, w3idCitizenshipV1, "https://w3id.org/citizenship/v1"); err != nil {
return nil, err
}
if err := preloadContext(docLoader, w3NamespaceODRL, "https://www.w3.org/ns/odrl.jsonld"); err != nil {
return nil, err
}
return docLoader, nil
}
func preloadContext(docLoader *ld.CachingDocumentLoader, contents string, url string) error {
f, err := os.CreateTemp("", "")
if err != nil {
return err
}
defer func(name string) {
_ = os.Remove(name)
}(f.Name())
if _, err := f.Write([]byte(contents)); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return docLoader.PreloadWithMapping(map[string]string{
url: f.Name(),
})
}
func (l LDProcessor) GetOptions() *ld.JsonLdOptions {
return l.JsonLdOptions
}
func (l LDProcessor) GetContextFromMap(dataMap map[string]any) (*ld.Context, error) {
var activeCtx *ld.Context
var err error
ldCtx := ld.NewContext(nil, l.JsonLdOptions)
contextMap, ok := dataMap["@context"].(map[string]any)
if !ok {
activeCtx, err = ldCtx.Parse(dataMap)
} else {
activeCtx, err = ldCtx.Parse(contextMap)
}
if err != nil {
return nil, err
}
return activeCtx, nil
}
func LDNormalize(document any) (any, error) {
processor, err := NewLDProcessor()
if err != nil {
return nil, err
}
return processor.Normalize(document, processor.GetOptions())
}
// LDFrame runs https://www.w3.org/TR/json-ld11-framing/ to transform the data in a document according to its frame
func LDFrame(document any, frame any) (any, error) {
docAny := document
var err error
if _, ok := document.(map[string]any); !ok {
docAny, err = AnyToJSONInterface(document)
if err != nil {
return nil, err
}
}
frameAny := frame
if _, ok := frame.(map[string]any); !ok {
frameAny, err = AnyToJSONInterface(frame)
if err != nil {
return nil, err
}
}
docLoader, err := NewLDDocumentLoader()
if err != nil {
return nil, err
}
// use the aries processor for special framing logic necessary for blank nodes
return jsonld.Default().Frame(docAny.(map[string]any),
frameAny.(map[string]any), jsonld.WithDocumentLoader(docLoader), jsonld.WithFrameBlankNodes())
}
// LDCompact runs https://www.w3.org/TR/json-ld-api/#compaction-algorithms which shortens IRIs in the document
func LDCompact(document any, context string) (map[string]any, error) {
processor, err := NewLDProcessor()
if err != nil {
return nil, err
}
contextsMap := map[string]any{
"@context": context,
}
return processor.Compact(document, contextsMap, processor.GetOptions())
}
func GetRFC3339Timestamp() string {
return AsRFC3339Timestamp(time.Now())
}
func AsRFC3339Timestamp(t time.Time) string {
return t.UTC().Format(time.RFC3339)
}
// IsRFC3339Timestamp attempts to parse a string as an RFC3339 timestamp, which is a subset of an
// ISO-8601 timestamp. Returns true if the parsing is successful, false if not.
func IsRFC3339Timestamp(t string) bool {
_, err := time.Parse(time.RFC3339, t)
return err == nil
}
// Copy makes a 1:1 copy of src into dst.
func Copy(src any, dst any) error {
if err := validateCopy(src, dst); err != nil {
return err
}
bytes, err := json.Marshal(src)
if err != nil {
return err
}
return json.Unmarshal(bytes, dst)
}
func ToJSONInterface(data string) (any, error) {
var result any
err := json.Unmarshal([]byte(data), &result)
return result, err
}
func AnyToJSONInterface(data any) (any, error) {
dataBytes, err := json.Marshal(data)
if err != nil {
return nil, err
}
var result any
err = json.Unmarshal(dataBytes, &result)
return result, err
}
func validateCopy(src any, dst any) error {
if src == nil {
return errors.New("src is nil")
}
if dst == nil {
return errors.New("dst is nil")
}
// Type check
srcType := reflect.TypeOf(src)
dstType := reflect.TypeOf(dst)
if srcType != dstType {
return errors.New("type of src and dst must match")
}
// Kind checks
srcKind := srcType.Kind()
if !(srcKind == reflect.Ptr || srcKind == reflect.Slice) {
return errors.New("src is not of kind ptr or slice")
}
dstKind := dstType.Kind()
if !(dstKind == reflect.Ptr || dstKind == reflect.Slice) {
return errors.New("dst is not of kind ptr or slice")
}
return nil
}
func StringPtr(s string) *string {
return &s
}
type AppendError []string
func NewAppendError() *AppendError {
return new(AppendError)
}
func (a *AppendError) Append(err error) {
*a = append(*a, err.Error())
}
func (a *AppendError) AppendString(err string) {
*a = append(*a, err)
}
func (a *AppendError) IsEmpty() bool {
return a == nil || len(*a) == 0
}
func (a *AppendError) NumErrors() int {
if a == nil {
return 0
}
return len(*a)
}
func (a *AppendError) Error() error {
if a == nil || len(*a) == 0 {
return nil
}
return fmt.Errorf(strings.Join(*a, "\n"))
}
func Contains(needle string, haystack []string) bool {
for _, maybe := range haystack {
if maybe == needle {
return true
}
}
return false
}
func ArrayInterfaceToStr(have []any) ([]string, error) {
var want []string
for _, item := range have {
strItem, ok := item.(string)
if !ok {
return nil, fmt.Errorf("found non-string item in array: %v", item)
}
want = append(want, strItem)
}
return want, nil
}
func ArrayStrToInterface(have []string) []any {
var want []any
for _, v := range have {
want = append(want, v)
}
return want
}
// InterfaceToInterfaceArray attempts to array-ify an interface type
func InterfaceToInterfaceArray(have any) ([]any, error) {
// case 1: it's a string
strVal, ok := have.(string)
if ok {
return []any{strVal}, nil
}
// case 2: it's an array of string types
strVals, ok := have.([]string)
if ok {
var want []any
for _, s := range strVals {
want = append(want, s)
}
return want, nil
}
// case 3: it's an array of interface types
interVals, ok := have.([]any)
if ok {
return interVals, nil
}
// case 4: it's another interface type
return []any{have}, nil
}
// InterfaceToStrings assumes we are given an interface of either `string`, `[]string` or `[]any` types
// and attempts to flatten into an array of strings
func InterfaceToStrings(have any) ([]string, error) {
// case 1: it's a string
strVal, ok := have.(string)
if ok {
return []string{strVal}, nil
}
// case 2: it's an array of string types
strVals, ok := have.([]string)
if ok {
var want []string
for _, s := range strVals {
want = append(want, s)
}
return want, nil
}
// case 3: it's an array of interface types
interVals, ok := have.([]any)
if ok {
return ArrayInterfaceToStr(interVals)
}
return nil, errors.New("could not turn interface into strings")
}
func ToJSONMap(data any) (map[string]any, error) {
dataBytes, err := json.Marshal(data)
if err != nil {
return nil, err
}
var jsonMap map[string]any
if err := json.Unmarshal(dataBytes, &jsonMap); err != nil {
return nil, err
}
return jsonMap, nil
}
// MergeUniqueValues takes in two string arrays and returns the union set
// the input arrays are not modified
func MergeUniqueValues(a, b []string) []string {
seen := make(map[string]bool)
var res []string
for _, v := range a {
// this check is necessary if a contains duplicates
if _, s := seen[v]; !s {
res = append(res, v)
seen[v] = true
}
}
for _, v := range b {
if _, s := seen[v]; !s {
res = append(res, v)
}
}
return res
}
// PrettyJSON JSON-ifies data in a 'pretty-print' fashion
func PrettyJSON(data any) ([]byte, error) {
return json.MarshalIndent(data, "", " ")
}