Skip to content

Commit cbbeeb8

Browse files
authored
Merge pull request #1659 from 0chain/fix/txn-split-key
Fix txn split key
2 parents 271db06 + 01de195 commit cbbeeb8

File tree

6 files changed

+78
-27
lines changed

6 files changed

+78
-27
lines changed

core/client/http.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/0chain/gosdk/core/conf"
88
"github.com/0chain/gosdk/core/util"
99
"github.com/shopspring/decimal"
10+
"log"
1011
"net/http"
1112
"net/url"
1213
"sync"
@@ -30,7 +31,7 @@ func MakeSCRestAPICall(scAddress string, relativePath string, params map[string]
3031
restApiUrl = restApiUrls[0]
3132
}
3233

33-
sharders := nodeClient.Network().Sharders
34+
sharders := nodeClient.sharders.Healthy()
3435
responses := make(map[int]int)
3536
entityResult := make(map[string][]byte)
3637

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

6768
req, err := util.NewHTTPGetRequest(urlObj.String())
6869
if err != nil {
69-
fmt.Println("1Error creating request", err.Error())
70+
log.Println("Error creating request", err.Error())
7071
return
7172
}
7273

7374
response, err := req.Get()
7475
if err != nil {
75-
fmt.Println("2Error getting response", err.Error())
76+
nodeClient.sharders.Fail(sharder)
77+
log.Println("Error getting response", err.Error())
7678
return
7779
}
7880

core/client/set.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func init() {
5050
wallet = client.wallets[clients[0]]
5151
}
5252

53-
if wallet.PeerPublicKey == "" {
53+
if !wallet.IsSplit {
5454
return sys.Sign(hash, client.signatureScheme, GetClientSysKeys(clients...))
5555
}
5656

@@ -67,10 +67,22 @@ func init() {
6767
sys.VerifyEd25519With = verifyEd25519With
6868
}
6969

70+
var SignFn = func(hash string) (string, error) {
71+
ss := zcncrypto.NewSignatureScheme(client.signatureScheme)
72+
73+
err := ss.SetPrivateKey(client.wallet.Keys[0].PrivateKey)
74+
if err != nil {
75+
return "", err
76+
}
77+
78+
return ss.Sign(hash)
79+
}
80+
7081
func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string, error) {
7182
retSignature := ""
7283
for _, kv := range keys {
7384
ss := zcncrypto.NewSignatureScheme(signatureScheme)
85+
7486
err := ss.SetPrivateKey(kv.PrivateKey)
7587
if err != nil {
7688
return "", err
@@ -85,6 +97,7 @@ func signHash(hash string, signatureScheme string, keys []sys.KeyPair) (string,
8597
return "", err
8698
}
8799
}
100+
88101
return retSignature, nil
89102
}
90103

@@ -130,6 +143,7 @@ func GetClientSysKeys(clients ...string) []sys.KeyPair {
130143
PublicKey: kv.PublicKey,
131144
})
132145
}
146+
133147
return keys
134148
}
135149

core/transaction/entity.go

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8+
"net/http"
9+
"strings"
10+
"sync"
11+
"time"
12+
813
"github.com/0chain/gosdk/core/client"
914
"github.com/0chain/gosdk/core/conf"
1015
"github.com/0chain/gosdk/core/logger"
1116
"github.com/0chain/gosdk/core/sys"
1217
"go.uber.org/zap"
13-
"net/http"
14-
"strings"
15-
"sync"
16-
"time"
1718

1819
"github.com/0chain/errors"
1920
"github.com/0chain/gosdk/core/common"
@@ -165,7 +166,7 @@ const (
165166
FEES_TABLE = `/v1/fees_table`
166167
)
167168

168-
type SignFunc = func(msg string, clientId ...string) (string, error)
169+
type SignFunc = func(msg string) (string, error)
169170
type VerifyFunc = func(publicKey, signature, msgHash string) (bool, error)
170171
type SignWithWallet = func(msg string, wallet interface{}) (string, error)
171172

@@ -200,6 +201,24 @@ func (t *Transaction) ComputeHashAndSignWithWallet(signHandler SignWithWallet, s
200201
return nil
201202
}
202203

