Noise: encrypt and decrypt in NoiseConnection

TODO: Add a buffer to read only `n` bytes in `read(n)`
This commit is contained in:
mhchia
2020-02-17 16:30:44 +08:00
parent 6ea96e9313
commit f8739268e2
3 changed files with 34 additions and 7 deletions

View File

@ -1,11 +1,15 @@
from noise.connection import NoiseConnection as NoiseState
from libp2p.crypto.keys import PrivateKey
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.noise.io import MsgReadWriter, NoiseTransportReadWriter
class NoiseConnection(BaseSession):
conn: IRawConnection
read_writer: IRawConnection
noise_state: NoiseState
def __init__(
self,
@ -14,17 +18,21 @@ class NoiseConnection(BaseSession):
remote_peer: ID,
conn: IRawConnection,
is_initiator: bool,
noise_state: NoiseState,
) -> None:
super().__init__(local_peer, local_private_key, is_initiator, remote_peer)
self.conn = conn
self.noise_state = noise_state
def get_msg_read_writer(self) -> MsgReadWriter:
return NoiseTransportReadWriter(self.conn, self.noise_state)
async def read(self, n: int = None) -> bytes:
# TODO: Add decryption logic here
return await self.conn.read(n)
# TODO: Use a buffer to handle buffered messages.
return await self.get_msg_read_writer().read_msg()
async def write(self, data: bytes) -> None:
# TODO: Add encryption logic here
await self.conn.write(data)
await self.get_msg_read_writer().write_msg(data)
async def close(self) -> None:
await self.conn.close()