refactor: 重构trustlog-sdk目录结构到trustlog/go-trustlog
- 将所有trustlog-sdk文件移动到trustlog/go-trustlog/目录 - 更新README中所有import路径从trustlog-sdk改为go-trustlog - 更新cookiecutter配置文件中的项目名称 - 更新根目录.lefthook.yml以引用新位置的配置 - 添加go.sum文件到版本控制 - 删除过时的示例文件 这次重构与trustlog-server保持一致的目录结构, 为未来支持多语言SDK(Python、Java等)预留空间。
This commit is contained in:
146
api/model/proof.go
Normal file
146
api/model/proof.go
Normal file
@@ -0,0 +1,146 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"go.yandata.net/iod/iod/trustlog-sdk/api/grpc/pb"
|
||||
)
|
||||
|
||||
// MerkleTreeProofItem 表示Merkle树证明项.
|
||||
type MerkleTreeProofItem struct {
|
||||
Floor uint32 // 层级
|
||||
Hash string // 哈希值
|
||||
Left bool // 是否为左节点
|
||||
}
|
||||
|
||||
// Proof 表示取证证明.
|
||||
type Proof struct {
|
||||
ColItems []*MerkleTreeProofItem // 集合项证明
|
||||
RawItems []*MerkleTreeProofItem // 原始项证明
|
||||
ColRootItem []*MerkleTreeProofItem // 集合根项证明
|
||||
RawRootItem []*MerkleTreeProofItem // 原始根项证明
|
||||
Sign string // 签名
|
||||
Version string // 版本号
|
||||
}
|
||||
|
||||
// ProofFromProtobuf 将protobuf的Proof转换为model.Proof.
|
||||
func ProofFromProtobuf(pbProof *pb.Proof) *Proof {
|
||||
if pbProof == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
proof := &Proof{
|
||||
Sign: pbProof.GetSign(),
|
||||
Version: pbProof.GetVersion(),
|
||||
}
|
||||
|
||||
// 转换 ColItems
|
||||
if pbColItems := pbProof.GetColItems(); len(pbColItems) > 0 {
|
||||
proof.ColItems = make([]*MerkleTreeProofItem, 0, len(pbColItems))
|
||||
for _, item := range pbColItems {
|
||||
proof.ColItems = append(proof.ColItems, &MerkleTreeProofItem{
|
||||
Floor: item.GetFloor(),
|
||||
Hash: item.GetHash(),
|
||||
Left: item.GetLeft(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 转换 RawItems
|
||||
if pbRawItems := pbProof.GetRawItems(); len(pbRawItems) > 0 {
|
||||
proof.RawItems = make([]*MerkleTreeProofItem, 0, len(pbRawItems))
|
||||
for _, item := range pbRawItems {
|
||||
proof.RawItems = append(proof.RawItems, &MerkleTreeProofItem{
|
||||
Floor: item.GetFloor(),
|
||||
Hash: item.GetHash(),
|
||||
Left: item.GetLeft(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 转换 ColRootItem
|
||||
if pbColRootItem := pbProof.GetColRootItem(); len(pbColRootItem) > 0 {
|
||||
proof.ColRootItem = make([]*MerkleTreeProofItem, 0, len(pbColRootItem))
|
||||
for _, item := range pbColRootItem {
|
||||
proof.ColRootItem = append(proof.ColRootItem, &MerkleTreeProofItem{
|
||||
Floor: item.GetFloor(),
|
||||
Hash: item.GetHash(),
|
||||
Left: item.GetLeft(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 转换 RawRootItem
|
||||
if pbRawRootItem := pbProof.GetRawRootItem(); len(pbRawRootItem) > 0 {
|
||||
proof.RawRootItem = make([]*MerkleTreeProofItem, 0, len(pbRawRootItem))
|
||||
for _, item := range pbRawRootItem {
|
||||
proof.RawRootItem = append(proof.RawRootItem, &MerkleTreeProofItem{
|
||||
Floor: item.GetFloor(),
|
||||
Hash: item.GetHash(),
|
||||
Left: item.GetLeft(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return proof
|
||||
}
|
||||
|
||||
// ProofToProtobuf 将model.Proof转换为protobuf的Proof.
|
||||
func ProofToProtobuf(proof *Proof) *pb.Proof {
|
||||
if proof == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
pbProof := &pb.Proof{
|
||||
Sign: proof.Sign,
|
||||
Version: proof.Version,
|
||||
}
|
||||
|
||||
// 转换 ColItems
|
||||
if len(proof.ColItems) > 0 {
|
||||
pbProof.ColItems = make([]*pb.MerkleTreeProofItem, 0, len(proof.ColItems))
|
||||
for _, item := range proof.ColItems {
|
||||
pbProof.ColItems = append(pbProof.ColItems, &pb.MerkleTreeProofItem{
|
||||
Floor: item.Floor,
|
||||
Hash: item.Hash,
|
||||
Left: item.Left,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 转换 RawItems
|
||||
if len(proof.RawItems) > 0 {
|
||||
pbProof.RawItems = make([]*pb.MerkleTreeProofItem, 0, len(proof.RawItems))
|
||||
for _, item := range proof.RawItems {
|
||||
pbProof.RawItems = append(pbProof.RawItems, &pb.MerkleTreeProofItem{
|
||||
Floor: item.Floor,
|
||||
Hash: item.Hash,
|
||||
Left: item.Left,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 转换 ColRootItem
|
||||
if len(proof.ColRootItem) > 0 {
|
||||
pbProof.ColRootItem = make([]*pb.MerkleTreeProofItem, 0, len(proof.ColRootItem))
|
||||
for _, item := range proof.ColRootItem {
|
||||
pbProof.ColRootItem = append(pbProof.ColRootItem, &pb.MerkleTreeProofItem{
|
||||
Floor: item.Floor,
|
||||
Hash: item.Hash,
|
||||
Left: item.Left,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 转换 RawRootItem
|
||||
if len(proof.RawRootItem) > 0 {
|
||||
pbProof.RawRootItem = make([]*pb.MerkleTreeProofItem, 0, len(proof.RawRootItem))
|
||||
for _, item := range proof.RawRootItem {
|
||||
pbProof.RawRootItem = append(pbProof.RawRootItem, &pb.MerkleTreeProofItem{
|
||||
Floor: item.Floor,
|
||||
Hash: item.Hash,
|
||||
Left: item.Left,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return pbProof
|
||||
}
|
||||
Reference in New Issue
Block a user