mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
portfolio: Add support for bech32 segwit addresses (#691)
* added portfolio support for bech32 segwit addresses * segwit addresses may have l values so including this * removed debug line * Expands bech32 address validation support * lint Co-authored-by: gloriousCode <scott.grant@thrasher.io> Co-authored-by: Scott <gloriousCode@users.noreply.github.com>
This commit is contained in:
@@ -21,34 +21,27 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/log"
|
||||
)
|
||||
|
||||
// Vars for common.go operations
|
||||
var (
|
||||
HTTPClient *http.Client
|
||||
HTTPUserAgent string
|
||||
|
||||
// ErrNotYetImplemented defines a common error across the code base that
|
||||
// alerts of a function that has not been completed or tied into main code
|
||||
ErrNotYetImplemented = errors.New("not yet implemented")
|
||||
|
||||
// ErrFunctionNotSupported defines a standardised error for an unsupported
|
||||
// wrapper function by an API
|
||||
ErrFunctionNotSupported = errors.New("unsupported wrapper function")
|
||||
m sync.Mutex
|
||||
)
|
||||
|
||||
// Const declarations for common.go operations
|
||||
const (
|
||||
SatoshisPerBTC = 100000000
|
||||
SatoshisPerLTC = 100000000
|
||||
WeiPerEther = 1000000000000000000
|
||||
// SimpleTimeFormat a common, but non-implemented time format in golang
|
||||
SimpleTimeFormat = "2006-01-02 15:04:05"
|
||||
// SimpleTimeFormatWithTimezone a common, but non-implemented time format in golang
|
||||
SimpleTimeFormatWithTimezone = "2006-01-02 15:04:05 MST"
|
||||
// GctExt is the extension for GCT Tengo script files
|
||||
GctExt = ".gct"
|
||||
)
|
||||
|
||||
// SimpleTimeFormat a common, but non-implemented time format in golang
|
||||
const (
|
||||
SimpleTimeFormat = "2006-01-02 15:04:05"
|
||||
SimpleTimeFormatWithTimezone = "2006-01-02 15:04:05 MST"
|
||||
// Vars for common.go operations
|
||||
var (
|
||||
HTTPClient *http.Client
|
||||
HTTPUserAgent string
|
||||
m sync.Mutex
|
||||
// ErrNotYetImplemented defines a common error across the code base that
|
||||
// alerts of a function that has not been completed or tied into main code
|
||||
ErrNotYetImplemented = errors.New("not yet implemented")
|
||||
// ErrFunctionNotSupported defines a standardised error for an unsupported
|
||||
// wrapper function by an API
|
||||
ErrFunctionNotSupported = errors.New("unsupported wrapper function")
|
||||
errInvalidCryptoCurrency = errors.New("invalid crypto currency")
|
||||
)
|
||||
|
||||
func initialiseHTTPClient() {
|
||||
@@ -153,13 +146,13 @@ func IsEnabled(isEnabled bool) string {
|
||||
func IsValidCryptoAddress(address, crypto string) (bool, error) {
|
||||
switch strings.ToLower(crypto) {
|
||||
case "btc":
|
||||
return regexp.MatchString("^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$", address)
|
||||
return regexp.MatchString("^(bc1|[13])[a-zA-HJ-NP-Z0-9]{25,90}$", address)
|
||||
case "ltc":
|
||||
return regexp.MatchString("^[L3M][a-km-zA-HJ-NP-Z1-9]{25,34}$", address)
|
||||
case "eth":
|
||||
return regexp.MatchString("^0x[a-km-z0-9]{40}$", address)
|
||||
default:
|
||||
return false, errors.New("invalid crypto currency")
|
||||
return false, fmt.Errorf("%w %s", errInvalidCryptoCurrency, crypto)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -31,45 +31,100 @@ func TestIsEnabled(t *testing.T) {
|
||||
func TestIsValidCryptoAddress(t *testing.T) {
|
||||
t.Parallel()
|
||||
b, err := IsValidCryptoAddress("1Mz7153HMuxXTuR2R1t78mGSdzaAtNbBWX", "bTC")
|
||||
if err != nil && !b {
|
||||
t.Errorf("Common IsValidCryptoAddress error: %s", err)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v' expected '%v'", err, nil)
|
||||
}
|
||||
if !b {
|
||||
t.Errorf("expected address '%s' to be valid", "1Mz7153HMuxXTuR2R1t78mGSdzaAtNbBWX")
|
||||
}
|
||||
|
||||
b, err = IsValidCryptoAddress("bc1qw508d6qejxtdg4y5r3zarvaly0c5xw7kv8f3t4", "bTC")
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v' expected '%v'", err, nil)
|
||||
}
|
||||
if !b {
|
||||
t.Errorf("expected address '%s' to be valid", "bc1qw508d6qejxtdg4y5r3zarvaly0c5xw7kv8f3t4")
|
||||
}
|
||||
|
||||
b, err = IsValidCryptoAddress("an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx", "bTC")
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v' expected '%v'", err, nil)
|
||||
}
|
||||
if b {
|
||||
t.Errorf("expected address '%s' to be invalid", "an84characterslonghumanreadablepartthatcontainsthenumber1andtheexcludedcharactersbio1569pvx")
|
||||
}
|
||||
|
||||
b, err = IsValidCryptoAddress("bc1qc7slrfxkknqcq2jevvvkdgvrt8080852dfjewde450xdlk4ugp7szw5tk9", "bTC")
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v' expected '%v'", err, nil)
|
||||
}
|
||||
if !b {
|
||||
t.Errorf("expected address '%s' to be valid", "bc1qc7slrfxkknqcq2jevvvkdgvrt8080852dfjewde450xdlk4ugp7szw5tk9")
|
||||
}
|
||||
|
||||
b, err = IsValidCryptoAddress("0Mz7153HMuxXTuR2R1t78mGSdzaAtNbBWX", "btc")
|
||||
if err == nil && b {
|
||||
t.Error("Common IsValidCryptoAddress error")
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v' expected '%v'", err, nil)
|
||||
}
|
||||
if b {
|
||||
t.Errorf("expected address '%s' to be invalid", "0Mz7153HMuxXTuR2R1t78mGSdzaAtNbBWX")
|
||||
}
|
||||
|
||||
b, err = IsValidCryptoAddress("1Mz7153HMuxXTuR2R1t78mGSdzaAtNbBWX", "lTc")
|
||||
if err == nil && b {
|
||||
t.Error("Common IsValidCryptoAddress error")
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v' expected '%v'", err, nil)
|
||||
}
|
||||
if b {
|
||||
t.Errorf("expected address '%s' to be invalid", "1Mz7153HMuxXTuR2R1t78mGSdzaAtNbBWX")
|
||||
}
|
||||
|
||||
b, err = IsValidCryptoAddress("3CDJNfdWX8m2NwuGUV3nhXHXEeLygMXoAj", "ltc")
|
||||
if err != nil && !b {
|
||||
t.Errorf("Common IsValidCryptoAddress error: %s", err)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v' expected '%v'", err, nil)
|
||||
}
|
||||
if !b {
|
||||
t.Errorf("expected address '%s' to be valid", "3CDJNfdWX8m2NwuGUV3nhXHXEeLygMXoAj")
|
||||
}
|
||||
|
||||
b, err = IsValidCryptoAddress("NCDJNfdWX8m2NwuGUV3nhXHXEeLygMXoAj", "lTc")
|
||||
if err == nil && b {
|
||||
t.Error("Common IsValidCryptoAddress error")
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v' expected '%v'", err, nil)
|
||||
}
|
||||
if b {
|
||||
t.Errorf("expected address '%s' to be invalid", "NCDJNfdWX8m2NwuGUV3nhXHXEeLygMXoAj")
|
||||
}
|
||||
|
||||
b, err = IsValidCryptoAddress(
|
||||
"0xb794f5ea0ba39494ce839613fffba74279579268",
|
||||
"eth",
|
||||
)
|
||||
if err != nil && b {
|
||||
t.Errorf("Common IsValidCryptoAddress error: %s", err)
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v' expected '%v'", err, nil)
|
||||
}
|
||||
if !b {
|
||||
t.Errorf("expected address '%s' to be valid", "0xb794f5ea0ba39494ce839613fffba74279579268")
|
||||
}
|
||||
|
||||
b, err = IsValidCryptoAddress(
|
||||
"xxb794f5ea0ba39494ce839613fffba74279579268",
|
||||
"eTh",
|
||||
)
|
||||
if err == nil && b {
|
||||
t.Error("Common IsValidCryptoAddress error")
|
||||
if !errors.Is(err, nil) {
|
||||
t.Errorf("received '%v' expected '%v'", err, nil)
|
||||
}
|
||||
if b {
|
||||
t.Errorf("expected address '%s' to be invalid", "xxb794f5ea0ba39494ce839613fffba74279579268")
|
||||
}
|
||||
|
||||
b, err = IsValidCryptoAddress(
|
||||
"xxb794f5ea0ba39494ce839613fffba74279579268",
|
||||
"ding",
|
||||
)
|
||||
if err == nil && b {
|
||||
t.Error("Common IsValidCryptoAddress error")
|
||||
if !errors.Is(err, errInvalidCryptoCurrency) {
|
||||
t.Errorf("received '%v' expected '%v'", err, errInvalidCryptoCurrency)
|
||||
}
|
||||
if b {
|
||||
t.Errorf("expected address '%s' to be invalid", "xxb794f5ea0ba39494ce839613fffba74279579268")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user