github.com/ava-labs/avalanchego@v1.11.11/vms/platformvm/warp/payload/hash.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  	"fmt"
     8  
     9  	"github.com/ava-labs/avalanchego/ids"
    10  )
    11  
    12  var _ Payload = (*Hash)(nil)
    13  
    14  type Hash struct {
    15  	Hash ids.ID `serialize:"true"`
    16  
    17  	bytes []byte
    18  }
    19  
    20  // NewHash creates a new *Hash and initializes it.
    21  func NewHash(hash ids.ID) (*Hash, error) {
    22  	bhp := &Hash{
    23  		Hash: hash,
    24  	}
    25  	return bhp, initialize(bhp)
    26  }
    27  
    28  // ParseHash converts a slice of bytes into an initialized Hash.
    29  func ParseHash(b []byte) (*Hash, error) {
    30  	payloadIntf, err := Parse(b)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	payload, ok := payloadIntf.(*Hash)
    35  	if !ok {
    36  		return nil, fmt.Errorf("%w: %T", errWrongType, payloadIntf)
    37  	}
    38  	return payload, nil
    39  }
    40  
    41  // Bytes returns the binary representation of this payload. It assumes that the
    42  // payload is initialized from either NewHash or Parse.
    43  func (b *Hash) Bytes() []byte {
    44  	return b.bytes
    45  }
    46  
    47  func (b *Hash) initialize(bytes []byte) {
    48  	b.bytes = bytes
    49  }