/
commitchain.go
443 lines (378 loc) · 11.6 KB
/
commitchain.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
// Copyright 2017 Factom Foundation
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package entryCreditBlock
import (
"encoding/binary"
"fmt"
"os"
"github.com/FactomProject/factomd/common/constants"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
)
const (
// CommitChainSize = 1+6+32+32+32+1+32+64
// These are the sizes of the members of the data structure below
CommitChainSize int = 200
)
// CommitChain is a data structure which affects EC balances when committing a new user chain into the Factom blockchain
// see https://github.com/FactomProject/FactomDocs/blob/master/factomDataStructureDetails.md#entry_commit
type CommitChain struct {
Version uint8 `json:"version"` // The version of the CommitChain, currently 0
MilliTime *primitives.ByteSlice6 `json:"millitime"` // The millisecond time stamp (0~=1970) this commit is created
ChainIDHash interfaces.IHash `json:"chainidhash"` // The chain id hash is the double hash of the chain id
Weld interfaces.IHash `json:"weld"` // The double hash of the concatonated (entry hash | chain id)
EntryHash interfaces.IHash `json:"entryhash"` // SHA512+256 descriptor of the Entry to be the first in the Chain
Credits uint8 `json:"credits"` // number of entry credits to deduct for this entry, must be 10 < Credits <= 20
ECPubKey *primitives.ByteSlice32 `json:"ecpubkey"` // EC public key that will have balanced reduced
Sig *primitives.ByteSlice64 `json:"sig"` // signature of the chain commit by the public key
}
var _ interfaces.Printable = (*CommitChain)(nil)
var _ interfaces.BinaryMarshallable = (*CommitChain)(nil)
var _ interfaces.ShortInterpretable = (*CommitChain)(nil)
var _ interfaces.IECBlockEntry = (*CommitChain)(nil)
var _ interfaces.ISignable = (*CommitChain)(nil)
// Init initializes all nil objects to their starting values/objects
func (c *CommitChain) Init() {
if c.MilliTime == nil {
c.MilliTime = new(primitives.ByteSlice6)
}
if c.ChainIDHash == nil {
c.ChainIDHash = primitives.NewZeroHash()
}
if c.Weld == nil {
c.Weld = primitives.NewZeroHash()
}
if c.EntryHash == nil {
c.EntryHash = primitives.NewZeroHash()
}
if c.ECPubKey == nil {
c.ECPubKey = new(primitives.ByteSlice32)
}
if c.Sig == nil {
c.Sig = new(primitives.ByteSlice64)
}
}
// IsSameAs only checks if everything in the item is identical.
// It does not catch if the private key holder has created a malleated version
// which is functionally identical in come cases from the protocol perspective,
// but would fail comparison here
func (c *CommitChain) IsSameAs(b interfaces.IECBlockEntry) bool {
if c == nil || b == nil {
if c == nil && b == nil {
return true
}
return false
}
if c.ECID() != b.ECID() {
return false
}
bb, ok := b.(*CommitChain)
if ok == false {
return false
}
if c.Version != bb.Version {
return false
}
if c.MilliTime.IsSameAs(bb.MilliTime) == false {
return false
}
if c.ChainIDHash.IsSameAs(bb.ChainIDHash) == false {
return false
}
if c.Weld.IsSameAs(bb.Weld) == false {
return false
}
if c.EntryHash.IsSameAs(bb.EntryHash) == false {
return false
}
if c.Credits != bb.Credits {
return false
}
if c.ECPubKey.IsSameAs(bb.ECPubKey) == false {
return false
}
if c.Sig.IsSameAs(bb.Sig) == false {
return false
}
return true
}
// String returns this object as a string
func (c *CommitChain) String() string {
c.Init()
var out primitives.Buffer
out.WriteString(fmt.Sprintf(" %s\n", "CommitChain"))
out.WriteString(fmt.Sprintf(" %-20s %d\n", "Version", c.Version))
out.WriteString(fmt.Sprintf(" %-20s %s\n", "MilliTime", c.MilliTime))
out.WriteString(fmt.Sprintf(" %-20s %x\n", "ChainIDHash", c.ChainIDHash.Bytes()[:3]))
out.WriteString(fmt.Sprintf(" %-20s %x\n", "Weld", c.Weld.Bytes()[:3]))
out.WriteString(fmt.Sprintf(" %-20s %x\n", "EntryHash", c.EntryHash.Bytes()[:3]))
out.WriteString(fmt.Sprintf(" %-20s %d\n", "Credits", c.Credits))
out.WriteString(fmt.Sprintf(" %-20s %x\n", "ECPubKey", c.ECPubKey[:3]))
out.WriteString(fmt.Sprintf(" %-20s %x\n", "Sig", c.Sig[:3]))
return (string)(out.DeepCopyBytes())
}
// NewCommitChain creates a newly initialized commit chain
func NewCommitChain() *CommitChain {
c := new(CommitChain)
c.Init()
c.Version = 0
c.Credits = 0
return c
}
// GetEntryHash returns the entry hash
func (c *CommitChain) GetEntryHash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "CommitChain.GetEntryHash") }()
return c.EntryHash
}
// Hash marshals the object and computes the sha
func (c *CommitChain) Hash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "CommitChain.Hash") }()
bin, err := c.MarshalBinary()
if err != nil {
panic(err)
}
return primitives.Sha(bin)
}
// IsInterpretable always returns false
func (c *CommitChain) IsInterpretable() bool {
return false
}
// Interpret always returns the empty string ""
func (c *CommitChain) Interpret() string {
return ""
}
// CommitMsg returns the binary marshalled message section of the CommitEntry
// that is covered by the CommitEntry.Sig.
func (c *CommitChain) CommitMsg() []byte {
p, err := c.MarshalBinarySig()
if err != nil {
return []byte{byte(0)}
}
return p
}
// GetTimestamp returns the timestamp in milliseconds
func (c *CommitChain) GetTimestamp() interfaces.Timestamp {
c.Init()
a := make([]byte, 2, 8)
a = append(a, c.MilliTime[:]...)
milli := uint64(binary.BigEndian.Uint64(a))
return primitives.NewTimestampFromMilliseconds(milli)
}
// IsValid checks that the commit chain is valid: 11 < Credits <= 20, version==0, and valid signature
func (c *CommitChain) IsValid() bool {
c.Init()
//double check the credits in the commit
if c.Credits < 11 || c.Version != 0 || c.Credits > 20 {
return false
}
//if there were no errors in processing the signature, formatting or if didn't validate
if nil == c.ValidateSignatures() {
return true
}
return false
}
// GetHash marshals the entire object and computes the sha
func (c *CommitChain) GetHash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "CommitChain.GetHash") }()
data, _ := c.MarshalBinary()
return primitives.Sha(data)
}
// GetSigHash marshals the object covered by the signature, and computes its sha (version through entry credits hashed)
func (c *CommitChain) GetSigHash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "CommitChain.GetSigHash") }()
data := c.CommitMsg()
return primitives.Sha(data)
}
// MarshalBinarySig marshals the object covered by the signature (version through entry credits marshalled)
// If this serialized data is hashed, it becomes the transaction hash of chain commit. (version through entry credits)
func (c *CommitChain) MarshalBinarySig() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "CommitChain.MarshalBinarySig err:%v", *pe)
}
}(&err)
c.Init()
buf := primitives.NewBuffer(nil)
// 1 byte Version
err = buf.PushUInt8(c.Version)
if err != nil {
return nil, err
}
// 6 byte MilliTime
err = buf.PushBinaryMarshallable(c.MilliTime)
if err != nil {
return nil, err
}
// 32 byte double sha256 hash of the ChainID
err = buf.PushBinaryMarshallable(c.ChainIDHash)
if err != nil {
return nil, err
}
// 32 byte Commit Weld sha256(sha256(Entry Hash + ChainID))
err = buf.PushBinaryMarshallable(c.Weld)
if err != nil {
return nil, err
}
// 32 byte Entry Hash
err = buf.PushBinaryMarshallable(c.EntryHash)
if err != nil {
return nil, err
}
// 1 byte number of Entry Credits
err = buf.PushUInt8(c.Credits)
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
// MarshalBinaryTransaction partially marshals the object (version through pub key)
// NOTE: Contrary to what the name implies, this is not used to get a transaction hash, that
// seems to be done with the MarshalBinarySig function. Its unclear the origin of this function here.
func (c *CommitChain) MarshalBinaryTransaction() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "CommitChain.MarshalBinaryTransaction err:%v", *pe)
}
}(&err)
c.Init()
buf := new(primitives.Buffer)
b, err := c.MarshalBinarySig()
if err != nil {
return nil, err
}
buf.Write(b)
// 32 byte Public Key
buf.Write(c.ECPubKey[:])
return buf.DeepCopyBytes(), nil
}
// MarshalBinary marshals the entire object
func (c *CommitChain) MarshalBinary() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "CommitChain.MarshalBinary err:%v", *pe)
}
}(&err)
c.Init()
buf := new(primitives.Buffer)
b, err := c.MarshalBinaryTransaction()
if err != nil {
return nil, err
}
buf.Write(b)
// 32 byte Public Key
//buf.Write(c.ECPubKey[:])
// 64 byte Signature
buf.Write(c.Sig[:])
return buf.DeepCopyBytes(), nil
}
// Sign signs the object with the input private key
func (c *CommitChain) Sign(privateKey []byte) error {
c.Init()
sig, err := primitives.SignSignable(privateKey, c)
if err != nil {
return err
}
if c.Sig == nil {
c.Sig = new(primitives.ByteSlice64)
}
err = c.Sig.UnmarshalBinary(sig)
if err != nil {
return err
}
pub, err := primitives.PrivateKeyToPublicKey(privateKey)
if err != nil {
return err
}
if c.ECPubKey == nil {
c.ECPubKey = new(primitives.ByteSlice32)
}
err = c.ECPubKey.UnmarshalBinary(pub)
if err != nil {
return err
}
return nil
}
// ValidateSignatures validates that the object is properly signed
func (c *CommitChain) ValidateSignatures() error {
if c.ECPubKey == nil {
return fmt.Errorf("No public key present")
}
if c.Sig == nil {
return fmt.Errorf("No signature present")
}
data, err := c.MarshalBinarySig()
if err != nil {
return err
}
return primitives.VerifySignature(data, c.ECPubKey[:], c.Sig[:])
}
// ECID returns the hard coded type ECIDChainCommit
func (c *CommitChain) ECID() byte {
return constants.ECIDChainCommit
}
// UnmarshalBinaryData unmarshals the input data into this object
func (c *CommitChain) UnmarshalBinaryData(data []byte) ([]byte, error) {
c.Init()
buf := primitives.NewBuffer(data)
var err error
// 1 byte Version
c.Version, err = buf.PopUInt8()
if err != nil {
return nil, err
}
c.MilliTime = new(primitives.ByteSlice6)
err = buf.PopBinaryMarshallable(c.MilliTime)
if err != nil {
return nil, err
}
// 32 byte ChainIDHash
err = buf.PopBinaryMarshallable(c.ChainIDHash)
if err != nil {
return nil, err
}
// 32 byte Weld
err = buf.PopBinaryMarshallable(c.Weld)
if err != nil {
return nil, err
}
// 32 byte Entry Hash
err = buf.PopBinaryMarshallable(c.EntryHash)
if err != nil {
return nil, err
}
// 1 byte number of Entry Credits
c.Credits, err = buf.PopUInt8()
if err != nil {
return nil, err
}
// 32 byte Public Key
c.ECPubKey = new(primitives.ByteSlice32)
err = buf.PopBinaryMarshallable(c.ECPubKey)
if err != nil {
return nil, err
}
c.Sig = new(primitives.ByteSlice64)
err = buf.PopBinaryMarshallable(c.Sig)
if err != nil {
return nil, err
}
err = c.ValidateSignatures()
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
// UnmarshalBinary unmarshals the input data into this object
func (c *CommitChain) UnmarshalBinary(data []byte) (err error) {
_, err = c.UnmarshalBinaryData(data)
return
}
// JSONByte returns the json encoded byte array
func (c *CommitChain) JSONByte() ([]byte, error) {
return primitives.EncodeJSON(c)
}
// JSONString returns the json encoded string
func (c *CommitChain) JSONString() (string, error) {
return primitives.EncodeJSONString(c)
}