This repository has been archived by the owner on Jan 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
claimer.go
242 lines (216 loc) · 7.72 KB
/
claimer.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
// +build wasm
package wasm
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"syscall/js"
"github.com/KILTprotocol/portablegabi/go-wasm/pkg/credentials"
"github.com/privacybydesign/gabi"
"github.com/privacybydesign/gabi/revocation"
)
// GenKey creates the private key for the claimer
func GenKey(this js.Value, inputs []js.Value) (interface{}, error) {
keyLength := DefaultKeyLength
if len(inputs) > 0 && !inputs[0].IsUndefined() {
keyLength = inputs[0].Int()
}
sysParams, success := gabi.DefaultSystemParameters[keyLength]
if !success {
return nil, errors.New("invalid key length")
}
claimer, err := credentials.NewClaimer(sysParams)
if err != nil {
return nil, err
}
return claimer, nil
}
// KeyFromSeed derives a key from a given seed
func KeyFromSeed(this js.Value, inputs []js.Value) (interface{}, error) {
if len(inputs) < 1 {
return nil, errors.New("missing seed to generate claimer keys")
}
// get seed
hexString := inputs[0].String()
if len(hexString) < 4 || hexString[:2] != "0x" {
return nil, errors.New("seed should be a hexadecimal string starting with '0x' followed by at least two hexadecimal digits")
}
seed, err := hex.DecodeString(hexString[2:])
// get optional key length
keyLength := DefaultKeyLength
if len(inputs) > 2 && !inputs[2].IsUndefined() {
keyLength = inputs[2].Int()
}
sysParams, success := gabi.DefaultSystemParameters[keyLength]
if !success {
return nil, errors.New("invalid key length")
}
// create claimer
claimer, err := credentials.NewClaimerFromSecret(sysParams, seed)
if err != nil {
return nil, err
}
return claimer, nil
}
// RequestAttestation creates a session object and a message which request the
// attestation of specific attributes. The second object should be sent to an
// attester. This method expects as inputs the private key of the claimer, a
// json encoded string containing the claim which should be attested, the
// handshake message from the attester and the public key of the attester.
func RequestAttestation(this js.Value, inputs []js.Value) (interface{}, error) {
if len(inputs) < 4 {
return nil, errors.New("missing inputs to request attestation")
}
claimer := &credentials.Claimer{}
claim := credentials.Claim{}
handshakeMsg := &credentials.StartSessionMsg{}
issuerPubKey := &gabi.PublicKey{}
if err := json.Unmarshal([]byte(inputs[0].String()), claimer); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[1].String()), &claim); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[2].String()), handshakeMsg); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[3].String()), issuerPubKey); err != nil {
return nil, fmt.Errorf("Error in public key: %v", err)
}
session, msg, err := claimer.RequestAttestationForClaim(issuerPubKey, handshakeMsg, claim)
if err != nil {
return nil, err
}
return map[string]interface{}{
"session": session,
"message": msg,
}, nil
}
// BuildCredential creates a credential which can be used to create proofs that
// the claimer posseses certain attributes. This method expects as input the
// private key of the claimer, the session object created by requestAttestation
// and the signature message send the attester.
func BuildCredential(this js.Value, inputs []js.Value) (interface{}, error) {
if len(inputs) < 3 {
return nil, errors.New("missing inputs to build credential")
}
claimer := &credentials.Claimer{}
session := &credentials.UserIssuanceSession{}
signature := &gabi.IssueSignatureMessage{}
if err := json.Unmarshal([]byte(inputs[0].String()), claimer); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[1].String()), session); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[2].String()), signature); err != nil {
return nil, err
}
credential, err := claimer.BuildCredential(signature, session)
if err != nil {
return nil, err
}
return credential, nil
}
// BuildPresentation creates a proof that the claimer possesses the requested
// attributes. This method takes as input the private key of the claimer, the
// credential which contains the requested attributes, a json encoded list
// containing the requested attributes and the public key of the attester.
// It returns a proof containing the values of the requested attributes.
func BuildPresentation(this js.Value, inputs []js.Value) (interface{}, error) {
if len(inputs) < 4 {
return nil, errors.New("missing inputs to build presentation")
}
claimer := &credentials.Claimer{}
credential := &credentials.AttestedClaim{}
request := &credentials.PresentationRequest{}
issuerPubKey := &gabi.PublicKey{}
if err := json.Unmarshal([]byte(inputs[0].String()), claimer); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[1].String()), credential); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[2].String()), request); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[3].String()), issuerPubKey); err != nil {
return nil, fmt.Errorf("Error in public key: %v", err)
}
disclosedAttr, err := claimer.BuildPresentation(issuerPubKey, credential, request)
if err != nil {
return nil, err
}
return disclosedAttr, nil
}
func BuildCombinedPresentation(this js.Value, inputs []js.Value) (interface{}, error) {
if len(inputs) < 4 {
return nil, errors.New("missing inputs to build combined presentation")
}
claimer := &credentials.Claimer{}
creds := []*credentials.AttestedClaim{}
request := &credentials.CombinedPresentationRequest{}
attesterPubKey := []*gabi.PublicKey{}
if err := json.Unmarshal([]byte(inputs[0].String()), claimer); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[1].String()), &creds); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[2].String()), request); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[3].String()), &attesterPubKey); err != nil {
return nil, fmt.Errorf("Error in public key: %v", err)
}
disclosedAttr, err := claimer.BuildCombinedPresentation(attesterPubKey, creds, request)
if err != nil {
return nil, err
}
return disclosedAttr, nil
}
// UpdateCredential updates the non revocation witness using the provided update.
func UpdateCredential(this js.Value, inputs []js.Value) (interface{}, error) {
if len(inputs) < 3 {
return nil, errors.New("missing inputs to update credential")
}
credential := &credentials.AttestedClaim{}
update := &revocation.Update{}
issuerPubKey := &gabi.PublicKey{}
if err := json.Unmarshal([]byte(inputs[0].String()), credential); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[1].String()), update); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[2].String()), issuerPubKey); err != nil {
return nil, fmt.Errorf("Error in public key: %v", err)
}
if err := credential.Update(issuerPubKey, update); err != nil {
return nil, err
}
return credential, nil
}
// UpdateAllCredential updates the non revocation witness using all the provided updates.
func UpdateAllCredential(this js.Value, inputs []js.Value) (interface{}, error) {
if len(inputs) < 3 {
return nil, errors.New("missing inputs to update credential")
}
credential := &credentials.AttestedClaim{}
updates := []*revocation.Update{}
issuerPubKey := &gabi.PublicKey{}
if err := json.Unmarshal([]byte(inputs[0].String()), credential); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[1].String()), &updates); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(inputs[2].String()), issuerPubKey); err != nil {
return nil, fmt.Errorf("Error in public key: %v", err)
}
if err := credential.UpdateAll(issuerPubKey, updates); err != nil {
return nil, err
}
return credential, nil
}