-
Notifications
You must be signed in to change notification settings - Fork 1
/
address_validator.go
56 lines (48 loc) · 1.24 KB
/
address_validator.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 water
import (
"errors"
)
var (
ErrAddressValidatorNotInitialized = errors.New("address validator not initialized properly")
ErrAddressValidationDenied = errors.New("address validation denied")
)
type addressValidator struct {
catchAll bool
allowlist map[string][]string // map[address]networks
denylist map[string][]string // map[address]networks
}
func (a *addressValidator) validate(network, address string) error {
if a.catchAll {
// only check denylist, otherwise allow
if a.denylist == nil {
return ErrAddressValidatorNotInitialized
}
if deniedNetworks, ok := a.denylist[address]; ok {
if deniedNetworks == nil {
return ErrAddressValidatorNotInitialized
}
for _, deniedNet := range deniedNetworks {
if deniedNet == network {
return ErrAddressValidationDenied
}
}
}
return nil
} else {
// only check allowlist, otherwise deny
if a.allowlist == nil {
return ErrAddressValidatorNotInitialized
}
if allowedNetworks, ok := a.allowlist[address]; ok {
if allowedNetworks == nil {
return ErrAddressValidatorNotInitialized
}
for _, allowedNet := range allowedNetworks {
if allowedNet == network {
return nil
}
}
}
return ErrAddressValidationDenied
}
}