forked from holochain/holochain-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.go
363 lines (321 loc) · 9.33 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
352
353
354
355
356
357
358
359
360
361
362
363
// 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"
)
const (
SysEntryTypePrefix = "%"
VirtualEntryTypePrefix = "%%"
// System defined entry types
DNAEntryType = SysEntryTypePrefix + "dna"
AgentEntryType = SysEntryTypePrefix + "agent"
HeadersEntryType = SysEntryTypePrefix + "header"
KeyEntryType = VirtualEntryTypePrefix + "key" // virtual entry type, not actually on the chain
// Entry type formats
DataFormatLinks = "links"
DataFormatJSON = "json"
DataFormatString = "string"
DataFormatRawJS = "js"
DataFormatRawZygo = "zygo"
DataFormatSysDNA = "_DNA"
DataFormatSysKey = "_key"
// Entry sharing types
Public = "public"
Partial = "partial"
Private = "private"
)
// AgentEntry structure for building AgentEntryType entries
type AgentEntry struct {
Identity AgentIdentity
Revocation string // marshaled revocation
PublicKey string // marshaled public key
}
// LinksEntry holds one or more links
type LinksEntry struct {
Links []Link
}
// Link structure for holding meta tagging of linking entry
type Link struct {
LinkAction string // StatusAction (either AddAction or DelAction)
Base string // hash of entry (perhaps elsewhere) to which we are attaching the link
Link string // hash of entry being linked to
Tag string // tag
}
// DelEntry struct holds the record of an entry's deletion
type DelEntry struct {
Hash Hash
Message string
}
// EntryDef struct holds an entry definition
type EntryDef struct {
Name string
DataFormat string
Sharing string
Schema string
validator SchemaValidator
}
const (
AgentEntrySchema = `
{
"$id": "http://example.com/example.json",
"type": "object",
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"Identity": {
"$id": "/properties/Identity",
"type": "string",
"title": "The Identity Schema ",
"default": ""
},
"Revocation": {
"$id": "/properties/Revocation",
"type": "string",
"title": "The Revocation Schema ",
"default": ""
},
"PublicKey": {
"$id": "/properties/PublicKey",
"type": "string",
"title": "The Publickey Schema ",
"default": ""
}
},
"required": ["Identity", "PublicKey"]
}`
HeadersEntrySchema = `
{
"$id": "http://example.com/example.json",
"type": "array",
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"items": {
"$id": "http://example.com/example.json/items",
"type": "object",
"properties": {
"Source": {
"$id": "http://example.com/example.json/items/properties/Source",
"type": "string",
"title": "The Source Schema ",
"default": "",
"examples": [
"QmeLEGdTHwM4XYGggePJAYXLx968GiuiNooU1p7fa8T8zd"
]
},
"Header": {
"$id": "http://example.com/example.json/items/properties/Header",
"type": "object",
"properties": {
"Type": {
"$id": "http://example.com/example.json/items/properties/Header/properties/Type",
"type": "string",
"title": "The Type Schema ",
"default": "",
"examples": [
"someType"
]
},
"Time": {
"$id": "http://example.com/example.json/items/properties/Header/properties/Time",
"type": "string",
"title": "The Time Schema ",
"default": "",
"examples": [
"2018-03-15 19:30:05.740445736 -0400 EDT"
]
},
"EntryLink": {
"$id": "http://example.com/example.json/items/properties/Header/properties/EntryLink",
"type": "string",
"title": "The Entrylink Schema ",
"default": "",
"examples": [
"QmeLEGdTHwM4XYGggePJAYXLx968GiuiNooU1p7fa8T8zd"
]
},
"HeaderLink": {
"$id": "http://example.com/example.json/items/properties/Header/properties/HeaderLink",
"type": "string",
"title": "The Headerlink Schema ",
"default": "",
"examples": [
"QmWr1C3CeX12iZz98JGhzfsvfQpif29Ptwe86miZ9N9snU"
]
},
"TypeLink": {
"$id": "http://example.com/example.json/items/properties/Header/properties/TypeLink",
"type": "string",
"title": "The Typelink Schema ",
"default": "",
"examples": [
"1"
]
},
"Signature": {
"$id": "http://example.com/example.json/items/properties/Header/properties/Signature",
"type": "string",
"title": "The Signature Schema ",
"default": "",
"examples": [
"StwmRCJtj9Ymjdo7ws8ZeNdmEi2GZzNdtbubT8MZBfxpXWQDLtQPDZWeSA2qHTsVtyN7tZCrYTeWmeCdcoYe197"
]
}
}
},
"Role": {
"$id": "http://example.com/example.json/items/properties/Role",
"type": "string",
"title": "The Role Schema ",
"default": "",
"examples": [
"someRole"
]
}
},
"required": ["Header","Source"]
}
}
`
)
var DNAEntryDef = &EntryDef{Name: DNAEntryType, DataFormat: DataFormatSysDNA}
var AgentEntryDef = &EntryDef{Name: AgentEntryType, DataFormat: DataFormatJSON, Sharing: Public, Schema: AgentEntrySchema}
var KeyEntryDef = &EntryDef{Name: KeyEntryType, DataFormat: DataFormatSysKey}
var HeadersEntryDef = &EntryDef{Name: HeadersEntryType, DataFormat: DataFormatJSON, Sharing: Public, Schema: HeadersEntrySchema}
// 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
err = h.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
}
func (ae *AgentEntry) ToJSON() (encodedEntry string, err error) {
var j []byte
j, err = json.Marshal(ae)
encodedEntry = string(j)
return
}
func AgentEntryFromJSON(j string) (entry AgentEntry, err error) {
err = json.Unmarshal([]byte(j), &entry)
return
}