github.com/0chain/gosdk@v1.17.11/README.md (about)

     1  # 0chain/gosdk
     2  
     3  The Züs client SDK written in Go programming language.
     4  
     5  - [GoSDK - a Go based SDK for Züs dStorage]()
     6    - [Züs Overview ](#overview)
     7    - [Installation](#installation)
     8    - [Mobile Builds(iOS and Android)](#mobile-builds)
     9    - [Exposing a gosdk function to mobilesdk](#expose-a-gosdk-function-to-mobilesdk)
    10    - [Export a gosdk function to WebAssembly](#export-a-gosdk-function-to-webassembly)
    11    - [Running Unit Tests](#running-unit-tests)
    12    - [FAQ](#faq)
    13  
    14  ## Overview
    15  
    16  [Züs](https://zus.network/) is a high-performance cloud on a fast blockchain offering privacy and configurable uptime. It is an alternative to traditional cloud S3 and has shown better performance on a test network due to its parallel data architecture. The technology uses erasure code to distribute the data between data and parity servers. Züs storage is configurable to provide flexibility for IT managers to design for desired security and uptime, and can design a hybrid or a multi-cloud architecture with a few clicks using [Blimp's](https://blimp.software/) workflow, and can change redundancy and providers on the fly.
    17  
    18  For instance, the user can start with 10 data and 5 parity providers and select where they are located globally, and later decide to add a provider on-the-fly to increase resilience, performance, or switch to a lower cost provider.
    19  
    20  Users can also add their own servers to the network to operate in a hybrid cloud architecture. Such flexibility allows the user to improve their regulatory, content distribution, and security requirements with a true multi-cloud architecture. Users can also construct a private cloud with all of their own servers rented across the globe to have a better content distribution, highly available network, higher performance, and lower cost.
    21  
    22  [The QoS protocol](https://medium.com/0chain/qos-protocol-weekly-debrief-april-12-2023-44524924381f) is time-based where the blockchain challenges a provider on a file that the provider must respond within a certain time based on its size to pass. This forces the provider to have a good server and data center performance to earn rewards and income.
    23  
    24  The [privacy protocol](https://zus.network/build) from Züs is unique where a user can easily share their encrypted data with their business partners, friends, and family through a proxy key sharing protocol, where the key is given to the providers, and they re-encrypt the data using the proxy key so that only the recipient can decrypt it with their private key.
    25  
    26  Züs has ecosystem apps to encourage traditional storage consumption such as [Blimp](https://blimp.software/), a S3 server and cloud migration platform, and [Vult](https://vult.network/), a personal cloud app to store encrypted data and share privately with friends and family, and [Chalk](https://chalk.software/), a high-performance story-telling storage solution for NFT artists.
    27  
    28  Other apps are [Bolt](https://bolt.holdings/), a wallet that is very secure with air-gapped 2FA split-key protocol to prevent hacks from compromising your digital assets, and it enables you to stake and earn from the storage providers; [Atlus](https://atlus.cloud/), a blockchain explorer and [Chimney](https://demo.chimney.software/), which allows anyone to join the network and earn using their server or by just renting one, with no prior knowledge required.
    29  
    30  ## Installation
    31  
    32  ### Supported Platforms
    33  
    34  This repository currently supports the following platforms:
    35  
    36  - Mac OSX Mojave 10.14.5 or Above
    37  - Linux (Ubuntu/bionic): This includes all Ubuntu 18+ platforms, so Ubuntu 19, Linux Mint 19 etc. (apt based package installer)
    38  - Linux (RHEL/CENTOS 7+): All Releases based on RHEL 7+, Centos 7+, Fedora 30 etc. (yum based package installer)
    39  
    40  ### Instructions
    41  
    42  - Go is required to build gosdk code. Instructions can be found [here](https://go.dev/doc/install)
    43  
    44  1.  Save below code as `sdkversion.go`
    45  
    46          package main
    47  
    48          import (
    49              "fmt"
    50  
    51              "github.com/0chain/gosdk/zcncore"
    52          )
    53  
    54          func main() {
    55              fmt.Println("gosdk version: ", zcncore.GetVersion())
    56          }
    57  
    58  2.  Run below command: (if you don't have gosdk already in your GOPATH)
    59  
    60          go get github.com/0chain/gosdk
    61  
    62  3.  Build the sample application sdkversion
    63  
    64          go build -o sdkversion sdkversion.go
    65  
    66  4.  Run the executable
    67  
    68          ./sdkversion
    69  
    70  5.  If it prints the gosdk version installed then setup is complete.
    71  
    72  ## Mobile Builds
    73  
    74  - gosdk can be built for iOS and Android using gomobile.
    75  - Xcode Command Line Tools is required to build the SDK for iOS.
    76  - Android studio with NDK is required to build the SDK for Android.
    77  - See [FAQ](#faq) for installing Go, gomobile Xcode or Android Studio.
    78  
    79  Steps:
    80  
    81  1.  Run the command below for the first time to setup the gomobile environment:
    82  
    83          make setup-gomobile
    84  
    85  2.  In case the Go package is not found in `golang.org/x/mobile/bind`, run:
    86      `go get golang.org/x/mobile/bind`
    87  3.  Run below commands in the root folder of the repo to build the Mobile SDK:
    88  
    89          For iOS only:
    90                  make build-ios
    91          For Android only:
    92                  make build-android
    93  
    94  ## Expose a gosdk function to mobilesdk
    95  
    96  Examples:
    97  
    98  - `mobilesdk/sdk/common.go`, which exports the functions in `core/encryption/hash.go`.
    99  
   100  Steps:
   101  
   102  1. If you are exposing:
   103  
   104     - a new function from an existing file, such as `zboxcore/sdk/allocation.go`, you should add a function to `mobilesdksdk/zbox/allocation.go`. This new function should call the gosdk function you intend to expose.
   105     - a function from a new file, you should create a new `<filename>.go` file for it. This should follow the same style as `mobilesdksdk/zbox/allocation.go`. In the new file, call the gosdk function you intend to expose.
   106  
   107  2. Build the Mobile SDK as mentioned in the 'Mobile Builds' section of this file to build the aar file used in the mobile application you are developing.
   108  
   109  ## Export a gosdk function to WebAssembly
   110  
   111  Examples:
   112  
   113  - `wasmsdk/ethwallet.go` which exports the functions in `zcncore/ethwallet.go`.
   114  - `wasmsdk/wallet.go` which exports one function in `zcncore/wallet.go`.
   115  
   116  Steps:
   117  
   118  1. If you are exporting:
   119  
   120     - a new function from `zcncore/wallet.go`, you should add to `wasmsdk/wallet.go`
   121  
   122     - a function from a new file, you should create a new `<filename>.go` file for it, in the same style as `wasmsdk/wallet.go` or `wasmsdk/ethwallet.go`
   123  
   124  2. In func main(), `https://github.com/0chain/gosdk/wasmsdk/proxy.go`, you need to add this line:
   125  
   126     ```golang
   127         js.Global().Set("YOURFUNC", js.FuncOf(YOURFUNC))
   128     ```
   129  
   130  3. Now you need to compile a new `<any_name>.wasm` (e.g. proxy.wasm). Currently, the right version to compile wasm is with Go version 1.16. So make sure you have it to make the wasm build works properly. In order to compile, run the following command:
   131  
   132     ```bash
   133     $ GOOS=js CGO_ENABLED=0 GOARCH=wasm go build -o <any_name>.wasm github.com/0chain/gosdk/wasmsdk
   134     ```
   135  
   136  ### An important note regarding export of an async function
   137  
   138  If your golang function needs to suport asynchronous execution, you need to add more wrapper code where you are returning a Promise object.
   139  
   140  See "InitZCNSDK" example:
   141  
   142  ```golang
   143  func InitZCNSDK(this js.Value, p []js.Value) interface{} {
   144  	blockWorker := p[0].String()
   145  	signscheme := p[1].String()
   146  
   147  	handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
   148  		resolve := args[0]
   149  		// reject := args[1]
   150  
   151  		go func() {
   152  			err := zcncore.InitZCNSDK(blockWorker, signscheme)
   153  			if err != nil {
   154  				fmt.Println("error:", err)
   155  			}
   156  			resolve.Invoke()
   157  		}()
   158  
   159  		return nil
   160  	})
   161  
   162  	// Create and return the Promise object
   163  	promiseConstructor := js.Global().Get("Promise")
   164  	return promiseConstructor.New(handler)
   165  }
   166  ```
   167  
   168  ## Running Unit Tests
   169  
   170  ### BLS unit test
   171  
   172  It's advisable to put GOPATH as `$TOP/../go`, to avoid conflicts with this command: `go build ./...`
   173  
   174  To run all the unit tests in `gosdk`: `go test github.com/0chain/gosdk/zboxcore/sdk -v`
   175  
   176  ```bash
   177  $ go test ./...
   178  ```
   179  
   180  To run all the unit tests in `bls0chain_test.go`, run this command from $TOP: `go test github.com/0chain/gosdk/core/zcncrypto -v`
   181  
   182  To run a specific unit test in `bls0chain_test.go`, such as `TestSignatureScheme`, run: `go test github.com/0chain/gosdk/core/zcncrypto -v -run TestSignatureScheme`
   183  
   184  To run the coverage test in `gosdk`:
   185  
   186  ```bash
   187  $ go test <path_to_folder> -coverprofile=coverage.out
   188  $ go tool cover -html=coverage.out
   189  ```
   190  
   191  ### WebAssembly
   192  
   193  #### Using go test
   194  
   195  1. You need to install nodejs first, see [this page](https://nodejs.org/en/download/) for further instructions
   196  
   197  2. Add `/path/to/go/misc/wasm` to your `$PATH` environment variable (so that `go test` can find `go_js_wasm_exec`). For example in Ubuntu, run `$export PATH=$PATH:/usr/local/go/misc/wasm/`.
   198  
   199  3. You can then run the test by following the [BLS unit test](#bls-unit-test) above by adding the prefix environment `GOOS=js CGO_ENABLED=0 GOARCH=wasm`:
   200     ```bash
   201     go test -tags test -v github.com/0chain/gosdk/wasmsdk
   202     ```
   203  
   204  #### Test in the client
   205  
   206  1. After you successfully [export the wasm package to proxy.wasm](#how-to-export-a-gosdk-function-to-webassembly), you can test the exported `proxy.wasm`.
   207  
   208  2. We currently have a test page going at the js-client-sdk repo: `https://github.com/0chain/js-client-sdk/blob/gosdk/test/index.html`
   209  
   210  3. You can replace the proxy.wasm at `https://github.com/0chain/js-client-sdk/blob/gosdk/test/proxy.wasm`
   211  
   212  4. You need to start a special test server in order to stream WASM files. Use the following command from js-client-sdk $TOP: `sudo php -S localhost:82 test/server.php`
   213  
   214  5. See "testethwallet" function in index.html for how the testing for ethwallet.go is done.
   215  
   216  6. To test the function you exported, it's probably as simple as calling "HelloWorld()". It should be a 1-liner.
   217  
   218  ### How to install `ffmpeg`
   219  
   220  #### On Ubuntu Linux
   221  
   222  ```bash
   223  sudo apt-get install ffmpeg
   224  sudo apt-get install v4l-utils
   225  ```
   226  
   227  ### FAQ
   228  
   229  - [How to install GO on any platform](https://golang.org/doc/install)
   230  - [How to install different version of GO](https://golang.org/doc/install#extra_versions)
   231  - [How to use go mod](https://blog.golang.org/using-go-modules)
   232  - [What is gomobile](https://godoc.org/golang.org/x/mobile/cmd/gomobile)
   233  - [About XCode](https://developer.apple.com/xcode/)
   234  - [Android Studio](https://developer.android.com/studio)
   235  - [Android NDK](https://developer.android.com/ndk/)