diff --git a/libp2p/security/base_session.py b/libp2p/security/base_session.py new file mode 100644 index 0000000..3cfd019 --- /dev/null +++ b/libp2p/security/base_session.py @@ -0,0 +1,32 @@ +from libp2p.network.connection.raw_connection_interface import IRawConnection +from libp2p.peer.id import ID +from libp2p.security.base_transport import BaseSecureTransport +from libp2p.security.secure_conn_interface import ISecureConn + + +class BaseSession(ISecureConn, IRawConnection): + """ + ``BaseSession`` is not fully instantiated from its abstract classes as it + is only meant to be used in clases that derive from it. + """ + + def __init__( + self, transport: BaseSecureTransport, conn: IRawConnection, peer_id: ID + ) -> None: + self.local_peer = self.transport.local_peer + self.local_private_key = self.transport.local_private_key + self.insecure_conn = conn + self.remote_peer_id = peer_id + self.remote_permanent_pubkey = b"" + + def get_local_peer(self) -> ID: + return self.local_peer + + def get_local_private_key(self) -> bytes: + return self.local_private_key + + def get_remote_peer(self) -> ID: + return self.remote_peer_id + + def get_remote_public_key(self) -> bytes: + return self.remote_permanent_pubkey diff --git a/libp2p/security/base_transport.py b/libp2p/security/base_transport.py new file mode 100644 index 0000000..65c9757 --- /dev/null +++ b/libp2p/security/base_transport.py @@ -0,0 +1,13 @@ +from libp2p.peer.id import ID +from libp2p.security.secure_transport_interface import ISecureTransport + + +class BaseSecureTransport(ISecureTransport): + """ + ``BaseSecureTransport`` is not fully instantiated from its abstract classes as it + is only meant to be used in clases that derive from it. + """ + + def __init__(self, local_private_key: bytes) -> None: + self.local_private_key = local_private_key + self.local_peer = ID.from_privkey(local_private_key) diff --git a/libp2p/security/insecure/transport.py b/libp2p/security/insecure/transport.py index 4ec378b..262b61a 100644 --- a/libp2p/security/insecure/transport.py +++ b/libp2p/security/insecure/transport.py @@ -1,10 +1,24 @@ from libp2p.network.connection.raw_connection_interface import IRawConnection from libp2p.peer.id import ID +from libp2p.security.base_session import BaseSession +from libp2p.security.base_transport import BaseSecureTransport from libp2p.security.secure_conn_interface import ISecureConn -from libp2p.security.secure_transport_interface import ISecureTransport -class InsecureTransport(ISecureTransport): +class InsecureSession(BaseSession): + def __init__( + self, transport: BaseSecureTransport, conn: IRawConnection, peer_id: ID + ) -> None: + super(InsecureSession, self).__init__(transport, conn, peer_id) + + async def write(self, data: bytes) -> None: + await self.insecure_conn.write(data) + + async def read(self) -> bytes: + return await self.insecure_conn.read() + + +class InsecureTransport(BaseSecureTransport): """ ``InsecureTransport`` provides the "identity" upgrader for a ``IRawConnection``, i.e. the upgraded transport does not add any additional security. @@ -16,7 +30,7 @@ class InsecureTransport(ISecureTransport): for an inbound connection (i.e. we are not the initiator) :return: secure connection object (that implements secure_conn_interface) """ - return conn + return InsecureSession(self, conn, ID(b"")) async def secure_outbound(self, conn: IRawConnection, peer_id: ID) -> ISecureConn: """ @@ -24,4 +38,4 @@ class InsecureTransport(ISecureTransport): for an inbound connection (i.e. we are the initiator) :return: secure connection object (that implements secure_conn_interface) """ - return conn + return InsecureSession(self, conn, peer_id)