add typing to security

This commit is contained in:
Chih Cheng Liang
2019-08-01 19:12:11 +08:00
parent 030abcc959
commit a86f010c95
7 changed files with 102 additions and 26 deletions

View File

@ -2,12 +2,21 @@ import asyncio
from libp2p.security.secure_transport_interface import ISecureTransport
from libp2p.security.secure_conn_interface import ISecureConn
from typing import TYPE_CHECKING, cast
if TYPE_CHECKING:
from libp2p.network.connection.raw_connection_interface import IRawConnection
from libp2p.peer.id import ID
from .typing import TSecurityDetails
class SimpleSecurityTransport(ISecureTransport):
def __init__(self, key_phrase):
key_phrase: str
def __init__(self, key_phrase: str) -> None:
self.key_phrase = key_phrase
async def secure_inbound(self, conn):
async def secure_inbound(self, conn: "IRawConnection") -> "ISecureConn":
"""
Secure the connection, either locally or by communicating with opposing node via conn,
for an inbound connection (i.e. we are not the initiator)
@ -24,7 +33,9 @@ class SimpleSecurityTransport(ISecureTransport):
secure_conn = SimpleSecureConn(conn, self.key_phrase)
return secure_conn
async def secure_outbound(self, conn, peer_id):
async def secure_outbound(
self, conn: "IRawConnection", peer_id: "ID"
) -> "ISecureConn":
"""
Secure the connection, either locally or by communicating with opposing node via conn,
for an inbound connection (i.e. we are the initiator)
@ -47,18 +58,22 @@ class SimpleSecurityTransport(ISecureTransport):
class SimpleSecureConn(ISecureConn):
def __init__(self, conn, key_phrase):
conn: "IRawConnection"
key_phrase: str
details: "TSecurityDetails"
def __init__(self, conn: "IRawConnection", key_phrase: str) -> None:
self.conn = conn
self.details = {}
self.details = cast("TSecurityDetails", {})
self.details["key_phrase"] = key_phrase
def get_conn(self):
def get_conn(self) -> "ISecureConn":
"""
:return: connection object that has been made secure
"""
return self.conn
return cast("ISecureConn", self.conn)
def get_security_details(self):
def get_security_details(self) -> "TSecurityDetails":
"""
:return: map containing details about the connections security
"""