-
Notifications
You must be signed in to change notification settings - Fork 19
/
chain.go
324 lines (270 loc) · 7.66 KB
/
chain.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
// 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"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
)
var (
ErrChainPending = errors.New("Chain not yet included in a Directory Block")
)
// A Chain is a blockchain datastructure in Factom. The Chain is defined by its
// First Entry from wich the ChainID is derived. Every Entry in the Chain will
// share the ChainID and may be found searching the Factom Entry Blocks.
type Chain struct {
//chainid was originally required as a paramater passed with the json.
//it is now overwritten with the chainid derived from the extid elements
ChainID string `json:"chainid"`
FirstEntry *Entry `json:"firstentry"`
}
// NewChain creates a new Factom Chain from an Entry.
func NewChain(e *Entry) *Chain {
c := new(Chain)
c.FirstEntry = e
c.ChainID = ChainIDFromFields(e.ExtIDs)
c.FirstEntry.ChainID = c.ChainID
return c
}
// NewChainFromBytes creates a new Factom Chain from byte data used to construct an Entry.
func NewChainFromBytes(content []byte, extids ...[]byte) *Chain {
e := NewEntryFromBytes(nil, content, extids...)
c := NewChain(e)
return c
}
// NewChainFromStrings creates a new Factom Chain from strings used to construct an Entry.
func NewChainFromStrings(content string, extids ...string) *Chain {
e := NewEntryFromStrings("", content, extids...)
c := NewChain(e)
return c
}
// ChainIDFromFields computes a ChainID based on the binary External IDs of that
// Chain's First Entry.
func ChainIDFromFields(fields [][]byte) string {
hs := sha256.New()
for _, id := range fields {
h := sha256.Sum256(id)
hs.Write(h[:])
}
cid := hs.Sum(nil)
return hex.EncodeToString(cid)
}
// ChainIDFromStrings computes the ChainID of a Chain Created with External IDs
// that would match the given string (in order).
func ChainIDFromStrings(fields []string) string {
var bin [][]byte
for _, str := range fields {
bin = append(bin, []byte(str))
}
return ChainIDFromFields(bin)
}
// ChainExists returns true if a Chain with the given chainid exists within the
// Factom Blockchain.
func ChainExists(chainid string) bool {
if _, _, err := GetChainHead(chainid); err == nil {
// no error means we found the Chain
return true
}
return false
}
// ComposeChainCommit creates a JSON2Request to commit a new Chain via the
// factomd web api. The request includes the marshaled MessageRequest with the
// Entry Credit Signature.
func ComposeChainCommit(c *Chain, ec *ECAddress) (*JSON2Request, error) {
buf := new(bytes.Buffer)
// 1 byte version
buf.Write([]byte{0})
// 6 byte milliTimestamp
buf.Write(milliTime())
e := c.FirstEntry
// 32 byte ChainID Hash
if p, err := hex.DecodeString(c.ChainID); err != nil {
return nil, err
} else {
// double sha256 hash of ChainID
buf.Write(shad(p))
}
// 32 byte Weld; sha256(sha256(EntryHash + ChainID))
if cid, err := hex.DecodeString(c.ChainID); err != nil {
return nil, err
} else {
s := append(e.Hash(), cid...)
buf.Write(shad(s))
}
// 32 byte Entry Hash of the First Entry
buf.Write(e.Hash())
// 1 byte number of Entry Credits to pay
if d, err := EntryCost(e); err != nil {
return nil, err
} else {
buf.WriteByte(byte(d + 10))
}
// 32 byte Entry Credit Address Public Key + 64 byte Signature
sig := ec.Sign(buf.Bytes())
buf.Write(ec.PubBytes())
buf.Write(sig[:])
params := messageRequest{Message: hex.EncodeToString(buf.Bytes())}
req := NewJSON2Request("commit-chain", APICounter(), params)
return req, nil
}
// ComposeChainReveal creates a JSON2Request to reveal the Chain via the factomd
// web api.
func ComposeChainReveal(c *Chain) (*JSON2Request, error) {
p, err := c.FirstEntry.MarshalBinary()
if err != nil {
return nil, err
}
params := entryRequest{Entry: hex.EncodeToString(p)}
req := NewJSON2Request("reveal-chain", APICounter(), params)
return req, nil
}
// CommitChain sends the signed ChainID, the 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 Chain it may be published by revealing
// the First Entry in the Chain.
func CommitChain(c *Chain, ec *ECAddress) (string, error) {
type commitResponse struct {
Message string `json:"message"`
TxID string `json:"txid"`
}
req, err := ComposeChainCommit(c, 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
}
// RevealChain sends the Chain data to the factom network to create a chain that
// has previously been commited.
func RevealChain(c *Chain) (string, error) {
type revealResponse struct {
Message string `json:"message"`
Entry string `json:"entryhash"`
}
req, err := ComposeChainReveal(c)
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
}
// GetChainHead returns the hash of the most recent Entry made into a given
// Factom Chain.
func GetChainHead(chainid string) (string, bool, error) {
params := chainIDRequest{ChainID: chainid}
req := NewJSON2Request("chain-head", APICounter(), params)
resp, err := factomdRequest(req)
if err != nil {
return "", false, err
}
if resp.Error != nil {
return "", false, resp.Error
}
head := new(struct {
ChainHead string `json:"chainhead"`
ChainInProcessList bool `json:"chaininprocesslist"`
})
if err := json.Unmarshal(resp.JSONResult(), head); err != nil {
return "", false, err
}
return head.ChainHead, head.ChainInProcessList, nil
}
// GetAllChainEntries returns a list of all Factom Entries for a given Chain.
func GetAllChainEntries(chainid string) ([]*Entry, error) {
es := make([]*Entry, 0)
head, inPL, err := GetChainHead(chainid)
if err != nil {
return es, err
}
if head == "" && inPL {
return nil, ErrChainPending
}
for ebhash := head; ebhash != ZeroHash; {
eb, err := GetEBlock(ebhash)
if err != nil {
return es, err
}
s, err := GetAllEBlockEntries(ebhash)
if err != nil {
return es, err
}
es = append(s, es...)
ebhash = eb.Header.PrevKeyMR
}
return es, nil
}
// GetAllChainEntriesAtHeight returns a list of all Factom Entries for a given
// Chain at a given point in the Chain's history.
func GetAllChainEntriesAtHeight(chainid string, height int64) ([]*Entry, error) {
es := make([]*Entry, 0)
head, inPL, err := GetChainHead(chainid)
if err != nil {
return es, err
}
if head == "" && inPL {
return nil, ErrChainPending
}
for ebhash := head; ebhash != ZeroHash; {
eb, err := GetEBlock(ebhash)
if err != nil {
return es, err
}
if eb.Header.DBHeight > height {
ebhash = eb.Header.PrevKeyMR
continue
}
s, err := GetAllEBlockEntries(ebhash)
if err != nil {
return es, err
}
es = append(s, es...)
ebhash = eb.Header.PrevKeyMR
}
return es, nil
}
// GetFirstEntry returns the first Entry used to create the given Factom Chain.
func GetFirstEntry(chainid string) (*Entry, error) {
e := new(Entry)
head, inPL, err := GetChainHead(chainid)
if err != nil {
return e, err
}
if head == "" && inPL {
return nil, ErrChainPending
}
eb, err := GetEBlock(head)
if err != nil {
return e, err
}
for eb.Header.PrevKeyMR != ZeroHash {
ebhash := eb.Header.PrevKeyMR
eb, err = GetEBlock(ebhash)
if err != nil {
return e, err
}
}
return GetEntry(eb.EntryList[0].EntryHash)
}