From a98d707e48c5d852e9b2518ef06d809cb73cbdcd Mon Sep 17 00:00:00 2001 From: zhaoweijie Date: Sat, 8 Mar 2025 11:43:49 +0800 Subject: [PATCH] =?UTF-8?q?ref(clientactor):=20=E4=BC=98=E5=8C=96=20Execut?= =?UTF-8?q?eContract=20=E5=87=BD=E6=95=B0=E5=8F=82=E6=95=B0=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将 ExecuteContract 函数的 arg 参数类型从 string 改为 any - 增加对 arg 参数的类型判断和处理,支持字符串和其他类型的数据 - 修改签名逻辑,确保兼容新的 arg 参数类型 - 更新单元测试,演示新的 ExecuteContract 调用方式 --- client/client.go | 15 +++++++++++++-- client/client_test.go | 8 ++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/client/client.go b/client/client.go index b1382f0..69ff9aa 100644 --- a/client/client.go +++ b/client/client.go @@ -280,7 +280,7 @@ func (c *Client) StartContractByYPK(isPrivate bool, path string, script string) func (c *Client) ExecuteContract( contractID string, operation string, - arg string, + arg any, withDynamicAnalysis bool, withSignature bool, ) (*HttpResponse[ExecuteContractResponse[any]], error) { @@ -293,7 +293,18 @@ func (c *Client) ExecuteContract( } if withSignature { - body["signature"] = c.Sign(contractID+"|"+operation+"|"+arg+"|"+c.pubHex, nil, nil) + argStr := "" + if v, ok := arg.(string); ok { + argStr = v + } else { + v, err := json.Marshal(arg) + if err != nil { + return nil, fmt.Errorf("arg %v marshal err", arg) + } + argStr = string(v) + } + + body["signature"] = c.Sign(contractID+"|"+operation+"|"+argStr+"|"+c.pubHex, nil, nil) body["pubkey"] = c.pubHex } diff --git a/client/client_test.go b/client/client_test.go index ebdbd9d..b7a8dac 100644 --- a/client/client_test.go +++ b/client/client_test.go @@ -19,14 +19,14 @@ func TestStruct(t *testing.T) { } func genClient() (*Client, error) { - url := "http://021.node.internetapi.cn:43030/SCIDE" + url := "http://127.0.0.1:21030/SCIDE" - pub, err := sm2util.ParsePublicKey("04153ad2d70e67741e0fc33e0c92c702e2afba2480dbea73d23fd02a3ce3a1b69979a7006a8e045f8836ae4797a8fe426823d7ad3450817e794948c8e47b60b711") + pub, err := sm2util.ParsePublicKey("04180354fdb6507f8ab98ccfbe165ce11da74ba733f81af86ad6d32216b32cf4f797c559d50ceeefbf4c760c3483840471c67471b90acdffb388cd7d496d9a1610") if err != nil { return nil, err } - priv, err := sm2util.ParsePrivateKey("31672b434458fdb6b854e64ededeb51305bc4d0bd258a5276ccb9bc86f7c03f6", pub) + priv, err := sm2util.ParsePrivateKey("1d4196947f59532db6f8f4055e58474a48db8f30b476ae3edc66406464521b3b", pub) if err != nil { return nil, err } @@ -86,7 +86,7 @@ func TestClient_ExecuteContract(t *testing.T) { t.Error(err) } - resp, err := client.ExecuteContract("GlobalRouter", "resolveDoIdTest", "{\"doId\":\"bdtest021/ControlProxy\"}", false, true) + resp, err := client.ExecuteContract("GlobalRouter", "listID", map[string]any{"offset": 0, "count": 12, "queryByCreateTime": "true", "filter": "prefixId"}, false, true) if err != nil { t.Log(resp)