/
keys.go
129 lines (103 loc) · 4.19 KB
/
keys.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
package types
import (
"crypto/sha256"
"encoding/hex"
"github.com/tendermint/tendermint/crypto"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func concat(bytes ...[]byte) []byte {
a := []byte{}
for _, b := range bytes {
a = append(a, b...)
}
return a
}
var (
// certifierStoreKeyPrefix is the prefix of certifier kv-store keys.
certifierStoreKeyPrefix = []byte{0x0}
// validatorStoreKeyPrefix is the prefix of certified validator kv-store keys.
validatorStoreKeyPrefix = []byte{0x1}
// platformStoreKeyPrefix is the prefix of validator host platform kv-store keys.
platformStoreKeyPrefix = []byte{0x2}
// certificateStoreKeyPrefix is the prefix of certificate kv-store keys.
certificateStoreKeyPrefix = []byte{0x5}
// libraryStoreKeyPrefix is the prefix of library kv-store keys.
libraryStoreKeyPrefix = []byte{0x6}
// certifierAliasStoreKeyPrefix is the prefix of certifier alias kv-store keys.
certifierAliasStoreKeyPrefix = []byte{0x7}
)
// CertifierStoreKey returns the kv-store key for the certifier registration.
func CertifierStoreKey(certifier sdk.AccAddress) []byte {
return concat(certifierStoreKeyPrefix, certifier.Bytes())
}
// CertifiersStoreKey returns the kv-store key for accessing all current certifiers in the security council.
func CertifiersStoreKey() []byte {
return certifierStoreKeyPrefix
}
// CertifierAliasStoreKey returns the kv-store key for the certifier alias.
func CertifierAliasStoreKey(alias string) []byte {
return concat(certifierAliasStoreKeyPrefix, []byte(alias))
}
// CertifierAliasesStoreKey returns the kv-store key for accessing aliases of all current certifiers.
func CertifierAliasesStoreKey() []byte {
return certifierAliasStoreKeyPrefix
}
// ValidatorStoreKey returns the kv-store key for the validator node certification.
func ValidatorStoreKey(validator crypto.PubKey) []byte {
return concat(validatorStoreKeyPrefix, validator.Bytes())
}
// ValidatorsStoreKey returns the kv-store key for accessing all validator node certifications.
func ValidatorsStoreKey() []byte {
return validatorStoreKeyPrefix
}
// CertificateStoreKey returns the kv-store key for accessing a given certificate (ID).
func CertificateStoreKey(bz []byte) []byte {
return concat(certificateStoreKeyPrefix, bz)
}
// CertificateStoreContentKey gets the prefix for certificate key of given certifier, certificate type,
// content type, and content.
func CertificateStoreContentKey(certType CertificateType, reqContentType RequestContentType, reqContent string) []byte {
content := concat(reqContentType.Bytes(), []byte(reqContent))
contentHash := sha256.Sum224(content)
return concat(
certificateStoreKeyPrefix,
certType.Bytes(),
contentHash[:],
)
}
// GetCertificateID constructs CertificateID (hex string) given certificate information.
// Its binary representation is the certificate store key without prefix.
func GetCertificateID(certType CertificateType, reqContent RequestContent, i uint8) CertificateID {
// Construct certificate store key (without prefix):
// certificate type | sha224(request content type | request content) | uint8
bz := make([]byte, 1)
bz[0] = i
content := concat(reqContent.RequestContentType.Bytes(), []byte(reqContent.RequestContent))
contentHash := sha256.Sum224(content)
keyWoPrefix := concat(
certType.Bytes(),
contentHash[:],
bz,
)
return CertificateID(hex.EncodeToString(keyWoPrefix))
}
// CertificatesStoreKey returns the kv-store key for accessing all certificates.
func CertificatesStoreKey() []byte {
return certificateStoreKeyPrefix
}
// LibraryStoreKey returns the kv-store key for accessing certificate library address.
func LibraryStoreKey(library sdk.AccAddress) []byte {
return concat(libraryStoreKeyPrefix, library.Bytes())
}
// LibrariesStoreKey returns the kv-store key for accessing all certificate library addresses.
func LibrariesStoreKey() []byte {
return libraryStoreKeyPrefix
}
// PlatformStoreKey returns the kv-store key for the validator host platform certificate.
func PlatformStoreKey(validator crypto.PubKey) []byte {
return append(platformStoreKeyPrefix, validator.Bytes()...)
}
// PlatformsStoreKey returns the kv-store key for accessing all platform certificates.
func PlatformsStoreKey() []byte {
return platformStoreKeyPrefix
}