-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathapproved_root_certificates.go
87 lines (68 loc) · 2.63 KB
/
approved_root_certificates.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
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"
)
// SetApprovedRootCertificates set approvedRootCertificates in the store.
func (k Keeper) SetApprovedRootCertificates(ctx sdk.Context, approvedRootCertificates types.ApprovedRootCertificates) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(pkitypes.ApprovedRootCertificatesKeyPrefix))
b := k.cdc.MustMarshal(&approvedRootCertificates)
store.Set(pkitypes.ApprovedRootCertificatesKey, b)
}
// GetApprovedRootCertificates returns approvedRootCertificates.
func (k Keeper) GetApprovedRootCertificates(ctx sdk.Context) (val types.ApprovedRootCertificates, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(pkitypes.ApprovedRootCertificatesKeyPrefix))
b := store.Get(pkitypes.ApprovedRootCertificatesKey)
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// RemoveApprovedRootCertificates removes approvedRootCertificates from the store.
func (k Keeper) RemoveApprovedRootCertificates(ctx sdk.Context) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), pkitypes.KeyPrefix(pkitypes.ApprovedRootCertificatesKeyPrefix))
store.Delete(pkitypes.ApprovedRootCertificatesKey)
}
// Add root certificate to the list.
func (k Keeper) AddApprovedRootCertificate(ctx sdk.Context, certificate types.Certificate) {
rootCertificates, _ := k.GetApprovedRootCertificates(ctx)
certID := types.CertificateIdentifier{
Subject: certificate.Subject,
SubjectKeyId: certificate.SubjectKeyId,
}
// Check if the root cert is already there
for _, existingCertID := range rootCertificates.Certs {
if *existingCertID == certID {
return
}
}
rootCertificates.Certs = append(rootCertificates.Certs, &certID)
k.SetApprovedRootCertificates(ctx, rootCertificates)
}
// Remove root certificate from the list.
func (k Keeper) RemoveApprovedRootCertificate(
ctx sdk.Context,
subject string,
subjectKeyID string,
) {
certID := types.CertificateIdentifier{
Subject: subject,
SubjectKeyId: subjectKeyID,
}
rootCertificates, _ := k.GetApprovedRootCertificates(ctx)
certIDIndex := -1
for i, existingIdentifier := range rootCertificates.Certs {
if *existingIdentifier == certID {
certIDIndex = i
break
}
}
if certIDIndex == -1 {
return
}
rootCertificates.Certs = append(rootCertificates.Certs[:certIDIndex], rootCertificates.Certs[certIDIndex+1:]...)
k.SetApprovedRootCertificates(ctx, rootCertificates)
}