Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cdc2a77042 | ||
|
|
a08ed7e35f | ||
|
|
a98d707e48 | ||
|
|
e575463bad | ||
|
|
93d969ce86 | ||
|
|
f46b1a08ee | ||
|
|
a4c99c70f4 |
300
client/client.go
300
client/client.go
@ -18,7 +18,7 @@ import (
|
||||
"github.com/tjfoc/gmsm/sm2"
|
||||
"golang.org/x/sync/errgroup"
|
||||
|
||||
"go.fusiongalaxy.cn/bdware/bdcontract-client/sm2util"
|
||||
"go.yandata.net/bdware/bdcontract-client/sm2util"
|
||||
)
|
||||
|
||||
type HttpCOptions struct {
|
||||
@ -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 string,
|
||||
arg any,
|
||||
withDynamicAnalysis bool,
|
||||
withSignature bool,
|
||||
) (*HttpResponse[ExecuteContractResponse[any]], error) {
|
||||
) (*HttpResponse[ExecuteContractResponse[T]], error) {
|
||||
body := map[string]any{
|
||||
"action": "executeContract",
|
||||
"contractID": contractID,
|
||||
@ -293,29 +288,38 @@ 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
|
||||
}
|
||||
|
||||
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},
|
||||
@ -328,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"},
|
||||
}
|
||||
@ -353,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},
|
||||
@ -379,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,
|
||||
@ -411,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,
|
||||
@ -444,7 +440,7 @@ func (c *Client) AuthNodeRole(
|
||||
//
|
||||
// return retry(func() (*HttpResponse[any], error) {
|
||||
// result := PingResponse{}
|
||||
// resp, err := c.RequestWithSignature(
|
||||
// return RequestWithSignature(
|
||||
// httpPath,
|
||||
// "GET",
|
||||
// &result,
|
||||
@ -458,7 +454,8 @@ func (c *Client) AuthNodeRole(
|
||||
//}
|
||||
|
||||
// SaveFile TODO 待测试
|
||||
func (c *Client) SaveFile(
|
||||
func SaveFile(
|
||||
c *Client,
|
||||
content string,
|
||||
isAppend bool,
|
||||
isPrivate bool,
|
||||
@ -475,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) {
|
||||
@ -503,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,
|
||||
@ -539,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"},
|
||||
}
|
||||
@ -564,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)
|
||||
})
|
||||
}
|
||||
|
||||
@ -582,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},
|
||||
@ -592,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"},
|
||||
}
|
||||
@ -616,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"},
|
||||
}
|
||||
@ -640,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"},
|
||||
}
|
||||
@ -664,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},
|
||||
@ -688,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},
|
||||
@ -713,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)},
|
||||
@ -738,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"},
|
||||
}
|
||||
@ -761,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"},
|
||||
}
|
||||
@ -784,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
|
||||
@ -818,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"},
|
||||
}
|
||||
@ -843,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"},
|
||||
}
|
||||
@ -868,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},
|
||||
@ -896,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)
|
||||
})
|
||||
}
|
||||
|
||||
@ -920,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
|
||||
@ -930,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
|
||||
}
|
||||
@ -945,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
|
||||
}
|
||||
@ -959,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
|
||||
}
|
||||
@ -976,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
|
||||
}
|
||||
|
||||
@ -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 := "https://cpnode.demo.internetapi.cn/api/ctrlproxy/SCIDE"
|
||||
url := "http://021.node.internetapi.cn:21030/SCIDE"
|
||||
|
||||
pub, err := sm2util.ParsePublicKey("04153ad2d70e67741e0fc33e0c92c702e2afba2480dbea73d23fd02a3ce3a1b69979a7006a8e045f8836ae4797a8fe426823d7ad3450817e794948c8e47b60b711")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pub := "042731bc66608ba21a4301cd6522e3d6a6c7964f8dc3618cfe5d0aae493229a98623de23dfb35a5f9b7b4ac53e1f82ea79325ddf96d88a6bbcaf075df7e98acc5a"
|
||||
|
||||
priv, err := sm2util.ParsePrivateKey("31672b434458fdb6b854e64ededeb51305bc4d0bd258a5276ccb9bc86f7c03f6", 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("ShanxiControlProxy", "listRepository", "", 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)
|
||||
|
||||
@ -11,11 +11,7 @@ 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"`
|
||||
Result T `json:"result,omitempty"`
|
||||
IsInsnLimit bool `json:"isInsnLimit,omitempty"`
|
||||
TotalGas int `json:"totalGas,omitempty"`
|
||||
ExecutionGas int `json:"executionGas,omitempty"`
|
||||
|
||||
9
go.mod
9
go.mod
@ -1,7 +1,8 @@
|
||||
module go.fusiongalaxy.cn/bdware/bdcontract-client
|
||||
module go.yandata.net/bdware/bdcontract-client
|
||||
|
||||
go 1.23
|
||||
|
||||
require github.com/tjfoc/gmsm v1.4.1
|
||||
|
||||
require golang.org/x/sync v0.9.0 // indirect
|
||||
require (
|
||||
github.com/tjfoc/gmsm v1.4.1
|
||||
golang.org/x/sync v0.9.0
|
||||
)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -1,113 +0,0 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"go.fusiongalaxy.cn/bdware/bdcontract-client/sm2util"
|
||||
)
|
||||
|
||||
func TestStruct(t *testing.T) {
|
||||
type a struct {
|
||||
name string
|
||||
age int
|
||||
}
|
||||
|
||||
b := &a{age: 1}
|
||||
|
||||
t.Log(b.age)
|
||||
t.Log(b.name == "")
|
||||
}
|
||||
|
||||
func genClient() (*Client, error) {
|
||||
url := "https://cpnode.demo.internetapi.cn/api/ctrlproxy/SCIDE"
|
||||
|
||||
pub, err := sm2util.ParsePublicKey("04153ad2d70e67741e0fc33e0c92c702e2afba2480dbea73d23fd02a3ce3a1b69979a7006a8e045f8836ae4797a8fe426823d7ad3450817e794948c8e47b60b711")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
priv, err := sm2util.ParsePrivateKey("31672b434458fdb6b854e64ededeb51305bc4d0bd258a5276ccb9bc86f7c03f6", pub)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client, err := NewClient(url, priv, pub, HttpCOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func TestNewClient(t *testing.T) {
|
||||
client, err := genClient()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log(client)
|
||||
}
|
||||
|
||||
func TestClient_Ping(t *testing.T) {
|
||||
client, err := genClient()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resp, err := client.Ping()
|
||||
if err != nil {
|
||||
t.Log(resp.Status)
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
t.Log(resp.Data, resp.Status)
|
||||
}
|
||||
|
||||
func TestClient_StartContract(t *testing.T) {
|
||||
client, err := genClient()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resp, err := client.StartContract("contract TestContract {}")
|
||||
|
||||
if err != nil {
|
||||
t.Log(resp)
|
||||
return
|
||||
}
|
||||
|
||||
t.Log(resp)
|
||||
}
|
||||
|
||||
func TestClient_ExecuteContract(t *testing.T) {
|
||||
client, err := genClient()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
resp, err := client.ExecuteContract("ShanxiControlProxy", "listRepository", "", false, true)
|
||||
|
||||
if err != nil {
|
||||
t.Log(resp)
|
||||
return
|
||||
}
|
||||
|
||||
t.Log(resp)
|
||||
}
|
||||
|
||||
func TestClient_LoadNodeConfig(t *testing.T) {
|
||||
client, err := genClient()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
config, err := client.LoadNodeConfig()
|
||||
if err != nil {
|
||||
t.Log(config)
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
t.Log(config)
|
||||
}
|
||||
@ -1,129 +0,0 @@
|
||||
package client
|
||||
|
||||
type HttpResponse[D any] struct {
|
||||
Data D `json:"data"`
|
||||
ErrData string `json:"errData"`
|
||||
Status int `json:"status"`
|
||||
}
|
||||
|
||||
// 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"`
|
||||
IsInsnLimit bool `json:"isInsnLimit,omitempty"`
|
||||
TotalGas int `json:"totalGas,omitempty"`
|
||||
ExecutionGas int `json:"executionGas,omitempty"`
|
||||
ExtraGas int `json:"extraGas,omitempty"`
|
||||
Size int `json:"size,omitempty"`
|
||||
EventRelated bool `json:"eventRelated,omitempty"`
|
||||
ResponseID string `json:"responseID,omitempty"`
|
||||
Action string `json:"action,omitempty"`
|
||||
ExecuteTime string `json:"executeTime,omitempty"`
|
||||
}
|
||||
|
||||
// PingResponse is a specific response type for ping operations
|
||||
type PingResponse struct {
|
||||
Action string `json:"action"`
|
||||
}
|
||||
|
||||
// SaveFileRequest represents the request structure for saving files
|
||||
type SaveFileRequest struct {
|
||||
Content string `json:"content"`
|
||||
IsAppend bool `json:"isAppend"`
|
||||
IsPrivate bool `json:"isPrivate"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
// ListProjectPermissionRequest represents the request for listing project permissions
|
||||
type ListProjectPermissionRequest struct {
|
||||
IsPrivate bool `json:"isPrivate"`
|
||||
Path string `json:"path"`
|
||||
}
|
||||
|
||||
// ListProjectPermissionResponseData contains permission response data
|
||||
type ListProjectPermissionResponseData struct {
|
||||
Permissions []string `json:"permissions"`
|
||||
YPK string `json:"ypk"`
|
||||
}
|
||||
|
||||
// StartContractByYpkRequest represents the request for starting a contract
|
||||
type StartContractByYpkRequest struct {
|
||||
IsPrivate bool `json:"isPrivate"`
|
||||
Path string `json:"path"`
|
||||
Script string `json:"script"`
|
||||
}
|
||||
|
||||
// ListAllUsersResponseDataListItem represents a key-value pair
|
||||
type ListAllUsersResponseDataListItem struct {
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
// ListAllUsersResponseData contains user listing response data
|
||||
type ListAllUsersResponseData struct {
|
||||
KV []ListAllUsersResponseDataListItem `json:"kv"`
|
||||
Time []ListAllUsersResponseDataListItem `json:"time"`
|
||||
}
|
||||
|
||||
// OnlineContractsItem represents an online contract
|
||||
type OnlineContractsItem struct {
|
||||
ContractID string `json:"contractID"`
|
||||
ContractName string `json:"contractName"`
|
||||
IsMaster bool `json:"isMaster"`
|
||||
Type string `json:"type"`
|
||||
YjsType string `json:"yjsType"`
|
||||
Extra map[string]interface{} `json:"-"`
|
||||
}
|
||||
|
||||
// OnlineItem represents an online node
|
||||
type OnlineItem struct {
|
||||
CIManager string `json:"cimanager"`
|
||||
ContractVersion int `json:"contractVersion"`
|
||||
Events int `json:"events"`
|
||||
IPPort string `json:"ipPort"`
|
||||
MasterAddress string `json:"masterAddress"`
|
||||
NodeName string `json:"nodeName"`
|
||||
PeerID string `json:"peerID"`
|
||||
PubKey string `json:"pubKey"`
|
||||
Contracts []OnlineContractsItem `json:"contracts"`
|
||||
}
|
||||
|
||||
// ListNodesResponse represents the response for listing nodes
|
||||
type ListNodesResponse struct {
|
||||
Action string `json:"action"`
|
||||
Offline []string `json:"offline"`
|
||||
Online []OnlineItem `json:"online"`
|
||||
}
|
||||
|
||||
// DistributeContractResponse represents the response for contract distribution
|
||||
type DistributeContractResponse struct {
|
||||
Action string `json:"action"`
|
||||
Progress string `json:"progress"`
|
||||
}
|
||||
|
||||
// ExecuteContractResponse represents the response from contract execution
|
||||
type ExecuteContractResponse[T any] struct {
|
||||
ClientResponse[T]
|
||||
}
|
||||
|
||||
// ConfigNodeArgs represents configuration arguments for a node
|
||||
type ConfigNodeArgs struct {
|
||||
NodeName string `json:"nodeName,omitempty"`
|
||||
DataChain string `json:"dataChain,omitempty"`
|
||||
MasterAddress string `json:"masterAddress,omitempty"`
|
||||
NodeCenter string `json:"nodeCenter,omitempty"`
|
||||
LHSProxyAddress string `json:"LHSProxyAddress,omitempty"`
|
||||
ExtraConfig map[string]string `json:"-"`
|
||||
}
|
||||
|
||||
// LoadNodeConfigResponseData represents the response data for node configuration
|
||||
type LoadNodeConfigResponseData struct {
|
||||
DoipConfig string `json:"doipConfig"`
|
||||
ExtraData map[string]string `json:"-"`
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
module go.fusiongalaxy.cn/bdware/bdcontract-client
|
||||
|
||||
go 1.23
|
||||
|
||||
require github.com/tjfoc/gmsm v1.4.1
|
||||
|
||||
require golang.org/x/sync v0.9.0 // indirect
|
||||
@ -1,73 +0,0 @@
|
||||
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
|
||||
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
|
||||
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
|
||||
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
|
||||
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
|
||||
github.com/golang/protobuf v1.4.0-rc.1/go.mod h1:ceaxUfeHdC40wWswd/P6IGgMaK3YpKi5j83Wpe3EHw8=
|
||||
github.com/golang/protobuf v1.4.0-rc.1.0.20200221234624-67d41d38c208/go.mod h1:xKAWHe0F5eneWXFV3EuXVDTCmh+JuBKY0li0aMyXATA=
|
||||
github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrUpVNzEA03Pprs=
|
||||
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
|
||||
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
|
||||
github.com/tjfoc/gmsm v1.4.1 h1:aMe1GlZb+0bLjn+cKTPEvvn9oUEBlJitaZiiBwsbgho=
|
||||
github.com/tjfoc/gmsm v1.4.1/go.mod h1:j4INPkHWMrhJb38G+J6W4Tw0AbuN8Thu3PbdVYhVcTE=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20201012173705-84dcc777aaee/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
|
||||
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20201010224723-4f7140c49acb/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
|
||||
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ=
|
||||
golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY=
|
||||
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
|
||||
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
|
||||
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
|
||||
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
|
||||
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
|
||||
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
|
||||
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
|
||||
google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY=
|
||||
google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
|
||||
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
|
||||
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
|
||||
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
|
||||
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
|
||||
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||
@ -1,123 +0,0 @@
|
||||
package sm2util
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"math/big"
|
||||
|
||||
"github.com/tjfoc/gmsm/sm2"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrPrivateKeyIsNil = errors.New("private key is nil")
|
||||
ErrPublicAndKeysNotMatch = errors.New("public and private keys don't match")
|
||||
)
|
||||
|
||||
func CheckSm2KeyPair(priv *sm2.PrivateKey, pub *sm2.PublicKey) (pubHex string, err error) {
|
||||
if priv == nil {
|
||||
return "", ErrPrivateKeyIsNil
|
||||
}
|
||||
|
||||
if pub == nil {
|
||||
pub = &priv.PublicKey
|
||||
return PublicKeyToHex(pub), nil
|
||||
}
|
||||
|
||||
pubHex = PublicKeyToHex(pub)
|
||||
if pubHex != PublicKeyToHex(&priv.PublicKey) {
|
||||
return "", ErrPublicAndKeysNotMatch
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
/* From: https://github.com/tjfoc/gmsm/issues/207 */
|
||||
|
||||
// 国密sm2 非对称加密算法 (对标rsa使用场景)
|
||||
//
|
||||
// ParsePublicKey 公钥字符串还原为 sm2.PublicKey 对象(与java中org.bouncycastle.crypto生成的公私钥完全互通使用)
|
||||
func ParsePublicKey(publicKeyStr string) (*sm2.PublicKey, error) {
|
||||
publicKeyBytes, err := hex.DecodeString(publicKeyStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 提取 x 和 y 坐标字节切片
|
||||
curve := sm2.P256Sm2().Params()
|
||||
byteLen := (curve.BitSize + 7) / 8
|
||||
xBytes := publicKeyBytes[1 : byteLen+1]
|
||||
yBytes := publicKeyBytes[byteLen+1 : 2*byteLen+1]
|
||||
// 将字节切片转换为大整数
|
||||
x := new(big.Int).SetBytes(xBytes)
|
||||
y := new(big.Int).SetBytes(yBytes)
|
||||
// 创建 sm2.PublicKey 对象
|
||||
publicKey := &sm2.PublicKey{
|
||||
Curve: curve,
|
||||
X: x,
|
||||
Y: y,
|
||||
}
|
||||
return publicKey, nil
|
||||
}
|
||||
|
||||
// 国密 非对称加密算法
|
||||
//
|
||||
// ParsePublicKey 公钥字符串还原为 sm2.PublicKey 对象(与java中org.bouncycastle.crypto生成的公私钥完全互通使用)
|
||||
func ParsePublicKeyByXY(xHex, yHex string) (*sm2.PublicKey, error) {
|
||||
xBytes, err := hex.DecodeString(xHex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
yBytes, err := hex.DecodeString(yHex)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 提取 x 和 y 坐标字节切片
|
||||
curve := sm2.P256Sm2().Params()
|
||||
// byteLen := (curve.BitSize + 7) / 8
|
||||
// 将字节切片转换为大整数
|
||||
x := new(big.Int).SetBytes(xBytes)
|
||||
y := new(big.Int).SetBytes(yBytes)
|
||||
// 创建 sm2.PublicKey 对象
|
||||
publicKey := &sm2.PublicKey{
|
||||
Curve: curve,
|
||||
X: x,
|
||||
Y: y,
|
||||
}
|
||||
return publicKey, nil
|
||||
}
|
||||
|
||||
func PublicKeyToHex(pk *sm2.PublicKey) (pubKeyString string) {
|
||||
bs := append([]byte{byte(4)}, pk.X.Bytes()...)
|
||||
bs = append(bs, pk.Y.Bytes()...)
|
||||
|
||||
return hex.EncodeToString(bs)
|
||||
}
|
||||
|
||||
func PublicKeyToXYHex(pk *sm2.PublicKey) (x, y string) {
|
||||
x = hex.EncodeToString(pk.X.Bytes())
|
||||
y = hex.EncodeToString(pk.Y.Bytes())
|
||||
return x, y
|
||||
}
|
||||
|
||||
// 将私钥字符串反序列化转为私钥对象:
|
||||
// ParsePrivateKey 私钥还原为 sm2.PrivateKey对象(与java中org.bouncycastle.crypto生成的公私钥完全互通使用)
|
||||
func ParsePrivateKey(privateKeyStr string, publicKey *sm2.PublicKey) (*sm2.PrivateKey, error) {
|
||||
privateKeyBytes, err := hex.DecodeString(privateKeyStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 将字节切片转换为大整数
|
||||
d := new(big.Int).SetBytes(privateKeyBytes)
|
||||
// 创建 sm2.PrivateKey 对象
|
||||
privateKey := &sm2.PrivateKey{
|
||||
PublicKey: *publicKey,
|
||||
D: d,
|
||||
}
|
||||
return privateKey, nil
|
||||
}
|
||||
|
||||
func PrivateKeyToHex(pk *sm2.PrivateKey) (d string) {
|
||||
d = hex.EncodeToString(pk.D.Bytes())
|
||||
return d
|
||||
}
|
||||
|
||||
/**/
|
||||
Loading…
x
Reference in New Issue
Block a user