Skip to content

Commit

Permalink
Merge pull request #1720 from 0chain/sprint-1.19
Browse files Browse the repository at this point in the history
Sprint-1.19
  • Loading branch information
dabasov authored Feb 5, 2025
2 parents 88a3aca + c18d6a5 commit 233e527
Show file tree
Hide file tree
Showing 27 changed files with 420 additions and 104 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-release-wasm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
ls -lha
# - name: 'Upload Artifact'
# uses: actions/upload-artifact@v3
# uses: actions/upload-artifact@v4
# with:
# name: zcn.wasm
# path: zcn.wasm
10 changes: 5 additions & 5 deletions .github/workflows/build-sdks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
file_glob: true

- name: Upload Artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: zcncore-ios
path: |
Expand Down Expand Up @@ -186,7 +186,7 @@ jobs:
file_glob: true

- name: Upload Artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: zcncore-android
path: zcncore-android.tar.gz
Expand Down Expand Up @@ -257,7 +257,7 @@ jobs:
file_glob: true

- name: Upload Artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: zcncore-macos
path: |
Expand Down Expand Up @@ -325,7 +325,7 @@ jobs:
file_glob: true

- name: Upload Artifact
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: zcn-windows
path: |
Expand All @@ -352,7 +352,7 @@ jobs:
run: docker run --rm -v $PWD:/gosdk -w /gosdk golang:1.23 make wasm-build

- name: 'Upload Artifact'
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: zcn.wasm
path: zcn.wasm
13 changes: 7 additions & 6 deletions core/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package client
import (
"encoding/json"
"fmt"
"github.com/0chain/errors"
"github.com/0chain/gosdk/core/conf"
"github.com/0chain/gosdk/core/util"
"github.com/shopspring/decimal"
"log"
"net/http"
"net/url"
"sync"

"github.com/0chain/errors"
"github.com/0chain/gosdk/core/conf"
"github.com/0chain/gosdk/core/util"
"github.com/shopspring/decimal"
)

