Compare commits

...

4 Commits
v0.0.2 ... main

Author SHA1 Message Date
zhaoweijie
a98d707e48 ref(clientactor): 优化 ExecuteContract 函数参数类型
- 将 ExecuteContract 函数的 arg 参数类型从 string 改为 any
- 增加对 arg 参数的类型判断和处理,支持字符串和其他类型的数据
- 修改签名逻辑,确保兼容新的 arg 参数类型
- 更新单元测试,演示新的 ExecuteContract 调用方式
2025-03-08 11:43:49 +08:00
zhaoweijie
e575463bad feat: change ClientResponse.result struct 2024-12-05 18:21:05 +08:00
zhaoweijie
93d969ce86 feat: change ClientResponse.result struct 2024-12-05 18:06:56 +08:00
zhaoweijie
f46b1a08ee feat: change ClientResponse.result struct 2024-12-05 17:50:39 +08:00
3 changed files with 22 additions and 15 deletions

View File

@ -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
}

View File

@ -19,14 +19,14 @@ func TestStruct(t *testing.T) {
}
func genClient() (*Client, error) {
url := "https://cpnode.demo.internetapi.cn/api/ctrlproxy/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
}
@ -70,7 +70,7 @@ func TestClient_StartContract(t *testing.T) {
t.Error(err)
}
resp, err := client.StartContract("contract TestContract {}")
resp, err := client.StartContract("contract TestContract")
if err != nil {
t.Log(resp)
@ -86,7 +86,7 @@ func TestClient_ExecuteContract(t *testing.T) {
t.Error(err)
}
resp, err := client.ExecuteContract("ShanxiControlProxy", "listRepository", "", 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)

View File

@ -8,14 +8,10 @@ type HttpResponse[D any] struct {
// ClientResponse represents a generic response structure
type ClientResponse[T any] struct {
NeedSeq bool `json:"needSeq,omitempty"`
Seq int `json:"seq,omitempty"`
Status string `json:"status,omitempty"`
Result struct {
Data T `json:"data,omitempty"`
Count int `json:"count,omitempty"`
Code int `json:"code,omitempty"`
} `json:"result,omitempty"`
NeedSeq bool `json:"needSeq,omitempty"`
Seq int `json:"seq,omitempty"`
Status string `json:"status,omitempty"`
Result T `json:"result,omitempty"`
IsInsnLimit bool `json:"isInsnLimit,omitempty"`
TotalGas int `json:"totalGas,omitempty"`
ExecutionGas int `json:"executionGas,omitempty"`