-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
credential_exchange_v2.go
289 lines (258 loc) · 9.97 KB
/
credential_exchange_v2.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
package acapy
import (
"fmt"
)
type createCredentialExchangeRecordRequestV2 struct {
CredentialPreview CredentialPreviewV2 `json:"credential_preview"` // required
Filter Filter `json:"filter"`
Comment string `json:"comment,omitempty"`
Trace bool `json:"trace,omitempty"`
AutoRemove bool `json:"auto_remove,omitempty"`
}
func (c *Client) CreateCredentialExchangeRecordV2(
credentialPreview CredentialPreviewV2, // required
credentialDefinitionID string, // optional
issuerDID string, // optional
schemaID string, // optional
comment string, // optional
) (CredentialExchangeRecordResult, error) {
var request = createCredentialExchangeRecordRequestV2{
CredentialPreview: credentialPreview,
Filter: Filter{
DIF: map[string]string{},
Indy: IndyFilter{
CredentialDefinitionID: credentialDefinitionID,
IssuerDID: issuerDID,
SchemaID: schemaID,
},
},
Comment: comment,
Trace: c.tracing,
AutoRemove: !c.preserveExchangeRecords,
}
var credentialExchange CredentialExchangeRecordResult
err := c.post("/issue-credential-2.0/create", nil, request, &credentialExchange)
if err != nil {
return CredentialExchangeRecordResult{}, err
}
return credentialExchange, nil
}
type credentialProposalRequestV2 struct {
ConnectionID string `json:"connection_id"` // required
Comment string `json:"comment,omitempty"`
CredentialPreview CredentialPreviewV2 `json:"credential_preview"` // required
Filter Filter `json:"filter"`
Trace bool `json:"trace,omitempty"`
AutoRemove bool `json:"auto_remove,omitempty"`
}
// ProposeCredential tells the issuer what the holder hopes to receive
func (c *Client) ProposeCredentialV2(
connectionID string, // required
credentialPreview CredentialPreviewV2, // required
comment string, // optional
credentialDefinitionID string, // optional
issuerDID string, // optional
schemaID string, // optional
) (CredentialExchangeRecordResult, error) {
var response CredentialExchangeRecordResult
var request = credentialProposalRequestV2{
ConnectionID: connectionID,
Comment: comment,
CredentialPreview: credentialPreview,
Filter: Filter{
DIF: map[string]string{},
Indy: IndyFilter{
CredentialDefinitionID: credentialDefinitionID,
IssuerDID: issuerDID,
SchemaID: schemaID,
},
},
Trace: c.tracing,
AutoRemove: !c.preserveExchangeRecords,
}
err := c.post("/issue-credential-2.0/send-proposal", nil, request, &response)
if err != nil {
return CredentialExchangeRecordResult{}, err
}
return response, nil
}
type credentialOfferRequestV2 struct {
CredentialDefinitionID string `json:"cred_def_id,omitempty"`
ConnectionID string `json:"connection_id"` // required
CredentialPreview CredentialPreviewV2 `json:"credential_preview"` // required
Comment string `json:"comment,omitempty"`
Filter Filter `json:"filter"`
// filled automatically
Trace bool `json:"trace,omitempty"`
AutoRemove bool `json:"auto_remove,omitempty"`
AutoIssue bool `json:"auto_issue,omitempty"`
}
type Filter struct {
DIF map[string]string `json:"dif"`
Indy IndyFilter `json:"indy"`
}
type IndyFilter struct {
CredentialDefinitionID string `json:"cred_def_id,omitempty"`
IssuerDID string `json:"issuer_did,omitempty"`
SchemaID string `json:"schema_id,omitempty"`
SchemaName string `json:"schema_name,omitempty"`
SchemaVersion string `json:"schema_version,omitempty"`
SchemaIssuerDID string `json:"schema_issuer_did,omitempty"`
}
// OfferCredential sends to the holder what the issuer can offer
// TODO support payment decorator
func (c *Client) OfferCredentialV2(
connectionID string, // required
credentialPreview CredentialPreviewV2, // required
credentialDefinitionID string, // optional
comment string, // optional
) (CredentialExchangeRecordResult, error) {
var offer = credentialOfferRequestV2{
CredentialDefinitionID: credentialDefinitionID,
ConnectionID: connectionID,
CredentialPreview: credentialPreview,
Comment: comment,
Filter: Filter{
DIF: map[string]string{},
Indy: IndyFilter{
CredentialDefinitionID: credentialDefinitionID,
},
},
Trace: c.tracing,
AutoRemove: !c.preserveExchangeRecords,
AutoIssue: c.autoRespondCredentialOffer,
}
var credentialExchangeRecord CredentialExchangeRecordResult
err := c.post("/issue-credential-2.0/send-offer", nil, offer, &credentialExchangeRecord)
if err != nil {
return CredentialExchangeRecordResult{}, err
}
return credentialExchangeRecord, nil
}
// OfferCredentialByID sends an offer to the holder based on a previously received proposal
func (c *Client) OfferCredentialByIDV2(credentialExchangeID string) (CredentialExchangeRecordResult, error) {
var credentialExchange CredentialExchangeRecordResult
err := c.post(fmt.Sprintf("/issue-credential-2.0/records/%s/send-offer", credentialExchangeID), nil, nil, &credentialExchange)
if err != nil {
return CredentialExchangeRecordResult{}, err
}
return credentialExchange, nil
}
// RequestCredentialByID sends a credential request to the issuer based on a previously received offer
func (c *Client) RequestCredentialByIDV2(credentialExchangeID string) (CredentialExchangeRecordResult, error) {
var credentialExchange CredentialExchangeRecordResult
err := c.post(fmt.Sprintf("/issue-credential-2.0/records/%s/send-request", credentialExchangeID), nil, nil, &credentialExchange)
if err != nil {
return CredentialExchangeRecordResult{}, err
}
return credentialExchange, nil
}
type issueCredentialRequestV2 struct {
ConnectionID string `json:"connection_id"` // required
Comment string `json:"comment,omitempty"`
CredentialPreview CredentialPreviewV2 `json:"credential_preview"` // required
Filter Filter `json:"filter"`
Trace bool `json:"trace,omitempty"`
AutoRemove bool `json:"auto_remove,omitempty"`
}
// IssueCredential issues a credential to the holder
func (c *Client) IssueCredentialV2(
connectionID string, // required
credentialPreview CredentialPreviewV2, // required
comment string, // optional
credentialDefinitionID string, // optional
issuerDID string, // optional
schemaID string, // optional
) (CredentialExchangeRecordResult, error) {
var credentialExchange CredentialExchangeRecordResult
var request = issueCredentialRequestV2{
ConnectionID: connectionID,
Comment: comment,
CredentialPreview: credentialPreview,
Filter: Filter{
DIF: map[string]string{},
Indy: IndyFilter{
CredentialDefinitionID: credentialDefinitionID,
IssuerDID: issuerDID,
SchemaID: schemaID,
},
},
Trace: c.tracing,
AutoRemove: !c.preserveExchangeRecords,
}
err := c.post("/issue-credential-2.0/send", nil, request, &credentialExchange)
if err != nil {
return CredentialExchangeRecordResult{}, err
}
return credentialExchange, nil
}
// IssueCredentialByID issues a credential to the holder based on a previously received request
func (c *Client) IssueCredentialByIDV2(credentialExchangeID string, comment string) (CredentialExchangeRecordResult, error) {
var credentialExchange CredentialExchangeRecordResult
var body = struct {
Comment string `json:"comment"`
}{
Comment: comment,
}
err := c.post(fmt.Sprintf("/issue-credential-2.0/records/%s/issue", credentialExchangeID), nil, body, &credentialExchange)
if err != nil {
return CredentialExchangeRecordResult{}, 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) StoreCredentialByIDV2(credentialExchangeID string, credentialID string) (CredentialExchangeRecordResult, error) {
var credentialExchange CredentialExchangeRecordResult
var body = struct {
CredentialID string `json:"credential_id,omitempty"`
}{
CredentialID: credentialID,
}
err := c.post(fmt.Sprintf("/issue-credential-2.0/records/%s/store", credentialExchangeID), nil, body, &credentialExchange)
if err != nil {
return CredentialExchangeRecordResult{}, err
}
return credentialExchange, nil
}
type QueryCredentialExchangeParamsV2 struct {
ConnectionID string `json:"connection_id"`
Role string `json:"role"`
State string `json:"state"`
ThreadID string `json:"thread_id"`
}
func (c *Client) QueryCredentialExchangeV2(params QueryCredentialExchangeParamsV2) ([]CredentialExchangeRecordResult, error) {
var result = struct {
Results []CredentialExchangeRecordResult `json:"results"`
}{}
var queryParams = map[string]string{
"connection_id": params.ConnectionID,
"role": params.Role,
"state": params.State,
"thread_id": params.ThreadID,
}
err := c.get("/issue-credential-2.0/records", queryParams, &result)
if err != nil {
return nil, err
}
return result.Results, nil
}
func (c *Client) GetCredentialExchangeV2(credentialExchangeID string) (CredentialExchangeRecordResult, error) {
var credentialExchange CredentialExchangeRecordResult
err := c.get(fmt.Sprintf("/issue-credential-2.0/records/%s", credentialExchangeID), nil, &credentialExchange)
if err != nil {
return CredentialExchangeRecordResult{}, err
}
return credentialExchange, nil
}
func (c *Client) RemoveCredentialExchangeV2(credentialExchangeID string) error {
return c.delete(fmt.Sprintf("/issue-credential-2.0/records/%s", credentialExchangeID))
}
func (c *Client) ReportCredentialExchangeProblemV2(credentialExchangeID string, message string) error {
var body = struct {
Message string `json:"explain_ltxt"`
}{
Message: message,
}
return c.post(fmt.Sprintf("/issue-credential-2.0/records/%s/problem-report", credentialExchangeID), nil, body, nil)
}