// SCRestAPIHandler is a function type to handle the response from the SC Rest API
Expand All @@ -20,7 +21,7 @@ import (
// `err` - the error if any
type SCRestAPIHandler func(response map[string][]byte, numSharders int, err error)

func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, restApiUrls ...string) ([]byte, error) {
func MakeSCRestAPICallToSharder(scAddress string, relativePath string, params map[string]string, restApiUrls ...string) ([]byte, error) {
const (
consensusThresh = float32(25.0)
ScRestApiUrl = "v1/screst/"
Expand Down Expand Up @@ -159,7 +160,7 @@ func GetBalance(clientIDs ...string) (*GetBalanceResponse, error) {
clientID = Id()
}

if res, err = MakeSCRestAPICall("", GetBalance, map[string]string{
if res, err = MakeSCRestAPICallToSharder("", GetBalance, map[string]string{
"client_id": clientID,
}, "v1/"); err != nil {
return nil, err
Expand Down
5 changes: 5 additions & 0 deletions core/client/init_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
var (
logging = logger.GetLogger()
nodeClient *Node
IsAppFlow = false
)

// Node Maintains central states of SDK (client's context, network).
Expand All @@ -34,6 +35,10 @@ type Node struct {
networkGuard sync.RWMutex
}

func SetIsAppFlow(val bool) {
IsAppFlow = true
}

// GetStableMiners Returns stable miner urls.
// Length of stable miners is depedent on config's MinSubmit and number of miners in network.
func (n *Node) GetStableMiners() []string {
Expand Down
65 changes: 64 additions & 1 deletion core/client/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,26 @@ type Client struct {
sign SignFunc

Check failure on line 36 in core/client/set.go

View workflow job for this annotation

GitHub Actions / lint

field `sign` is unused (unused)
}

type InitSdkOptions struct {
WalletJSON string
BlockWorker string
ChainID string
SignatureScheme string
Nonce int64
IsSplitWallet bool
AddWallet bool
TxnFee *int
MinConfirmation *int
MinSubmit *int
ConfirmationChainLength *int
SharderConsensous *int
ZboxHost string
ZboxAppType string
}

func init() {
sys.Sign = signHash
sys.SignWithAuth = signHash
sys.SignWithAuth = signHashWithAuth

sigC <- struct{}{}

Expand Down Expand Up @@ -78,6 +95,42 @@ var SignFn = func(hash string) (string, error) {
return ss.Sign(hash)
}

func signHashWithAuth(hash, signatureScheme string, keys []sys.KeyPair) (string, error) {
sig, err := sys.Sign(hash, signatureScheme, keys)
if err != nil {
return "", fmt.Errorf("failed to sign with split key: %v", err)
}

data, err := json.Marshal(AuthMessage{
Hash: hash,
Signature: sig,
ClientID: client.wallet.ClientID,
})
if err != nil {
return "", err
}

if sys.AuthCommon == nil {
return "", errors.New("authCommon is not set")
}

rsp, err := sys.AuthCommon(string(data))
if err != nil {
return "", err
}

var sigpk struct {
Sig string `json:"sig"`
}

err = json.Unmarshal([]byte(rsp), &sigpk)
if err != nil {
return "", err
}

return sigpk.Sig, nil
}

func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, error) {
retSignature := ""
for _, kv := range keys {
Expand Down Expand Up @@ -326,6 +379,16 @@ func InitSDK(walletJSON string,
return nil
}

func InitSDKWithWebApp(params InitSdkOptions) error {
err := InitSDK(params.WalletJSON, params.BlockWorker, params.ChainID, params.SignatureScheme, params.Nonce, params.AddWallet, *params.MinConfirmation, *params.MinSubmit, *params.ConfirmationChainLength, *params.SharderConsensous)
if err != nil {
return err
}
conf.SetZboxAppConfigs(params.ZboxHost, params.ZboxAppType)
SetIsAppFlow(true)
return nil
}

func IsSDKInitialized() bool {
return sdkInitialized
}
Expand Down
7 changes: 3 additions & 4 deletions zcncore/zauth.go → core/client/zauth.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zcncore
package client

import (
"bytes"
Expand All @@ -7,7 +7,6 @@ import (
"io"
"net/http"

"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/core/sys"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -534,7 +533,7 @@ func ZauthSignTxn(serverAddr string) sys.AuthorizeFunc {
return "", errors.Wrap(err, "failed to create HTTP request")
}
req.Header.Set("Content-Type", "application/json")
c := client.GetClient()
c := GetClient()
pubkey := c.Keys[0].PublicKey
req.Header.Set("X-Peer-Public-Key", pubkey)

Expand Down Expand Up @@ -571,7 +570,7 @@ func ZauthAuthCommon(serverAddr string) sys.AuthorizeFunc {
return "", errors.Wrap(err, "failed to create HTTP request")
}

c := client.GetClient()
c := GetClient()
pubkey := c.Keys[0].PublicKey
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Peer-Public-Key", pubkey)
Expand Down
1 change: 1 addition & 0 deletions core/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ func LoadConfig(v Reader) (Config, error) {
cfg.SignatureScheme = v.GetString("signature_scheme")
cfg.ChainID = v.GetString("chain_id")
cfg.ZauthServer = v.GetString("zauth_server")
cfg.EthereumNode = v.GetString("ethereum_node_url")

return cfg, nil
}
Expand Down
5 changes: 5 additions & 0 deletions core/conf/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ func GetClientConfig() (*Config, error) {
return cfg, nil
}

func SetZboxAppConfigs(zboxHost, zboxAppType string) {
cfg.ZboxHost = zboxHost
cfg.ZboxAppType = zboxAppType
}

// InitClientConfig set global client config
func InitClientConfig(c *Config) {
onceCfg.Do(func() {
Expand Down
86 changes: 86 additions & 0 deletions core/screstapi/http.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package screstapi

import (
"context"
"encoding/json"

"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/core/conf"
"github.com/0chain/gosdk/zboxapi"
)

var urlPathSharderToZboxMap = map[string]string{
"/getStakePoolStat": "/getStakePoolStat",
"/getUserStakePoolStat": "/getUserStakePoolStat",
"/getChallengePoolStat": "/getChallengePoolStat",
"/getBlobber": "/blobber",
"/getblobbers": "/blobbers",
"/blobber_ids": "/blobber_ids",
"/alloc_blobbers": "/blobbers/allocation",
"/get_validator": "/validator",
"/validators": "/validators",
"/allocation": "/getAllocation",
"/allocations": "/getAllocations",
"/v1/mint_nonce": "/mintNonce",
"client/get/balance": "/balance",
"/v1/not_processed_burn_tickets": "/not_processed_burn_tickets",
}

func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]string, restApiUrls ...string) (resp []byte, err error) {
_, ok := urlPathSharderToZboxMap[relativePath]
if client.IsAppFlow && ok {
resp, err = MakeSCRestAPICallToZbox(urlPathSharderToZboxMap[relativePath], params)
if err != nil {
resp, err = client.MakeSCRestAPICallToSharder(scAddress, relativePath, params, restApiUrls...)
}
} else {
resp, err = client.MakeSCRestAPICallToSharder(scAddress, relativePath, params, restApiUrls...)
}

return resp, err
}

func MakeSCRestAPICallToZbox(relativePath string, params map[string]string) ([]byte, error) {
// req, err := http.NewRequest(method, relativePath)
zboxApiClient := zboxapi.NewClient()
configObj, err := conf.GetClientConfig()
if err != nil {
return nil, err
}
zboxApiClient.SetRequest(configObj.ZboxHost, configObj.ZboxAppType)

resp, err := zboxApiClient.MakeRestApiCallToZbox(context.TODO(), relativePath, params)
if err != nil {
return nil, err
}

return resp, nil
}

func GetBalance(clientIDs ...string) (*client.GetBalanceResponse, error) {
var clientID string
if len(clientIDs) > 0 {
clientID = clientIDs[0]
} else {
clientID = client.Id()
}

const GetBalanceUrl = "client/get/balance"
var (
balance client.GetBalanceResponse
err error
resp []byte
)

if resp, err = MakeSCRestAPICall("", GetBalanceUrl, map[string]string{
"client_id": clientID,
}, "v1/"); err != nil {
return nil, err
}

if err = json.Unmarshal(resp, &balance); err != nil {
return nil, err
}

return &balance, err
}
3 changes: 2 additions & 1 deletion core/transaction/get_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package transaction

import (
"encoding/json"

"github.com/0chain/errors"
coreHttp "github.com/0chain/gosdk/core/client"
)
Expand Down Expand Up @@ -45,7 +46,7 @@ func GetConfig(configType string) (conf *InputMap, err error) {
relativePath = GET_MINERSC_CONFIGS
}

b, err = coreHttp.MakeSCRestAPICall(scAddress, relativePath, nil)
b, err = coreHttp.MakeSCRestAPICallToSharder(scAddress, relativePath, nil)
if err != nil {
return nil, errors.Wrap(err, "error requesting storage SC configs:")
}
Expand Down
Loading

0 comments on commit 233e527

Please sign in to comment.