mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 15:09:42 +00:00
codebase: Cleanup various things (#1935)
* codebase: Rid base64/hex to string common funcs * codebase: Rid local scope variable usage and other improvements * codebase: Refactor currency pair usage across multiple exchanges - Updated HitBTC tests to use the new currency pair format. - Modified Kraken futures types to use currency.Pair instead of string for Symbol. - Adjusted Kraken wrapper methods to handle currency pairs correctly. - Refined OKX tests and types to utilize currency.Pair for instrument IDs. - Enhanced Poloniex tests to consistently use predefined currency pairs. - Streamlined order and orderbook tests to replace string pairs with currency.NewBTCUSD(). - Improved Yobit tests to utilize a standardized currency pair format. - Updated validator wrapper to use currency pairs directly instead of string conversions. * codebase: Use types.Number where possible * refactor: update PayoutFee type to types.Number for consistency * Refactor: Remove crypto functions to use standard library and other minor changes - Removed custom crypto functions for SHA256, SHA512, and MD5 from the common/crypto package. - Replaced usages of removed functions with standard library implementations in various files including: - cmd/websocket_client/main.go - engine/apiserver.go - exchanges/kraken/kraken.go - exchanges/lbank/lbank.go - exchanges/okx/okx_business_websocket.go - exchanges/kucoin/kucoin_websocket.go - gctscript/vm/vm.go - Updated tests to reflect changes in the crypto functions. - Renamed several functions for clarity, particularly in the context of order book updates across multiple exchanges. * refactor: replace assert with require for consistency in test assertions * refactor: Improve Binance futures candlestick test, standardise orderbook update function names and improve test parallelism * refactor: Replace require.Len with require.Equal for better output in TestGetFuturesKlineData
This commit is contained in:
@@ -7,8 +7,6 @@ import (
|
||||
"crypto/sha1" //nolint:gosec // Used for exchanges
|
||||
"crypto/sha256"
|
||||
"crypto/sha512"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"hash"
|
||||
"io"
|
||||
@@ -23,25 +21,6 @@ const (
|
||||
HashMD5
|
||||
)
|
||||
|
||||
// HexEncodeToString takes in a hexadecimal byte array and returns a string
|
||||
func HexEncodeToString(input []byte) string {
|
||||
return hex.EncodeToString(input)
|
||||
}
|
||||
|
||||
// Base64Decode takes in a Base64 string and returns a byte array and an error
|
||||
func Base64Decode(input string) ([]byte, error) {
|
||||
result, err := base64.StdEncoding.DecodeString(input)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// Base64Encode takes in a byte array then returns an encoded base64 string
|
||||
func Base64Encode(input []byte) string {
|
||||
return base64.StdEncoding.EncodeToString(input)
|
||||
}
|
||||
|
||||
// GetRandomSalt returns a random salt
|
||||
func GetRandomSalt(input []byte, saltLen int) ([]byte, error) {
|
||||
if saltLen <= 0 {
|
||||
@@ -60,27 +39,6 @@ func GetRandomSalt(input []byte, saltLen int) ([]byte, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetMD5 returns a MD5 hash of a byte array
|
||||
func GetMD5(input []byte) ([]byte, error) {
|
||||
m := md5.New() //nolint:gosec // hash function used by some exchanges
|
||||
_, err := m.Write(input)
|
||||
return m.Sum(nil), err
|
||||
}
|
||||
|
||||
// GetSHA512 returns a SHA512 hash of a byte array
|
||||
func GetSHA512(input []byte) ([]byte, error) {
|
||||
sha := sha512.New()
|
||||
_, err := sha.Write(input)
|
||||
return sha.Sum(nil), err
|
||||
}
|
||||
|
||||
// GetSHA256 returns a SHA256 hash of a byte array
|
||||
func GetSHA256(input []byte) ([]byte, error) {
|
||||
sha := sha256.New()
|
||||
_, err := sha.Write(input)
|
||||
return sha.Sum(nil), err
|
||||
}
|
||||
|
||||
// GetHMAC returns a keyed-hash message authentication code using the desired
|
||||
// hashtype
|
||||
func GetHMAC(hashType int, input, key []byte) ([]byte, error) {
|
||||
|
||||
@@ -2,36 +2,12 @@ package crypto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/hex"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestHexEncodeToString(t *testing.T) {
|
||||
t.Parallel()
|
||||
assert.Equal(t, "737472696e67", HexEncodeToString([]byte("string")))
|
||||
}
|
||||
|
||||
func TestBase64Decode(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
r, err := Base64Decode("aGVsbG8=")
|
||||
require.NoError(t, err, "Base64Decode must not error")
|
||||
assert.Equal(t, []byte("hello"), r, "Base64Decode should return the correct byte slice")
|
||||
|
||||
_, err = Base64Decode("-")
|
||||
assert.Error(t, err, "Base64Decode should error on invalid input")
|
||||
}
|
||||
|
||||
func TestBase64Encode(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
assert.Equal(t, "aGVsbG8=", Base64Encode([]byte("hello")),
|
||||
"Base64Encode should return the correct base64 string")
|
||||
}
|
||||
|
||||
func TestGetRandomSalt(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
@@ -47,48 +23,6 @@ func TestGetRandomSalt(t *testing.T) {
|
||||
assert.Len(t, salt, 16, "GetRandomSalt should return a salt of the specified length plus input length")
|
||||
}
|
||||
|
||||
func TestGetMD5(t *testing.T) {
|
||||
t.Parallel()
|
||||
r, err := GetMD5([]byte("I am testing the MD5 function in common!"))
|
||||
require.NoError(t, err, "GetMD5 must not error")
|
||||
assert.Equal(t, "18fddf4a41ba90a7352765e62e7a8744", hex.EncodeToString(r), "GetMD5 result should match the expected output")
|
||||
}
|
||||
|
||||
func TestGetSHA512(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalString := []byte("I am testing the GetSHA512 function in common!")
|
||||
expectedOutput := []byte(
|
||||
`a2273f492ea73fddc4f25c267b34b3b74998bd8a6301149e1e1c835678e3c0b90859fce22e4e7af33bde1711cbb924809aedf5d759d648d61774b7185c5dc02b`,
|
||||
)
|
||||
actualOutput, err := GetSHA512(originalString)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actualStr := HexEncodeToString(actualOutput)
|
||||
if !bytes.Equal(expectedOutput, []byte(actualStr)) {
|
||||
t.Errorf("Expected '%x'. Actual '%x'",
|
||||
expectedOutput, []byte(actualStr))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetSHA256(t *testing.T) {
|
||||
t.Parallel()
|
||||
originalString := []byte("I am testing the GetSHA256 function in common!")
|
||||
expectedOutput := []byte(
|
||||
"0962813d7a9f739cdcb7f0c0be0c2a13bd630167e6e54468266e4af6b1ad9303",
|
||||
)
|
||||
actualOutput, err := GetSHA256(originalString)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
actualStr := HexEncodeToString(actualOutput)
|
||||
if !bytes.Equal(expectedOutput, []byte(actualStr)) {
|
||||
t.Errorf("Expected '%x'. Actual '%x'", expectedOutput,
|
||||
[]byte(actualStr))
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetHMAC(t *testing.T) {
|
||||
t.Parallel()
|
||||
expectedSha1 := []byte{
|
||||
|
||||
Reference in New Issue
Block a user