-
Notifications
You must be signed in to change notification settings - Fork 48
/
keys.go
102 lines (81 loc) · 3.65 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
package v2
import (
"encoding/binary"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// DONTCOVER
var (
SubspacePrefix = []byte{0x01}
GroupIDPrefix = []byte{0x02}
GroupsPrefix = []byte{0x03}
GroupMembersStorePrefix = []byte{0x04}
UserPermissionsStorePrefix = []byte{0x05}
)
// 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)
}
// SubspaceKey returns the key for a specific subspace
func SubspaceKey(subspaceID uint64) []byte {
return append(SubspacePrefix, GetSubspaceIDBytes(subspaceID)...)
}
// PermissionsStoreKey returns the key used to store the entire ACL for a given subspace
func PermissionsStoreKey(subspaceID uint64) []byte {
return append(UserPermissionsStorePrefix, GetSubspaceIDBytes(subspaceID)...)
}
func GetAddressBytes(user sdk.AccAddress) []byte {
return user
}
func GetAddressFromBytes(bz []byte) sdk.AccAddress {
return bz
}
// --------------------------------------------------------------------------------------------------------------------
// GroupIDStoreKey returns the store key that is used to store the group id to be used next for the given subspace
func GroupIDStoreKey(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)
}
// GroupsStoreKey returns the key used to store all the groups of a given subspace
func GroupsStoreKey(subspaceID uint64) []byte {
return append(GroupsPrefix, GetSubspaceIDBytes(subspaceID)...)
}
// GroupStoreKey returns the key used to store a group for a subspace
func GroupStoreKey(subspaceID uint64, groupID uint32) []byte {
return append(GroupsStoreKey(subspaceID), GetGroupIDBytes(groupID)...)
}
// GroupMembersStoreKey returns the key used to store all the members of the given group inside the given subspace
func GroupMembersStoreKey(subspaceID uint64, groupID uint32) []byte {
return append(append(GroupMembersStorePrefix, GetSubspaceIDBytes(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 sdk.AccAddress) []byte {
return append(GroupMembersStoreKey(subspaceID, groupID), GetAddressBytes(user)...)
}
// --------------------------------------------------------------------------------------------------------------------
// UserPermissionStoreKey returns the key used to store the permission for the given user inside the given subspace
func UserPermissionStoreKey(subspaceID uint64, user sdk.AccAddress) []byte {
return append(PermissionsStoreKey(subspaceID), GetAddressBytes(user)...)
}
// SplitUserPermissionKey splits the provided UserPermissionStoreKey into the related subspace id and user address
func SplitUserPermissionKey(key []byte) (subspaceID uint64, user sdk.AccAddress) {
key = key[1:]
subspaceID = GetSubspaceIDFromBytes(key[:8])
user = GetAddressFromBytes(key[8:])
return subspaceID, user
}