-
Notifications
You must be signed in to change notification settings - Fork 1
/
key.go
114 lines (94 loc) · 2.82 KB
/
key.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
package types
import (
"bytes"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
// ModuleName is the name of this module
ModuleName = "longy"
// StoreKey is the key used to access the store
StoreKey = ModuleName
// RouterKey is the package route
RouterKey = ModuleName
)
const (
//ServiceSeed is the seed for the service account
ServiceSeed = "master"
//BonusServiceSeed is the seed for the bonus service account
BonusServiceSeed = "bonus"
//ClaimServiceSeed is the seed for the claim service account
ClaimServiceSeed = "claim"
)
var (
//AttendeePrefix is the prefix for the attendee type
AttendeePrefix = []byte{0x0}
//ScanPrefix is the prefix for the scan type
ScanPrefix = []byte{0x1}
//PrizePrefix is the prefix for the prize type
PrizePrefix = []byte{0x2}
//ServicePrefix is the prefix for storing the public address of the service account
ServicePrefix = []byte{0x3}
//BonusKeyPrefix is the prefix for retrieving the active bonus
BonusKeyPrefix = []byte{0x4}
//BonusServicePrefix =
BonusServicePrefix = []byte{0x5}
//ClaimServicePrefix is the prefix for the account that sends claims
ClaimServicePrefix = []byte{0x6}
//KeySeparator is the separator between the prefix and the type key
KeySeparator = []byte("::")
)
// AttendeeKey will construct the appropriate key for the attendee with `id`
func AttendeeKey(addr sdk.AccAddress) []byte {
return PrefixKey(AttendeePrefix, addr[:])
}
//ScanKey returns the prefixed key for managing scans in the store
func ScanKey(id []byte) []byte {
return PrefixKey(ScanPrefix, id)
}
//PrizeKey returns the prefixed key for managing prizes in the store
func PrizeKey(id []byte) []byte {
return PrefixKey(PrizePrefix, id)
}
// ServiceKey will return the store key for the service
func ServiceKey() []byte {
return ServicePrefix
}
// BonusServiceKey -
func BonusServiceKey() []byte {
return BonusServicePrefix
}
// ClaimServiceKey returns the prefix for the claim key
func ClaimServiceKey() []byte {
return ClaimServicePrefix
}
// BonusKey -
func BonusKey() []byte {
return BonusKeyPrefix
}
//IsAttendeeKey checks the key to see if its for an attendee by checking it starts with the AttendeePrefix
func IsAttendeeKey(key []byte) bool {
return isKeyOf(key, AttendeePrefix)
}
//IsScanKey checks the key to see if its for a scan by checking it starts with the ScanPrefix
func IsScanKey(key []byte) bool {
return isKeyOf(key, ScanPrefix)
}
func isKeyOf(key []byte, prefix []byte) bool {
l := len(prefix)
if len(key) < l {
return false
}
return bytes.Equal(key[:l], prefix)
}
//PrefixKey adds prifix bits to the key
//nolint:gosec
func PrefixKey(pre []byte, key []byte) []byte {
buf := new(bytes.Buffer)
buf.Write(Prefix(pre))
buf.Write(key)
return buf.Bytes()
}
//Prefix returns the prefix for a given pre key
func Prefix(pre []byte) []byte {
return append(pre, KeySeparator...)
}