forked from ferranbt/fastssz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytes.go
73 lines (60 loc) · 1.68 KB
/
bytes.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
package external
import (
ssz "github.com/bboris-dev/fastssz"
)
// Signature is a 96 bytes array external reference
type Signature struct {
Data [96]byte
}
// SizeSSZ implements the fastssz Marshaler interface
func (s *Signature) SizeSSZ() (size int) {
return 96
}
// MarshalSSZTo implements the fastssz Marshaler interface
func (s *Signature) MarshalSSZTo(buf []byte) ([]byte, error) {
return append(buf, s.Data[:]...), nil
}
// HashTreeRootWith implements the fastssz HashRoot interface
func (s *Signature) HashTreeRootWith(hh ssz.HashWalker) (err error) {
hh.PutBytes(s.Data[:])
return
}
// UnmarshalSSZ implements the fastssz Unmarshaler interface
func (s *Signature) UnmarshalSSZ(buf []byte) error {
copy(s.Data[:], buf)
return nil
}
// FixedSignature is a signature of fixed size
type FixedSignature [96]byte
// Bytes is a dynamic array of bytes
type Bytes []byte
// DynamicBytes is a dynamic reference
type DynamicBytes []byte
// SizeSSZ implements the fastssz Marshaler interface
func (d *DynamicBytes) SizeSSZ() (size int) {
return len(*d)
}
// MarshalSSZTo implements the fastssz Marshaler interface
func (d *DynamicBytes) MarshalSSZTo(buf []byte) ([]byte, error) {
if len(*d) > 256 {
return nil, ssz.ErrBytesLength
}
return append(buf, *d...), nil
}
// HashTreeRootWith implements the fastssz HashRoot interface
func (d *DynamicBytes) HashTreeRootWith(hh ssz.HashWalker) (err error) {
if len(*d) > 256 {
err = ssz.ErrBytesLength
return
}
hh.PutBytes(*d)
return
}
// UnmarshalSSZ implements the fastssz Unmarshaler interface
func (d *DynamicBytes) UnmarshalSSZ(buf []byte) error {
if len(buf) > 256 {
return ssz.ErrBytesLength
}
(*d) = append(*d, buf...)
return nil
}