Skip to content

Commit

Permalink
Merge pull request #1659 from 0chain/fix/txn-split-key
Browse files Browse the repository at this point in the history
Fix txn split key
  • Loading branch information
dabasov authored Nov 5, 2024
2 parents 271db06 + 01de195 commit cbbeeb8
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 27 deletions.
10 changes: 6 additions & 4 deletions core/client/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/0chain/gosdk/core/conf"
"github.com/0chain/gosdk/core/util"
"github.com/shopspring/decimal"
"log"
"net/http"
"net/url"
"sync"
Expand All @@ -30,7 +31,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]
restApiUrl = restApiUrls[0]
}

sharders := nodeClient.Network().Sharders
sharders := nodeClient.sharders.Healthy()
responses := make(map[int]int)
entityResult := make(map[string][]byte)

Expand All @@ -55,7 +56,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]
urlString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath)
urlObj, err := url.Parse(urlString)
if err != nil {
fmt.Println(err.Error())
log.Println(err.Error())
return
}
q := urlObj.Query()
Expand All @@ -66,13 +67,14 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]

req, err := util.NewHTTPGetRequest(urlObj.String())
if err != nil {
fmt.Println("1Error creating request", err.Error())
log.Println("Error creating request", err.Error())
return
}

response, err := req.Get()
if err != nil {
fmt.Println("2Error getting response", err.Error())
nodeClient.sharders.Fail(sharder)
log.Println("Error getting response", err.Error())
return
}

Expand Down
16 changes: 15 additions & 1 deletion core/client/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func init() {
wallet = client.wallets[clients[0]]
}

if wallet.PeerPublicKey == "" {
if !wallet.IsSplit {
return sys.Sign(hash, client.signatureScheme, GetClientSysKeys(clients...))
}

Expand All @@ -67,10 +67,22 @@ func init() {
sys.VerifyEd25519With = verifyEd25519With
}

var SignFn = func(hash string) (string, error) {
ss := zcncrypto.NewSignatureScheme(client.signatureScheme)

err := ss.SetPrivateKey(client.wallet.Keys[0].PrivateKey)
if err != nil {
return "", err
}

return ss.Sign(hash)
}

func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, error) {
retSignature := ""
for _, kv := range keys {
ss := zcncrypto.NewSignatureScheme(signatureScheme)

err := ss.SetPrivateKey(kv.PrivateKey)
if err != nil {
return "", err
Expand All @@ -85,6 +97,7 @@ func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string,
return "", err
}
}

return retSignature, nil
}

Expand Down Expand Up @@ -130,6 +143,7 @@ func GetClientSysKeys(clients ...string) []sys.KeyPair {
PublicKey: kv.PublicKey,
})
}

return keys
}

Expand Down
50 changes: 44 additions & 6 deletions core/transaction/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
"sync"
"time"

"github.com/0chain/gosdk/core/client"
"github.com/0chain/gosdk/core/conf"
"github.com/0chain/gosdk/core/logger"
"github.com/0chain/gosdk/core/sys"
"go.uber.org/zap"
"net/http"
"strings"
"sync"
"time"

"github.com/0chain/errors"
"github.com/0chain/gosdk/core/common"
Expand Down Expand Up @@ -165,7 +166,7 @@ const (
FEES_TABLE = `/v1/fees_table`
)

type SignFunc = func(msg string, clientId ...string) (string, error)
type SignFunc = func(msg string) (string, error)
type VerifyFunc = func(publicKey, signature, msgHash string) (bool, error)
type SignWithWallet = func(msg string, wallet interface{}) (string, error)

Expand Down Expand Up @@ -200,6 +201,24 @@ func (t *Transaction) ComputeHashAndSignWithWallet(signHandler SignWithWallet, s
return nil
}

func (t *Transaction) getAuthorize() (string, error) {
jsonByte, err := json.Marshal(t)
if err != nil {
return "", err
}

if sys.Authorize == nil {
return "", errors.New("not_initialized", "no authorize func is set, define it in native code and set in sys")
}

authorize, err := sys.Authorize(string(jsonByte))
if err != nil {
return "", err
}

return authorize, nil
}

