github.com/MetalBlockchain/metalgo@v1.11.9/vms/platformvm/warp/payload/payload.go (about) 1 // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. 2 // See the file LICENSE for licensing terms. 3 4 package payload 5 6 import ( 7 "errors" 8 "fmt" 9 ) 10 11 var errWrongType = errors.New("wrong payload type") 12 13 // Payload provides a common interface for all payloads implemented by this 14 // package. 15 type Payload interface { 16 // Bytes returns the binary representation of this payload. 17 Bytes() []byte 18 19 // initialize the payload with the provided binary representation. 20 initialize(b []byte) 21 } 22 23 func Parse(bytes []byte) (Payload, error) { 24 var payload Payload 25 if _, err := Codec.Unmarshal(bytes, &payload); err != nil { 26 return nil, err 27 } 28 payload.initialize(bytes) 29 return payload, nil 30 } 31 32 func initialize(p Payload) error { 33 bytes, err := Codec.Marshal(CodecVersion, &p) 34 if err != nil { 35 return fmt.Errorf("couldn't marshal %T payload: %w", p, err) 36 } 37 p.initialize(bytes) 38 return nil 39 }