You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Golang验证BAND Protocol网络钱包地址?

因为BAND Protocol同样基于Cosmos SDK,其地址格式和ATOM一致,唯一区别是Bech32前缀为band,基于你现有的ATOM验证逻辑,只需要修改前缀相关的常量即可实现BAND地址验证:

package band_validator

import (
    "regexp"

    "github.com/btcsuite/btcutil/bech32"
)

const allowed_chars = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"
const bandRegex = "^(band)1([" + allowed_chars + "]+)$" // band + bech32 separated by "1"

func IsValidAddress(address string) bool {
    match, _ := regexp.MatchString(bandRegex, address)
    if match {
        return verifyChecksum(address)
    } else {
        return false
    }
}

func verifyChecksum(address string) bool {
    _, decoded, err := bech32.Decode(address)
    // 补充错误判断,避免忽略解码失败的情况
    if err != nil {
        return false
    }
    return len(decoded) == 32
}

关键改动说明:

  • 将正则表达式中的地址前缀从cosmos替换为BAND的专属前缀band
  • 优化了verifyChecksum函数,增加了对bech32.Decode返回错误的判断,避免原逻辑中忽略解码失败导致的误判

内容的提问来源于stack exchange,提问作者Seyit Gokce

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 11:20:31