Use types for {Private,Public}Key and address other missing type hints

This commit is contained in:
Alex Stokes 2019-08-15 16:26:22 -07:00
parent e7d2681fc0
commit 2e3ffb9d53
No known key found for this signature in database
GPG Key ID: 51CE1721B245C086
5 changed files with 26 additions and 28 deletions

View File

@ -1,7 +1,7 @@
import asyncio
from typing import Mapping, Sequence
from libp2p.crypto.keys import KeyPair
from libp2p.crypto.keys import KeyPair, PrivateKey
from libp2p.crypto.rsa import create_new_key_pair
from libp2p.host.basic_host import BasicHost
from libp2p.kademlia.network import KademliaServer
@ -38,7 +38,7 @@ def generate_new_rsa_identity() -> KeyPair:
return create_new_key_pair()
def generate_peer_id_from_rsa_identity(key_pair=None) -> ID:
def generate_peer_id_from_rsa_identity(key_pair: KeyPair = None) -> ID:
if not key_pair:
key_pair = generate_new_rsa_identity()
public_key = key_pair.public_key
@ -69,7 +69,7 @@ def initialize_default_kademlia_router(
def initialize_default_swarm(
private_key: bytes,
private_key: PrivateKey,
id_opt: ID = None,
transport_opt: Sequence[str] = None,
muxer_opt: Sequence[str] = None,
@ -98,11 +98,9 @@ def initialize_default_swarm(
# TODO TransportUpgrader is not doing anything really
# TODO parse muxer and sec to pass into TransportUpgrader
muxer = muxer_opt or ["mplex/6.7.0"]
private_key_bytes = private_key.export_key("DER")
public_key_bytes = private_key.publickey().export_key("DER")
security_transports_by_protocol = sec_opt or {
TProtocol("insecure/1.0.0"): InsecureTransport(
private_key_bytes, public_key_bytes
private_key, private_key.get_public_key()
)
}
upgrader = TransportUpgrader(security_transports_by_protocol, muxer)
@ -115,7 +113,7 @@ def initialize_default_swarm(
async def new_node(
private_key=None,
private_key: PrivateKey = None,
swarm_opt: INetwork = None,
id_opt: ID = None,
transport_opt: Sequence[str] = None,

View File

@ -1,3 +1,6 @@
from typing import Optional
from libp2p.crypto.keys import PrivateKey, PublicKey
from libp2p.network.connection.raw_connection_interface import IRawConnection
from libp2p.peer.id import ID
from libp2p.security.base_transport import BaseSecureTransport
@ -17,25 +20,16 @@ class BaseSession(ISecureConn, IRawConnection):
self.local_private_key = transport.local_private_key
self.conn = conn
self.remote_peer_id = peer_id
self.remote_permanent_pubkey = b""
self.remote_permanent_pubkey = None
# TODO clean up how this is passed around?
@property
def initiator(self) -> bool:
return self.conn.initiator
self.initiator = self.conn.initiator
self.writer = self.conn.writer
self.reader = self.conn.reader
# TODO clean up how this is passed around?
def next_stream_id(self) -> int:
return self.conn.next_stream_id()
@property
def writer(self):
return self.conn.writer
@property
def reader(self):
return self.conn.reader
async def write(self, data: bytes) -> None:
await self.conn.write(data)
@ -48,11 +42,11 @@ class BaseSession(ISecureConn, IRawConnection):
def get_local_peer(self) -> ID:
return self.local_peer
def get_local_private_key(self) -> bytes:
def get_local_private_key(self) -> PrivateKey:
return self.local_private_key
def get_remote_peer(self) -> ID:
return self.remote_peer_id
def get_remote_public_key(self) -> bytes:
def get_remote_public_key(self) -> Optional[PublicKey]:
return self.remote_permanent_pubkey

View File

@ -1,3 +1,4 @@
from libp2p.crypto.keys import PrivateKey, PublicKey
from libp2p.peer.id import ID
from libp2p.security.secure_transport_interface import ISecureTransport
@ -8,6 +9,8 @@ class BaseSecureTransport(ISecureTransport):
is only meant to be used in clases that derive from it.
"""
def __init__(self, local_private_key: bytes, local_public_key: bytes) -> None:
def __init__(
self, local_private_key: PrivateKey, local_public_key: PublicKey
) -> None:
self.local_private_key = local_private_key
self.local_peer = ID.from_pubkey(local_public_key)

View File

@ -1,7 +1,8 @@
from abc import ABC, abstractmethod
from libp2p.peer.id import ID
from libp2p.crypto.keys import PrivateKey, PublicKey
from libp2p.network.connection.raw_connection_interface import IRawConnection
from libp2p.peer.id import ID
"""
@ -18,7 +19,7 @@ class AbstractSecureConn(ABC):
pass
@abstractmethod
def get_local_private_key(self) -> bytes:
def get_local_private_key(self) -> PrivateKey:
pass
@abstractmethod
@ -26,7 +27,7 @@ class AbstractSecureConn(ABC):
pass
@abstractmethod
def get_remote_public_key(self) -> bytes:
def get_remote_public_key(self) -> PublicKey:
pass

View File

@ -1,5 +1,5 @@
from abc import ABC
from typing import Dict
from typing import Dict, Mapping
from libp2p.network.connection.raw_connection_interface import IRawConnection
from libp2p.peer.id import ID
@ -23,7 +23,9 @@ class SecurityMultistream(ABC):
multiselect: Multiselect
multiselect_client: MultiselectClient
def __init__(self, secure_transports_by_protocol) -> None:
def __init__(
self, secure_transports_by_protocol: Mapping[TProtocol, ISecureTransport]
) -> None:
self.transports = {}
self.multiselect = Multiselect()
self.multiselect_client = MultiselectClient()