Files
go-trustlog/api/model/proof.go
ryan 0ec1d3b87d refactor: 更改module路径为独立仓库路径
- go.yandata.net/iod/iod/go-trustlog → go.yandata.net/wangsiyuan/go-trustlog
- 更新 go.mod module声明
- 更新 README.md 安装说明
- 批量更新所有 .go 文件中的 import 路径
- 61个文件受影响

这样go-trustlog可以作为独立SDK使用
2025-12-26 14:35:39 +08:00

147 lines
3.8 KiB
Go

package model
import (
"go.yandata.net/wangsiyuan/go-trustlog/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
}