英文:
How to validate BAND Protocol Network address?
问题
我正在尝试使用Golang编写一个钱包地址验证器。我已经为ATOM(Cosmos)编写了验证器。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 Protocol进行相同验证的方法。谢谢。
英文:
I am trying to write a wallet address validator with golang. I wrote the validator for ATOM (Cosmos). BAND network also using the Cosmos SDK. BAND network and ATOM network are similiar.
This is the code of the Cosmos wallet validator I wrote:
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
}
}
I am looking for a way to do the same validation for BAND Protocol using Golang. Thank you.
答案1
得分: 1
我解决了这个问题。只需在正则表达式字符串中将"atom"替换为"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
}
}
英文:
I solved the problem. Just replaced "atom" with "band" in the regex string.
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
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论