我正试图用戈朗写一个钱包地址验证器。我为ATOM (宇宙)编写了验证器。波段网络也使用宇宙SDK。带网络和原子网络是相似的。
这是我写的宇宙钱包验证器的代码:
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乐队协议。谢谢。
发布于 2022-09-21 23:47:28
我解决了问题。在regex字符串中将“原子”替换为"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
}
}https://stackoverflow.com/questions/73807928
复制相似问题