ref(clientactor): 优化 ExecuteContract 函数参数类型
- 将 ExecuteContract 函数的 arg 参数类型从 string 改为 any - 增加对 arg 参数的类型判断和处理,支持字符串和其他类型的数据 - 修改签名逻辑,确保兼容新的 arg 参数类型 - 更新单元测试,演示新的 ExecuteContract 调用方式
This commit is contained in:
parent
e575463bad
commit
a98d707e48
@ -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
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user