feat(sdk-go): initial version

This commit is contained in:
Nex Zhu 2024-11-26 18:54:32 +08:00
commit edfed1bcf2
6 changed files with 510 additions and 0 deletions

192
sdk-go/client/client.go Normal file
View File

@ -0,0 +1,192 @@
package client
import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"time"
"github.com/tjfoc/gmsm/sm2"
"go.fusiongalaxy.cn/bdware/bdcontract-client/sm2util"
)
type HttpCOptions struct {
MaxRetries int
}
type Client struct {
baseUrl string
priv *sm2.PrivateKey
pub *sm2.PublicKey
pubHex string
httpc *http.Client
maxRetry int
}
func NewClient(
baseUrl string,
priv *sm2.PrivateKey,
pub *sm2.PublicKey,
opt HttpCOptions,
) (*Client, error) {
pubHex, err := sm2util.CheckSm2KeyPair(priv, pub)
if err != nil {
return nil, err
}
if opt.MaxRetries == 0 {
opt.MaxRetries = 3
}
return &Client{
baseUrl: baseUrl,
pub: pub,
pubHex: pubHex,
priv: priv,
httpc: &http.Client{Timeout: 10 * time.Second},
maxRetry: opt.MaxRetries,
}, nil
}
func (c *Client) RequestWithSignature(
path string,
method string,
body map[string]interface{},
priv *sm2.PrivateKey,
pub *sm2.PublicKey,
) (statusCode int, respBody io.ReadCloser, err error) {
var pubHex string
if priv != nil {
var err error
pubHex, err = sm2util.CheckSm2KeyPair(priv, pub)
if err != nil {
return 0, nil, err
}
} else {
priv = c.priv
pubHex = c.pubHex
}
rawUrl := c.baseUrl + path
u := fmt.Sprintf("%s%spubKey=%s",
rawUrl,
func() string {
if strings.Contains(path, "?") {
return "&"
}
return "?"
}(),
pubHex,
)
switch strings.ToUpper(method) {
case "POST":
body["sign"] = c.Sign(u[strings.Index(u, "?")+1:], priv)
bodyJson, err := json.Marshal(body)
if err != nil {
return 0, nil, err
}
resp, err := c.httpc.Post(rawUrl, "application/json", bytes.NewBuffer(bodyJson))
if err != nil {
return 0, nil, err
}
respBody = resp.Body
}
return statusCode, respBody, nil
}
func (c *Client) Sign(data string, priv *sm2.PrivateKey) string {
if priv == nil {
priv = c.priv
}
sig, err := priv.Sign(rand.Reader, []byte(data), nil)
if err != nil {
log.Fatal(err)
}
return hex.EncodeToString(sig)
}
func genUrlParamsFromObject(obj map[string]interface{}) string {
params := make([]string, 0)
for key, value := range obj {
params = append(params, fmt.Sprintf("%s=%v", key, value))
}
return strings.Join(params, "&")
}
func retry[T any](fn func() (int, T, error)) (int, T, error) {
var lastErr error
for attempt := 0; attempt < 3; attempt++ {
resp, statusCode, err := fn()
if err == nil {
return resp, statusCode, nil
}
lastErr = err
time.Sleep(100 * time.Millisecond * time.Duration(attempt+1))
}
var zero T
return 0, zero, lastErr
}
func (c *Client) Ping() (statusCode int, resp *PingResponse, err error) {
return retry(func() (statusCode int, resp *PingResponse, err error) {
statusCode, respBody, err := c.RequestWithSignature(
"/SCManager?action=ping",
"GET",
nil,
nil,
nil,
)
if err != nil {
return 0, nil, err
}
defer respBody.Close()
decoder := json.NewDecoder(respBody)
if err := decoder.Decode(&resp); err != nil {
return 0, nil, err
}
return statusCode, resp, nil
})
}
func (c *Client) StartContract(code string) (statusCode int, resp string, err error) {
params := url.Values{
"action": {"startContract"},
"script": {code},
}
path := fmt.Sprintf("/SCManager?%s", params.Encode())
return retry(func() (int, string, error) {
statusCode, respBody, err := c.RequestWithSignature(
path,
"GET",
nil,
nil,
nil,
)
if err != nil {
return 0, "", err
}
var resp string
defer respBody.Close()
buf := new(strings.Builder)
if _, err := io.Copy(buf, respBody); err != nil {
return 0, "", err
}
return statusCode, resp, nil
})
}

119
sdk-go/client/dto.go Normal file
View File

@ -0,0 +1,119 @@
package client
// ClientResponse represents a generic response structure
type ClientResponse[T any] struct {
Data *T `json:"data,omitempty"`
}
// PingResponse is a specific response type for ping operations
type PingResponse struct {
ClientResponse[string] // will contain "pong"
}
// 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"`
}
// ExecuteContractArgs represents arguments for contract execution
type ExecuteContractArgs struct {
Method string `json:"method,omitempty"`
WithSignature bool `json:"withSignature,omitempty"`
WithDynamicAnalysis bool `json:"withDynamicAnalysis,omitempty"`
}
// ExecuteContractResponse represents the response from contract execution
type ExecuteContractResponse[T any] struct {
Status *bool `json:"status,omitempty"`
Data *T `json:"data,omitempty"`
ExecuteTime *float64 `json:"executeTime,omitempty"`
CID *string `json:"cid,omitempty"`
IsPrivate *bool `json:"isPrivate,omitempty"`
AdditionalData map[string]interface{} `json:"-"`
}
// 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:"-"`
}

5
sdk-go/go.mod Normal file
View File

@ -0,0 +1,5 @@
module go.fusiongalaxy.cn/bdware/bdcontract-client
go 1.23
require github.com/tjfoc/gmsm v1.4.1 // indirect

71
sdk-go/go.sum Normal file
View File

@ -0,0 +1,71 @@
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/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=

0
sdk-go/main.go Normal file
View File

123
sdk-go/sm2util/util.go Normal file
View File

@ -0,0 +1,123 @@
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
}
/**/