-
Notifications
You must be signed in to change notification settings - Fork 4
/
holder_domestic.go
185 lines (148 loc) · 5.01 KB
/
holder_domestic.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
package mobilecore
import (
"encoding/json"
"github.com/go-errors/errors"
idemixcommon "github.com/minvws/nl-covid19-coronacheck-idemix/common"
"github.com/minvws/nl-covid19-coronacheck-idemix/holder"
"github.com/privacybydesign/gabi"
"github.com/privacybydesign/gabi/big"
"strconv"
"time"
)
type CreateCredentialResultValue struct {
Credential *gabi.Credential `json:"credential"`
Attributes map[string]string `json:"attributes"`
}
func GenerateHolderSk() *Result {
holderSkJson, err := json.Marshal(holder.GenerateSk())
if err != nil {
return WrappedErrorResult(err, "Could not serialize secret key")
}
return &Result{holderSkJson, ""}
}
func CreateCommitmentMessage(holderSkJson, issueSpecificationMessageJson []byte) *Result {
holderSk, err := unmarshalHolderSk(holderSkJson)
if err != nil {
return ErrorResult(err)
}
ism := &idemixcommon.IssueSpecificationMessage{}
err = json.Unmarshal(issueSpecificationMessageJson, ism)
if err != nil {
return WrappedErrorResult(err, "Could not JSON unmarshal issue specification message")
}
var icm *gabi.IssueCommitmentMessage
lastCredBuilders, icm, err = domesticHolder.CreateCommitments(holderSk, ism)
if err != nil {
return WrappedErrorResult(err, "Could not create commitments")
}
icmJson, err := json.Marshal(icm)
if err != nil {
return WrappedErrorResult(err, "Could not marshal issue commitment message")
}
return &Result{icmJson, ""}
}
func CreateCredentials(ccmsJson []byte) *Result {
credBuilders := lastCredBuilders
lastCredBuilders = nil
if credBuilders == nil {
return ErrorResult(errors.Errorf("CreateCommitmentMessage should be called before CreateCredentials"))
}
var ccms []*idemixcommon.CreateCredentialMessage
err := json.Unmarshal(ccmsJson, &ccms)
if err != nil {
return WrappedErrorResult(err, "Could not unmarshal create credential messages")
}
creds, err := domesticHolder.CreateCredentials(credBuilders, ccms)
if err != nil {
return WrappedErrorResult(err, "Could not create credentials")
}
results := make([]*CreateCredentialResultValue, 0, len(creds))
for _, cred := range creds {
attributes, err := readCredentialWithVersion(cred)
if err != nil {
return ErrorResult(err)
}
result := &CreateCredentialResultValue{
Credential: cred,
Attributes: attributes,
}
results = append(results, result)
}
resultsJson, err := json.Marshal(results)
if err != nil {
return WrappedErrorResult(err, "Could not marshal read credential result")
}
return &Result{resultsJson, ""}
}
func ReadDomesticCredential(credJson []byte) *Result {
cred, err := unmarshalCredential(credJson)
if err != nil {
return ErrorResult(err)
}
attributes, err := readCredentialWithVersion(cred)
if err != nil {
return ErrorResult(err)
}
attributesJson, err := json.Marshal(attributes)
if err != nil {
return WrappedErrorResult(err, "Could marshal attributes")
}
return &Result{attributesJson, ""}
}
func Disclose(holderSkJson, credJson []byte, disclosurePolicy string) *Result {
return disclose(holderSkJson, credJson, disclosurePolicy, time.Now())
}
func DiscloseWithTime(holderSkJson, credJson []byte, disclosurePolicy string, unixTimeSeconds int64) *Result {
return disclose(holderSkJson, credJson, disclosurePolicy, time.Unix(unixTimeSeconds, 0))
}
func HasDomesticPrefix(proofQREncoded []byte) bool {
return idemixcommon.HasNLPrefix(proofQREncoded)
}
func disclose(holderSkJson, credJson []byte, disclosurePolicy string, now time.Time) *Result {
holderSk, err := unmarshalHolderSk(holderSkJson)
if err != nil {
return ErrorResult(err)
}
cred, err := unmarshalCredential(credJson)
if err != nil {
return ErrorResult(err)
}
var categoryMode int
if disclosurePolicy == DISCLOSURE_POLICY_1G {
categoryMode = holder.CATEGORY_DISCLOSED_V3_SERIALIZATION
} else if disclosurePolicy == DISCLOSURE_POLICY_3G {
categoryMode = holder.CATEGORY_HIDDEN
} else {
return ErrorResult(errors.Errorf("Unrecognized disclosure policy"))
}
proofPrefixed, _, err := domesticHolder.DiscloseWithTimeQREncoded(holderSk, cred, categoryMode, now)
if err != nil {
return WrappedErrorResult(err, "Could not disclosure credential")
}
return &Result{proofPrefixed, ""}
}
func unmarshalHolderSk(holderSkJson []byte) (*big.Int, error) {
holderSk := new(big.Int)
err := json.Unmarshal(holderSkJson, holderSk)
if err != nil {
return nil, errors.WrapPrefix(err, "Could not unmarshal holdercore sk", 0)
}
return holderSk, nil
}
func unmarshalCredential(credJson []byte) (*gabi.Credential, error) {
cred := new(gabi.Credential)
err := json.Unmarshal(credJson, cred)
if err != nil {
return nil, errors.WrapPrefix(err, "Could not unmarshal credential", 0)
}
return cred, nil
}
func readCredentialWithVersion(cred *gabi.Credential) (map[string]string, error) {
attributes, credVersion, err := domesticHolder.ReadCredential(cred)
if err != nil {
return nil, errors.WrapPrefix(err, "Could not read credential", 0)
}
// Add the credential version to the attributes
attributes["credentialVersion"] = strconv.Itoa(credVersion)
return attributes, nil
}