From a08ed7e35fccf39421003eb82d7dd43bc58f812d Mon Sep 17 00:00:00 2001 From: zhaoweijie Date: Tue, 3 Jun 2025 15:56:02 +0800 Subject: [PATCH] =?UTF-8?q?refactor(client):=20=E9=87=8D=E6=9E=84=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E6=96=B9=E6=B3=95=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=AF=86=E9=92=A5=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -将私钥和公钥的解析逻辑移至 NewClient 方法中- 优化 RequestWithSignature 方法,使其支持泛型返回值 - 移除不必要的 genHttpResponse 函数 - 更新客户端方法,使其使用新的 RequestWithSignature 实现 - 调整测试代码以适应新的客户端实现 --- client/client.go | 283 +++++++++++++++++------------------------- client/client_test.go | 22 ++-- 2 files changed, 118 insertions(+), 187 deletions(-) diff --git a/client/client.go b/client/client.go index 69ff9aa..ff2a73b 100644 --- a/client/client.go +++ b/client/client.go @@ -36,10 +36,20 @@ type Client struct { func NewClient( baseUrl string, - priv *sm2.PrivateKey, - pub *sm2.PublicKey, + privStr string, + pubStr string, opt HttpCOptions, ) (*Client, error) { + pub, err := sm2util.ParsePublicKey(pubStr) + if err != nil { + return nil, err + } + + priv, err := sm2util.ParsePrivateKey(privStr, pub) + if err != nil { + return nil, err + } + pubHex, err := sm2util.CheckSm2KeyPair(priv, pub) if err != nil { return nil, err @@ -61,27 +71,27 @@ func NewClient( // RequestWithSignature // @Description 发送带有签名的请求 +// @Param c 客户端 // @Param path 请求路径 // @Param http 请求方法(GET | POST) // @Param result 接收请求返回结果的结构体指针 // @Param body 请求体(尽在method为POST时使用) // @Param priv 请求私钥 // @Param pub 请求公钥 -func (c *Client) RequestWithSignature( +func RequestWithSignature[T any]( + c *Client, path string, method string, - result any, body map[string]interface{}, priv *sm2.PrivateKey, pub *sm2.PublicKey, -) (response HttpResponse[any], err error) { - response = HttpResponse[any]{ +) (response *HttpResponse[T], err error) { + response = &HttpResponse[T]{ Status: 0, } var pubHex string if priv != nil { - var err error pubHex, err = sm2util.CheckSm2KeyPair(priv, pub) if err != nil { return response, err @@ -151,13 +161,15 @@ func (c *Client) RequestWithSignature( return response, nil } - err = json.Unmarshal(bodyBytes, result) + var result T + err = json.Unmarshal(bodyBytes, &result) // 如果bodyBytes解析失败直接将信息保存到ErrData if err != nil { response.ErrData = string(bodyBytes) return response, nil } + response.Data = result response.Status = resp.StatusCode return response, nil } @@ -195,60 +207,45 @@ func retry[T any](fn func() (T, error)) (T, error) { return zero, lastErr } -func genHttpResponse[D any](data D, response *HttpResponse[any], err error) (*HttpResponse[D], error) { - return &HttpResponse[D]{ - Data: data, - Status: response.Status, - ErrData: response.ErrData, - }, err -} - // Ping https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html#id15 -func (c *Client) Ping() (*HttpResponse[PingResponse], error) { +func Ping(c *Client) (*HttpResponse[PingResponse], error) { return retry(func() (*HttpResponse[PingResponse], error) { - result := PingResponse{} - - resp, err := c.RequestWithSignature( + return RequestWithSignature[PingResponse]( + c, "/SCManager?action=ping", "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[PingResponse](result, &resp, err) }) } // StartContract TODO 未跑通 // StartContract 启动合约 // https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html#id60 -func (c *Client) StartContract(code string) (*HttpResponse[any], error) { +func StartContract(c *Client, code string) (*HttpResponse[any], error) { params := url.Values{ "action": {"startContract"}, "script": {code}, } path := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, path, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // StartContractByYPK TODO 待测试 // StartContractByYPK 启动合约 // https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html#id13 -func (c *Client) StartContractByYPK(isPrivate bool, path string, script string) (*HttpResponse[any], error) { +func StartContractByYPK(c *Client, isPrivate bool, path string, script string) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"startContractByYPK"}, "script": []string{script}, @@ -261,29 +258,27 @@ func (c *Client) StartContractByYPK(isPrivate bool, path string, script string) httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // ExecuteContract 调用合约 // https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html#id69 -func (c *Client) ExecuteContract( +func ExecuteContract[T any]( + c *Client, contractID string, operation string, arg any, withDynamicAnalysis bool, withSignature bool, -) (*HttpResponse[ExecuteContractResponse[any]], error) { +) (*HttpResponse[ExecuteContractResponse[T]], error) { body := map[string]any{ "action": "executeContract", "contractID": contractID, @@ -308,25 +303,23 @@ func (c *Client) ExecuteContract( body["pubkey"] = c.pubHex } - return retry(func() (*HttpResponse[ExecuteContractResponse[any]], error) { - result := ExecuteContractResponse[any]{} - resp, err := c.RequestWithSignature( + return retry(func() (*HttpResponse[ExecuteContractResponse[T]], error) { + return RequestWithSignature[ExecuteContractResponse[T]]( + c, "/SCManager", "POST", - &result, body, nil, nil, ) - return genHttpResponse[ExecuteContractResponse[any]](result, &resp, err) }) } // KillContractProcess TODO 待测试 // KillContractProcess 停止合约 // https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html#id122 -func (c *Client) KillContractProcess(contractID string, requestID string) (*HttpResponse[any], error) { +func KillContractProcess(c *Client, contractID string, requestID string) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"killContractProcess"}, "id": []string{contractID}, @@ -339,24 +332,22 @@ func (c *Client) KillContractProcess(contractID string, requestID string) (*Http httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - return genHttpResponse[any](result, &resp, err) }) } // KillAllContract TODO 待测试 // KillAllContract 停止所有合约 // https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html#id131 -func (c *Client) KillAllContract() (*HttpResponse[any], error) { +func KillAllContract(c *Client) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"killAllContract"}, } @@ -364,24 +355,22 @@ func (c *Client) KillAllContract() (*HttpResponse[any], error) { httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - return genHttpResponse[any](result, &resp, err) }) } // ApplyNodeRole TODO 待测试 // ApplyNodeRole 申请角色 // https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html -func (c *Client) ApplyNodeRole(role string) (*HttpResponse[any], error) { +func ApplyNodeRole(c *Client, role string) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"applyNodeRole"}, "role": []string{role}, @@ -390,24 +379,23 @@ func (c *Client) ApplyNodeRole(role string) (*HttpResponse[any], error) { httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - return genHttpResponse[any](result, &resp, err) }) } // AuthNodeRole TODO 待测试 // AuthNodeRole 授权角色 // https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html -func (c *Client) AuthNodeRole( +func AuthNodeRole( + c *Client, isAccept bool, authorizedPubKey string, priv *sm2.PrivateKey, @@ -422,24 +410,21 @@ func (c *Client) AuthNodeRole( httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, priv, pub, ) - - return genHttpResponse[any](result, &resp, err) }) } // DistributeContract TODO 待测试, 用sse获取问题未解决!!! // 分发合约项目 // https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html -//func (c *Client) DistributeContract( +//func DistributeContract( // nodeIDs string, // projectName string, // isPrivate bool, @@ -455,7 +440,7 @@ func (c *Client) AuthNodeRole( // // return retry(func() (*HttpResponse[any], error) { // result := PingResponse{} -// resp, err := c.RequestWithSignature( +// return RequestWithSignature( // httpPath, // "GET", // &result, @@ -469,7 +454,8 @@ func (c *Client) AuthNodeRole( //} // SaveFile TODO 待测试 -func (c *Client) SaveFile( +func SaveFile( + c *Client, content string, isAppend bool, isPrivate bool, @@ -486,22 +472,21 @@ func (c *Client) SaveFile( httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - return genHttpResponse[any](result, &resp, err) }) } // ListProjectPermission TODO 待测试 -func (c *Client) ListProjectPermission( +func ListProjectPermission( + c *Client, isPrivate bool, path string, ) (*HttpResponse[any], error) { @@ -514,22 +499,20 @@ func (c *Client) ListProjectPermission( httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // StartContractMultiPoint TODO 待测试 -func (c *Client) StartContractMultiPoint( +func StartContractMultiPoint( + c *Client, peersID string, contractType int, selectUnitNum int, @@ -550,24 +533,21 @@ func (c *Client) StartContractMultiPoint( httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // LoadNodeConfig TODO 待测试 // 获取节点配置信息 // https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html#id497 -func (c *Client) LoadNodeConfig() (*HttpResponse[any], error) { +func LoadNodeConfig(c *Client) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"loadNodeConfig"}, } @@ -575,17 +555,14 @@ func (c *Client) LoadNodeConfig() (*HttpResponse[any], error) { httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } @@ -593,7 +570,7 @@ func (c *Client) LoadNodeConfig() (*HttpResponse[any], error) { // 修改节点配置 // https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html#id504 // @Param key {licence,projectDir,yjsPath,dataChain,doipConfig,nodeCenter,nodeName,masterAddress,resetNodeCenterWS} -func (c *Client) UpdateConfig(key string, val string) (*HttpResponse[any], error) { +func UpdateConfig(c *Client, key string, val string) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"updateConfig"}, "key": []string{key}, @@ -603,23 +580,20 @@ func (c *Client) UpdateConfig(key string, val string) (*HttpResponse[any], error httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // ResetNodeManager TODO 待测试 // 设置pubkey为node manager -func (c *Client) ResetNodeManager() (*HttpResponse[any], error) { +func ResetNodeManager(c *Client) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"resetNodeManager"}, } @@ -627,23 +601,20 @@ func (c *Client) ResetNodeManager() (*HttpResponse[any], error) { httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // LockEdit TODO 待测试 // 锁定某个用户的的私有目录编辑功能 -func (c *Client) LockEdit() (*HttpResponse[any], error) { +func LockEdit(c *Client) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"lockEdit"}, } @@ -651,23 +622,20 @@ func (c *Client) LockEdit() (*HttpResponse[any], error) { httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // UnlockEdit TODO 待测试 // 解锁某个用户的的私有目录编辑功能 -func (c *Client) UnlockEdit() (*HttpResponse[any], error) { +func UnlockEdit(c *Client) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"unlockEdit"}, } @@ -675,22 +643,19 @@ func (c *Client) UnlockEdit() (*HttpResponse[any], error) { httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // AddNode TODO 待测试 -func (c *Client) AddNode(nodePubKey string) (*HttpResponse[any], error) { +func AddNode(c *Client, nodePubKey string) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"addNode"}, "nodePubKey": []string{nodePubKey}, @@ -699,23 +664,20 @@ func (c *Client) AddNode(nodePubKey string) (*HttpResponse[any], error) { httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // ApplyRole TODO 待测试 // 申请角色 -func (c *Client) ApplyRole(role string) (*HttpResponse[any], error) { +func ApplyRole(c *Client, role string) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"applyRole"}, "role": []string{role}, @@ -724,22 +686,19 @@ func (c *Client) ApplyRole(role string) (*HttpResponse[any], error) { httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // AuthNodeManager TODO 待测试 -func (c *Client) AuthNodeManager(isAccept bool, authorizedPubKey string) (*HttpResponse[any], error) { +func AuthNodeManager(c *Client, isAccept bool, authorizedPubKey string) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"authNodeManager"}, "isAccept": []string{strconv.FormatBool(isAccept)}, @@ -749,22 +708,19 @@ func (c *Client) AuthNodeManager(isAccept bool, authorizedPubKey string) (*HttpR httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // ListAllUsers TODO 待测试 -func (c *Client) ListAllUsers() (*HttpResponse[any], error) { +func ListAllUsers(c *Client) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"listAllUsers"}, } @@ -772,22 +728,19 @@ func (c *Client) ListAllUsers() (*HttpResponse[any], error) { httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // ListNodes TODO 待测试 -func (c *Client) ListNodes() (*HttpResponse[any], error) { +func ListNodes(c *Client) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"listNodes"}, } @@ -795,24 +748,22 @@ func (c *Client) ListNodes() (*HttpResponse[any], error) { httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // CreateTrustUnit TODO 待测试 // 建立可信执行集群 // https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html#id664 -func (c *Client) CreateTrustUnit( +func CreateTrustUnit( + c *Client, data []struct { nodeName string pubkey string @@ -829,24 +780,21 @@ func (c *Client) CreateTrustUnit( } return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, "/SCManager", "POST", - &result, body, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // ListTrustUnits TODO 待测试 // 查看可信执行集群列表 // https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html#id657 -func (c *Client) ListTrustUnits() (*HttpResponse[any], error) { +func ListTrustUnits(c *Client) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"listTrustUnits"}, } @@ -854,24 +802,21 @@ func (c *Client) ListTrustUnits() (*HttpResponse[any], error) { httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // ListContractProcess TODO 待测试 // 查询合约进程 // https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html#id444 -func (c *Client) ListContractProcess() (*HttpResponse[any], error) { +func ListContractProcess(c *Client) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"listContractProcess"}, } @@ -879,24 +824,21 @@ func (c *Client) ListContractProcess() (*HttpResponse[any], error) { httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } // DownloadContract TODO 待测试 // 下载合约项目 // https://public.internetapi.cn/docs/bdcontract/doc/ContractAPI.html#id25 -func (c *Client) DownloadContract(projectName string, isPrivate bool, timestamp int) (*HttpResponse[any], error) { +func DownloadContract(c *Client, projectName string, isPrivate bool, timestamp int) (*HttpResponse[any], error) { params := url.Values{ "action": []string{"downloadContract"}, "projectName": []string{projectName}, @@ -907,17 +849,14 @@ func (c *Client) DownloadContract(projectName string, isPrivate bool, timestamp httpPath := fmt.Sprintf("/SCManager?%s", params.Encode()) return retry(func() (*HttpResponse[any], error) { - result := PingResponse{} - resp, err := c.RequestWithSignature( + return RequestWithSignature[any]( + c, httpPath, "GET", - &result, nil, nil, nil, ) - - return genHttpResponse[any](result, &resp, err) }) } @@ -931,8 +870,8 @@ func (c *Client) DownloadContract(projectName string, isPrivate bool, timestamp // // 配置中心节点时使用 // LHSProxyAddress string // } -func (c *Client) ConfigNode(arg map[string]string) bool { - res, err := c.ResetNodeManager() +func ConfigNode(c *Client, arg map[string]string) bool { + res, err := ResetNodeManager(c) if err != nil || res.Status == 0 { return false @@ -941,7 +880,7 @@ func (c *Client) ConfigNode(arg map[string]string) bool { eg := errgroup.Group{} for key, value := range arg { eg.Go(func() error { - res, err = c.UpdateConfig(key, value) + res, err = UpdateConfig(c, key, value) if err != nil { return err } @@ -956,7 +895,7 @@ func (c *Client) ConfigNode(arg map[string]string) bool { for _, value := range []string{"ContractProvider", "ContractUser", "ContractInstanceManager"} { eg.Go(func() error { - res, err = c.ApplyNodeRole(value) + res, err = ApplyNodeRole(c, value) if err != nil { return err } @@ -970,7 +909,7 @@ func (c *Client) ConfigNode(arg map[string]string) bool { } eg.Go(func() error { - res, err = c.AuthNodeRole(true, c.pubHex, nil, nil) + res, err = AuthNodeRole(c, true, c.pubHex, nil, nil) if err != nil { return err } @@ -987,7 +926,7 @@ func (c *Client) ConfigNode(arg map[string]string) bool { return false } - res, err = c.LoadNodeConfig() + res, err = LoadNodeConfig(c) if err != nil || res.Status == 0 { return false } diff --git a/client/client_test.go b/client/client_test.go index b7a8dac..9efb079 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -2,8 +2,6 @@ package client import ( "testing" - - "go.fusiongalaxy.cn/bdware/bdcontract-client/sm2util" ) func TestStruct(t *testing.T) { @@ -19,17 +17,11 @@ func TestStruct(t *testing.T) { } func genClient() (*Client, error) { - url := "http://127.0.0.1:21030/SCIDE" + url := "http://021.node.internetapi.cn:21030/SCIDE" - pub, err := sm2util.ParsePublicKey("04180354fdb6507f8ab98ccfbe165ce11da74ba733f81af86ad6d32216b32cf4f797c559d50ceeefbf4c760c3483840471c67471b90acdffb388cd7d496d9a1610") - if err != nil { - return nil, err - } + pub := "042731bc66608ba21a4301cd6522e3d6a6c7964f8dc3618cfe5d0aae493229a98623de23dfb35a5f9b7b4ac53e1f82ea79325ddf96d88a6bbcaf075df7e98acc5a" - priv, err := sm2util.ParsePrivateKey("1d4196947f59532db6f8f4055e58474a48db8f30b476ae3edc66406464521b3b", pub) - if err != nil { - return nil, err - } + priv := "481343eac82e0d18f8ea3a9f82a8f065543d720209e9f0d8d508f7e343883c45" client, err := NewClient(url, priv, pub, HttpCOptions{}) if err != nil { @@ -54,7 +46,7 @@ func TestClient_Ping(t *testing.T) { t.Error(err) } - resp, err := client.Ping() + resp, err := Ping(client) if err != nil { t.Log(resp.Status) t.Error(err) @@ -70,7 +62,7 @@ func TestClient_StartContract(t *testing.T) { t.Error(err) } - resp, err := client.StartContract("contract TestContract") + resp, err := StartContract(client, "contract TestContract") if err != nil { t.Log(resp) @@ -86,7 +78,7 @@ func TestClient_ExecuteContract(t *testing.T) { t.Error(err) } - resp, err := client.ExecuteContract("GlobalRouter", "listID", map[string]any{"offset": 0, "count": 12, "queryByCreateTime": "true", "filter": "prefixId"}, false, true) + resp, err := ExecuteContract[any](client, "GlobalRouter", "listLRS", map[string]any{"offset": 0, "count": 12, "queryByCreateTime": "true", "filter": "prefixId"}, false, true) if err != nil { t.Log(resp) @@ -102,7 +94,7 @@ func TestClient_LoadNodeConfig(t *testing.T) { t.Error(err) } - config, err := client.LoadNodeConfig() + config, err := LoadNodeConfig(client) if err != nil { t.Log(config) t.Error(err)