You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
129 lines
3.3 KiB
129 lines
3.3 KiB
package zxchainsdk
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"git.yuanzoo.cn/zhangzhengqiang/popi-zxl/third_party/official/gosdk_demo/zxsdk"
|
|
"git.yuanzoo.cn/zhangzhengqiang/popi-zxl/third_party/official/gosdk_demo/zxsdk/wallet_sdk"
|
|
)
|
|
|
|
// SDKClient 基于官方至信链 Go SDK 实现 Client。
|
|
type SDKClient struct {
|
|
client *zxsdk.ZxChainClient
|
|
}
|
|
|
|
// NewSDKClient 使用官方凭据创建至信链 SDK 客户端。
|
|
func NewSDKClient(secretID, secretKey, privateKey string) (*SDKClient, error) {
|
|
client, err := zxsdk.NewZxChainClient(secretID, secretKey, privateKey)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &SDKClient{client: client}, nil
|
|
}
|
|
|
|
// CreateHashAttestation 使用官方 EvSave 发起 Hash 存证。
|
|
func (c *SDKClient) CreateHashAttestation(_ context.Context, req HashAttestationRequest) (*AttestationResult, error) {
|
|
data := req.ContentBytes
|
|
if len(data) == 0 && req.Content != "" {
|
|
data = []byte(req.Content)
|
|
}
|
|
if len(data) == 0 {
|
|
return nil, ErrMissingContent
|
|
}
|
|
|
|
sm3Hash, err := wallet_sdk.SM3Hash(data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
createdAt := req.CreatedAt
|
|
if createdAt == "" {
|
|
createdAt = time.Now().Format(time.RFC3339)
|
|
}
|
|
|
|
resp, err := c.client.EvSave(data, req.Scene)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
raw, _ := json.Marshal(resp)
|
|
return &AttestationResult{
|
|
ReceiptID: resp.EvId,
|
|
RequestID: resp.TxId,
|
|
Status: "success",
|
|
RawResponse: raw,
|
|
ExternalRefID: req.BizID,
|
|
BlockHeight: resp.BlockHeight,
|
|
TxTime: resp.TxTime,
|
|
FileName: req.FileName,
|
|
FileSize: int64(len(data)),
|
|
ContentType: req.ContentType,
|
|
StorageURI: req.StorageURI,
|
|
SM3Hash: sm3Hash,
|
|
CreatedAt: createdAt,
|
|
}, nil
|
|
}
|
|
|
|
// QueryHashAttestation 使用官方 EvQuery 查询 Hash 存证链上记录。
|
|
func (c *SDKClient) QueryHashAttestation(_ context.Context, req HashAttestationQueryRequest) (*HashAttestationQueryResult, error) {
|
|
if req.EvID == "" && req.Hash == "" && req.TxID == "" {
|
|
return nil, ErrMissingQueryKey
|
|
}
|
|
|
|
resp, err := c.client.EvQuery(req.EvID, req.Hash, req.TxID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
raw, _ := json.Marshal(resp)
|
|
return &HashAttestationQueryResult{
|
|
EvID: resp.EvId,
|
|
TxID: resp.TxId,
|
|
BlockHeight: resp.BlockHeight,
|
|
TxTime: resp.TxTime,
|
|
ExtendInfo: resp.ExtendInfo,
|
|
RawResponse: raw,
|
|
}, nil
|
|
}
|
|
|
|
// CreateKVAttestation 使用官方 KvSave 发起 KV 存证。
|
|
func (c *SDKClient) CreateKVAttestation(_ context.Context, req KVAttestationRequest) (*AttestationResult, error) {
|
|
kvValueBytes, _ := json.Marshal(req.Fields)
|
|
err := c.client.KvSave(req.BizID, string(kvValueBytes), req.Scene)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
resp, err := c.client.KvQuery(req.BizID, true)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
raw, _ := json.Marshal(resp)
|
|
return &AttestationResult{
|
|
ReceiptID: resp.TxId,
|
|
RequestID: resp.TxId,
|
|
Status: "success",
|
|
RawResponse: raw,
|
|
ExternalRefID: resp.KvKey,
|
|
}, nil
|
|
}
|
|
|
|
// GetHashCert 根据 evId 获取官方证书地址。
|
|
func (c *SDKClient) GetHashCert(_ context.Context, evID string) (*HashCertResult, error) {
|
|
if evID == "" {
|
|
return nil, ErrMissingEvID
|
|
}
|
|
|
|
certURL, err := c.client.GetEvCert(evID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &HashCertResult{
|
|
EvID: evID,
|
|
CertURL: certURL,
|
|
}, nil
|
|
}
|
|
|