-
Notifications
You must be signed in to change notification settings - Fork 412
/
params.go
227 lines (205 loc) · 5.73 KB
/
params.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package types
import (
"encoding/json"
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/gogo/protobuf/jsonpb"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
var (
ParamStoreKeyUploadAccess = []byte("uploadAccess")
ParamStoreKeyInstantiateAccess = []byte("instantiateAccess")
)
var AllAccessTypes = []AccessType{
AccessTypeNobody,
AccessTypeOnlyAddress,
AccessTypeAnyOfAddresses,
AccessTypeEverybody,
}
func (a AccessType) With(addrs ...sdk.AccAddress) AccessConfig {
switch a {
case AccessTypeNobody:
return AllowNobody
case AccessTypeOnlyAddress:
if n := len(addrs); n != 1 {
panic(fmt.Sprintf("expected exactly 1 address but got %d", n))
}
if err := sdk.VerifyAddressFormat(addrs[0]); err != nil {
panic(err)
}
return AccessConfig{Permission: AccessTypeOnlyAddress, Address: addrs[0].String()}
case AccessTypeEverybody:
return AllowEverybody
case AccessTypeAnyOfAddresses:
bech32Addrs := make([]string, len(addrs))
for i, v := range addrs {
bech32Addrs[i] = v.String()
}
if err := assertValidAddresses(bech32Addrs); err != nil {
panic(sdkerrors.Wrap(err, "addresses"))
}
return AccessConfig{Permission: AccessTypeAnyOfAddresses, Addresses: bech32Addrs}
}
panic("unsupported access type")
}
func (a AccessType) String() string {
switch a {
case AccessTypeNobody:
return "Nobody"
case AccessTypeOnlyAddress:
return "OnlyAddress"
case AccessTypeEverybody:
return "Everybody"
case AccessTypeAnyOfAddresses:
return "AnyOfAddresses"
}
return "Unspecified"
}
func (a *AccessType) UnmarshalText(text []byte) error {
for _, v := range AllAccessTypes {
if v.String() == string(text) {
*a = v
return nil
}
}
*a = AccessTypeUnspecified
return nil
}
func (a AccessType) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}
func (a *AccessType) MarshalJSONPB(_ *jsonpb.Marshaler) ([]byte, error) {
return json.Marshal(a)
}
func (a *AccessType) UnmarshalJSONPB(_ *jsonpb.Unmarshaler, data []byte) error {
return json.Unmarshal(data, a)
}
func (a AccessConfig) Equals(o AccessConfig) bool {
return a.Permission == o.Permission && a.Address == o.Address
}
var (
DefaultUploadAccess = AllowEverybody
AllowEverybody = AccessConfig{Permission: AccessTypeEverybody}
AllowNobody = AccessConfig{Permission: AccessTypeNobody}
)
// ParamKeyTable returns the parameter key table.
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// DefaultParams returns default wasm parameters
func DefaultParams() Params {
return Params{
CodeUploadAccess: AllowEverybody,
InstantiateDefaultPermission: AccessTypeEverybody,
}
}
func (p Params) String() string {
out, err := yaml.Marshal(p)
if err != nil {
panic(err)
}
return string(out)
}
// ParamSetPairs returns the parameter set pairs.
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(ParamStoreKeyUploadAccess, &p.CodeUploadAccess, validateAccessConfig),
paramtypes.NewParamSetPair(ParamStoreKeyInstantiateAccess, &p.InstantiateDefaultPermission, validateAccessType),
}
}
// ValidateBasic performs basic validation on wasm parameters
func (p Params) ValidateBasic() error {
if err := validateAccessType(p.InstantiateDefaultPermission); err != nil {
return errors.Wrap(err, "instantiate default permission")
}
if err := validateAccessConfig(p.CodeUploadAccess); err != nil {
return errors.Wrap(err, "upload access")
}
return nil
}
func validateAccessConfig(i interface{}) error {
v, ok := i.(AccessConfig)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
return v.ValidateBasic()
}
func validateAccessType(i interface{}) error {
a, ok := i.(AccessType)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if a == AccessTypeUnspecified {
return sdkerrors.Wrap(ErrEmpty, "type")
}
for _, v := range AllAccessTypes {
if v == a {
return nil
}
}
return sdkerrors.Wrapf(ErrInvalid, "unknown type: %q", a)
}
// ValidateBasic performs basic validation
func (a AccessConfig) ValidateBasic() error {
switch a.Permission {
case AccessTypeUnspecified:
return sdkerrors.Wrap(ErrEmpty, "type")
case AccessTypeNobody, AccessTypeEverybody:
if len(a.Address) != 0 {
return sdkerrors.Wrap(ErrInvalid, "address not allowed for this type")
}
return nil
case AccessTypeOnlyAddress:
if len(a.Addresses) != 0 {
return ErrInvalid.Wrap("addresses field set")
}
_, err := sdk.AccAddressFromBech32(a.Address)
return err
case AccessTypeAnyOfAddresses:
if a.Address != "" {
return ErrInvalid.Wrap("address field set")
}
return sdkerrors.Wrap(assertValidAddresses(a.Addresses), "addresses")
}
return sdkerrors.Wrapf(ErrInvalid, "unknown type: %q", a.Permission)
}
func assertValidAddresses(addrs []string) error {
if len(addrs) == 0 {
return ErrEmpty
}
idx := make(map[string]struct{}, len(addrs))
for _, a := range addrs {
if _, err := sdk.AccAddressFromBech32(a); err != nil {
return sdkerrors.Wrapf(err, "address: %s", a)
}
if _, exists := idx[a]; exists {
return ErrDuplicate.Wrapf("address: %s", a)
}
idx[a] = struct{}{}
}
return nil
}
// Allowed returns if permission includes the actor.
// Actor address must be valid and not nil
func (a AccessConfig) Allowed(actor sdk.AccAddress) bool {
switch a.Permission {
case AccessTypeNobody:
return false
case AccessTypeEverybody:
return true
case AccessTypeOnlyAddress:
return a.Address == actor.String()
case AccessTypeAnyOfAddresses:
for _, v := range a.Addresses {
if v == actor.String() {
return true
}
}
return false
default:
panic("unknown type")
}
}