-
Notifications
You must be signed in to change notification settings - Fork 46
/
permission.go
56 lines (47 loc) · 1.2 KB
/
permission.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
package types
import (
"errors"
"fmt"
"slices"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (ap *AddressPermissions) Validate() error {
if strings.TrimSpace(ap.Address) == "" {
return errors.New("address field cannot be blank")
}
if _, err := sdk.AccAddressFromBech32(ap.Address); err != nil {
return fmt.Errorf("address format error: %s", err.Error())
}
return ap.PermissionList.Validate()
}
func (p *PermissionList) Validate() error {
if len(p.Permissions) == 0 {
return errors.New("permissions field cannot be empty")
}
perms := p.Permissions
slices.Sort(perms)
perms = slices.Compact(perms)
// Check if duplicates
if len(perms) != len(p.Permissions) {
return fmt.Errorf("duplicated permission in AddressPermissions")
}
// Check if permissions list is sorted
if !p.Equal(NewPermissionsList(perms)) {
return fmt.Errorf("PermissionList is not sorted yet")
}
return nil
}
func EmptyPermissionList() PermissionList {
return PermissionList{
Permissions: []string{},
}
}
func NewPermissionsList(permission []string) PermissionList {
if len(permission) == 0 || permission == nil {
return EmptyPermissionList()
}
return PermissionList{
Permissions: permission,
}
}