Apply PR feedback: add_key_pair

This commit is contained in:
NIC619 2019-11-26 11:52:31 +08:00
parent 076dae50f3
commit 828ae69c66
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17
4 changed files with 22 additions and 7 deletions

View File

@ -106,8 +106,7 @@ def initialize_default_swarm(
peerstore = peerstore_opt or PeerStore() peerstore = peerstore_opt or PeerStore()
# Store our key pair in peerstore # Store our key pair in peerstore
peerstore.add_pubkey(id_opt, key_pair.public_key) peerstore.add_key_pair(id_opt, key_pair)
peerstore.add_privkey(id_opt, key_pair.private_key)
# TODO: Initialize discovery if not presented # TODO: Initialize discovery if not presented
return Swarm(id_opt, peerstore, upgrader, transport) return Swarm(id_opt, peerstore, upgrader, transport)

View File

@ -2,7 +2,7 @@ from typing import Any, Dict, List, Optional, Sequence, Set
from multiaddr import Multiaddr from multiaddr import Multiaddr
from libp2p.crypto.keys import PrivateKey, PublicKey from libp2p.crypto.keys import KeyPair, PrivateKey, PublicKey
from .id import ID from .id import ID
from .peerdata import PeerData, PeerDataError from .peerdata import PeerData, PeerDataError
@ -34,7 +34,7 @@ class PeerStore(IPeerStore):
return self.peer_data_map[peer_id] return self.peer_data_map[peer_id]
data = PeerData() data = PeerData()
self.peer_data_map[peer_id] = data self.peer_data_map[peer_id] = data
return self.peer_data_map[peer_id] return data
def peer_info(self, peer_id: ID) -> Optional[PeerInfo]: def peer_info(self, peer_id: ID) -> Optional[PeerInfo]:
""" """
@ -196,6 +196,15 @@ class PeerStore(IPeerStore):
return self.peer_privkey_map[peer_id] return self.peer_privkey_map[peer_id]
raise PeerStoreError("peer ID not found") raise PeerStoreError("peer ID not found")
def add_key_pair(self, peer_id: ID, key_pair: KeyPair) -> None:
"""
:param peer_id: peer ID to add private key for
:param key_pair:
:raise PeerStoreError: if peer ID already has pubkey or privkey set
"""
self.add_pubkey(peer_id, key_pair.public_keypubkey)
self.add_privkey(peer_id, key_pair.private_key)
def peers_with_keys(self) -> Set[ID]: def peers_with_keys(self) -> Set[ID]:
""" """
:return: all of the peer IDs which has pubkey/privkey stored in peer store :return: all of the peer IDs which has pubkey/privkey stored in peer store

View File

@ -3,7 +3,7 @@ from typing import Any, List, Sequence, Set
from multiaddr import Multiaddr from multiaddr import Multiaddr
from libp2p.crypto.keys import PrivateKey, PublicKey from libp2p.crypto.keys import KeyPair, PrivateKey, PublicKey
from .addrbook_interface import IAddrBook from .addrbook_interface import IAddrBook
from .id import ID from .id import ID
@ -131,6 +131,14 @@ class IPeerStore(IAddrBook, IPeerMetadata):
:raise PeerStoreError: if peer ID not found :raise PeerStoreError: if peer ID not found
""" """
@abstractmethod
def add_key_pair(self, peer_id: ID, key_pair: KeyPair) -> None:
"""
:param peer_id: peer ID to add private key for
:param key_pair:
:raise PeerStoreError: if peer ID already has pubkey or privkey set
"""
@abstractmethod @abstractmethod
def peers_with_keys(self) -> Set[ID]: def peers_with_keys(self) -> Set[ID]:
""" """

View File

@ -36,8 +36,7 @@ from .utils import connect, connect_swarm
def initialize_peerstore_with_our_keypair(self_id: ID, key_pair: KeyPair) -> PeerStore: def initialize_peerstore_with_our_keypair(self_id: ID, key_pair: KeyPair) -> PeerStore:
peer_store = PeerStore() peer_store = PeerStore()
peer_store.add_pubkey(self_id, key_pair.public_key) peer_store.add_key_pair(self_id, key_pair)
peer_store.add_privkey(self_id, key_pair.private_key)
return peer_store return peer_store