Added basic validity check for crypto address string

Added tests
Changed const to pascal case
This commit is contained in:
Ryan O'Hara-Reid
2017-07-21 19:53:55 +10:00
committed by Adrian Gallagher
parent e1c68e5ffb
commit c4e09fad08
2 changed files with 88 additions and 13 deletions

View File

@@ -1,7 +1,6 @@
package common
import (
//"bytes"
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
@@ -21,19 +20,20 @@ import (
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
)
const (
HASH_SHA1 = iota
HASH_SHA256
HASH_SHA512
HASH_SHA512_384
SATOSHIS_PER_BTC = 100000000
SATOSHIS_PER_LTC = 100000000
WEI_PER_ETHER = 1000000000000000000
HashSHA1 = iota
HashSHA256
HashSHA512
HashSHA512_348
SatoshisPerBTC = 100000000
SatoshisPerLTC = 100000000
WeiPerEther = 1000000000000000000
)
func GetMD5(input []byte) []byte {
@@ -58,19 +58,19 @@ func GetHMAC(hashType int, input, key []byte) []byte {
var hash func() hash.Hash
switch hashType {
case HASH_SHA1:
case HashSHA1:
{
hash = sha1.New
}
case HASH_SHA256:
case HashSHA256:
{
hash = sha256.New
}
case HASH_SHA512:
case HashSHA512:
{
hash = sha512.New
}
case HASH_SHA512_384:
case HashSHA512_348:
{
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 {
if StringToLower(input) == "y" || StringToLower(input) == "yes" {
return true
@@ -346,7 +360,7 @@ func WriteFile(file string, data []byte) error {
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 {
urip, err := url.Parse(uri)
if err != nil {

View File

@@ -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) {
t.Parallel()
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) {
t.Parallel()
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) {
t.Parallel()
originalInput := float64(1)