-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
48 lines (36 loc) · 944 Bytes
/
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
package bananogo
import (
"golang.org/x/crypto/blake2b"
"regexp"
"strings"
)
// AddressIsValid checks if the wallet address is valid,
// address: the wallet address to check,
// returns true if the wallet address is valid, false otherwise.
func AddressIsValid(address string) bool {
if address == "" {
return false
}
if len(address) != 64 {
return false
}
pattern := `^(ban)_[13]{1}[13456789abcdefghijkmnopqrstuwxyz]{59}$`
re := regexp.MustCompile(pattern)
if !re.MatchString(address) {
return false
}
pubKey, err := AddressToPublicKey(address)
if err != nil {
return false
}
withoutPrefix := strings.ReplaceAll(address, "ban_", "")
origSum := withoutPrefix[len(withoutPrefix)-8 : len(withoutPrefix)]
h, err := blake2b.New(5, nil)
if err != nil {
return false
}
h.Write(pubKey[:])
sumBytes := h.Sum(nil)
sum := base32Encode(revertBytes(sumBytes[:5]))
return sum == origSum[len(origSum)-len(sum):]
}