Initial commit
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
package zxchainsdk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"wallet_sdk"
|
||||
"zxsdk"
|
||||
)
|
||||
|
||||
// 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, // ReceiptID 保存官方 EvSave 返回的 evId。
|
||||
RequestID: resp.TxId, // RequestID 保存官方 EvSave 返回的 txId。
|
||||
Status: "success", // Status 表示本次 SDK 调用成功。
|
||||
RawResponse: raw, // RawResponse 保存官方 SDK 原始响应 JSON。
|
||||
ExternalRefID: req.BizID, // ExternalRefID 关联业务侧传入的 BizID。
|
||||
BlockHeight: resp.BlockHeight, // BlockHeight 表示上链区块高度。
|
||||
TxTime: resp.TxTime, // TxTime 表示官方返回的上链时间。
|
||||
FileName: req.FileName, // FileName 回传业务侧传入的文件名。
|
||||
FileSize: int64(len(data)), // FileSize 表示本次参与 Hash 存证的内容字节数。
|
||||
ContentType: req.ContentType, // ContentType 回传业务侧传入的内容类型。
|
||||
StorageURI: req.StorageURI, // StorageURI 回传业务侧保存原始文件的位置。
|
||||
SM3Hash: sm3Hash, // SM3Hash 表示本次上链内容的 SM3 哈希。
|
||||
CreatedAt: createdAt, // CreatedAt 表示业务侧传入或 SDK 自动生成的创建时间。
|
||||
}, 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, // EvID 表示查询到的 Hash 存证 evId。
|
||||
TxID: resp.TxId, // TxID 表示查询到的链上交易 txId。
|
||||
BlockHeight: resp.BlockHeight, // BlockHeight 表示查询到的上链区块高度。
|
||||
TxTime: resp.TxTime, // TxTime 表示查询到的上链时间。
|
||||
ExtendInfo: resp.ExtendInfo, // ExtendInfo 表示存证时传入的业务场景信息。
|
||||
RawResponse: raw, // RawResponse 保存官方 SDK 原始响应 JSON。
|
||||
}, 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, // ReceiptID 保存 KV 查询结果中的 txId。
|
||||
RequestID: resp.TxId, // RequestID 保存 KV 查询结果中的 txId。
|
||||
Status: "success", // Status 表示本次 SDK 调用成功。
|
||||
RawResponse: raw, // RawResponse 保存官方 SDK 原始响应 JSON。
|
||||
ExternalRefID: resp.KvKey, // ExternalRefID 保存链上的 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, // EvID 表示 Hash 存证回执中的 evId。
|
||||
CertURL: certURL, // CertURL 表示官方证书访问地址。
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user