go-libp2p-core/test/peer.go
Raúl Kripalani 07fbce0b46
absorb {crypto,peer} test utils. (#8)
Avoids circular module dependency.
2019-05-23 16:07:58 +01:00

31 lines
515 B
Go

package test
import (
"io"
"math/rand"
"testing"
"time"
"github.com/libp2p/go-libp2p-core/peer"
mh "github.com/multiformats/go-multihash"
)
func RandPeerID() (peer.ID, error) {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
buf := make([]byte, 16)
if _, err := io.ReadFull(r, buf); err != nil {
return "", err
}
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
}