-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathall_certificates_by_subject_key_id.go
147 lines (121 loc) · 5.03 KB
/
all_certificates_by_subject_key_id.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
package keeper
import (
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
pkitypes "github.com/zigbee-alliance/distributed-compliance-ledger/types/pki"
"github.com/zigbee-alliance/distributed-compliance-ledger/x/pki/types"
)
// SetAllCertificatesBySubjectKeyID set a specific AllCertificatesBySubjectKeyID in the store from its index.
func (k Keeper) SetAllCertificatesBySubjectKeyID(ctx sdk.Context, allCertificatesBySubjectKeyID types.AllCertificatesBySubjectKeyId) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.AllCertificatesBySubjectKeyIDKeyPrefix))
b := k.cdc.MustMarshal(&allCertificatesBySubjectKeyID)
store.Set(types.AllCertificatesBySubjectKeyIDKey(
allCertificatesBySubjectKeyID.SubjectKeyId,
), b)
}
// Add an All certificate to the list of All certificates with the subjectKeyId map.
func (k Keeper) AddAllCertificateBySubjectKeyID(ctx sdk.Context, certificate types.Certificate) {
k.addAllCertificatesBySubjectKeyID(ctx, certificate.SubjectKeyId, []*types.Certificate{&certificate})
}
// Add an All certificates list to All certificates with the subjectKeyId map.
func (k Keeper) AddAllCertificatesBySubjectKeyID(ctx sdk.Context, allCertificate types.AllCertificates) {
k.addAllCertificatesBySubjectKeyID(ctx, allCertificate.SubjectKeyId, allCertificate.Certs)
}
func (k Keeper) addAllCertificatesBySubjectKeyID(ctx sdk.Context, subjectKeyID string, certs []*types.Certificate) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.AllCertificatesBySubjectKeyIDKeyPrefix))
AllCertificatesBytes := store.Get(types.AllCertificatesBySubjectKeyIDKey(
subjectKeyID,
))
var AllCertificates types.AllCertificatesBySubjectKeyId
if AllCertificatesBytes == nil {
AllCertificates = types.AllCertificatesBySubjectKeyId{
SubjectKeyId: subjectKeyID,
Certs: []*types.Certificate{},
}
} else {
k.cdc.MustUnmarshal(AllCertificatesBytes, &AllCertificates)
}
AllCertificates.Certs = append(AllCertificates.Certs, certs...)
k.SetAllCertificatesBySubjectKeyID(ctx, AllCertificates)
}
// GetAllCertificatesBySubjectKeyID returns a AllCertificatesBySubjectKeyID from its index.
func (k Keeper) GetAllCertificatesBySubjectKeyID(
ctx sdk.Context,
subjectKeyID string,
) (val types.AllCertificatesBySubjectKeyId, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.AllCertificatesBySubjectKeyIDKeyPrefix))
b := store.Get(types.AllCertificatesBySubjectKeyIDKey(
subjectKeyID,
))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// RemoveAllCertificatesBySubjectKeyID removes a AllCertificatesBySubjectKeyID from the store.
func (k Keeper) RemoveAllCertificatesBySubjectKeyID(
ctx sdk.Context,
subject string,
subjectKeyID string,
) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.AllCertificatesBySubjectKeyIDKeyPrefix))
certs, found := k.GetAllCertificatesBySubjectKeyID(ctx, subjectKeyID)
if !found {
return
}
for i := 0; i < len(certs.Certs); {
if certs.Certs[i].Subject == subject {
certs.Certs = append(certs.Certs[:i], certs.Certs[i+1:]...)
} else {
i++
}
}
if len(certs.Certs) == 0 {
store.Delete(types.AllCertificatesBySubjectKeyIDKey(
subjectKeyID,
))
} else {
k.SetAllCertificatesBySubjectKeyID(ctx, certs)
}
}
func (k Keeper) RemoveAllCertificatesBySubjectKeyIDBySerialNumber(ctx sdk.Context, subject, subjectKeyID, serialNumber string) {
k._removeAllCertificatesFromSubjectKeyIDState(ctx, subjectKeyID, func(cert *types.Certificate) bool {
return cert.Subject == subject && cert.SubjectKeyId == subjectKeyID && cert.SerialNumber == serialNumber
})
}
// GetAllAllCertificatesBySubjectKeyID returns all AllCertificatesBySubjectKeyID.
func (k Keeper) GetAllAllCertificatesBySubjectKeyID(ctx sdk.Context) (list []types.AllCertificatesBySubjectKeyId) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.AllCertificatesBySubjectKeyIDKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.AllCertificatesBySubjectKeyId
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
func (k Keeper) _removeAllCertificatesFromSubjectKeyIDState(ctx sdk.Context, subjectKeyID string, filter func(cert *types.Certificate) bool) {
certs, found := k.GetAllCertificatesBySubjectKeyID(ctx, subjectKeyID)
if !found {
return
}
numCertsBefore := len(certs.Certs)
for i := 0; i < len(certs.Certs); {
cert := certs.Certs[i]
if filter(cert) {
certs.Certs = append(certs.Certs[:i], certs.Certs[i+1:]...)
} else {
i++
}
}
if len(certs.Certs) == 0 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(types.AllCertificatesBySubjectKeyIDKeyPrefix))
store.Delete(types.AllCertificatesBySubjectKeyIDKey(
subjectKeyID,
))
} else if numCertsBefore > len(certs.Certs) { // Update state only if any certificate is removed
k.SetAllCertificatesBySubjectKeyID(ctx, certs)
}
}