github.com/koko1123/flow-go-1@v0.29.6/model/fingerprint/fingerprint.go (about)

     1  package fingerprint
     2  
     3  import (
     4  	"github.com/koko1123/flow-go-1/model/encoding/rlp"
     5  )
     6  
     7  // Fingerprinter is a type that allows customization of the data used for the fingerprint of the entity. If a type does
     8  // not implement Fingerprinter, RLP encoding is used.
     9  type Fingerprinter interface {
    10  	Fingerprint() []byte
    11  }
    12  
    13  // Fingerprint returns a unique byte representation of the passed interface, which can be used as a pre-image for
    14  // hashing, signing or creating IDs/identifiers of the entity. By default, MakeID uses RLP to encode the data. If the
    15  // input defines its own canonical encoding by implementing Fingerprinter, it uses that instead. That allows removal of
    16  // non-unique fields from structs or overwriting of the used encoder. Fingerprint servers two purposes: a) JSON (the
    17  // default encoding) does not specify an order for the elements of arrays and objects, which could lead to different
    18  // hashes of the same entity depending on the JSON implementation and b) the Fingerprinter interface allows to exclude
    19  // fields not needed in the pre-image of the hash that comprises the Identifier, which could be different from the
    20  // encoding for sending entities in messages or for storing them.
    21  func Fingerprint(entity interface{}) []byte {
    22  	if fingerprinter, ok := entity.(Fingerprinter); ok {
    23  		return fingerprinter.Fingerprint()
    24  	}
    25  
    26  	return rlp.NewMarshaler().MustMarshal(entity)
    27  }