Skip to content

Commit b769b69

Browse files
committed
fix example test
1 parent bbb3497 commit b769b69

File tree

4 files changed

+84
-47
lines changed

4 files changed

+84
-47
lines changed

examples/api_node_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package performance_test
2+
3+
import (
4+
"fmt"
5+
"github.com/blocktree/go-openw-sdk/v2/openwsdk"
6+
"github.com/blocktree/openwallet/v2/log"
7+
"github.com/blocktree/openwallet/v2/owtp"
8+
"strings"
9+
"testing"
10+
)
11+
12+
const (
13+
sslkey = "FXJXCtxAfHWhAvnpsnciEfVCkThn7NGMA1kBofYRECRe"
14+
host = "127.0.0.1:8422"
15+
appid = "e10adc3949ba59abbe56e057f20f883e"
16+
appkey = "8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92"
17+
)
18+
19+
var api *openwsdk.APINode
20+
21+
func init() {
22+
api = testNewAPINode()
23+
}
24+
25+
func testNewAPINode() *openwsdk.APINode {
26+
cert, _ := owtp.NewCertificate(sslkey)
27+
config := &openwsdk.APINodeConfig{
28+
AppID: appid,
29+
AppKey: appkey,
30+
Host: host,
31+
Cert: cert,
32+
ConnectType: owtp.HTTP,
33+
EnableSignature: false,
34+
EnableKeyAgreement: false,
35+
TimeoutSEC: 120,
36+
}
37+
api := openwsdk.NewAPINode(config)
38+
api.BindAppDevice()
39+
return api
40+
}
41+
42+
// 获取节点配置信息
43+
func TestAPINode_GetNotifierNodeInfo(t *testing.T) {
44+
pubKey, nodeId, err := api.GetNotifierNodeInfo()
45+
if err != nil {
46+
t.Logf("GetNotifierNodeInfo unexpected error: %v", err)
47+
return
48+
}
49+
50+
log.Infof("pubKey: %s", pubKey)
51+
log.Infof("nodeID: %s", nodeId)
52+
}
53+
54+
// 获取币种列表,包含推荐费率和区块高度
55+
func TestAPINode_GetSymbolList(t *testing.T) {
56+
api := testNewAPINode()
57+
api.GetSymbolList("QTUM", 0, 1000, 0, true, func(status uint64, msg string, total int, symbols []*openwsdk.Symbol) {
58+
symbolStrArray := make([]string, 0)
59+
for _, s := range symbols {
60+
fmt.Printf("symbol: %+v\n", s)
61+
symbolStrArray = append(symbolStrArray, s.Symbol)
62+
}
63+
allSymbols := strings.Join(symbolStrArray, ", ")
64+
log.Infof("all symbols: %s", allSymbols)
65+
})
66+
}

openwsdk/api_node.go

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"encoding/json"
88
"fmt"
99
"github.com/blocktree/openwallet/v2/common"
10-
"github.com/blocktree/openwallet/v2/crypto"
1110
"github.com/blocktree/openwallet/v2/hdkeystore"
1211
"github.com/blocktree/openwallet/v2/log"
1312
"github.com/blocktree/openwallet/v2/openwallet"
@@ -264,22 +263,12 @@ func (api *APINode) GetSymbolList(symbol string, offset, limit, hasRole int, syn
264263
"hasRole": hasRole,
265264
}
266265

267-
return api.node.Call(HostNodeID, "getSymbolList", params, sync, func(resp owtp.Response) {
268-
data := resp.JsonData()
269-
symbols := make([]*Symbol, 0)
270-
symbolArray := data.Get("symbols")
271-
total := data.Get("total").Int()
272-
if symbolArray.IsArray() {
273-
for _, s := range symbolArray.Array() {
274-
var sym Symbol
275-
err := json.Unmarshal([]byte(s.Raw), &sym)
276-
if err == nil {
277-
symbols = append(symbols, &sym)
278-
}
279-
}
266+
return api.node.Call(HostNodeID, "getSymbolBlockList", params, sync, func(resp owtp.Response) {
267+
var result []*Symbol
268+
if err := json.Unmarshal([]byte(resp.JsonData().Raw), &result); err != nil {
269+
log.Error("json unmarshal failed: ", err)
280270
}
281-
282-
reqFunc(resp.Status, resp.Msg, int(total), symbols)
271+
reqFunc(resp.Status, resp.Msg, 0, nil)
283272
})
284273
}
285274

@@ -1102,30 +1091,6 @@ func (api *APINode) ImportAccount(
11021091
})
11031092
}
11041093

