-
Notifications
You must be signed in to change notification settings - Fork 8
/
domain.go
57 lines (46 loc) · 1.37 KB
/
domain.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
package types
import (
"fmt"
fssz "github.com/ferranbt/fastssz"
)
var _ fssz.HashRoot = (Domain)([]byte{})
var _ fssz.Marshaler = (*Domain)(nil)
var _ fssz.Unmarshaler = (*Domain)(nil)
// Domain represents a 32 bytes domain object in Ethereum beacon chain consensus.
type Domain []byte
// HashTreeRoot returns calculated hash root.
func (e Domain) HashTreeRoot() ([32]byte, error) {
return fssz.HashWithDefaultHasher(e)
}
// HashTreeRootWith hashes a Domain object with a Hasher from the default HasherPool.
func (e Domain) HashTreeRootWith(hh *fssz.Hasher) error {
hh.PutBytes(e[:])
return nil
}
// UnmarshalSSZ deserializes the provided bytes buffer into the Domain object.
func (e *Domain) UnmarshalSSZ(buf []byte) error {
if len(buf) != e.SizeSSZ() {
return fmt.Errorf("expected buffer of length %d received %d", e.SizeSSZ(), len(buf))
}
var b [32]byte
item := Domain(b[:])
copy(item, buf)
*e = item
return nil
}
// MarshalSSZTo marshals Domain with the provided byte slice.
func (e *Domain) MarshalSSZTo(dst []byte) ([]byte, error) {
marshalled, err := e.MarshalSSZ()
if err != nil {
return nil, err
}
return append(dst, marshalled...), nil
}
// MarshalSSZ marshals Domain into a serialized object.
func (e *Domain) MarshalSSZ() ([]byte, error) {
return *e, nil
}
// SizeSSZ returns the size of the serialized object.
func (e *Domain) SizeSSZ() int {
return 32
}