github.com/InjectiveLabs/sdk-go@v1.53.0/client/common/util.go (about)

     1  package common
     2  
     3  import (
     4  	"crypto/tls"
     5  	"crypto/x509"
     6  	"encoding/hex"
     7  	"fmt"
     8  	"os"
     9  	"strings"
    10  
    11  	"github.com/shopspring/decimal"
    12  
    13  	chaintypes "github.com/InjectiveLabs/sdk-go/chain/types"
    14  	"google.golang.org/grpc/credentials"
    15  )
    16  
    17  func HexToBytes(str string) ([]byte, error) {
    18  	str = strings.TrimPrefix(str, "0x")
    19  
    20  	data, err := hex.DecodeString(str)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  
    25  	return data, nil
    26  }
    27  
    28  func LoadTLSCert(path, serverName string) credentials.TransportCredentials {
    29  	if path == "" {
    30  		return nil
    31  	}
    32  
    33  	// build cert obj
    34  	rootCert, err := os.ReadFile(path)
    35  	if err != nil {
    36  		fmt.Println(err, "cannot load tls cert from path")
    37  		return nil
    38  	}
    39  	certPool := x509.NewCertPool()
    40  	if !certPool.AppendCertsFromPEM(rootCert) {
    41  		fmt.Println(err, "failed to add server CA's certificate")
    42  		return nil
    43  	}
    44  	// get domain from tcp://domain:port
    45  	domain := strings.Split(serverName, ":")[1][2:]
    46  	// nolint:gosec // we ignore the MinVersion validation because it's not a security issue
    47  	config := &tls.Config{
    48  		RootCAs:    certPool,
    49  		ServerName: domain,
    50  	}
    51  	return credentials.NewTLS(config)
    52  }
    53  
    54  func MsgResponse(data []byte) []*chaintypes.TxResponseGenericMessage {
    55  	response := chaintypes.TxResponseData{}
    56  	err := response.Unmarshal(data)
    57  	if err != nil {
    58  		panic(err)
    59  	}
    60  	return response.Messages
    61  }
    62  
    63  func RemoveExtraDecimals(value decimal.Decimal, decimalsToRemove int32) decimal.Decimal {
    64  	return value.Div(decimal.New(1, decimalsToRemove))
    65  }