/
account.go
170 lines (138 loc) · 4.37 KB
/
account.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
package types
import (
"fmt"
yaml "gopkg.in/yaml.v3"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/tendermint/tendermint/crypto"
authExported "github.com/maticnetwork/heimdall/auth/exported"
authTypes "github.com/maticnetwork/heimdall/auth/types"
"github.com/maticnetwork/heimdall/supply/exported"
"github.com/maticnetwork/heimdall/types"
)
//
// Module account
//
var _ exported.ModuleAccountI = (*ModuleAccount)(nil)
var _ authTypes.Account = (*ModuleAccount)(nil)
type (
// ModuleAccountInterface exported module account interface
ModuleAccountInterface = exported.ModuleAccountI
)
// ModuleAccount defines an account for modules that holds coins on a pool
type ModuleAccount struct {
*authTypes.BaseAccount
Name string `json:"name" yaml:"name"` // name of the module
Permissions []string `json:"permissions" yaml:"permissions"` // permissions of module account
}
// NewModuleAddress creates an AccAddress from the hash of the module's name
func NewModuleAddress(name string) types.HeimdallAddress {
return types.BytesToHeimdallAddress(crypto.AddressHash([]byte(name)).Bytes())
}
// NewEmptyModuleAccount creates empty module account
func NewEmptyModuleAccount(name string, permissions ...string) *ModuleAccount {
moduleAddress := NewModuleAddress(name)
baseAcc := authTypes.NewBaseAccountWithAddress(moduleAddress)
if err := validatePermissions(permissions...); err != nil {
panic(err)
}
return &ModuleAccount{
BaseAccount: &baseAcc,
Name: name,
Permissions: permissions,
}
}
// NewModuleAccount creates a new ModuleAccount instance
func NewModuleAccount(ba *authTypes.BaseAccount, name string, permissions ...string) *ModuleAccount {
if err := validatePermissions(permissions...); err != nil {
panic(err)
}
return &ModuleAccount{
BaseAccount: ba,
Name: name,
Permissions: permissions,
}
}
// AddPermissions adds the permissions to the module account's list of granted
// permissions.
func (ma *ModuleAccount) AddPermissions(permissions ...string) {
ma.Permissions = append(ma.Permissions, permissions...)
}
// RemovePermission removes the permission from the list of granted permissions
// or returns an error if the permission is has not been granted.
func (ma *ModuleAccount) RemovePermission(permission string) error {
for i, perm := range ma.Permissions {
if perm == permission {
ma.Permissions = append(ma.Permissions[:i], ma.Permissions[i+1:]...)
return nil
}
}
return fmt.Errorf("cannot remove non granted permission %s", permission)
}
// HasPermission returns whether or not the module account has permission.
func (ma ModuleAccount) HasPermission(permission string) bool {
for _, perm := range ma.Permissions {
if perm == permission {
return true
}
}
return false
}
// GetName returns the the name of the holder's module
func (ma ModuleAccount) GetName() string {
return ma.Name
}
// GetPermissions returns permissions granted to the module account
func (ma ModuleAccount) GetPermissions() []string {
return ma.Permissions
}
// SetPubKey - Implements Account
func (ma ModuleAccount) SetPubKey(pubKey crypto.PubKey) error {
return fmt.Errorf("not supported for module accounts")
}
// SetSequence - Implements Account
func (ma ModuleAccount) SetSequence(seq uint64) error {
return fmt.Errorf("not supported for module accounts")
}
// String follows stringer interface
func (ma ModuleAccount) String() string {
b, err := yaml.Marshal(ma)
if err != nil {
panic(err)
}
return string(b)
}
// MarshalYAML returns the YAML representation of a ModuleAccount.
func (ma ModuleAccount) MarshalYAML() (interface{}, error) {
bs, err := yaml.Marshal(struct {
Address types.HeimdallAddress
Coins sdk.Coins
PubKey string
AccountNumber uint64
Sequence uint64
Name string
Permissions []string
}{
Address: ma.Address,
Coins: ma.Coins,
PubKey: "",
AccountNumber: ma.AccountNumber,
Sequence: ma.Sequence,
Name: ma.Name,
Permissions: ma.Permissions,
})
if err != nil {
return nil, err
}
return string(bs), nil
}
//
// Account processor
//
// AccountProcessor process supply's module account
func AccountProcessor(ga *authTypes.GenesisAccount, ba *authTypes.BaseAccount) authExported.Account {
// module accounts
if ga.ModuleName != "" {
return NewModuleAccount(ba, ga.ModuleName, ga.ModulePermissions...)
}
return ba
}