diff --git a/BUILD.bazel b/BUILD.bazel index e502174..1c29b7a 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -61,6 +61,7 @@ go_library( visibility = ["//visibility:public"], deps = [ "//types:go_default_library", + "@com_github_ferranbt_fastssz//:go_default_library", "@com_github_pkg_errors//:go_default_library", "@com_github_prysmaticlabs_go_bitfield//:go_default_library", ], diff --git a/ssz.go b/ssz.go index 61fb9ef..24755fc 100644 --- a/ssz.go +++ b/ssz.go @@ -5,6 +5,7 @@ import ( "reflect" "strings" + fssz "github.com/ferranbt/fastssz" "github.com/pkg/errors" "github.com/prysmaticlabs/go-bitfield" "github.com/prysmaticlabs/go-ssz/types" @@ -48,6 +49,11 @@ func Marshal(val interface{}) ([]byte, error) { if val == nil { return nil, errors.New("untyped-value nil cannot be marshaled") } + + if v, ok := val.(fssz.Marshaler); ok { + return v.MarshalSSZ() + } + rval := reflect.ValueOf(val) // We pre-allocate a buffer-size depending on the value's calculated total byte size. @@ -87,6 +93,9 @@ func Unmarshal(input []byte, val interface{}) error { if val == nil { return errors.New("cannot unmarshal into untyped, nil value") } + if v, ok := val.(fssz.Unmarshaler); ok { + return v.UnmarshalSSZ(input) + } if len(input) == 0 { return errors.New("no data to unmarshal from, input is an empty byte slice []byte{}") }