Repository navigation
Expand file tree
/
Copy pathproto_convert.go
More file actions
154 lines (130 loc) · 3.96 KB
/
Copy pathproto_convert.go
File metadata and controls
154 lines (130 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package securelog
import (
"fmt"
pb "github.com/karasz/securelog/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
// ToProtoInitCommitment converts InitCommitment to protobuf message
func ToProtoInitCommitment(c InitCommitment) *pb.InitCommitment {
return &pb.InitCommitment{
LogId: c.LogID,
StartTime: timestamppb.New(c.StartTime),
KeyA0: c.KeyA0[:],
KeyB0: c.KeyB0[:],
UpdateFreq: c.UpdateFreq,
}
}
// FromProtoInitCommitment converts protobuf message to InitCommitment
func FromProtoInitCommitment(p *pb.InitCommitment) (InitCommitment, error) {
var c InitCommitment
c.LogID = p.LogId
c.StartTime = p.StartTime.AsTime()
if len(p.KeyA0) != KeySize {
return c, fmt.Errorf("invalid KeyA0 size: expected %d, got %d", KeySize, len(p.KeyA0))
}
copy(c.KeyA0[:], p.KeyA0)
if len(p.KeyB0) != KeySize {
return c, fmt.Errorf("invalid KeyB0 size: expected %d, got %d", KeySize, len(p.KeyB0))
}
copy(c.KeyB0[:], p.KeyB0)
c.UpdateFreq = p.UpdateFreq
return c, nil
}
// ToProtoOpenMessage converts OpenMessage to protobuf message
func ToProtoOpenMessage(o OpenMessage) *pb.OpenMessage {
return &pb.OpenMessage{
LogId: o.LogID,
OpenTime: timestamppb.New(o.OpenTime),
FirstIndex: o.FirstIndex,
FirstTagV: o.FirstTagV[:],
FirstTagT: o.FirstTagT[:],
}
}
// FromProtoOpenMessage converts protobuf message to OpenMessage
func FromProtoOpenMessage(p *pb.OpenMessage) (OpenMessage, error) {
var o OpenMessage
o.LogID = p.LogId
o.OpenTime = p.OpenTime.AsTime()
o.FirstIndex = p.FirstIndex
if len(p.FirstTagV) != 32 {
return o, fmt.Errorf("invalid FirstTagV size: expected 32, got %d", len(p.FirstTagV))
}
copy(o.FirstTagV[:], p.FirstTagV)
if len(p.FirstTagT) != 32 {
return o, fmt.Errorf("invalid FirstTagT size: expected 32, got %d", len(p.FirstTagT))
}
copy(o.FirstTagT[:], p.FirstTagT)
return o, nil
}
// ToProtoCloseMessage converts CloseMessage to protobuf message
func ToProtoCloseMessage(c CloseMessage) *pb.CloseMessage {
return &pb.CloseMessage{
LogId: c.LogID,
CloseTime: timestamppb.New(c.CloseTime),
FinalIndex: c.FinalIndex,
FinalTagV: c.FinalTagV[:],
FinalTagT: c.FinalTagT[:],
}
}
// FromProtoCloseMessage converts protobuf message to CloseMessage
func FromProtoCloseMessage(p *pb.CloseMessage) (CloseMessage, error) {
var c CloseMessage
c.LogID = p.LogId
c.CloseTime = p.CloseTime.AsTime()
c.FinalIndex = p.FinalIndex
if len(p.FinalTagV) != 32 {
return c, fmt.Errorf("invalid FinalTagV size: expected 32, got %d", len(p.FinalTagV))
}
copy(c.FinalTagV[:], p.FinalTagV)
if len(p.FinalTagT) != 32 {
return c, fmt.Errorf("invalid FinalTagT size: expected 32, got %d", len(p.FinalTagT))
}
copy(c.FinalTagT[:], p.FinalTagT)
return c, nil
}
// ToProtoRecord converts Record to protobuf message
func ToProtoRecord(r Record) *pb.Record {
return &pb.Record{
Index: r.Index,
Ts: r.TS,
Msg: r.Msg,
TagV: r.TagV[:],
TagT: r.TagT[:],
}
}
// FromProtoRecord converts protobuf message to Record
func FromProtoRecord(p *pb.Record) (Record, error) {
var r Record
r.Index = p.Index
r.TS = p.Ts
r.Msg = append([]byte(nil), p.Msg...)
if len(p.TagV) != 32 {
return r, fmt.Errorf("invalid TagV size: expected 32, got %d", len(p.TagV))
}
copy(r.TagV[:], p.TagV)
if len(p.TagT) != 32 {
return r, fmt.Errorf("invalid TagT size: expected 32, got %d", len(p.TagT))
}
copy(r.TagT[:], p.TagT)
return r, nil
}
// ToProtoRecords converts a slice of Records to protobuf messages
func ToProtoRecords(records []Record) []*pb.Record {
result := make([]*pb.Record, len(records))
for i, r := range records {
result[i] = ToProtoRecord(r)
}
return result
}
// FromProtoRecords converts protobuf messages to a slice of Records
func FromProtoRecords(pRecords []*pb.Record) ([]Record, error) {
result := make([]Record, len(pRecords))
for i, p := range pRecords {
r, err := FromProtoRecord(p)
if err != nil {
return nil, fmt.Errorf("record %d: %w", i, err)
}
result[i] = r
}
return result, nil
}