github.com/0chain/gosdk@v1.17.11/mobilesdk/zboxapi/client.go (about)

     1  // Subpackage to provide interface for zboxapi SDK (dealing with apps backend) to be used to build the mobile SDK.
     2  package zboxapi
     3  
     4  /*
     5  #include <stdlib.h>
     6  */
     7  
     8  import (
     9  	"C"
    10  )
    11  
    12  import (
    13  	"context"
    14  	"errors"
    15  
    16  	"github.com/0chain/gosdk/core/logger"
    17  	"github.com/0chain/gosdk/zboxapi"
    18  	"github.com/0chain/gosdk/zboxcore/client"
    19  	"go.uber.org/zap"
    20  )
    21  
    22  var (
    23  	zboxApiClient            *zboxapi.Client
    24  	ErrZboxApiNotInitialized = errors.New("zboxapi: zboxapi client is not initialized")
    25  	ErrZboxApiInvalidWallet  = errors.New("zboxapi: invalid wallet")
    26  	logging                  logger.Logger
    27  )
    28  
    29  // Init initialize the zbox api client for the mobile sdk
    30  //   - baseUrl is the base url of the server
    31  //   - appType is the type of the application
    32  func Init(baseUrl, appType string) {
    33  	zboxApiClient = zboxapi.NewClient()
    34  	zboxApiClient.SetRequest(baseUrl, appType)
    35  
    36  	c := client.GetClient()
    37  	if c != nil {
    38  		err := SetWallet(client.GetClientID(), client.GetClientPrivateKey(), client.GetClientPublicKey()) //nolint: errcheck
    39  		if err != nil {
    40  			logging.Error("SetWallet", zap.Error(err))
    41  		}
    42  	} else {
    43  		logging.Info("SetWallet: skipped")
    44  	}
    45  }
    46  
    47  // SetWallet set the client's wallet information for the zbox api client
    48  //   - clientID is the client id
    49  //   - clientPrivateKey is the client private key
    50  //   - clientPublicKey is the client public key
    51  func SetWallet(clientID, clientPrivateKey, clientPublicKey string) error {
    52  	if zboxApiClient == nil {
    53  		return ErrZboxApiNotInitialized
    54  	}
    55  	if clientID != "" && clientPrivateKey != "" && clientPublicKey != "" {
    56  		zboxApiClient.SetWallet(clientID, clientPrivateKey, clientPublicKey)
    57  		return nil
    58  	}
    59  
    60  	return ErrZboxApiInvalidWallet
    61  }
    62  
    63  // GetCsrfToken create a fresh CSRF token
    64  func GetCsrfToken() (string, error) {
    65  	if zboxApiClient == nil {
    66  		return "", ErrZboxApiNotInitialized
    67  	}
    68  	return zboxApiClient.GetCsrfToken(context.TODO())
    69  }
    70  
    71  // CreateJwtToken creates JWT token with the help of provided userID.
    72  func CreateJwtToken(userID, accessToken string) (string, error) {
    73  	if zboxApiClient == nil {
    74  		return "", ErrZboxApiNotInitialized
    75  	}
    76  	return zboxApiClient.CreateJwtToken(context.TODO(), userID, accessToken)
    77  }
    78  
    79  // RefreshJwtToken refreshes JWT token
    80  func RefreshJwtToken(userID string, token string) (string, error) {
    81  	if zboxApiClient == nil {
    82  		return "", ErrZboxApiNotInitialized
    83  	}
    84  	return zboxApiClient.RefreshJwtToken(context.TODO(), userID, token)
    85  }