-
Notifications
You must be signed in to change notification settings - Fork 6
/
data.go
52 lines (44 loc) · 1.29 KB
/
data.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
package merkletree
import (
"bytes"
"encoding/hex"
"fmt"
)
// Data is the type used to represent the data stored in an entry of the MT.
// It consists of 8 elements: e0, e1, e2, e3, ...;
// where v = [e0,e1], index = [e2,e3].
type Data [DataLen]ElemBytes
func (d *Data) String() string {
return fmt.Sprintf("%s%s%s%s", hex.EncodeToString(d[0][:]), hex.EncodeToString(d[1][:]),
hex.EncodeToString(d[2][:]), hex.EncodeToString(d[3][:]))
}
func (d *Data) Bytes() (b [ElemBytesLen * DataLen]byte) {
for i := 0; i < DataLen; i++ {
copy(b[i*ElemBytesLen:(i+1)*ElemBytesLen], d[i][:])
}
return b
}
func (d1 *Data) Equal(d2 *Data) bool {
return bytes.Equal(d1[0][:], d2[0][:]) && bytes.Equal(d1[1][:], d2[1][:]) &&
bytes.Equal(d1[2][:], d2[2][:]) && bytes.Equal(d1[3][:], d2[3][:])
}
func (d Data) MarshalText() ([]byte, error) {
dataBytes := d.Bytes()
return []byte(hex.EncodeToString(dataBytes[:])), nil
}
func (d *Data) UnmarshalText(text []byte) error {
var dataBytes [ElemBytesLen * DataLen]byte
_, err := hex.Decode(dataBytes[:], text)
if err != nil {
return err
}
*d = *NewDataFromBytes(dataBytes)
return nil
}
func NewDataFromBytes(b [ElemBytesLen * DataLen]byte) *Data {
d := &Data{}
for i := 0; i < DataLen; i++ {
copy(d[i][:], b[i*ElemBytesLen : (i+1)*ElemBytesLen][:])
}
return d
}