Skip to content

Commit

Permalink
Merge pull request #8 from 0chain/feat/enterprise-blobber
Browse files Browse the repository at this point in the history
Merge staging
  • Loading branch information
dabasov authored Sep 3, 2024
2 parents c0ae8c0 + 75bab85 commit c027015
Show file tree
Hide file tree
Showing 59 changed files with 1,776 additions and 611 deletions.
15 changes: 12 additions & 3 deletions core/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ type Config struct {
// ZboxAppType app type name
ZboxAppType string `json:"zbox_app_type"`
// SharderConsensous is consensous for when quering for SCRestAPI calls
SharderConsensous int `json:"sharder_consensous"`
SharderConsensous int `json:"sharder_consensous"`
ZauthServer string `json:"zauth_server"`
V *viper.Viper `json:"-"`
}

// LoadConfigFile load and parse SDK Config from file
Expand All @@ -104,7 +106,14 @@ func LoadConfigFile(file string) (Config, error) {
return cfg, thrown.Throw(ErrBadParsing, err.Error())
}

return LoadConfig(v)
cfg, err = LoadConfig(v)
if err != nil {
return cfg, err
}

cfg.V = v

return cfg, nil
}

// LoadConfig load and parse config
Expand Down Expand Up @@ -170,9 +179,9 @@ func LoadConfig(v Reader) (Config, error) {

cfg.SignatureScheme = v.GetString("signature_scheme")
cfg.ChainID = v.GetString("chain_id")
cfg.ZauthServer = v.GetString("zauth.server")

return cfg, nil

}

