diff --git a/p2p/key.go b/p2p/key.go index 3ebae995d..ff750ccc4 100644 --- a/p2p/key.go +++ b/p2p/key.go @@ -21,6 +21,12 @@ type NodeID string // FIXME: support other length addresses? const NodeIDByteLength = crypto.AddressSize +// NodeIDFromPubKey returns the noe ID corresponding to the given PubKey. It's +// the hex-encoding of the pubKey.Address(). +func NodeIDFromPubKey(pubKey crypto.PubKey) NodeID { + return NodeID(hex.EncodeToString(pubKey.Address())) +} + // Bytes converts the node ID to it's binary byte representation. func (id NodeID) Bytes() ([]byte, error) { bz, err := hex.DecodeString(string(id)) @@ -76,12 +82,6 @@ func (nodeKey NodeKey) SaveAs(filePath string) error { return nil } -// PubKeyToID returns the ID corresponding to the given PubKey. -// It's the hex-encoding of the pubKey.Address(). -func PubKeyToID(pubKey crypto.PubKey) NodeID { - return NodeID(hex.EncodeToString(pubKey.Address())) -} - // LoadOrGenNodeKey attempts to load the NodeKey from the given filePath. If // the file does not exist, it generates and saves a new NodeKey. func LoadOrGenNodeKey(filePath string) (NodeKey, error) { @@ -106,7 +106,7 @@ func LoadOrGenNodeKey(filePath string) (NodeKey, error) { func GenNodeKey() NodeKey { privKey := ed25519.GenPrivKey() return NodeKey{ - ID: PubKeyToID(privKey.PubKey()), + ID: NodeIDFromPubKey(privKey.PubKey()), PrivKey: privKey, } } @@ -122,6 +122,6 @@ func LoadNodeKey(filePath string) (NodeKey, error) { if err != nil { return NodeKey{}, err } - nodeKey.ID = PubKeyToID(nodeKey.PubKey()) + nodeKey.ID = NodeIDFromPubKey(nodeKey.PubKey()) return nodeKey, nil } diff --git a/p2p/peer.go b/p2p/peer.go index f5bb9fa8e..a7632d03a 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -162,7 +162,7 @@ func newPeerConn(outbound, persistent bool, conn Connection) peerConn { // ID only exists for SecretConnection. func (pc peerConn) ID() NodeID { - return PubKeyToID(pc.conn.PubKey()) + return NodeIDFromPubKey(pc.conn.PubKey()) } // Return the IP from the connection RemoteAddr diff --git a/p2p/peer_test.go b/p2p/peer_test.go index cc556bf5b..92023c43f 100644 --- a/p2p/peer_test.go +++ b/p2p/peer_test.go @@ -81,7 +81,7 @@ func createOutboundPeerAndPerformHandshake( {ID: testCh, Priority: 1}, } pk := ed25519.GenPrivKey() - ourNodeInfo := testNodeInfo(PubKeyToID(pk.PubKey()), "host_peer") + ourNodeInfo := testNodeInfo(NodeIDFromPubKey(pk.PubKey()), "host_peer") transport := NewMConnTransport(log.TestingLogger(), ourNodeInfo, pk, mConfig) transport.SetChannelDescriptors(chDescs) reactorsByCh := map[byte]Reactor{testCh: NewTestReactor(chDescs, true)} @@ -154,7 +154,7 @@ func (rp *remotePeer) Addr() *NetAddress { } func (rp *remotePeer) ID() NodeID { - return PubKeyToID(rp.PrivKey.PubKey()) + return NodeIDFromPubKey(rp.PrivKey.PubKey()) } func (rp *remotePeer) Start() { @@ -167,7 +167,7 @@ func (rp *remotePeer) Start() { golog.Fatalf("net.Listen tcp :0: %+v", e) } rp.listener = l - rp.addr = NewNetAddress(PubKeyToID(rp.PrivKey.PubKey()), l.Addr()) + rp.addr = NewNetAddress(NodeIDFromPubKey(rp.PrivKey.PubKey()), l.Addr()) if rp.channels == nil { rp.channels = []byte{testCh} } diff --git a/p2p/transport_mconn.go b/p2p/transport_mconn.go index 324746a39..d44796779 100644 --- a/p2p/transport_mconn.go +++ b/p2p/transport_mconn.go @@ -466,7 +466,7 @@ func newMConnConnection( // For outgoing conns, ensure connection key matches dialed key. if expectPeerID != "" { - peerID := PubKeyToID(c.PubKey()) + peerID := NodeIDFromPubKey(c.PubKey()) if expectPeerID != peerID { err = ErrRejected{ conn: tcpConn, diff --git a/p2p/transport_memory.go b/p2p/transport_memory.go index 44ea9a56c..6768ff186 100644 --- a/p2p/transport_memory.go +++ b/p2p/transport_memory.go @@ -65,7 +65,7 @@ func (n *MemoryNetwork) CreateTransport( // Transport.Endpoints(). func (n *MemoryNetwork) GenerateTransport() *MemoryTransport { privKey := ed25519.GenPrivKey() - nodeID := PubKeyToID(privKey.PubKey()) + nodeID := NodeIDFromPubKey(privKey.PubKey()) nodeInfo := NodeInfo{ NodeID: nodeID, ListenAddr: fmt.Sprintf("%v:%v", MemoryProtocol, nodeID),