go-libp2p-core/test/peer.go

26 lines
395 B
Go
Raw Normal View History

package test
import (
"math/rand"
"testing"
"github.com/libp2p/go-libp2p-core/peer"
mh "github.com/multiformats/go-multihash"
)
func RandPeerID() (peer.ID, error) {
buf := make([]byte, 16)
rand.Read(buf)
h, _ := mh.Sum(buf, mh.SHA2_256, -1)
return peer.ID(h), nil
}
func RandPeerIDFatal(t testing.TB) peer.ID {
p, err := RandPeerID()
if err != nil {
t.Fatal(err)
}
return p
}