func isURL(s string) bool {
Expand Down
4 changes: 4 additions & 0 deletions core/conf/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ func TestLoadConfig(t *testing.T) {
var mockDefaultReader = func() Reader {
reader := &mocks.Reader{}
reader.On("GetString", "block_worker").Return("http://127.0.0.1:9091/dns")
reader.On("GetString", "zauth.server").Return("http://127.0.0.1:8090/")
reader.On("GetInt", "min_submit").Return(0)
reader.On("GetInt", "min_confirmation").Return(0)
reader.On("GetInt", "max_txn_query").Return(0)
Expand Down Expand Up @@ -41,6 +42,7 @@ func TestLoadConfig(t *testing.T) {

reader := &mocks.Reader{}
reader.On("GetString", "block_worker").Return("")
reader.On("GetString", "zauth.server").Return("")
reader.On("GetInt", "min_submit").Return(0)
reader.On("GetInt", "min_confirmation").Return(0)
reader.On("GetInt", "max_txn_query").Return(0)
Expand Down Expand Up @@ -85,6 +87,7 @@ func TestLoadConfig(t *testing.T) {

reader := &mocks.Reader{}
reader.On("GetString", "block_worker").Return("https://127.0.0.1:9091/dns")
reader.On("GetString", "zauth.server").Return("http://127.0.0.1:8090/")
reader.On("GetInt", "min_submit").Return(101)
reader.On("GetInt", "min_confirmation").Return(0)
reader.On("GetInt", "max_txn_query").Return(0)
Expand Down Expand Up @@ -119,6 +122,7 @@ func TestLoadConfig(t *testing.T) {

reader := &mocks.Reader{}
reader.On("GetString", "block_worker").Return("https://127.0.0.1:9091/dns")
reader.On("GetString", "zauth.server").Return("http://127.0.0.1:8090/")
reader.On("GetInt", "min_submit").Return(0)
reader.On("GetInt", "min_confirmation").Return(101)
reader.On("GetInt", "max_txn_query").Return(0)
Expand Down
14 changes: 13 additions & 1 deletion core/sys/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var (
Sleep = time.Sleep

// Sign sign method. it should be initialized on different platform.
Sign SignFunc
Sign SignFunc
SignWithAuth SignFunc

// Verify verify method. it should be initialized on different platform.
Verify VerifyFunc
Expand All @@ -23,4 +24,15 @@ var (
VerifyWith VerifyWithFunc

Authorize AuthorizeFunc

AuthCommon AuthorizeFunc
)

// SetAuthorize sets the authorize callback function
func SetAuthorize(auth AuthorizeFunc) {
Authorize = auth
}

func SetAuthCommon(auth AuthorizeFunc) {
AuthCommon = auth
}
5 changes: 3 additions & 2 deletions core/transaction/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,14 +253,15 @@ func NewTransactionReceipt(t *Transaction) *TxnReceipt {
return &TxnReceipt{Transaction: t}
}

func (t *Transaction) VerifyTransaction(verifyHandler VerifyFunc) (bool, error) {
// VerifySigWith verify the signature with the given public key and handler
func (t *Transaction) VerifySigWith(pubkey string, verifyHandler VerifyFunc) (bool, error) {
// Store the hash
hash := t.Hash
t.ComputeHashData()
if t.Hash != hash {
return false, errors.New("verify_transaction", fmt.Sprintf(`{"error":"hash_mismatch", "expected":"%v", "actual":%v"}`, t.Hash, hash))
}
return verifyHandler(t.PublicKey, t.Signature, t.Hash)
return verifyHandler(pubkey, t.Signature, t.Hash)
}

func SendTransactionSync(txn *Transaction, miners []string) error {
Expand Down
3 changes: 1 addition & 2 deletions core/version/version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

//====== THIS IS AUTOGENERATED FILE. DO NOT MODIFY ========

package version
const VERSIONSTR = "v1.16.3-10-g66360b13"

const VERSIONSTR = "v1.16.3-10-g66360b13"
7 changes: 6 additions & 1 deletion core/zcncrypto/bls0chain_herumi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,14 @@ func TestCombinedSignAndVerify(t *testing.T) {
}

func TestSplitKey(t *testing.T) {
primaryKeyStr := `c36f2f92b673cf057a32e8bd0ca88888e7ace40337b737e9c7459fdc4c521918`
primaryKeyStr := `872eac6370c72093535fa395ad41a08ee90c9d0d46df9461eb2515451f389d1b`
// primaryKeyStr := `c36f2f92b673cf057a32e8bd0ca88888e7ace40337b737e9c7459fdc4c521918`
sig0 := NewSignatureScheme("bls0chain")
err := sig0.SetPrivateKey(primaryKeyStr)
if err != nil {
t.Fatalf("Set private key failed - %s", errors.Top(err))
}
data = "823bb3dc0b80a6c86922a884e63908cb9e963ef488688b41e32cbf4d84471a1f"
hash := Sha3Sum256(data)
signature, err := sig0.Sign(hash)
if err != nil {
Expand All @@ -170,15 +172,18 @@ func TestSplitKey(t *testing.T) {
for i := 0; i < numSplitKeys; i++ {
sigAggScheme[i] = NewSignatureScheme("bls0chain")
err = sigAggScheme[i].SetPrivateKey(w.Keys[i].PrivateKey)
fmt.Println("seckey:", sigAggScheme[i].GetPrivateKey())

require.NoError(t, err)
}
var aggrSig string
for i := 1; i < numSplitKeys; i++ {
tmpSig, _ := sigAggScheme[i].Sign(hash)
fmt.Println("tmpSig:", tmpSig)
aggrSig, _ = sigAggScheme[0].Add(tmpSig, hash)
}
if aggrSig != signature {
t.Fatalf("split key signature failed")
}
fmt.Println("aggrSig:", aggrSig)
}
47 changes: 27 additions & 20 deletions core/zcncrypto/signature_scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package zcncrypto

import (
"encoding/json"
"fmt"
"os"

"github.com/0chain/errors"
"github.com/0chain/gosdk/core/encryption"
Expand All @@ -20,26 +22,15 @@ type KeyPair struct {

// Wallet represents client wallet information
type Wallet struct {
// ClientID client unique identifier
ClientID string `json:"client_id"`

// ClientKey client public key
ClientKey string `json:"client_key"`

// Keys private and public key pair
Keys []KeyPair `json:"keys"`

// Mnemonic recovery phrase of the wallet
Mnemonic string `json:"mnemonics"`

// Version version of the wallet
Version string `json:"version"`

// DateCreated date of wallet creation
DateCreated string `json:"date_created"`

// Nonce nonce of the wallet
Nonce int64 `json:"nonce"`
ClientID string `json:"client_id"`
ClientKey string `json:"client_key"`
PeerPublicKey string `json:"peer_public_key"` // Peer public key exists only in split wallet
Keys []KeyPair `json:"keys"`
Mnemonic string `json:"mnemonics"`
Version string `json:"version"`
DateCreated string `json:"date_created"`
Nonce int64 `json:"nonce"`
IsSplit bool `json:"is_split"`
}

// SignatureScheme - an encryption scheme for signing and verifying messages
Expand Down Expand Up @@ -96,6 +87,22 @@ func (w *Wallet) Sign(hash, scheme string) (string, error) {
return sigScheme.Sign(hash)
}

// SetSplitKeys sets split keys and wipes out mnemonic and original primary keys
func (w *Wallet) SetSplitKeys(sw *Wallet) {
*w = *sw
}

func (w *Wallet) SaveTo(file string) error {
d, err := json.Marshal(w)
if err != nil {
return err
}

fmt.Println("Saving wallet to file: ", string(d))

return os.WriteFile(file, d, 0644)
}

func IsMnemonicValid(mnemonic string) bool {
return bip39.IsMnemonicValid(mnemonic)
}
Expand Down
14 changes: 7 additions & 7 deletions mobilesdk/zboxapi/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,30 @@ func GetCsrfToken() (string, error) {

// CreateJwtSession create a jwt session for the given phone number
// - phoneNumber is the phone number
func CreateJwtSession(phoneNumber string) (int64, error) {
func CreateJwtSession(userID string) (int64, error) {
if zboxApiClient == nil {
return 0, ErrZboxApiNotInitialized
}
return zboxApiClient.CreateJwtSession(context.TODO(), phoneNumber)
return zboxApiClient.CreateJwtSession(context.TODO(), userID)
}

// CreateJwtToken create a fresh jwt token for the given phone number
// CreateJwtToken create a fresh jwt token for the given user id
// - phoneNumber is the phone number
// - jwtSessionID is the jwt session id
// - otp is the one time password
func CreateJwtToken(phoneNumber string, jwtSessionID int64, otp string) (string, error) {
func CreateJwtToken(userID string, jwtSessionID int64) (string, error) {
if zboxApiClient == nil {
return "", ErrZboxApiNotInitialized
}
return zboxApiClient.CreateJwtToken(context.TODO(), phoneNumber, jwtSessionID, otp)
return zboxApiClient.CreateJwtToken(context.TODO(), userID, jwtSessionID)
}

// RefreshJwtToken refresh jwt token
// - phoneNumber is the phone number for which the token is to be refreshed
// - token is the token to be refreshed
func RefreshJwtToken(phoneNumber string, token string) (string, error) {
func RefreshJwtToken(userID string, token string) (string, error) {
if zboxApiClient == nil {
return "", ErrZboxApiNotInitialized
}
return zboxApiClient.RefreshJwtToken(context.TODO(), phoneNumber, token)
return zboxApiClient.RefreshJwtToken(context.TODO(), userID, token)
}
11 changes: 9 additions & 2 deletions mobilesdk/zcn/smartcontract.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package zcn
import (
"encoding/json"
"fmt"
"strconv"
"sync"

"github.com/0chain/gosdk/zcncore"
Expand All @@ -19,13 +20,19 @@ func Faucet(methodName, jsonInput string, zcnToken float64) (string, error) {
func ExecuteSmartContract(address, methodName, input string, sasToken string) (string, error) {
wg := &sync.WaitGroup{}
cb := &transactionCallback{wg: wg}
txn, err := zcncore.NewTransaction(cb, "0", 0)
txn, err := zcncore.NewTransaction(cb, 0, 0)
if err != nil {
return "", err
}

wg.Add(1)
err = txn.ExecuteSmartContract(address, methodName, input, sasToken)

v, err := strconv.ParseUint(sasToken, 10, 64)
if err != nil {
return "", fmt.Errorf("invalid token value: %v, err: %v", sasToken, err)
}

_, err = txn.ExecuteSmartContract(address, methodName, input, v)
if err != nil {
return "", err

Expand Down
5 changes: 5 additions & 0 deletions wasmsdk/allocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ func unlockStakePool(providerType, fee uint64, providerID string) (int64, error)
return unstake, err
}

func collectRewards(providerType int, providerID string) (string, error) {
hash, _, err := sdk.CollectRewards(providerID, sdk.ProviderType(providerType))
return hash, err
}

// getSkatePoolInfo is to get information about the stake pool for the allocation
// - providerType: provider type (1: miner, 2:sharder, 3:blobber, 4:validator, 5:authorizer)
// - providerID: provider id
Expand Down
61 changes: 61 additions & 0 deletions wasmsdk/auth_txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ import (
"syscall/js"

"github.com/0chain/gosdk/core/sys"
"github.com/0chain/gosdk/wasmsdk/jsbridge"
"github.com/0chain/gosdk/zcncore"
)

type AuthCallbackFunc func(msg string) string

var authMsgCallback AuthCallbackFunc
var authCallback AuthCallbackFunc
var authResponseC chan string
var authMsgResponseC chan string
var authMsgLock = make(chan struct{}, 1)

// registerAuthorizer Register the callback function to authorize the transaction.
// This function is called from JavaScript.
Expand All @@ -30,6 +35,62 @@ func registerAuthorizer(this js.Value, args []js.Value) interface{} {
return nil
}

func registerZauthServer(serverAddr string) {
fmt.Println("registerZauthServer...")
jsbridge.SetZauthServer(serverAddr)
sys.SetAuthorize(zcncore.ZauthSignTxn(serverAddr))
sys.SetAuthCommon(zcncore.ZauthAuthCommon(serverAddr))
}

// zvaultNewWallet generates new split wallet
func zvaultNewWallet(serverAddr, token string) (string, error) {
return zcncore.CallZvaultNewWalletString(serverAddr, token, "")
}

// zvaultNewSplit generates new split wallet from existing clientID
func zvaultNewSplit(clientID, serverAddr, token string) (string, error) {
return zcncore.CallZvaultNewWalletString(serverAddr, token, clientID)
}

func zvaultStoreKey(serverAddr, token, privateKey string) (string, error) {
return zcncore.CallZvaultStoreKeyString(serverAddr, token, privateKey)
}

func zvaultRetrieveKeys(serverAddr, token, clientID string) (string, error) {
return zcncore.CallZvaultRetrieveKeys(serverAddr, token, clientID)
}

func zvaultRevokeKey(serverAddr, token, clientID, publicKey string) error {
return zcncore.CallZvaultRevokeKey(serverAddr, token, clientID, publicKey)
}

func zvaultDeletePrimaryKey(serverAddr, token, clientID string) error {
return zcncore.CallZvaultDeletePrimaryKey(serverAddr, token, clientID)
}

func zvaultRetrieveWallets(serverAddr, token string) (string, error) {
return zcncore.CallZvaultRetrieveWallets(serverAddr, token)
}

func zvaultRetrieveSharedWallets(serverAddr, token string) (string, error) {
return zcncore.CallZvaultRetrieveSharedWallets(serverAddr, token)
}

func registerAuthCommon(this js.Value, args []js.Value) interface{} {
authMsgCallback = parseAuthorizerCallback(args[0])
authMsgResponseC = make(chan string, 1)

sys.AuthCommon = func(msg string) (string, error) {
authMsgLock <- struct{}{}
defer func() {
<-authMsgLock
}()
authMsgCallback(msg)
return <-authMsgResponseC, nil
}
return nil
}

// authResponse Publishes the response to the authorization request.
// `response` is the response to the authorization request.
func authResponse(response string) {
Expand Down
Loading

0 comments on commit c027015

Please sign in to comment.