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
/
entry.go
351 lines (309 loc) · 8.89 KB
/
entry.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright (C) 2013-2018, 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 entry structures and functions
package holochain
import (
"encoding/binary"
"encoding/json"
. "github.com/holochain/holochain-proto/hash"
"github.com/lestrrat/go-jsval"
"io"
"strings"
"fmt"
"errors"
)
const (
SysEntryTypePrefix = "%"
VirtualEntryTypePrefix = "%%"
// Entry type formats
DataFormatJSON = "json"
DataFormatString = "string"
DataFormatRawJS = "js"
DataFormatRawZygo = "zygo"
// Entry sharing types
Public = "public"
Partial = "partial"
Private = "private"
)
// EntryDef struct holds an entry definition
type EntryDef struct {
Name string
DataFormat string
Sharing string
Schema string
validator SchemaValidator
}
func (def EntryDef) isSharingPublic() bool {
return def.Sharing == Public || def.DataFormat == DataFormatLinks
}
// Entry describes serialization and deserialziation of entry data
type Entry interface {
Marshal() ([]byte, error)
Unmarshal([]byte) error
Content() interface{}
Sum(s HashSpec) (hash Hash, err error)
}
// SchemaValidator interface for schema validation
type SchemaValidator interface {
Validate(interface{}) error
}
// GobEntry is a structure for implementing Gob encoding of Entry content
type GobEntry struct {
C interface{}
}
// JSONEntry is a structure for implementing JSON encoding of Entry content
type JSONEntry struct {
C interface{}
}
// IsSysEntry returns true if the entry type is system defined
func (def *EntryDef) IsSysEntry() bool {
return strings.HasPrefix(def.Name, SysEntryTypePrefix)
}
// IsVirtualEntry returns true if the entry type is virtual
func (def *EntryDef) IsVirtualEntry() bool {
return strings.HasPrefix(def.Name, VirtualEntryTypePrefix)
}
// MarshalEntry serializes an entry to a writer
func MarshalEntry(writer io.Writer, e Entry) (err error) {
var b []byte
b, err = e.Marshal()
l := uint64(len(b))
err = binary.Write(writer, binary.LittleEndian, l)
if err != nil {
return
}
err = binary.Write(writer, binary.LittleEndian, b)
return
}
// UnmarshalEntry unserializes an entry from a reader
func UnmarshalEntry(reader io.Reader) (e Entry, err error) {
var l uint64
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
}
var g GobEntry
err = g.Unmarshal(b)
e = &g
return
}
// implementation of Entry interface with gobs
func (e *GobEntry) Marshal() (b []byte, err error) {
b, err = ByteEncoder(&e.C)
return
}
func (e *GobEntry) Unmarshal(b []byte) (err error) {
err = ByteDecoder(b, &e.C)
return
}
func (e *GobEntry) Content() interface{} { return e.C }
func (e *GobEntry) Sum(s HashSpec) (h Hash, err error) {
// encode the entry into bytes
m, err := e.Marshal()
if err != nil {
return
}
// calculate the entry's hash and store it in the header
h, err = Sum(s, m)
if err != nil {
return
}
return
}
// implementation of Entry interface with JSON
func (e *JSONEntry) Marshal() (b []byte, err error) {
j, err := json.Marshal(e.C)
if err != nil {
return
}
b = []byte(j)
return
}
func (e *JSONEntry) Unmarshal(b []byte) (err error) {
err = json.Unmarshal(b, &e.C)
return
}
func (e *JSONEntry) Content() interface{} { return e.C }
type JSONSchemaValidator struct {
v *jsval.JSVal
}
// implementation of SchemaValidator with JSONSchema
func (v *JSONSchemaValidator) Validate(entry interface{}) (err error) {
err = v.v.Validate(entry)
return
}
// BuildJSONSchemaValidator builds a validator in an EntryDef
func (d *EntryDef) BuildJSONSchemaValidator(path string) (err error) {
validator, err := BuildJSONSchemaValidatorFromFile(path)
if err != nil {
return
}
validator.v.SetName(d.Name)
d.validator = validator
return
}
func (d *EntryDef) BuildJSONSchemaValidatorFromString(schema string) (err error) {
validator, err := BuildJSONSchemaValidatorFromString(schema)
if err != nil {
return
}
validator.v.SetName(d.Name)
d.validator = validator
return
}
// sysValidateEntry does system level validation for adding an entry (put or commit)
// It checks that entry is not nil, and that it conforms to the entry schema in the definition
// if it's a Links entry that the contents are correctly structured
// if it's a new agent entry, that identity matches the defined identity structure
// if it's a key that the structure is actually a public key
func sysValidateEntry(h *Holochain, def *EntryDef, entry Entry, pkg *Package) (err error) {
switch def.Name {
case DNAEntryType:
err = ErrNotValidForDNAType
return
case KeyEntryType:
b58pk, ok := entry.Content().(string)
if !ok || !isValidPubKey(b58pk) {
err = ValidationFailed(ValidationFailureBadPublicKeyFormat)
return
}
case AgentEntryType:
j, ok := entry.Content().(string)
if !ok {
err = ValidationFailedErr
return
}
ae, _ := AgentEntryFromJSON(j)
// check that the public key is unmarshalable
if !isValidPubKey(ae.PublicKey) {
err = ValidationFailed(ValidationFailureBadPublicKeyFormat)
return err
}
// if there's a revocation, confirm that has a reasonable format
if ae.Revocation != "" {
revocation := &SelfRevocation{}
err := revocation.Unmarshal(ae.Revocation)
if err != nil {
err = ValidationFailed(ValidationFailureBadRevocationFormat)
return err
}
}
// TODO check anything in the package
case HeadersEntryType:
// TODO check signatures!
case DelEntryType:
// TODO checks according to CRDT configuration?
}
if entry == nil {
err = ValidationFailed(ErrNilEntryInvalid.Error())
return
}
// see if there is a schema validator for the entry type and validate it if so
if def.validator != nil {
var input interface{}
if def.DataFormat == DataFormatJSON {
if err = json.Unmarshal([]byte(entry.Content().(string)), &input); err != nil {
return
}
} else {
input = entry
}
h.Debugf("Validating %v against schema", input)
if err = def.validator.Validate(input); err != nil {
err = ValidationFailed(err.Error())
return
}
if def == DelEntryDef {
// @TODO refactor and use in other sys types
// @see https://github.com/holochain/holochain-proto/issues/733
hashValue, ok := input.(map[string]interface{})["Hash"].(string)
if !ok {
err = ValidationFailed("expected string!")
return
}
_, err = NewHash(hashValue)
if err != nil {
err = ValidationFailed(fmt.Sprintf("Error (%s) when decoding Hash value '%s'", err.Error(), hashValue))
return
}
}
if def == MigrateEntryDef {
// @TODO refactor with above
// @see https://github.com/holochain/holochain-proto/issues/733
dnaHashValue, ok := input.(map[string]interface{})["DNAHash"].(string)
if !ok {
err = ValidationFailed("expected string!")
return
}
_, err = NewHash(dnaHashValue)
if err != nil {
err = ValidationFailed(fmt.Sprintf("Error (%s) when decoding DNAHash value '%s'", err.Error(), dnaHashValue))
return
}
keyValue, ok := input.(map[string]interface{})["Key"].(string)
if !ok {
err = ValidationFailed("expected string!")
return
}
_, err = NewHash(keyValue)
if err != nil {
err = ValidationFailed(fmt.Sprintf("Error (%s) when decoding Key value '%s'", err.Error(), keyValue))
return
}
typeValue, ok := input.(map[string]interface{})["Type"].(string)
if !ok {
err = ValidationFailed("expected string!")
return
}
if !(typeValue == MigrateEntryTypeClose || typeValue == MigrateEntryTypeOpen) {
err = ValidationFailed(fmt.Sprintf("Type value '%s' must be either '%s' or '%s'", typeValue, MigrateEntryTypeOpen, MigrateEntryTypeClose))
return
}
}
} else if def.DataFormat == DataFormatLinks {
// Perform base validation on links entries, i.e. that all items exist and are of the right types
// so first unmarshall the json, and then check that the hashes are real.
var l struct{ Links []map[string]string }
err = json.Unmarshal([]byte(entry.Content().(string)), &l)
if err != nil {
err = fmt.Errorf("invalid links entry, invalid json: %v", err)
return
}
if len(l.Links) == 0 {
err = errors.New("invalid links entry: you must specify at least one link")
return
}
for _, link := range l.Links {
h, ok := link["Base"]
if !ok {
err = errors.New("invalid links entry: missing Base")
return
}
if _, err = NewHash(h); err != nil {
err = fmt.Errorf("invalid links entry: Base %v", err)
return
}
h, ok = link["Link"]
if !ok {
err = errors.New("invalid links entry: missing Link")
return
}
if _, err = NewHash(h); err != nil {
err = fmt.Errorf("invalid links entry: Link %v", err)
return
}
_, ok = link["Tag"]
if !ok {
err = errors.New("invalid links entry: missing Tag")
return
}
}
}
return
}