github.com/filecoin-project/specs-actors/v4@v4.0.2/support/testing/cid.go (about)

     1  package testing
     2  
     3  import (
     4  	"github.com/filecoin-project/go-state-types/abi"
     5  	"github.com/ipfs/go-cid"
     6  	"github.com/minio/sha256-simd"
     7  	mh "github.com/multiformats/go-multihash"
     8  )
     9  
    10  func MakeCID(input string, prefix *cid.Prefix) cid.Cid {
    11  	data := []byte(input)
    12  	if prefix == nil {
    13  		c, err := abi.CidBuilder.Sum(data)
    14  		if err != nil {
    15  			panic(err)
    16  		}
    17  		return c
    18  	}
    19  	c, err := prefix.Sum(data)
    20  	switch err {
    21  	case mh.ErrSumNotSupported:
    22  		// multihash library doesn't support this hash function.
    23  		// just fake it.
    24  	case nil:
    25  		return c
    26  	default:
    27  		panic(err)
    28  	}
    29  
    30  	sum := sha256.Sum256(data)
    31  	hash, err := mh.Encode(sum[:], prefix.MhType)
    32  	if err != nil {
    33  		panic(err)
    34  	}
    35  	return cid.NewCidV1(prefix.Codec, hash)
    36  }