forked from hyperledger-archives/aries-framework-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_support_test.go
113 lines (87 loc) · 2.62 KB
/
example_support_test.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package verifiable_test
import (
"encoding/base64"
"encoding/json"
"strings"
"github.com/hyperledger/aries-framework-go/pkg/crypto/primitive/bbs12381g2pub"
jld "github.com/hyperledger/aries-framework-go/pkg/doc/jsonld"
"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
"github.com/hyperledger/aries-framework-go/pkg/internal/jsonldtest"
)
type UniversityDegree struct {
Type string `json:"type,omitempty"`
University string `json:"university,omitempty"`
}
type UniversityDegreeSubject struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Spouse string `json:"spouse,omitempty"`
Degree UniversityDegree `json:"degree,omitempty"`
}
type UniversityDegreeCredential struct {
*verifiable.Credential
ReferenceNumber int `json:"referenceNumber,omitempty"`
}
func (udc *UniversityDegreeCredential) MarshalJSON() ([]byte, error) {
// todo too complex! (https://github.com/hyperledger/aries-framework-go/issues/847)
c := udc.Credential
cp := *c
cp.CustomFields = map[string]interface{}{
"referenceNumber": udc.ReferenceNumber,
}
return json.Marshal(&cp)
}
func getJSONLDDocumentLoader() *jld.DocumentLoader {
loader, err := jsonldtest.DocumentLoader()
if err != nil {
panic(err)
}
return loader
}
type bbsSigner struct {
privKeyBytes []byte
}
func newBBSSigner(privKey *bbs12381g2pub.PrivateKey) (*bbsSigner, error) {
privKeyBytes, err := privKey.Marshal()
if err != nil {
return nil, err
}
return &bbsSigner{privKeyBytes: privKeyBytes}, nil
}
func (s *bbsSigner) Sign(data []byte) ([]byte, error) {
msgs := s.textToLines(string(data))
return bbs12381g2pub.New().Sign(msgs, s.privKeyBytes)
}
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
}
func loadBBSKeyPair(pubKeyB64, privKeyB64 string) (*bbs12381g2pub.PublicKey, *bbs12381g2pub.PrivateKey, error) {
pubKeyBytes, err := base64.RawStdEncoding.DecodeString(pubKeyB64)
if err != nil {
return nil, nil, err
}
pubKey, err := bbs12381g2pub.UnmarshalPublicKey(pubKeyBytes)
if err != nil {
return nil, nil, err
}
privKeyBytes, err := base64.RawStdEncoding.DecodeString(privKeyB64)
if err != nil {
return nil, nil, err
}
privKey, err := bbs12381g2pub.UnmarshalPrivateKey(privKeyBytes)
if err != nil {
return nil, nil, err
}
return pubKey, privKey, nil
}