-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcredential.go
109 lines (95 loc) · 3.14 KB
/
credential.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
package acapy
import (
"encoding/json"
"fmt"
"strconv"
)
type Credential struct {
Referent string `json:"referent"` // Also know as CredentialID
CredentialDefinitionID string `json:"cred_def_id"`
CredentialRevokeID string `json:"cred_rev_id"`
SchemaID string `json:"schema_id"`
RevokeRegistryID string `json:"rev_reg_id"`
Attributes map[string]string `json:"attrs"`
}
// wql: https://ldej.nl/post/becoming-a-hyperledger-aries-developer-part-5-issue-credentials#wql-some-query-language
// https://github.com/hyperledger/aries-cloudagent-python/blob/master/aries_cloudagent/storage/basic.py#L135
func (c *Client) GetCredentials(max int, index int, wql string) ([]Credential, error) {
var results struct {
Credentials []Credential `json:"results"`
}
queryParams := map[string]string{
"max": strconv.Itoa(max),
"index": strconv.Itoa(index),
"wql": wql,
}
err := c.get("/credentials", queryParams, &results)
if err != nil {
return nil, err
}
return results.Credentials, nil
}
func (c *Client) GetCredential(credentialID string) (Credential, error) {
var credential Credential
err := c.get(fmt.Sprintf("/credential/%s", credentialID), nil, &credential)
if err != nil {
return Credential{}, err
}
return credential, nil
}
// TODO from/to query params
func (c *Client) IsCredentialRevoked(credentialID string) (bool, error) {
var result = struct {
Revoked bool `json:"revoked"`
}{}
err := c.get(fmt.Sprintf("/credential/revoked/%s", credentialID), nil, &result)
if err != nil {
return false, err
}
return result.Revoked, nil
}
func (c *Client) CredentialMimeTypes(credentialID string) (map[string]string, error) {
var result map[string]string
err := c.get(fmt.Sprintf("/credential/mime-types/%s", credentialID), nil, &result)
if err != nil {
return nil, err
}
return result, nil
}
func (c *Client) RemoveCredential(credentialID string) error {
return c.delete(fmt.Sprintf("/credential/%s", credentialID))
}
func (c *Client) FindMatchingCredentials(request PresentationRequest) (map[string]PresentationProofAttribute, error) {
requestedAttributes := map[string]PresentationProofAttribute{}
for attrName, attr := range request.RequestedAttributes {
restrictions, err := json.Marshal(attr.Restrictions[0])
if err != nil {
return nil, err
}
credentials, err := c.GetCredentials(10, 0, string(restrictions))
if err != nil {
return nil, err
}
if len(credentials) == 0 {
return nil, fmt.Errorf("no credentials found for %s", attrName)
} else if len(credentials) > 1 {
return nil, fmt.Errorf("multiple credentials found for %s", attrName)
}
if containsAllAttributes(credentials[0], attr.Names) {
requestedAttributes[attrName] = PresentationProofAttribute{
Revealed: true,
//Timestamp: time.Now().Unix(),
CredentialID: credentials[0].Referent,
}
}
}
return requestedAttributes, nil
}
func containsAllAttributes(credential Credential, attrs []string) bool {
for _, attr := range attrs {
if value, found := credential.Attributes[attr]; !found || value == "" {
return false
}
}
return true
}