如何验证BAND Protocol网络地址?

huangapple go评论83阅读模式
英文:

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
	}
}

huangapple
  • 本文由 发表于 2022年9月22日 07:40:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/73807928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定