forked from holochain/holochain-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader.go
221 lines (191 loc) · 4.42 KB
/
header.go
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
// Copyright (C) 2013-2017, The MetaCurrency Project (Eric Harris-Braun, Arthur Brock, et. al.)
// Use of this source code is governed by GPLv3 found in the LICENSE file
//----------------------------------------------------------------------------------------
// implements chain header structures & coding
package holochain
import (
"bytes"
"encoding/binary"
ic "github.com/libp2p/go-libp2p-crypto"
"io"
"time"
)
type Signature struct {
S []byte
}
// Header holds chain links, type, timestamp and signature
type Header struct {
Type string
Time time.Time
HeaderLink Hash // link to previous header
EntryLink Hash // link to entry
TypeLink Hash // link to header of previous header of this type
Sig Signature
// Meta interface{}
}
var DEBUG bool
// newHeader makes Header object linked to a previous Header by hash
func newHeader(h HashSpec, now time.Time, t string, entry Entry, key ic.PrivKey, prev Hash, prevType Hash) (hash Hash, header *Header, err error) {
var hd Header
hd.Type = t
hd.Time = now
hd.HeaderLink = prev
hd.TypeLink = prevType
// encode the entry into bytes
m, err := entry.Marshal()
if err != nil {
return
}
// calculate the entry's hash and store it in the header
err = hd.EntryLink.Sum(h, m)
if err != nil {
return
}
// sign the hash of the entry
sig, err := key.Sign(hd.EntryLink.H)
if err != nil {
return
}
hd.Sig = Signature{S: sig}
hash, _, err = (&hd).Sum(h)
if err != nil {
return
}
header = &hd
return
}
// Sum encodes and creates a hash digest of the header
func (hd *Header) Sum(spec HashSpec) (hash Hash, b []byte, err error) {
b, err = hd.Marshal()
if err == nil {
err = hash.Sum(spec, b)
}
return
}
// Marshal writes a header to bytes
func (hd *Header) Marshal() (b []byte, err error) {
var s bytes.Buffer
err = MarshalHeader(&s, hd)
if err == nil {
b = s.Bytes()
}
return
}
// MarshalHeader writes a header to a binary stream
func MarshalHeader(writer io.Writer, hd *Header) (err error) {
var b []byte
b = []byte(hd.Type)
l := uint8(len(b))
err = binary.Write(writer, binary.LittleEndian, l)
if err != nil {
return
}
err = binary.Write(writer, binary.LittleEndian, b)
if err != nil {
return
}
b, err = hd.Time.MarshalBinary()
err = binary.Write(writer, binary.LittleEndian, b)
if err != nil {
return
}
err = hd.HeaderLink.MarshalHash(writer)
if err != nil {
return
}
err = hd.EntryLink.MarshalHash(writer)
if err != nil {
return
}
err = hd.TypeLink.MarshalHash(writer)
if err != nil {
return
}
err = MarshalSignature(writer, &hd.Sig)
if err != nil {
return
}
// write out 0 for future expansion (meta)
z := uint64(0)
err = binary.Write(writer, binary.LittleEndian, &z)
if err != nil {
return
}
return
}
// Unmarshal reads a header from bytes
func (hd *Header) Unmarshal(b []byte, hashSize int) (err error) {
s := bytes.NewBuffer(b)
err = UnmarshalHeader(s, hd, hashSize)
return
}
// UnmarshalHeader reads a Header from a binary stream
func UnmarshalHeader(reader io.Reader, hd *Header, hashSize int) (err error) {
var l uint8
err = binary.Read(reader, binary.LittleEndian, &l)
if err != nil {
return
}
var b = make([]byte, l)
err = binary.Read(reader, binary.LittleEndian, b)
if err != nil {
return
}
hd.Type = string(b)
b = make([]byte, 15)
err = binary.Read(reader, binary.LittleEndian, b)
if err != nil {
return
}
hd.Time.UnmarshalBinary(b)
err = hd.HeaderLink.UnmarshalHash(reader)
if err != nil {
return
}
err = hd.EntryLink.UnmarshalHash(reader)
if err != nil {
return
}
err = hd.TypeLink.UnmarshalHash(reader)
if err != nil {
return
}
err = UnmarshalSignature(reader, &hd.Sig)
if err != nil {
return
}
z := uint64(0)
err = binary.Read(reader, binary.LittleEndian, &z)
if err != nil {
return
}
return
}
// MarshalSignature writes a signature to a binary stream
func MarshalSignature(writer io.Writer, s *Signature) (err error) {
l := uint8(len(s.S))
err = binary.Write(writer, binary.LittleEndian, l)
if err != nil {
return
}
err = binary.Write(writer, binary.LittleEndian, s.S)
if err != nil {
return
}
return
}
// UnmarshalSignature reads a Signature from a binary stream
func UnmarshalSignature(reader io.Reader, s *Signature) (err error) {
var l uint8
err = binary.Read(reader, binary.LittleEndian, &l)
if err != nil {
return
}
var b = make([]byte, l)
err = binary.Read(reader, binary.LittleEndian, b)
if err != nil {
return
}
s.S = b
return
}