ceremonyclient/go-libp2p/core/test/peer.go
2023-08-20 23:07:43 -05:00

26 lines
397 B
Go

package test
import (
"crypto/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
}