feat: upgrade go.mod. add bank card utils; add cryptocurrency utils.
This commit is contained in:
98
cryptocurrency/cryptocurrency.go
Normal file
98
cryptocurrency/cryptocurrency.go
Normal file
@@ -0,0 +1,98 @@
|
||||
package cryptocurrency
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var cryptoRegexMap = map[string]*regexp.Regexp{
|
||||
"btc": regexp.MustCompile("^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,39}$"),
|
||||
"btg": regexp.MustCompile("^([GA])[a-zA-HJ-NP-Z0-9]{24,34}$"),
|
||||
"dash": regexp.MustCompile("^([X7])[a-zA-Z0-9]{33}$"),
|
||||
"dgb": regexp.MustCompile("^(D)[a-zA-Z0-9]{24,33}$"),
|
||||
"eth": regexp.MustCompile("^(0x)[a-zA-Z0-9]{40}$"),
|
||||
"smart": regexp.MustCompile("^(S)[a-zA-Z0-9]{33}$"),
|
||||
"xrp": regexp.MustCompile("^(r)[a-zA-Z0-9]{33}$"),
|
||||
"zcr": regexp.MustCompile("^(Z)[a-zA-Z0-9]{33}$"),
|
||||
"zec": regexp.MustCompile("^(t)[a-zA-Z0-9]{34}$"),
|
||||
"xmr": regexp.MustCompile("/4[0-9AB][1-9A-HJ-NP-Za-km-z]{93}$"),
|
||||
"trc": regexp.MustCompile("T[A-Za-z1-9]{33}"),
|
||||
}
|
||||
|
||||
// DetermineWalletType 判断钱包地址的类型
|
||||
func DetermineWalletType(wallet string) (string, error) {
|
||||
if strings.HasPrefix(wallet, "0x") {
|
||||
if len(wallet) != 42 {
|
||||
return "", errors.New("无效的ETH地址")
|
||||
}
|
||||
return "eth", nil
|
||||
} else if strings.HasPrefix(wallet, "T") {
|
||||
if len(wallet) != 34 {
|
||||
return "", errors.New("无效的TRC地址")
|
||||
}
|
||||
return "trc", nil
|
||||
} else {
|
||||
if len(wallet) != 34 {
|
||||
return "", errors.New("无效的OMINI地址")
|
||||
}
|
||||
return "omini", nil
|
||||
}
|
||||
}
|
||||
|
||||
// IsValidBTCAddress 是否有效的BTC地址
|
||||
// 简称:OMNI
|
||||
// 使用的比特币地址的正确格式:
|
||||
// 1. BTC 地址是包含 26-35 个字母数字字符的标识符。
|
||||
// 2. BTC 地址以数字 1、3 或 bc1 开头。
|
||||
// 3. 它包含 0 到 9 范围内的数字。
|
||||
// 4. 它允许使用大写和小写字母字符。
|
||||
// 5. 有一点需要注意:没有使用大写字母 O、大写字母 I、小写字母 l 和数字 0,以避免视觉上的歧义。
|
||||
// 6. 它不应包含空格和其他特殊字符。
|
||||
func IsValidBTCAddress(address string) bool {
|
||||
return isValidCryptocurrencyAddress("btc", address)
|
||||
}
|
||||
|
||||
// IsValidETHAddress 是否有效的ETH地址
|
||||
// 简称:ERC20
|
||||
func IsValidETHAddress(address string) bool {
|
||||
return isValidCryptocurrencyAddress("eth", address)
|
||||
}
|
||||
|
||||
// IsValidTRONAddress 是否有效的TRON地址
|
||||
// 简称:TRC20
|
||||
func IsValidTRONAddress(address string) bool {
|
||||
return isValidCryptocurrencyAddress("trc", address)
|
||||
}
|
||||
|
||||
func isValidCryptocurrencyAddress(crypto, address string) bool {
|
||||
if len(address) == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
item, ok := cryptoRegexMap[crypto]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
|
||||
if item.MatchString(address) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// IsValidCryptocurrencyAddress 校验加密货币钱包地址
|
||||
func IsValidCryptocurrencyAddress(address string) string {
|
||||
if len(address) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
for k, re := range cryptoRegexMap {
|
||||
if re.MatchString(address) {
|
||||
return k
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
70
cryptocurrency/cryptocurrency_test.go
Normal file
70
cryptocurrency/cryptocurrency_test.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package cryptocurrency
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIsValidCryptocurrencyAddress(t *testing.T) {
|
||||
type walletAddressInput struct {
|
||||
Crypto string
|
||||
Address string
|
||||
}
|
||||
|
||||
var validWalletAddresses = []walletAddressInput{
|
||||
{Crypto: "btc", Address: "1CFNjwLjZdSKB8nZopxhLaR8vvqaQKD3Bi"}, //old btc type
|
||||
{Crypto: "BTC", Address: "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"},
|
||||
{Crypto: "BTC", Address: "1RAHUEYstWetqabcFn5Au4m4GFg7xJaNVN2"},
|
||||
{Crypto: "BTC", Address: "3J98t1RHT73CNmQwertyyWrnqRhWNLy"},
|
||||
{Crypto: "BTC", Address: "bc1qarsrrr7ASHy5643ydab9re59gtzzwfrah"},
|
||||
// {Crypto: "bch", Address: "qq7ujnfl6tqx7xcdsdsrsqlqgqz8rm5stsvgx2kcvu"}, // cash address
|
||||
// {Crypto: "bch", Address: "bitcoincash:qq7ujnfl6tqx7xcdsdsrsqlqgqz8rm5stsvgx2kcvu"}, // cash address
|
||||
// {Crypto: "bch", Address: "16dhNPnPp346wzrRTkArKhqPM1ELeJDvRr"},
|
||||
{Crypto: "BTG", Address: "GakMJVF7Du16VK9dpN6nhJyLUPLXkTfqSY"},
|
||||
{Crypto: "DGB", Address: "D59P8MiMXkjs7HPn31zAnUSvRNwvNZUBYa"},
|
||||
{Crypto: "DASH", Address: "XiHMBEic8q8wX5aKqVv6zRFec7cAuYGjBV"},
|
||||
{Crypto: "ETH", Address: "0x15cc4bf4fe84fea178d2b10f89f1a6c914dfc8c2"},
|
||||
{Crypto: "ETH", Address: "0x323b5d4c32345ced77393b3530b1eed0f346429d"},
|
||||
{Crypto: "ETH", Address: "0xZYXb5d4c32345ced77393b3530b1eed0f346429d"},
|
||||
{Crypto: "ETH", Address: "0xe41d2489571d322189246dafa5ebde1f4699f498"},
|
||||
{Crypto: "ETH", Address: "0x8e215d06ea7ec1fdb4fc5fd21768f4b34ee92ef4"},
|
||||
{Crypto: "SMART", Address: "SbsLb8eM583oraW89qhbkcqZmuR4aYKkea"},
|
||||
{Crypto: "XRP", Address: "rMkfgicNKuCfXojDhcX4W2LnGoHFqhFrr6"},
|
||||
{Crypto: "ZEC", Address: "t1SBt3V8MfG4ZJ2ZDTuWfDshn4PuyvqjJV3"},
|
||||
{Crypto: "ZCR", Address: "ZXvpr2M6wvKoFcTJ57WCjT9Wkd38xkL8Fo"},
|
||||
{Crypto: "trc", Address: "TC74QG8tbtixG5Raa4fEifywgjrFs45fNz"},
|
||||
{Crypto: "trc", Address: "TFUD8x3iAZ9dF7NDCGBtSjznemEomE5rP9"},
|
||||
{Crypto: "trc", Address: "TPcKtz5TRfP4xUZSos81RmXB9K2DBqj2iu"},
|
||||
}
|
||||
|
||||
var invalidWalletAddresses = []walletAddressInput{
|
||||
{Crypto: "btc", Address: "2CFNjwLjZdSKB8nZopxhLaR8vvqaQKD3Bi"},
|
||||
{Crypto: "BTC", Address: "bc2qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"},
|
||||
{Crypto: "BTC", Address: "b1qarsrrr7ASHy5643ydab9re59gtzzwfrah"},
|
||||
{Crypto: "BTC", Address: "0J98t1RHT73CNmQwertyyWrnqRhWNLy"},
|
||||
{Crypto: "BTG", Address: "DakMJVF7Du16VK9dpN6nhJyLUPLXkTfqSY"},
|
||||
{Crypto: "DGB", Address: "G59P8MiMXkjs7HPn31zAnUSvRNwvNZUBYa"},
|
||||
{Crypto: "DASH", Address: "QiHMBEic8q8wX5aKqVv6zRFec7cAuYGjBV"},
|
||||
{Crypto: "ETH", Address: "1x15cc4bf4fe84fea178d2b10f89f1a6c914dfc8c2"},
|
||||
{Crypto: "SMART", Address: "sbsLb8eM583oraW89qhbkcqZmuR4aYKkea"},
|
||||
{Crypto: "XRP", Address: "RMkfgicNKuCfXojDhcX4W2LnGoHFqhFrr6"},
|
||||
{Crypto: "ZEC", Address: "z1SBt3V8MfG4ZJ2ZDTuWfDshn4PuyvqjJV3"},
|
||||
{Crypto: "ZCR", Address: "zXvpr2M6wvKoFcTJ57WCjT9Wkd38xkL8Fo"},
|
||||
}
|
||||
|
||||
for _, w := range validWalletAddresses {
|
||||
t.Run("valid address "+w.Crypto, func(t *testing.T) {
|
||||
result := IsValidCryptocurrencyAddress(w.Address)
|
||||
assert.True(t, strings.Compare(strings.ToLower(w.Crypto), result) == 0)
|
||||
})
|
||||
}
|
||||
|
||||
for _, w := range invalidWalletAddresses {
|
||||
t.Run("invalid address "+w.Crypto, func(t *testing.T) {
|
||||
result := IsValidCryptocurrencyAddress(w.Address)
|
||||
assert.False(t, strings.Compare(strings.ToLower(w.Crypto), result) == 0)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user