-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathvalidate.go
96 lines (77 loc) · 2.11 KB
/
validate.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
package blockchair
import (
"regexp"
)
// Contains used with GetSupportedCrypto and/or GetSupportedCryptoEth to verify correct crypto.
func Contains(slice []string, item string) bool {
set := make(map[string]struct{}, len(slice))
for _, s := range slice {
set[s] = struct{}{}
}
_, ok := set[item]
return ok
}
// ValidateCrypto validate Bitcoin-like crypto.
func (c *Client) ValidateCrypto(crypto string) error {
if !Contains(GetSupportedCrypto(), crypto) {
return c.err1(ErrSC)
}
return nil
}
// ValidateCryptoEth validate Ethereum crypto.
func (c *Client) ValidateCryptoEth(crypto string) error {
if !Contains(GetSupportedCryptoEth(), crypto) {
return c.err1(ErrSCE)
}
return nil
}
// ValidateCryptoBoth validate both Bitcoin-like and Ethereum crypto.
func (c *Client) ValidateCryptoBoth(crypto string) error {
if !Contains(GetSupportedCryptoEth(), crypto) && !Contains(GetSupportedCrypto(), crypto) {
return c.err1(ErrSCG)
}
return nil
}
// ValidateCryptoMultichain validate crypto for multichain address check.
func (c *Client) ValidateCryptoMultichain(crypto string) error {
if !Contains(GetSupportedCryptoMultichain(), crypto) {
return c.err1(ErrSCG)
}
return nil
}
// ValidateHashEth validate Ethereum hash.
func (c *Client) ValidateHashEth(hash string) error {
r, _ := regexp.Compile(Hash)
if !r.MatchString(hash) {
return c.err4(ErrTHW, hash)
}
return nil
}
// ValidateHashesEth validate Ethereum hashes.
func (c *Client) ValidateHashesEth(hashes []string) error {
r, _ := regexp.Compile(Hash)
for i := range hashes {
if !r.MatchString(hashes[i]) {
return c.err4(ErrTHW, hashes[i])
}
}
return nil
}
// ValidateErc20Token validate ERC-20 token.
func (c *Client) ValidateErc20Token(token string) error {
r, _ := regexp.Compile("0x[0-9a-fA-F]{40}")
if !r.MatchString(token) {
return c.err4(ErrERC, token)
}
return nil
}
// ValidateErc20Tokens validate ERC-20 tokens.
func (c *Client) ValidateErc20Tokens(tokens []string) error {
r, _ := regexp.Compile("0x[0-9a-fA-F]{40}")
for i := range tokens {
if !r.MatchString(tokens[i]) {
return c.err4(ErrERC, tokens[i])
}
}
return nil
}