mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-04 23:16:54 +00:00
Added basic validity check for crypto address string
Added tests Changed const to pascal case
This commit is contained in:
committed by
Adrian Gallagher
parent
e1c68e5ffb
commit
c4e09fad08
@@ -1,7 +1,6 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
//"bytes"
|
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
@@ -21,19 +20,20 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
HASH_SHA1 = iota
|
HashSHA1 = iota
|
||||||
HASH_SHA256
|
HashSHA256
|
||||||
HASH_SHA512
|
HashSHA512
|
||||||
HASH_SHA512_384
|
HashSHA512_348
|
||||||
SATOSHIS_PER_BTC = 100000000
|
SatoshisPerBTC = 100000000
|
||||||
SATOSHIS_PER_LTC = 100000000
|
SatoshisPerLTC = 100000000
|
||||||
WEI_PER_ETHER = 1000000000000000000
|
WeiPerEther = 1000000000000000000
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetMD5(input []byte) []byte {
|
func GetMD5(input []byte) []byte {
|
||||||
@@ -58,19 +58,19 @@ func GetHMAC(hashType int, input, key []byte) []byte {
|
|||||||
var hash func() hash.Hash
|
var hash func() hash.Hash
|
||||||
|
|
||||||
switch hashType {
|
switch hashType {
|
||||||
case HASH_SHA1:
|
case HashSHA1:
|
||||||
{
|
{
|
||||||
hash = sha1.New
|
hash = sha1.New
|
||||||
}
|
}
|
||||||
case HASH_SHA256:
|
case HashSHA256:
|
||||||
{
|
{
|
||||||
hash = sha256.New
|
hash = sha256.New
|
||||||
}
|
}
|
||||||
case HASH_SHA512:
|
case HashSHA512:
|
||||||
{
|
{
|
||||||
hash = sha512.New
|
hash = sha512.New
|
||||||
}
|
}
|
||||||
case HASH_SHA512_384:
|
case HashSHA512_348:
|
||||||
{
|
{
|
||||||
hash = sha512.New384
|
hash = sha512.New384
|
||||||
}
|
}
|
||||||
@@ -176,6 +176,20 @@ func IsEnabled(isEnabled bool) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsValidCryptoAddress validates your cryptocurrency address string using the regexp package
|
||||||
|
func IsValidCryptoAddress(address, crypto string) (bool, error) {
|
||||||
|
switch StringToLower(crypto) {
|
||||||
|
case "btc":
|
||||||
|
return regexp.MatchString("^[13][a-km-zA-HJ-NP-Z1-9]{25,34}$", address)
|
||||||
|
case "ltc":
|
||||||
|
return regexp.MatchString("^[L3][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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func YesOrNo(input string) bool {
|
func YesOrNo(input string) bool {
|
||||||
if StringToLower(input) == "y" || StringToLower(input) == "yes" {
|
if StringToLower(input) == "y" || StringToLower(input) == "yes" {
|
||||||
return true
|
return true
|
||||||
@@ -346,7 +360,7 @@ func WriteFile(file string, data []byte) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetURIPath returns the path of a URL given a URL
|
// GetURIPath returns the path of a URL given a URI
|
||||||
func GetURIPath(uri string) string {
|
func GetURIPath(uri string) string {
|
||||||
urip, err := url.Parse(uri)
|
urip, err := url.Parse(uri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -23,6 +23,43 @@ func TestIsEnabled(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIsValidCryptoAddress(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
b, err := IsValidCryptoAddress("1Mz7153HMuxXTuR2R1t78mGSdzaAtNbBWX", "bTC")
|
||||||
|
if err != nil && !b {
|
||||||
|
t.Errorf("Test Failed - Common IsValidCryptoAddress error: %s", err)
|
||||||
|
}
|
||||||
|
b, err = IsValidCryptoAddress("0Mz7153HMuxXTuR2R1t78mGSdzaAtNbBWX", "btc")
|
||||||
|
if err == nil && b {
|
||||||
|
t.Error("Test Failed - Common IsValidCryptoAddress error")
|
||||||
|
}
|
||||||
|
b, err = IsValidCryptoAddress("1Mz7153HMuxXTuR2R1t78mGSdzaAtNbBWX", "lTc")
|
||||||
|
if err == nil && b {
|
||||||
|
t.Error("Test Failed - Common IsValidCryptoAddress error")
|
||||||
|
}
|
||||||
|
b, err = IsValidCryptoAddress("3CDJNfdWX8m2NwuGUV3nhXHXEeLygMXoAj", "ltc")
|
||||||
|
if err != nil && !b {
|
||||||
|
t.Errorf("Test Failed - Common IsValidCryptoAddress error: %s", err)
|
||||||
|
}
|
||||||
|
b, err = IsValidCryptoAddress("NCDJNfdWX8m2NwuGUV3nhXHXEeLygMXoAj", "lTc")
|
||||||
|
if err == nil && b {
|
||||||
|
t.Error("Test Failed - Common IsValidCryptoAddress error")
|
||||||
|
}
|
||||||
|
b, err = IsValidCryptoAddress("0xb794f5ea0ba39494ce839613fffba74279579268", "eth")
|
||||||
|
if err != nil && b {
|
||||||
|
t.Errorf("Test Failed - Common IsValidCryptoAddress error: %s", err)
|
||||||
|
}
|
||||||
|
b, err = IsValidCryptoAddress("xxb794f5ea0ba39494ce839613fffba74279579268", "eTh")
|
||||||
|
if err == nil && b {
|
||||||
|
t.Error("Test Failed - Common IsValidCryptoAddress error")
|
||||||
|
}
|
||||||
|
b, err = IsValidCryptoAddress("xxb794f5ea0ba39494ce839613fffba74279579268", "ding")
|
||||||
|
if err == nil && b {
|
||||||
|
t.Error("Test Failed - Common IsValidCryptoAddress error")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGetMD5(t *testing.T) {
|
func TestGetMD5(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
var originalString = []byte("I am testing the MD5 function in common!")
|
var originalString = []byte("I am testing the MD5 function in common!")
|
||||||
@@ -168,6 +205,17 @@ func TestSplitStrings(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestTrimString(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
originalInput := "abcd"
|
||||||
|
cutset := "ad"
|
||||||
|
expectedOutput := "bc"
|
||||||
|
actualResult := TrimString(originalInput, cutset)
|
||||||
|
if expectedOutput != actualResult {
|
||||||
|
t.Errorf("Test failed. Expected '%s'. Actual '%s'", expectedOutput, actualResult)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRoundFloat(t *testing.T) {
|
func TestRoundFloat(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
originalInput := float64(1.4545445445)
|
originalInput := float64(1.4545445445)
|
||||||
@@ -179,6 +227,19 @@ func TestRoundFloat(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestYesOrNo(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
if !YesOrNo("y") {
|
||||||
|
t.Error("Test failed - Common YesOrNo Error.")
|
||||||
|
}
|
||||||
|
if !YesOrNo("yes") {
|
||||||
|
t.Error("Test failed - Common YesOrNo Error.")
|
||||||
|
}
|
||||||
|
if YesOrNo("ding") {
|
||||||
|
t.Error("Test failed - Common YesOrNo Error.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestCalculateFee(t *testing.T) {
|
func TestCalculateFee(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
originalInput := float64(1)
|
originalInput := float64(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user