This repository has been archived by the owner on Sep 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
/
header.go
273 lines (235 loc) · 5.57 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
// 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"
"fmt"
. "github.com/holochain/holochain-proto/hash"
b58 "github.com/jbenet/go-base58"
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
Change Hash
}
// newHeader makes Header object linked to a previous Header by hash
func newHeader(hashSpec HashSpec, now time.Time, t string, entry Entry, privKey ic.PrivKey, prev Hash, prevType Hash, change Hash) (hash Hash, header *Header, err error) {
var hd Header
hd.Type = t
now = now.Round(0)
hd.Time = now
hd.HeaderLink = prev
hd.TypeLink = prevType
hd.Change = change
hd.EntryLink, err = entry.Sum(hashSpec)
if err != nil {
return
}
// sign the hash of the entry
sig, err := privKey.Sign([]byte(hd.EntryLink))
if err != nil {
return
}
hd.Sig = Signature{S: sig}
hash, _, err = (&hd).Sum(hashSpec)
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 {
hash, err = Sum(spec, b)
}
return
}
// B58String encodes a signature as a b58string
func (sig Signature) B58String() (result string) {
return b58.Encode(sig.S)
}
// Equal tests signature equality
func (sig1 Signature) Equal(sig2 Signature) bool {
return bytes.Equal(sig1.S, sig2.S)
}
// SignatureFromB58String encodes a signature as a b58string
func SignatureFromB58String(encoded string) (sig Signature) {
sig.S = b58.Decode(encoded)
return
}
// ToJSON serializes a header to JSON
func (hd *Header) ToJSON() (result string, err error) {
result = fmt.Sprintf(
`{"Type":"%s","Time":"%v","EntryLink":"%s","HeaderLink":"%s","TypeLink":"%s","Signature":"%s"}`,
jsSanitizeString(hd.Type),
hd.Time,
hd.EntryLink.String(),
hd.HeaderLink.String(),
hd.TypeLink.String(),
hd.Sig.B58String(),
)
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
}
func writeStr(writer io.Writer, str string) (err error) {
var b []byte
b = []byte(str)
l := uint8(len(b))
err = binary.Write(writer, binary.LittleEndian, l)
if err != nil {
return
}
err = binary.Write(writer, binary.LittleEndian, b)
return
}
// MarshalHeader writes a header to a binary stream
func MarshalHeader(writer io.Writer, hd *Header) (err error) {
err = writeStr(writer, hd.Type)
if err != nil {
return
}
var b []byte
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
}
err = hd.Change.MarshalHash(writer)
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
}
func readStr(reader io.Reader) (str string, 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
}
str = string(b)
return
}
// UnmarshalHeader reads a Header from a binary stream
func UnmarshalHeader(reader io.Reader, hd *Header, hashSize int) (err error) {
hd.Type, err = readStr(reader)
if err != nil {
return
}
var b = make([]byte, 15)
err = binary.Read(reader, binary.LittleEndian, b)
if err != nil {
return
}
hd.Time.UnmarshalBinary(b)
hd.HeaderLink, err = UnmarshalHash(reader)
if err != nil {
return
}
hd.EntryLink, err = UnmarshalHash(reader)
if err != nil {
return
}
hd.TypeLink, err = UnmarshalHash(reader)
if err != nil {
return
}
err = UnmarshalSignature(reader, &hd.Sig)
if err != nil {
return
}
hd.Change, err = UnmarshalHash(reader)
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
}