forked from FactomProject/FactomCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommitentry.go
More file actions
239 lines (191 loc) · 4.49 KB
/
commitentry.go
File metadata and controls
239 lines (191 loc) · 4.49 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2015 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package common
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"time"
ed "github.com/FactomProject/ed25519"
)
const (
// CommitEntrySize = 1 + 6 + 32 + 1 + 32 + 64
CommitEntrySize int = 136
)
type CommitEntry struct {
Version uint8
MilliTime *[6]byte
EntryHash *Hash
Credits uint8
ECPubKey *[32]byte
Sig *[64]byte
}
var _ Printable = (*CommitEntry)(nil)
var _ BinaryMarshallable = (*CommitEntry)(nil)
var _ ShortInterpretable = (*CommitEntry)(nil)
var _ ECBlockEntry = (*CommitEntry)(nil)
func (c *CommitEntry) MarshalledSize() uint64 {
return uint64(CommitEntrySize)
}
func NewCommitEntry() *CommitEntry {
c := new(CommitEntry)
c.Version = 0
c.MilliTime = new([6]byte)
c.EntryHash = NewHash()
c.Credits = 0
c.ECPubKey = new([32]byte)
c.Sig = new([64]byte)
return c
}
func (e *CommitEntry) Hash() *Hash {
bin, err := e.MarshalBinary()
if err != nil {
panic(err)
}
return Sha(bin)
}
func (b *CommitEntry) IsInterpretable() bool {
return false
}
func (b *CommitEntry) Interpret() string {
return ""
}
// CommitMsg returns the binary marshaled message section of the CommitEntry
// that is covered by the CommitEntry.Sig.
func (c *CommitEntry) CommitMsg() []byte {
p, err := c.MarshalBinary()
if err != nil {
return []byte{byte(0)}
}
return p[:len(p)-64-32]
}
// Return the timestamp in milliseconds.
func (c *CommitEntry) GetMilliTime() int64 {
a := make([]byte, 2, 8)
a = append(a, c.MilliTime[:]...)
milli := int64(binary.BigEndian.Uint64(a))
return milli
}
// InTime checks the CommitEntry.MilliTime and returns true if the timestamp is
// whitin +/- 12 hours of the current time.
func (c *CommitEntry) InTime() bool {
now := time.Now()
sec := c.GetMilliTime() / 1000
t := time.Unix(sec, 0)
return t.After(now.Add(-COMMIT_TIME_WINDOW*time.Hour)) && t.Before(now.Add(COMMIT_TIME_WINDOW*time.Hour))
}
func (c *CommitEntry) IsValid() bool {
//double check the credits in the commit
if c.Credits < 1 || c.Version != 0 {
return false
}
return ed.VerifyCanonical(c.ECPubKey, c.CommitMsg(), c.Sig)
}
func (c *CommitEntry)GetHash() *Hash {
h,_ := c.MarshalBinary()
return Sha(h)
}
func (c *CommitEntry) GetSigHash() *Hash {
data := c.CommitMsg()
return Sha(data)
}
func (c *CommitEntry) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
// 1 byte Version
if err := binary.Write(buf, binary.BigEndian, c.Version); err != nil {
return buf.Bytes(), err
}
// 6 byte MilliTime
buf.Write(c.MilliTime[:])
// 32 byte Entry Hash
buf.Write(c.EntryHash.Bytes())
// 1 byte number of Entry Credits
if err := binary.Write(buf, binary.BigEndian, c.Credits); err != nil {
return buf.Bytes(), err
}
// 32 byte Public Key
buf.Write(c.ECPubKey[:])
// 64 byte Signature
buf.Write(c.Sig[:])
return buf.Bytes(), nil
}
func (c *CommitEntry) ECID() byte {
return ECIDEntryCommit
}
func (c *CommitEntry) UnmarshalBinaryData(data []byte) (newData []byte, err error) {
buf := bytes.NewBuffer(data)
hash := make([]byte, 32)
var b byte
var p []byte
// 1 byte Version
if b, err = buf.ReadByte(); err != nil {
return
} else {
c.Version = uint8(b)
}
if buf.Len() < 6 {
err = io.EOF
return
}
// 6 byte MilliTime
if p = buf.Next(6); p == nil {
err = fmt.Errorf("Could not read MilliTime")
return
} else {
copy(c.MilliTime[:], p)
}
// 32 byte Entry Hash
if _, err = buf.Read(hash); err != nil {
return
} else if err = c.EntryHash.SetBytes(hash); err != nil {
return
}
// 1 byte number of Entry Credits
if b, err = buf.ReadByte(); err != nil {
return
} else {
c.Credits = uint8(b)
}
if buf.Len() < 32 {
err = io.EOF
return
}
// 32 byte Public Key
if p = buf.Next(32); p == nil {
err = fmt.Errorf("Could not read ECPubKey")
return
} else {
copy(c.ECPubKey[:], p)
}
if buf.Len() < 64 {
err = io.EOF
return
}
// 64 byte Signature
if p = buf.Next(64); p == nil {
err = fmt.Errorf("Could not read Sig")
return
} else {
copy(c.Sig[:], p)
}
newData = buf.Bytes()
return
}
func (c *CommitEntry) UnmarshalBinary(data []byte) (err error) {
_, err = c.UnmarshalBinaryData(data)
return
}
func (e *CommitEntry) JSONByte() ([]byte, error) {
return EncodeJSON(e)
}
func (e *CommitEntry) JSONString() (string, error) {
return EncodeJSONString(e)
}
func (e *CommitEntry) JSONBuffer(b *bytes.Buffer) error {
return EncodeJSONToBuffer(e, b)
}
func (e *CommitEntry) Spew() string {
return Spew(e)
}