-
Notifications
You must be signed in to change notification settings - Fork 45
/
keys.go
207 lines (170 loc) · 8.22 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package types
import (
"encoding/binary"
"fmt"
)
// DONTCOVER
const (
ModuleName = "subspaces"
RouterKey = ModuleName
StoreKey = ModuleName
QuerierRoute = ModuleName
ActionCreateSubspace = "create_subspace"
ActionEditSubspace = "edit_subspace"
ActionDeleteSubspace = "delete_subspace"
ActionCreateSection = "create_section"
ActionEditSection = "edit_section"
ActionMoveSection = "move_section"
ActionDeleteSection = "delete_section"
ActionCreateUserGroup = "create_user_group"
ActionEditUserGroup = "edit_user_group"
ActionMoveUserGroup = "move_user_group"
ActionSetUserGroupPermissions = "set_user_group_permissions"
ActionDeleteUserGroup = "delete_user_group"
ActionAddUserToUserGroup = "add_user_to_user_group"
ActionRemoveUserFromUserGroup = "remove_user_from_user_group"
ActionSetUserPermissions = "set_user_permissions"
DoNotModify = "[do-not-modify]"
)
var (
SubspaceIDKey = []byte{0x00}
SubspacePrefix = []byte{0x01}
GroupIDPrefix = []byte{0x02}
GroupsPrefix = []byte{0x03}
GroupsMembersPrefix = []byte{0x04}
UserPermissionsStorePrefix = []byte{0x05}
SectionIDPrefix = []byte{0x06}
SectionsPrefix = []byte{0x07}
)
// GetSubspaceIDBytes returns the byte representation of the subspaceID
func GetSubspaceIDBytes(subspaceID uint64) (subspaceIDBz []byte) {
subspaceIDBz = make([]byte, 8)
binary.BigEndian.PutUint64(subspaceIDBz, subspaceID)
return
}
// GetSubspaceIDFromBytes returns subspaceID in uint64 format from a byte array
func GetSubspaceIDFromBytes(bz []byte) (subspaceID uint64) {
return binary.BigEndian.Uint64(bz)
}
// SubspaceStoreKey returns the key for a specific subspace
func SubspaceStoreKey(subspaceID uint64) []byte {
return append(SubspacePrefix, GetSubspaceIDBytes(subspaceID)...)
}
// --------------------------------------------------------------------------------------------------------------------
// GetSectionIDBytes returns the byte representation of the sectionID
func GetSectionIDBytes(sectionID uint32) (sectionIDBz []byte) {
sectionIDBz = make([]byte, 4)
binary.BigEndian.PutUint32(sectionIDBz, sectionID)
return
}
// GetSectionIDFromBytes returns sectionID in uint32 format from a byte array
func GetSectionIDFromBytes(bz []byte) (sectionID uint32) {
return binary.BigEndian.Uint32(bz)
}
// NextSectionIDStoreKey returns the key used to store the next section id for the given subspace
func NextSectionIDStoreKey(subspaceID uint64) []byte {
return append(SectionIDPrefix, GetSubspaceIDBytes(subspaceID)...)
}
// SubspaceSectionsPrefix returns the prefix used to store all the sections for the given subspace
func SubspaceSectionsPrefix(subspaceID uint64) []byte {
return append(SectionsPrefix, GetSubspaceIDBytes(subspaceID)...)
}
// SectionStoreKey returns the key used to store the given section
func SectionStoreKey(subspaceID uint64, sectionID uint32) []byte {
return append(SubspaceSectionsPrefix(subspaceID), GetSectionIDBytes(sectionID)...)
}
// --------------------------------------------------------------------------------------------------------------------
var (
lenGroupMemberPrefix = len(GroupsMembersPrefix)
lenSubspaceID = len(GetSubspaceIDBytes(1))
lenGroupID = len(GetGroupIDBytes(1))
)
// NextGroupIDStoreKey returns the store key that is used to store the group id to be used next for the given subspace
func NextGroupIDStoreKey(subspaceID uint64) []byte {
return append(GroupIDPrefix, GetSubspaceIDBytes(subspaceID)...)
}
// GetGroupIDBytes returns the byte representation of the groupID
func GetGroupIDBytes(groupID uint32) (groupIDBz []byte) {
groupIDBz = make([]byte, 4)
binary.BigEndian.PutUint32(groupIDBz, groupID)
return
}
// GetGroupIDFromBytes returns groupID in uint32 format from a byte array
func GetGroupIDFromBytes(bz []byte) (subspaceID uint32) {
return binary.BigEndian.Uint32(bz)
}
// SubspaceGroupsPrefix returns the store prefix used to store all the groups of a given subspace
func SubspaceGroupsPrefix(subspaceID uint64) []byte {
return append(GroupsPrefix, GetSubspaceIDBytes(subspaceID)...)
}
// SectionGroupsPrefix returns the prefix used to store all the groups for the given section
func SectionGroupsPrefix(subspaceID uint64, sectionID uint32) []byte {
return append(SubspaceGroupsPrefix(subspaceID), GetSectionIDBytes(sectionID)...)
}
// GroupStoreKey returns the key used to store the group having the given id inside the specified section
func GroupStoreKey(subspaceID uint64, sectionID uint32, groupID uint32) []byte {
return append(SectionGroupsPrefix(subspaceID, sectionID), GetGroupIDBytes(groupID)...)
}
// SubspaceGroupsMembersPrefix returns the prefix used to store groups members for the subspace having the given id
func SubspaceGroupsMembersPrefix(subspaceID uint64) []byte {
return append(GroupsMembersPrefix, GetSubspaceIDBytes(subspaceID)...)
}
// GroupMembersPrefix returns the key used to store all the members of the given group inside the given subspace
func GroupMembersPrefix(subspaceID uint64, groupID uint32) []byte {
return append(SubspaceGroupsMembersPrefix(subspaceID), GetGroupIDBytes(groupID)...)
}
// GroupMemberStoreKey returns the key used to store the membership of the given user to the
// specified group inside the provided subspace
func GroupMemberStoreKey(subspaceID uint64, groupID uint32, user string) []byte {
return append(GroupMembersPrefix(subspaceID, groupID), GetAddressBytes(user)...)
}
// SplitGroupMemberStoreKey splits the given group member store key into the
// associated subspace id, group id and user address
func SplitGroupMemberStoreKey(key []byte) (subspaceID uint64, groupID uint32, user string) {
expectedMinLength := lenGroupMemberPrefix + lenSubspaceID + lenGroupID
if len(key) < expectedMinLength {
panic(fmt.Errorf("invalid key length; expected min %d got %d", expectedMinLength, len(key)))
}
key = key[lenGroupMemberPrefix:] // Trim the prefix
subspaceID = GetSubspaceIDFromBytes(key[:lenSubspaceID])
groupID = GetGroupIDFromBytes(key[+lenSubspaceID : lenSubspaceID+lenGroupID])
user = GetAddressFromBytes(key[lenSubspaceID+lenGroupID:])
return subspaceID, groupID, user
}
// --------------------------------------------------------------------------------------------------------------------
var (
lenUserPermissionPrefix = len(UserPermissionsStorePrefix)
lenSectionID = len(GetSectionIDBytes(1))
)
// GetAddressBytes returns the given user address as a byte array
func GetAddressBytes(user string) []byte {
return []byte(user)
}
// GetAddressFromBytes returns the sdk.AccAddress representation of the given user address
func GetAddressFromBytes(bz []byte) string {
return string(bz)
}
// SubspacePermissionsPrefix returns the prefix used to store user permissions for the given subspace
func SubspacePermissionsPrefix(subspaceID uint64) []byte {
return append(UserPermissionsStorePrefix, GetSubspaceIDBytes(subspaceID)...)
}
// SectionPermissionsPrefix returns the prefix used to store the permissions for the given section
func SectionPermissionsPrefix(subspaceID uint64, sectionID uint32) []byte {
return append(SubspacePermissionsPrefix(subspaceID), GetSectionIDBytes(sectionID)...)
}
// UserPermissionStoreKey returns the key used to store the permission for the given user inside the given subspace
func UserPermissionStoreKey(subspaceID uint64, sectionID uint32, user string) []byte {
return append(SectionPermissionsPrefix(subspaceID, sectionID), GetAddressBytes(user)...)
}
// SplitUserAddressPermissionKey splits a UserPermissionStoreKey into the subspace id, section id and user address
func SplitUserAddressPermissionKey(key []byte) (subspaceID uint64, sectionID uint32, user string) {
expectedMinLength := lenUserPermissionPrefix + lenSubspaceID + lenSectionID
if len(key) < expectedMinLength {
panic(fmt.Errorf("invalid key length; expected min %d but got %d", expectedMinLength, len(key)))
}
key = key[lenUserPermissionPrefix:] // Remove the prefix
subspaceID = GetSubspaceIDFromBytes(key[:lenSubspaceID])
sectionID = GetSectionIDFromBytes(key[lenSubspaceID : lenSubspaceID+lenSectionID])
user = GetAddressFromBytes(key[lenSubspaceID+lenSectionID:])
return subspaceID, sectionID, user
}