ceremonyclient/go-libp2p/core/test/peer.go

26 lines
397 B
Go
Raw Permalink Normal View History

2023-08-21 03:50:38 +00:00
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
}