-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
credential_exchange.go
306 lines (270 loc) · 11.3 KB
/
credential_exchange.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
package acapy
import (
"fmt"
)
func NewCredentialPreview(attributes []CredentialPreviewAttribute) CredentialPreview {
return CredentialPreview{
Type: "did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/issue-credential/1.0/credential-preview",
Attributes: attributes,
}
}
type CredentialPreview struct {
Type string `json:"@type"`
Attributes []CredentialPreviewAttribute `json:"attributes"`
}
type CredentialPreviewAttribute struct {
Name string `json:"name"`
MimeType string `json:"mime-type"` // optional
Value string `json:"value"`
}
type CreateCredentialExchangeRecordRequest struct {
CredentialDefinitionID string `json:"cred_def_id,omitempty"`
CredentialPreview CredentialPreview `json:"credential_proposal"` // required
IssuerDID string `json:"issuer_did,omitempty"`
Comment string `json:"comment,omitempty"`
SchemaID string `json:"schema_id,omitempty"`
SchemaVersion string `json:"schema_version,omitempty"`
SchemaIssuerDID string `json:"schema_issuer_did,omitempty"`
SchemaName string `json:"schema_name,omitempty"`
Trace bool `json:"trace,omitempty"`
AutoRemove bool `json:"auto_remove,omitempty"`
}
func (c *Client) CreateCredentialExchangeRecord(request CreateCredentialExchangeRecordRequest) (CredentialExchangeRecord, error) {
var credentialExchange CredentialExchangeRecord
err := c.post("/issue-credential/create", nil, request, &credentialExchange)
if err != nil {
return CredentialExchangeRecord{}, err
}
return credentialExchange, nil
}
type credentialProposalRequest struct {
CredentialDefinitionID string `json:"cred_def_id,omitempty"`
ConnectionID string `json:"connection_id"` // required
IssuerDID string `json:"issuer_did,omitempty"`
Comment string `json:"comment,omitempty"`
CredentialPreview CredentialPreview `json:"credential_proposal"` // required
SchemaID string `json:"schema_id,omitempty"`
Trace bool `json:"trace,omitempty"`
AutoRemove bool `json:"auto_remove,omitempty"`
// not supported
SchemaName string `json:"schema_name,omitempty"`
SchemaVersion string `json:"schema_version,omitempty"`
SchemaIssuerDID string `json:"schema_issuer_did,omitempty"`
}
// ProposeCredential tells the issuer what the holder hopes to receive
func (c *Client) ProposeCredential(
connectionID string, // required
credentialPreview CredentialPreview, // required
comment string, // optional
credentialDefinitionID string, // optional
issuerDID string, // optional
schemaID string, // optional
) (CredentialExchangeRecord, error) {
var response CredentialExchangeRecord
var request = credentialProposalRequest{
CredentialDefinitionID: credentialDefinitionID,
ConnectionID: connectionID,
IssuerDID: issuerDID,
Comment: comment,
CredentialPreview: credentialPreview,
SchemaID: schemaID,
Trace: c.tracing,
AutoRemove: !c.preserveExchangeRecords,
}
err := c.post("/issue-credential/send-proposal", nil, request, &response)
if err != nil {
return CredentialExchangeRecord{}, err
}
return response, nil
}
type credentialOfferRequest struct {
CredentialDefinitionID string `json:"cred_def_id,omitempty"`
ConnectionID string `json:"connection_id"` // required
CredentialPreview CredentialPreview `json:"credential_preview"` // required
Comment string `json:"comment,omitempty"`
// filled automatically
Trace bool `json:"trace,omitempty"`
AutoRemove bool `json:"auto_remove,omitempty"`
AutoIssue bool `json:"auto_issue,omitempty"`
}
// OfferCredential sends to the holder what the issuer can offer
// TODO support payment decorator
func (c *Client) OfferCredential(
connectionID string, // required
credentialPreview CredentialPreview, // required
credentialDefinitionID string, // optional
comment string, // optional
) (CredentialExchangeRecord, error) {
var offer = credentialOfferRequest{
CredentialDefinitionID: credentialDefinitionID,
ConnectionID: connectionID,
CredentialPreview: credentialPreview,
Comment: comment,
Trace: c.tracing,
AutoRemove: !c.preserveExchangeRecords,
AutoIssue: c.autoRespondCredentialOffer,
}
var credentialExchangeRecord CredentialExchangeRecord
err := c.post("/issue-credential/send-offer", nil, offer, &credentialExchangeRecord)
if err != nil {
return CredentialExchangeRecord{}, err
}
return credentialExchangeRecord, nil
}
// OfferCredentialByID sends an offer to the holder based on a previously received proposal
func (c *Client) OfferCredentialByID(credentialExchangeID string) (CredentialExchangeRecord, error) {
var credentialExchange CredentialExchangeRecord
err := c.post(fmt.Sprintf("/issue-credential/records/%s/send-offer", credentialExchangeID), nil, nil, &credentialExchange)
if err != nil {
return CredentialExchangeRecord{}, err
}
return credentialExchange, nil
}
// RequestCredentialByID sends a credential request to the issuer based on a previously received offer
func (c *Client) RequestCredentialByID(credentialExchangeID string) (CredentialExchangeRecord, error) {
var credentialExchange CredentialExchangeRecord
err := c.post(fmt.Sprintf("/issue-credential/records/%s/send-request", credentialExchangeID), nil, nil, &credentialExchange)
if err != nil {
return CredentialExchangeRecord{}, err
}
return credentialExchange, nil
}
type issueCredentialRequest struct {
CredentialDefinitionID string `json:"cred_def_id,omitempty"`
ConnectionID string `json:"connection_id"` // required
IssuerDID string `json:"issuer_did,omitempty"`
Comment string `json:"comment,omitempty"`
CredentialPreview CredentialPreview `json:"credential_proposal"` // required
SchemaID string `json:"schema_id,omitempty"`
Trace bool `json:"trace,omitempty"`
AutoRemove bool `json:"auto_remove,omitempty"`
// not supported
SchemaName string `json:"schema_name,omitempty"`
SchemaVersion string `json:"schema_version,omitempty"`
SchemaIssuerDID string `json:"schema_issuer_did,omitempty"`
}
// IssueCredential issues a credential to the holder
func (c *Client) IssueCredential(
connectionID string, // required
credentialPreview CredentialPreview, // required
comment string, // optional
credentialDefinitionID string, // optional
issuerDID string, // optional
schemaID string, // optional
) (CredentialExchangeRecord, error) {
var credentialExchange CredentialExchangeRecord
var request = issueCredentialRequest{
CredentialDefinitionID: credentialDefinitionID,
ConnectionID: connectionID,
IssuerDID: issuerDID,
Comment: comment,
CredentialPreview: credentialPreview,
SchemaID: schemaID,
Trace: c.tracing,
AutoRemove: !c.preserveExchangeRecords,
}
err := c.post("/issue-credential/send", nil, request, &credentialExchange)
if err != nil {
return CredentialExchangeRecord{}, err
}
return credentialExchange, nil
}
// IssueCredentialByID issues a credential to the holder based on a previously received request
func (c *Client) IssueCredentialByID(credentialExchangeID string, comment string) (CredentialExchangeRecord, error) {
var credentialExchange CredentialExchangeRecord
var body = struct {
Comment string `json:"comment"`
}{
Comment: comment,
}
err := c.post(fmt.Sprintf("/issue-credential/records/%s/issue", credentialExchangeID), nil, body, &credentialExchange)
if err != nil {
return CredentialExchangeRecord{}, err
}
return credentialExchange, nil
}
// StoreCredentialByID the holder stores a credential based on a previously received issued credential
// credentialID is optional: https://github.com/hyperledger/aries-cloudagent-python/issues/594#issuecomment-656113125
func (c *Client) StoreCredentialByID(credentialExchangeID string, credentialID string) (CredentialExchangeRecord, error) {
var credentialExchange CredentialExchangeRecord
var body = struct {
CredentialID string `json:"credential_id"`
}{
CredentialID: credentialID,
}
err := c.post(fmt.Sprintf("/issue-credential/records/%s/store", credentialExchangeID), nil, body, &credentialExchange)
if err != nil {
return CredentialExchangeRecord{}, err
}
return credentialExchange, nil
}
type QueryCredentialExchangeParams struct {
ConnectionID string `json:"connection_id"`
Role string `json:"role"`
State string `json:"state"`
ThreadID string `json:"thread_id"`
}
func (c *Client) QueryCredentialExchange(params QueryCredentialExchangeParams) ([]CredentialExchangeRecord, error) {
var result = struct {
Results []CredentialExchangeRecord `json:"result"`
}{}
var queryParams = map[string]string{
"connection_id": params.ConnectionID,
"role": params.Role,
"state": params.State,
"thread_id": params.ThreadID,
}
err := c.get("/issue-credential/records", queryParams, &result)
if err != nil {
return nil, err
}
return result.Results, nil
}
func (c *Client) GetCredentialExchange(credentialExchangeID string) (CredentialExchangeRecord, error) {
var credentialExchange CredentialExchangeRecord
err := c.get(fmt.Sprintf("/issue-credential/records/%s", credentialExchangeID), nil, &credentialExchange)
if err != nil {
return CredentialExchangeRecord{}, err
}
return credentialExchange, nil
}
func (c *Client) RemoveCredentialExchange(credentialExchangeID string) error {
return c.delete(fmt.Sprintf("/issue-credential/records/%s", credentialExchangeID))
}
func (c *Client) ReportCredentialExchangeProblem(credentialExchangeID string, message string) error {
var body = struct {
Message string `json:"explain_ltxt"`
}{
Message: message,
}
return c.post(fmt.Sprintf("/issue-credential/records/%s/problem-report", credentialExchangeID), nil, body, nil)
}
type OutOfBandCredential struct {
ID string `json:"@id"`
Type string `json:"@type"`
Comment string `json:"comment"`
Service Service `json:"~service"`
CredentialPreview CredentialPreview `json:"credential_preview"`
OffersAttach []OfferAttach `json:"offers~attach"`
}
func (c *Client) CreateOutOfBandCredential(request CreateCredentialExchangeRecordRequest) (OutOfBandCredential, error) {
record, err := c.CreateCredentialExchangeRecord(request)
if err != nil {
return OutOfBandCredential{}, err
}
invitation, err := c.CreateInvitation("", false, false, false)
if err != nil {
return OutOfBandCredential{}, err
}
return OutOfBandCredential{
Comment: request.Comment,
Type: record.CredentialOfferMap.Type,
CredentialPreview: record.CredentialOfferMap.CredentialPreview,
OffersAttach: record.CredentialOfferMap.OffersAttach,
Service: Service{
RecipientKeys: invitation.Invitation.RecipientKeys,
RoutingKeys: invitation.Invitation.RoutingKeys,
ServiceEndpoint: invitation.Invitation.ServiceEndpoint,
},
}, nil
}