-
Notifications
You must be signed in to change notification settings - Fork 105
/
validation.go
369 lines (323 loc) · 10.3 KB
/
validation.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package cli
import (
"encoding/hex"
"fmt"
"math/big"
"regexp"
"strconv"
"strings"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/tyler-smith/go-bip39"
"github.com/urfave/cli"
"github.com/rocket-pool/rocketpool-go/types"
"github.com/rocket-pool/smartnode/shared/services/passwords"
hexutils "github.com/rocket-pool/smartnode/shared/utils/hex"
)
// Config
const (
MinDAOMemberIDLength = 3
)
//
// General types
//
// Validate command argument count
func ValidateArgCount(c *cli.Context, count int) error {
if len(c.Args()) != count {
return fmt.Errorf("Incorrect argument count; usage: %s", c.Command.UsageText)
}
return nil
}
// Validate a big int
func ValidateBigInt(name, value string) (*big.Int, error) {
val, success := big.NewInt(0).SetString(value, 0)
if !success {
return nil, fmt.Errorf("Invalid %s '%s'", name, value)
}
return val, nil
}
// Validate a boolean value
func ValidateBool(name, value string) (bool, error) {
val := strings.ToLower(value)
if !(val == "true" || val == "yes" || val == "false" || val == "no") {
return false, fmt.Errorf("Invalid %s '%s' - valid values are 'true', 'yes', 'false' and 'no'", name, value)
}
if val == "true" || val == "yes" {
return true, nil
}
return false, nil
}
// Validate an unsigned integer value
func ValidateUint(name, value string) (uint64, error) {
val, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return 0, fmt.Errorf("Invalid %s '%s'", name, value)
}
return val, nil
}
// Validate an unsigned integer value
func ValidateUint32(name, value string) (uint32, error) {
val, err := strconv.ParseUint(value, 10, 32)
if err != nil {
return 0, fmt.Errorf("Invalid %s '%s'", name, value)
}
return uint32(val), nil
}
// Validate an address
func ValidateAddress(name, value string) (common.Address, error) {
if !common.IsHexAddress(value) {
return common.Address{}, fmt.Errorf("Invalid %s '%s'", name, value)
}
return common.HexToAddress(value), nil
}
// Validate a collection of addresses
func ValidateAddresses(name, value string) ([]common.Address, error) {
elements := strings.Split(value, ",")
addresses := make([]common.Address, len(elements))
for i, element := range elements {
if !common.IsHexAddress(element) {
return nil, fmt.Errorf("Invalid address %d in %s: '%s'", i, name, element)
}
addresses[i] = common.HexToAddress(element)
}
return addresses, nil
}
// Validate a wei amount
func ValidateWeiAmount(name, value string) (*big.Int, error) {
val := new(big.Int)
if _, ok := val.SetString(value, 10); !ok {
return nil, fmt.Errorf("Invalid %s '%s'", name, value)
}
return val, nil
}
// Validate an ether amount
func ValidateEthAmount(name, value string) (float64, error) {
val, err := strconv.ParseFloat(value, 64)
if err != nil {
return 0, fmt.Errorf("Invalid %s '%s'", name, value)
}
return val, nil
}
// Validate a fraction
func ValidateFraction(name, value string) (float64, error) {
val, err := strconv.ParseFloat(value, 64)
if err != nil || val < 0 || val > 1 {
return 0, fmt.Errorf("Invalid %s '%s' - must be a number between 0 and 1", name, value)
}
return val, nil
}
// Validate a percentage
func ValidatePercentage(name, value string) (float64, error) {
val, err := strconv.ParseFloat(value, 64)
if err != nil || val < 0 || val > 100 {
return 0, fmt.Errorf("Invalid %s '%s' - must be a number between 0 and 100", name, value)
}
return val, nil
}
// Validate a token type
func ValidateTokenType(name, value string) (string, error) {
// Check if this is a token address
// This was taken from the Ethereum library: https://github.com/ethereum/go-ethereum/blob/master/common/types.go
if strings.HasPrefix(value, "0x") {
// Remove the 0x prefix
val := value[2:]
// Zero pad if it's an odd number of chars
if len(val)%2 == 1 {
val = "0" + val
}
// Attempt parsing
_, err := hex.DecodeString(val)
if err != nil {
return "", fmt.Errorf("Invalid %s '%s' - could not parse address: %w", name, value, err)
}
// If it passes, return the original value
return value, nil
}
// Not a token address, check against the well-known names
val := strings.ToLower(value)
if !(val == "eth" || val == "rpl" || val == "fsrpl" || val == "reth") {
return "", fmt.Errorf("Invalid %s '%s' - valid types are 'ETH', 'RPL', 'fsRPL', and 'rETH'", name, value)
}
return val, nil
}
// Validate a proposal type
func ValidateProposalType(name, value string) (string, error) {
val := strings.ToLower(value)
if !(val == "pending" || val == "active" || val == "succeeded" || val == "executed" || val == "cancelled" || val == "defeated" || val == "expired" || val == "all") {
return "", fmt.Errorf("Invalid %s '%s' - valid types are 'pending', 'active', 'succeeded', 'executed', 'cancelled', 'defeated', 'expired', and 'all'", name, value)
}
return val, nil
}
//
// Command specific types
//
// Validate a positive unsigned integer value
func ValidatePositiveUint(name, value string) (uint64, error) {
val, err := ValidateUint(name, value)
if err != nil {
return 0, err
}
if val == 0 {
return 0, fmt.Errorf("Invalid %s '%s' - must be greater than 0", name, value)
}
return val, nil
}
// Validate a list of comma-separated positive unsigned integer values
func ValidatePositiveUints(name, value string) ([]uint64, error) {
elements := strings.Split(value, ",")
vals := []uint64{}
for i, element := range elements {
element = strings.TrimSpace(element)
val, err := ValidateUint(name, element)
if err != nil {
return nil, fmt.Errorf("Invalid %s '%s' - element %d (%s) could not be parsed: %w", name, value, i, element, err)
}
if val == 0 {
return nil, fmt.Errorf("Invalid %s '%s' - element %d (%s) must be greater than 0", name, value, i, element)
}
vals = append(vals, val)
}
return vals, nil
}
// Validate a positive 32-bit unsigned integer value
func ValidatePositiveUint32(name, value string) (uint32, error) {
val, err := ValidateUint32(name, value)
if err != nil {
return 0, err
}
if val == 0 {
return 0, fmt.Errorf("Invalid %s '%s' - must be greater than 0", name, value)
}
return val, nil
}
// Validate a positive wei amount
func ValidatePositiveWeiAmount(name, value string) (*big.Int, error) {
val, err := ValidateWeiAmount(name, value)
if err != nil {
return nil, err
}
if val.Cmp(big.NewInt(0)) < 1 {
return nil, fmt.Errorf("Invalid %s '%s' - must be greater than 0", name, value)
}
return val, nil
}
// Validate a positive or zero wei amount
func ValidatePositiveOrZeroWeiAmount(name, value string) (*big.Int, error) {
val, err := ValidateWeiAmount(name, value)
if err != nil {
return nil, err
}
if val.Cmp(big.NewInt(0)) < 0 {
return nil, fmt.Errorf("Invalid %s '%s' - must be greater or equal to 0", name, value)
}
return val, nil
}
// Validate a positive ether amount
func ValidatePositiveEthAmount(name, value string) (float64, error) {
val, err := ValidateEthAmount(name, value)
if err != nil {
return 0, err
}
if val <= 0 {
return 0, fmt.Errorf("Invalid %s '%s' - must be greater than 0", name, value)
}
return val, nil
}
// Validate a burnable token type
func ValidateBurnableTokenType(name, value string) (string, error) {
val := strings.ToLower(value)
if !(val == "reth") {
return "", fmt.Errorf("Invalid %s '%s' - valid types are 'rETH'", name, value)
}
return val, nil
}
// Validate a node password
func ValidateNodePassword(name, value string) (string, error) {
if len(value) < passwords.MinPasswordLength {
return "", fmt.Errorf("Invalid %s '%s' - must be at least %d characters long", name, value, passwords.MinPasswordLength)
}
return value, nil
}
// Validate a wallet mnemonic phrase
func ValidateWalletMnemonic(name, value string) (string, error) {
if !bip39.IsMnemonicValid(value) {
return "", fmt.Errorf("Invalid %s '%s'", name, value)
}
return value, nil
}
// Validate a timezone location
func ValidateTimezoneLocation(name, value string) (string, error) {
if !regexp.MustCompile("^([a-zA-Z_]{2,}\\/)+[a-zA-Z_]{2,}$").MatchString(value) {
return "", fmt.Errorf("Invalid %s '%s' - must be in the format 'Country/City'", name, value)
}
return value, nil
}
// Validate a DAO member ID
func ValidateDAOMemberID(name, value string) (string, error) {
val := strings.TrimSpace(value)
if len(val) < MinDAOMemberIDLength {
return "", fmt.Errorf("Invalid %s '%s' - must be at least %d characters long", name, val, MinDAOMemberIDLength)
}
return val, nil
}
// Validate a transaction hash
func ValidateTxHash(name, value string) (common.Hash, error) {
// Remove a 0x prefix if present
if strings.HasPrefix(value, "0x") {
value = value[2:]
}
// Hash should be 64 characters long
if len(value) != hex.EncodedLen(common.HashLength) {
return common.Hash{}, fmt.Errorf("Invalid %s '%s': it must have 64 characters.", name, value)
}
// Try to parse the string (removing the prefix)
bytes, err := hex.DecodeString(value)
if err != nil {
return common.Hash{}, fmt.Errorf("Invalid %s '%s': %w", name, value, err)
}
hash := common.BytesToHash(bytes)
return hash, nil
}
// Validate a validator pubkey
func ValidatePubkey(name, value string) (types.ValidatorPubkey, error) {
pubkey, err := types.HexToValidatorPubkey(hexutils.RemovePrefix(value))
if err != nil {
return types.ValidatorPubkey{}, fmt.Errorf("Invalid %s '%s': %w", name, value, err)
}
return pubkey, nil
}
// Validate a hex-encoded byte array
func ValidateByteArray(name, value string) ([]byte, error) {
// Remove a 0x prefix if present
if strings.HasPrefix(value, "0x") {
value = value[2:]
}
// Try to parse the string (removing the prefix)
bytes, err := hex.DecodeString(value)
if err != nil {
return nil, fmt.Errorf("Invalid %s '%s': %w", name, value, err)
}
return bytes, nil
}
// Validate a duration
func ValidateDuration(name, value string) (time.Duration, error) {
duration, err := time.ParseDuration(value)
if err != nil {
return 0, fmt.Errorf("Invalid %s '%s': %w", name, value, err)
}
return duration, nil
}
// Validate a vote direction
func ValidateVoteDirection(name, value string) (types.VoteDirection, error) {
switch value {
case "abstain":
return types.VoteDirection_Abstain, nil
case "for":
return types.VoteDirection_For, nil
case "against":
return types.VoteDirection_Against, nil
case "veto":
return types.VoteDirection_AgainstWithVeto, nil
}
return 0, fmt.Errorf("Invalid %s '%s': not a valid vote direction name", name, value)
}