-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcredential.go
158 lines (136 loc) · 3.61 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
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
package bitxid
import (
"fmt"
"github.com/meshplus/bitxhub-kit/storage"
)
// ClaimTyp represents claim type
type ClaimTyp struct {
ID string `json:"id"` // the universal id of claim type
Content []*FieldTyp `json:"content"`
}
// FieldTyp represents field type
type FieldTyp struct {
Field string `json:"field"` // field name
Typ string `json:"typ"` // field type e.g. int, float, string
}
// Marshal marshals claimTyp
func (c *ClaimTyp) Marshal() ([]byte, error) {
return Marshal(c)
}
// Unmarshal unmarshals claimTyp
func (c *ClaimTyp) Unmarshal(docBytes []byte) error {
err := Unmarshal(docBytes, &c)
return err
}
// Credential represents verifiable credential
type Credential struct {
ID string `json:"id"`
Typ string `json:"typ"`
Issuer DID `json:"issuer"`
Issued uint64 `json:"issued"`
Expiration uint64 `json:"expiration"`
Claim string `json:"claim"` // jsonSchema string
Signature Sig `json:"signature"`
}
// Sig represents signature data
type Sig struct {
Typ string `json:"typ"`
Content string `json:"content"`
}
// Marshal marshals credential
func (c *Credential) Marshal() ([]byte, error) {
return Marshal(c)
}
// Unmarshal unmarshals credential
func (c *Credential) Unmarshal(docBytes []byte) error {
err := Unmarshal(docBytes, &c)
return err
}
var _ VCManager = (*VCRegistry)(nil)
// VCRegistry represents verifiable credential management registry
type VCRegistry struct {
Store storage.Storage `json:"store"`
CTlist []string `json:"ct_list"`
}
// NewVCRegistry news a NewVCRegistry
func NewVCRegistry(s storage.Storage) (*VCRegistry, error) {
return &VCRegistry{
Store: s,
}, nil
}
// CreateClaimTyp creates new claim type
func (vcr *VCRegistry) CreateClaimTyp(ct *ClaimTyp) (string, error) {
ctb, err := ct.Marshal()
if err != nil {
return "", fmt.Errorf("claim type marshal: %w", err)
}
vcr.Store.Put(claimKey(ct.ID), ctb)
vcr.CTlist = append(vcr.CTlist, ct.ID)
return ct.ID, nil
}
// GetClaimTyp gets a claim type
func (vcr *VCRegistry) GetClaimTyp(ctid string) (*ClaimTyp, error) {
if !vcr.Store.Has(claimKey(ctid)) {
return nil, nil
}
ctb := vcr.Store.Get(claimKey(ctid))
c := &ClaimTyp{}
err := c.Unmarshal(ctb)
if err != nil {
return nil, fmt.Errorf("claim type marshal: %w", err)
}
return c, nil
}
// DeleteClaimtyp deletes a claim type
func (vcr *VCRegistry) DeleteClaimtyp(ctid string) {
vcr.Store.Delete(claimKey(ctid))
for i, ct := range vcr.CTlist {
if ct == ctid {
vcr.CTlist = append(vcr.CTlist[:i], vcr.CTlist[i+1:]...)
}
}
}
// GetAllClaimTyps gets all claim types
func (vcr *VCRegistry) GetAllClaimTyps() ([]*ClaimTyp, error) {
clist := []*ClaimTyp{}
for _, ctid := range vcr.CTlist {
ct, err := vcr.GetClaimTyp(ctid)
if err != nil {
return nil, fmt.Errorf("get claim type: %w", err)
}
clist = append(clist, ct)
}
return clist, nil
}
// StoreVC stores a vc
func (vcr *VCRegistry) StoreVC(c *Credential) (string, error) {
cb, err := c.Marshal()
if err != nil {
return "", fmt.Errorf("vc marshal: %w", err)
}
vcr.Store.Put(vcKey(c.ID), cb)
return c.ID, nil
}
// GetVC gets a vc
func (vcr *VCRegistry) GetVC(cid string) (*Credential, error) {
if !vcr.Store.Has(vcKey(cid)) {
return nil, nil
}
cb := vcr.Store.Get(vcKey(cid))
c := &Credential{}
err := c.Unmarshal(cb)
if err != nil {
return nil, fmt.Errorf("vc marshal: %w", err)
}
return c, nil
}
// DeleteVC deletes a vc
func (vcr *VCRegistry) DeleteVC(cid string) {
vcr.Store.Delete(vcKey(cid))
}
func claimKey(id string) []byte {
return []byte("claim-" + string(id))
}
func vcKey(id string) []byte {
return []byte("vc-" + string(id))
}