github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/util/testutil/identity.go (about) 1 package testutil 2 3 import ( 4 "testing" 5 6 ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" 7 ci "github.com/ipfs/go-ipfs/p2p/crypto" 8 peer "github.com/ipfs/go-ipfs/p2p/peer" 9 ) 10 11 type Identity interface { 12 Address() ma.Multiaddr 13 ID() peer.ID 14 PrivateKey() ci.PrivKey 15 PublicKey() ci.PubKey 16 } 17 18 // TODO add a cheaper way to generate identities 19 20 func RandIdentity() (Identity, error) { 21 p, err := RandPeerNetParams() 22 if err != nil { 23 return nil, err 24 } 25 return &identity{*p}, nil 26 } 27 28 func RandIdentityOrFatal(t *testing.T) Identity { 29 p, err := RandPeerNetParams() 30 if err != nil { 31 t.Fatal(err) 32 } 33 return &identity{*p} 34 } 35 36 // identity is a temporary shim to delay binding of PeerNetParams. 37 type identity struct { 38 PeerNetParams 39 } 40 41 func (p *identity) ID() peer.ID { 42 return p.PeerNetParams.ID 43 } 44 45 func (p *identity) Address() ma.Multiaddr { 46 return p.Addr 47 } 48 49 func (p *identity) PrivateKey() ci.PrivKey { 50 return p.PrivKey 51 } 52 53 func (p *identity) PublicKey() ci.PubKey { 54 return p.PubKey 55 }