1105-
// BindDevice 绑定设备ID
1106-
func (api *APINode) BindDevice(
1107-
deviceID string,
1108-
sync bool,
1109-
reqFunc func(status uint64, msg string)) error {
1110-
if api == nil {
1111-
return fmt.Errorf("APINode is not inited")
1112-
}
1113-
appID := api.config.AppID
1114-
appKey := api.config.AppKey
1115-
accessTime := time.Now().UnixNano() / 1e6
1116-
t := strconv.FormatInt(accessTime, 10)
1117-
sigStr := appID + "." + deviceID + "." + t + "." + appKey
1118-
params := map[string]interface{}{
1119-
"appID": appID,
1120-
"deviceID": deviceID,
1121-
"accessTime": accessTime,
1122-
"sign": crypto.GetMD5(sigStr),
1123-
}
1124-
return api.node.Call(HostNodeID, "bindAppDevice", params, sync, func(resp owtp.Response) {
1125-
reqFunc(resp.Status, resp.Msg)
1126-
})
1127-
}
1128-
11291094
// ImportBatchAddress 批量导入地址
11301095
func (api *APINode) ImportBatchAddress(
11311096
walletID, accountID, memo string,
@@ -1183,11 +1148,13 @@ func (api *APINode) GetNotifierNodeInfo() (string, string, error) {
11831148
)
11841149
appID := api.config.AppID
11851150
time := time.Now().UnixNano()
1186-
plainText := fmt.Sprintf("%s%d%s", appID, time, api.config.AppKey)
1187-
sign := crypto.GetMD5(plainText)
1151+
nonce := RandNonce()
1152+
plainText := fmt.Sprintf("%s%s%d", appID, nonce, time)
1153+
sign := HmacSHA256([]byte(plainText), []byte(api.config.AppKey))
11881154

11891155
params := map[string]interface{}{
11901156
"appID": appID,
1157+
"nonce": nonce,
11911158
"time": time,
11921159
"sign": sign,
11931160
}
@@ -1201,7 +1168,7 @@ func (api *APINode) GetNotifierNodeInfo() (string, string, error) {
12011168

12021169
data := resp.JsonData()
12031170
pubKey = data.Get("pubKey").String()
1204-
nodeId = data.Get("nodeId").String()
1171+
nodeId = data.Get("nodeID").String()
12051172
})
12061173
if err != nil {
12071174
return "", "", err

openwsdk/model_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestWallet_CreateAccount(t *testing.T) {
4040
wallet.RootPath = key.RootPath
4141

4242
symbol := &Symbol{}
43-
symbol.Coin = "BTC"
43+
symbol.Symbol = "BTC"
4444
symbol.Curve = int64(owcrypt.ECC_CURVE_SECP256K1)
4545

4646
account, err := wallet.CreateAccount("newacc", symbol, key)

openwsdk/models.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,10 @@ type Wallet struct {
110110

111111
type Symbol struct {
112112
Name string `json:"name" bson:"name" storm:"id"`
113-
Coin string `json:"coin" bson:"coin"`
113+
MainSymbol string `json:"mainSymbol" bson:"mainSymbol"`
114+
Symbol string `json:"symbol" bson:"symbol"`
114115
Curve int64 `json:"curve" bson:"curve"`
115-
Orderno int64 `json:"orderno" bson:"orderno"`
116+
OrderNo int64 `json:"orderNo" bson:"orderNo"`
116117
Confirm int64 `json:"confirm" bson:"confirm"`
117118
Decimals int64 `json:"decimals" bson:"decimals"`
118119
BalanceMode uint64 `json:"balanceMode" bson:"balanceMode"`
@@ -121,6 +122,9 @@ type Symbol struct {
121122
OnlyContract uint64 `json:"onlyContract"` //支持合约代币, 0: false, 1: true
122123
WithdrawStop int64 `json:"withdrawStop"`
123124
BlockStop int64 `json:"blockStop"`
125+
MaxHeight int64 `json:"maxHeight"`
126+
FeeRate string `json:"feeRate"`
127+
Unit string `json:"unit"`
124128
}
125129

126130
type Account struct {
@@ -363,7 +367,7 @@ func (wallet *Wallet) CreateAccount(alias string, symbol *Symbol, key *hdkeystor
363367
)
364368

365369
account.Alias = alias
366-
account.Symbol = symbol.Coin
370+
account.Symbol = symbol.Symbol
367371
account.ReqSigs = 1
368372

369373
newAccIndex := wallet.AccountIndex + 1

0 commit comments

Comments
 (0)