github.com/storacha/go-ucanto@v0.7.2/testing/helpers/helpers.go (about)

     1  package helpers
     2  
     3  import (
     4  	crand "crypto/rand"
     5  
     6  	"github.com/ipfs/go-cid"
     7  	"github.com/ipld/go-ipld-prime/datamodel"
     8  	cidlink "github.com/ipld/go-ipld-prime/linking/cid"
     9  	"github.com/multiformats/go-multihash"
    10  )
    11  
    12  // Must takes return values from a function and returns the non-error one. If
    13  // the error value is non-nil then it panics.
    14  func Must[T any](val T, err error) T {
    15  	if err != nil {
    16  		panic(err)
    17  	}
    18  	return val
    19  }
    20  
    21  func RandomBytes(size int) []byte {
    22  	bytes := make([]byte, size)
    23  	_, _ = crand.Read(bytes)
    24  	return bytes
    25  }
    26  
    27  func RandomCID() datamodel.Link {
    28  	bytes := RandomBytes(10)
    29  	c, _ := cid.Prefix{
    30  		Version:  1,
    31  		Codec:    cid.Raw,
    32  		MhType:   multihash.SHA2_256,
    33  		MhLength: -1,
    34  	}.Sum(bytes)
    35  	return cidlink.Link{Cid: c}
    36  }
    37  
    38  func RandomDigest() multihash.Multihash {
    39  	return RandomCID().(cidlink.Link).Hash()
    40  }