func (t *Transaction) ComputeHashAndSign(signHandler SignFunc) error {
t.ComputeHashData()
var err error
Expand Down Expand Up @@ -567,7 +586,26 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData,
txn.TransactionNonce = client.Cache.GetNextNonce(txn.ClientID)
}

if err = txn.ComputeHashAndSign(client.Sign); err != nil {
err = txn.ComputeHashAndSign(client.SignFn)
if err != nil {
return
}

if client.GetClient().IsSplit {
txn.Signature, err = txn.getAuthorize()
if err != nil {
return
}
}

ok, err := txn.VerifySigWith(txn.PublicKey, sys.VerifyWith)
if err != nil {
err = errors.New("", "verification failed for auth response")
return
}

if !ok {
err = errors.New("", "verification failed for auth response")
return
}

Expand Down
7 changes: 5 additions & 2 deletions wasmsdk/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,20 @@ func main() {
if c == nil || len(c.Keys) == 0 {
return "", errors.New("no keys found")
}

pk := c.Keys[0].PrivateKey
result, err := jsbridge.Await(jsSign.Invoke(hash, pk))

if len(err) > 0 && !err[0].IsNull() {
return "", errors.New("sign: " + err[0].String())
}

return result[0].String(), nil
}

//update sign with js sign
zcncrypto.Sign = signFunc
zcncore.SignFn = signFunc
client.SignFn = signFunc
sys.Sign = func(hash, signatureScheme string, keys []sys.KeyPair) (string, error) {
// js already has signatureScheme and keys
return signFunc(hash)
Expand Down Expand Up @@ -349,6 +351,7 @@ func main() {
if c == nil || len(c.Keys) == 0 {
return "", errors.New("no keys found")
}

pk := c.Keys[0].PrivateKey
result, err := jsbridge.Await(jsSign.Invoke(hash, pk))

Expand All @@ -359,7 +362,7 @@ func main() {
}
//update sign with js sign
zcncrypto.Sign = signFunc
zcncore.SignFn = signFunc
client.SignFn = signFunc
sys.Sign = func(hash, signatureScheme string, keys []sys.KeyPair) (string, error) {
// js already has signatureScheme and keys
return signFunc(hash)
Expand Down
16 changes: 4 additions & 12 deletions zcncore/wallet_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package zcncore
import (
"encoding/hex"
"fmt"
"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/util"
"net/http"
"strings"
"time"

"github.com/0chain/gosdk/constants"
"github.com/0chain/gosdk/core/common"
"github.com/0chain/gosdk/core/util"

"errors"

"github.com/0chain/gosdk/core/client"
Expand Down Expand Up @@ -375,15 +376,6 @@ func GetIdForUrl(url string) string {
return ""
}

var SignFn = func(hash string) (string, error) {
sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme())
err := sigScheme.SetPrivateKey(client.PrivateKey())
if err != nil {
return "", err
}
return sigScheme.Sign(hash)
}

// SetupAuth prepare auth app with clientid, key and a set of public, private key and local publickey
// which is running on PC/Mac.
func SetupAuth(authHost, clientID, clientKey, publicKey, privateKey, localPublicKey string, cb AuthCallback) error {
Expand Down
6 changes: 4 additions & 2 deletions zcncore/zauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,12 +553,14 @@ func ZauthSignTxn(serverAddr string) sys.AuthorizeFunc {
return "", errors.Errorf("unexpected status code: %d, res: %s", resp.StatusCode, string(rsp))
}

d, err := io.ReadAll(resp.Body)
var d string

err = json.NewDecoder(resp.Body).Decode(&d)
if err != nil {
return "", errors.Wrap(err, "failed to read response body")
}

return string(d), nil
return d, nil
}
}

Expand Down

0 comments on commit cbbeeb8

Please sign in to comment.