-
Notifications
You must be signed in to change notification settings - Fork 19
/
entry.go
337 lines (277 loc) · 7.55 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
// Copyright 2016 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package factom
import (
"bytes"
"encoding/binary"
"encoding/hex"
"encoding/json"
"fmt"
)
type Entry struct {
ChainID string `json:"chainid"`
ExtIDs [][]byte `json:"extids"`
Content []byte `json:"content"`
}
type PendingEntry struct {
ChainID string `json:"chainid,omitempty"`
EntryHash string `json:"entryhash"`
Status string `json:"status"`
}
// NewEntryFromBytes creates a new Factom Entry from byte data.
func NewEntryFromBytes(chainid []byte, content []byte, extids ...[]byte) *Entry {
entry := new(Entry)
entry.ChainID = hex.EncodeToString(chainid)
entry.Content = content
entry.ExtIDs = extids
return entry
}
// NewEntryFromStrings creates a new Factom Entry from strings.
func NewEntryFromStrings(chainid string, content string, extids ...string) *Entry {
entry := new(Entry)
entry.ChainID = chainid
entry.Content = []byte(content)
for _, eid := range extids {
entry.ExtIDs = append(entry.ExtIDs, []byte(eid))
}
return entry
}
func (e *Entry) Hash() []byte {
a, err := e.MarshalBinary()
if err != nil {
return make([]byte, 32)
}
return sha52(a)
}
func (e *Entry) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
ids, err := e.MarshalExtIDsBinary()
if err != nil {
return buf.Bytes(), err
}
// Header
// 1 byte Version
buf.Write([]byte{0})
// 32 byte chainid
if p, err := hex.DecodeString(e.ChainID); err != nil {
return buf.Bytes(), err
} else {
buf.Write(p)
}
// 2 byte size of extids
if err := binary.Write(buf, binary.BigEndian, int16(len(ids))); err != nil {
return buf.Bytes(), err
}
// Body
// ExtIDs
buf.Write(ids)
// Content
buf.Write(e.Content)
return buf.Bytes(), nil
}
func (e *Entry) MarshalExtIDsBinary() ([]byte, error) {
buf := new(bytes.Buffer)
for _, v := range e.ExtIDs {
// 2 byte length of extid
binary.Write(buf, binary.BigEndian, int16(len(v)))
// extid
buf.Write(v)
}
return buf.Bytes(), nil
}
func (e *Entry) MarshalJSON() ([]byte, error) {
type js struct {
ChainID string `json:"chainid"`
ExtIDs []string `json:"extids"`
Content string `json:"content"`
}
j := new(js)
j.ChainID = e.ChainID
for _, id := range e.ExtIDs {
j.ExtIDs = append(j.ExtIDs, hex.EncodeToString(id))
}
j.Content = hex.EncodeToString(e.Content)
return json.Marshal(j)
}
func (e *Entry) String() string {
var s string
s += fmt.Sprintf("EntryHash: %x\n", e.Hash())
s += fmt.Sprintln("ChainID:", e.ChainID)
for _, id := range e.ExtIDs {
s += fmt.Sprintln("ExtID:", string(id))
}
s += fmt.Sprintln("Content:")
s += fmt.Sprintln(string(e.Content))
return s
}
func (e *Entry) UnmarshalJSON(data []byte) error {
type js struct {
ChainID string `json:"chainid"`
ChainName []string `json:"chainname"`
ExtIDs []string `json:"extids"`
Content string `json:"content"`
}
j := new(js)
if err := json.Unmarshal(data, j); err != nil {
return err
}
e.ChainID = j.ChainID
if e.ChainID == "" {
n := new(Entry)
for _, v := range j.ChainName {
if p, err := hex.DecodeString(v); err != nil {
return fmt.Errorf("Could not decode ChainName %s: %s", v, err)
} else {
n.ExtIDs = append(n.ExtIDs, p)
}
}
m := NewChain(n)
e.ChainID = m.ChainID
}
for _, v := range j.ExtIDs {
if p, err := hex.DecodeString(v); err != nil {
return fmt.Errorf("Could not decode ExtID %s: %s", v, err)
} else {
e.ExtIDs = append(e.ExtIDs, p)
}
}
p, err := hex.DecodeString(j.Content)
if err != nil {
return fmt.Errorf("Could not decode Content %s: %s", j.Content, err)
}
e.Content = p
return nil
}
func EntryCommitMessage(e *Entry, ec *ECAddress) (*bytes.Buffer, error) {
buf := new(bytes.Buffer)
// 1 byte version
buf.Write([]byte{0})
// 6 byte milliTimestamp (truncated unix time)
buf.Write(milliTime())
// 32 byte Entry Hash
buf.Write(e.Hash())
// 1 byte number of entry credits to pay
if c, err := EntryCost(e); err != nil {
return nil, err
} else {
buf.WriteByte(byte(c))
}
// 32 byte Entry Credit Address Public Key + 64 byte Signature
sig := ec.Sign(buf.Bytes())
buf.Write(ec.PubBytes())
buf.Write(sig[:])
return buf, nil
}
// ComposeEntryCommit creates a JSON2Request to commit a new Entry via the
// factomd web api. The request includes the marshaled MessageRequest with the
// Entry Credit Signature.
func ComposeEntryCommit(e *Entry, ec *ECAddress) (*JSON2Request, error) {
b, err := EntryCommitMessage(e, ec)
if err != nil {
return nil, err
}
params := messageRequest{Message: hex.EncodeToString(b.Bytes())}
req := NewJSON2Request("commit-entry", APICounter(), params)
return req, nil
}
// ComposeEntryReveal creates a JSON2Request to reveal the Entry via the factomd
// web api.
func ComposeEntryReveal(e *Entry) (*JSON2Request, error) {
p, err := e.MarshalBinary()
if err != nil {
return nil, err
}
params := entryRequest{Entry: hex.EncodeToString(p)}
req := NewJSON2Request("reveal-entry", APICounter(), params)
return req, nil
}
// CommitEntry sends the signed Entry Hash and the Entry Credit public key to
// the factom network. Once the payment is verified and the network is commited
// to publishing the Entry it may be published with a call to RevealEntry.
func CommitEntry(e *Entry, ec *ECAddress) (string, error) {
type commitResponse struct {
Message string `json:"message"`
TxID string `json:"txid"`
}
req, err := ComposeEntryCommit(e, ec)
if err != nil {
return "", err
}
resp, err := factomdRequest(req)
if err != nil {
return "", err
}
if resp.Error != nil {
return "", resp.Error
}
r := new(commitResponse)
if err := json.Unmarshal(resp.JSONResult(), r); err != nil {
return "", err
}
return r.TxID, nil
}
// RevealEntrysends the Entry data to the factom network to create an Entry that
// has previously been commited.
func RevealEntry(e *Entry) (string, error) {
type revealResponse struct {
Message string `json:"message"`
Entry string `json:"entryhash"`
}
req, err := ComposeEntryReveal(e)
if err != nil {
return "", err
}
resp, err := factomdRequest(req)
if err != nil {
return "", err
}
if resp.Error != nil {
return "", resp.Error
}
r := new(revealResponse)
if err := json.Unmarshal(resp.JSONResult(), r); err != nil {
return "", err
}
return r.Entry, nil
}
// GetEntry requests an Entry from the factomd API by its Entry Hash
func GetEntry(hash string) (*Entry, error) {
params := hashRequest{Hash: hash}
req := NewJSON2Request("entry", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
e := new(Entry)
if err := json.Unmarshal(resp.JSONResult(), e); err != nil {
return nil, err
}
return e, nil
}
// GetPendingEntries requests a list of all Entries that are waiting to be
// written into the next block on the Factom Blockchain.
// Entry commits that are not yet revealed do not have a ChainID.
// The order of entries is:
// |VM1...|VM2...|VM3...|...|Unconfirmed...|
// Where entries in VM# are ordered inside and Unconfirmed are random.
// Unconfirmed entries are entry reveals with a status of NotConfirmed
// and only exist on the node, not the rest of the network.
func GetPendingEntries() ([]PendingEntry, error) {
req := NewJSON2Request("pending-entries", APICounter(), nil)
resp, err := factomdRequest(req)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, err
}
pending := make([]PendingEntry, 0)
if err := json.Unmarshal(resp.JSONResult(), &pending); err != nil {
return nil, err
}
return pending, nil
}