我正在尝试用golang编写一个钱包地址验证器。我为ATOM(宇宙(编写了验证器。BAND网络也使用Cosmos SDK。BAND网络和ATOM网络是相似的。
这是我写的Cosmos钱包验证器的代码:
package atom_validator
import (
"regexp"
"github.com/btcsuite/btcutil/bech32"
)
const allowed_chars = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
const atomRegex = "^(cosmos)1([" + allowed_chars + "]+)$" // cosmos + bech32 separated by "1"
func IsValidAddress(address string) bool {
match, _ := regexp.MatchString(atomRegex, address)
if match {
return verifyChecksum(address)
} else {
return false
}
}
func verifyChecksum(address string) bool {
_, decoded, _ := bech32.Decode(address)
if decoded != nil {
return len(decoded) == 32
} else {
return false
}
}
我正在寻找一种使用Golang对BAND协议进行同样验证的方法。非常感谢。
我解决了这个问题。刚刚替换了";原子";用";带";在正则表达式字符串中。
package band_validator
import (
"regexp"
"github.com/btcsuite/btcutil/bech32"
)
const allowed_chars = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
const atomRegex = "^(band)1([" + allowed_chars + "]+)$"
func IsValidAddress(address string) bool {
match, _ := regexp.MatchString(atomRegex, address)
if match {
return verifyChecksum(address)
} else {
return false
}
}
func verifyChecksum(address string) bool {
_, decoded, _ := bech32.Decode(address)
if decoded != nil {
return len(decoded) == 32
} else {
return false
}
}