diff --git a/domain.go b/domain.go index 77aff73..bdb6ae9 100644 --- a/domain.go +++ b/domain.go @@ -6,40 +6,56 @@ import ( fssz "github.com/ferranbt/fastssz" ) -var _ fssz.HashRoot = (Domain)([]byte{}) +var _ fssz.HashRoot = (Domain)([32]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 +type Domain [32]byte + +// Size of the array. +func (d *Domain) Size() int { + return len(d) +} + +// MarshalTo serializes the array to the buffer. +func (d *Domain) MarshalTo(data []byte) (int, error) { + return copy(data[:d.Size()], d[:]), nil +} + +// Unmarshal deserializes into the provided array type. +func (d *Domain) Unmarshal(data []byte) error { + copy(d[:], data) + return nil +} // HashTreeRoot returns calculated hash root. -func (e Domain) HashTreeRoot() ([32]byte, error) { - return fssz.HashWithDefaultHasher(e) +func (d Domain) HashTreeRoot() ([32]byte, error) { + return fssz.HashWithDefaultHasher(d) } // HashTreeRootWith hashes a Domain object with a Hasher from the default HasherPool. -func (e Domain) HashTreeRootWith(hh *fssz.Hasher) error { - hh.PutBytes(e[:]) +func (d Domain) HashTreeRootWith(hh *fssz.Hasher) error { + hh.PutBytes(d[:]) 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)) +func (d *Domain) UnmarshalSSZ(buf []byte) error { + if len(buf) != d.SizeSSZ() { + return fmt.Errorf("expected buffer of length %d received %d", d.SizeSSZ(), len(buf)) } var b [32]byte - item := Domain(b[:]) - copy(item, buf) - *e = item + item := Domain(b) + copy(item[:], buf) + *d = item return nil } // MarshalSSZTo marshals Domain with the provided byte slice. -func (e *Domain) MarshalSSZTo(dst []byte) ([]byte, error) { - marshalled, err := e.MarshalSSZ() +func (d *Domain) MarshalSSZTo(dst []byte) ([]byte, error) { + marshalled, err := d.MarshalSSZ() if err != nil { return nil, err } @@ -47,11 +63,12 @@ func (e *Domain) MarshalSSZTo(dst []byte) ([]byte, error) { } // MarshalSSZ marshals Domain into a serialized object. -func (e *Domain) MarshalSSZ() ([]byte, error) { - return *e, nil +func (d *Domain) MarshalSSZ() ([]byte, error) { + b := [32]byte(*d) + return b[:], nil } // SizeSSZ returns the size of the serialized object. -func (e *Domain) SizeSSZ() int { +func (d *Domain) SizeSSZ() int { return 32 }