204+
func (t *Transaction) getAuthorize() (string, error) {
205+
jsonByte, err := json.Marshal(t)
206+
if err != nil {
207+
return "", err
208+
}
209+
210+
if sys.Authorize == nil {
211+
return "", errors.New("not_initialized", "no authorize func is set, define it in native code and set in sys")
212+
}
213+
214+
authorize, err := sys.Authorize(string(jsonByte))
215+
if err != nil {
216+
return "", err
217+
}
218+
219+
return authorize, nil
220+
}
221+
203222
func (t *Transaction) ComputeHashAndSign(signHandler SignFunc) error {
204223
t.ComputeHashData()
205224
var err error
@@ -567,7 +586,26 @@ func SmartContractTxnValueFee(scAddress string, sn SmartContractTxnData,
567586
txn.TransactionNonce = client.Cache.GetNextNonce(txn.ClientID)
568587
}
569588

570-
if err = txn.ComputeHashAndSign(client.Sign); err != nil {
589+
err = txn.ComputeHashAndSign(client.SignFn)
590+
if err != nil {
591+
return
592+
}
593+
594+
if client.GetClient().IsSplit {
595+
txn.Signature, err = txn.getAuthorize()
596+
if err != nil {
597+
return
598+
}
599+
}
600+
601+
ok, err := txn.VerifySigWith(txn.PublicKey, sys.VerifyWith)
602+
if err != nil {
603+
err = errors.New("", "verification failed for auth response")
604+
return
605+
}
606+
607+
if !ok {
608+
err = errors.New("", "verification failed for auth response")
571609
return
572610
}
573611

wasmsdk/proxy.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,20 @@ func main() {
5959
if c == nil || len(c.Keys) == 0 {
6060
return "", errors.New("no keys found")
6161
}
62+
6263
pk := c.Keys[0].PrivateKey
6364
result, err := jsbridge.Await(jsSign.Invoke(hash, pk))
6465

6566
if len(err) > 0 && !err[0].IsNull() {
6667
return "", errors.New("sign: " + err[0].String())
6768
}
69+
6870
return result[0].String(), nil
6971
}
7072

7173
//update sign with js sign
7274
zcncrypto.Sign = signFunc
73-
zcncore.SignFn = signFunc
75+
client.SignFn = signFunc
7476
sys.Sign = func(hash, signatureScheme string, keys []sys.KeyPair) (string, error) {
7577
// js already has signatureScheme and keys
7678
return signFunc(hash)
@@ -349,6 +351,7 @@ func main() {
349351
if c == nil || len(c.Keys) == 0 {
350352
return "", errors.New("no keys found")
351353
}
354+
352355
pk := c.Keys[0].PrivateKey
353356
result, err := jsbridge.Await(jsSign.Invoke(hash, pk))
354357

@@ -359,7 +362,7 @@ func main() {
359362
}
360363
//update sign with js sign
361364
zcncrypto.Sign = signFunc
362-
zcncore.SignFn = signFunc
365+
client.SignFn = signFunc
363366
sys.Sign = func(hash, signatureScheme string, keys []sys.KeyPair) (string, error) {
364367
// js already has signatureScheme and keys
365368
return signFunc(hash)

zcncore/wallet_base.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package zcncore
33
import (
44
"encoding/hex"
55
"fmt"
6-
"github.com/0chain/gosdk/constants"
7-
"github.com/0chain/gosdk/core/common"
8-
"github.com/0chain/gosdk/core/util"
96
"net/http"
107
"strings"
118
"time"
129

10+
"github.com/0chain/gosdk/constants"
11+
"github.com/0chain/gosdk/core/common"
12+
"github.com/0chain/gosdk/core/util"
13+
1314
"errors"
1415

1516
"github.com/0chain/gosdk/core/client"
@@ -375,15 +376,6 @@ func GetIdForUrl(url string) string {
375376
return ""
376377
}
377378

378-
var SignFn = func(hash string) (string, error) {
379-
sigScheme := zcncrypto.NewSignatureScheme(client.SignatureScheme())
380-
err := sigScheme.SetPrivateKey(client.PrivateKey())
381-
if err != nil {
382-
return "", err
383-
}
384-
return sigScheme.Sign(hash)
385-
}
386-
387379
// SetupAuth prepare auth app with clientid, key and a set of public, private key and local publickey
388380
// which is running on PC/Mac.
389381
func SetupAuth(authHost, clientID, clientKey, publicKey, privateKey, localPublicKey string, cb AuthCallback) error {

zcncore/zauth.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -553,12 +553,14 @@ func ZauthSignTxn(serverAddr string) sys.AuthorizeFunc {
553553
return "", errors.Errorf("unexpected status code: %d, res: %s", resp.StatusCode, string(rsp))
554554
}
555555

556-
d, err := io.ReadAll(resp.Body)
556+
var d string
557+
558+
err = json.NewDecoder(resp.Body).Decode(&d)
557559
if err != nil {
558560
return "", errors.Wrap(err, "failed to read response body")
559561
}
560562

561-
return string(d), nil
563+
return d, nil
562564
}
563565
}
564566

0 commit comments

Comments
 (0)