mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Merge pull request #331 from dmuhs/fix/docs-format
Add automatic docstring formatting
This commit is contained in:
@ -6,10 +6,8 @@ from libp2p.security.secure_conn_interface import ISecureConn
|
||||
|
||||
|
||||
class BaseSession(ISecureConn):
|
||||
"""
|
||||
``BaseSession`` is not fully instantiated from its abstract classes as it
|
||||
is only meant to be used in clases that derive from it.
|
||||
"""
|
||||
"""``BaseSession`` is not fully instantiated from its abstract classes as
|
||||
it is only meant to be used in clases that derive from it."""
|
||||
|
||||
local_peer: ID
|
||||
local_private_key: PrivateKey
|
||||
|
||||
@ -12,12 +12,12 @@ def default_secure_bytes_provider(n: int) -> bytes:
|
||||
|
||||
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.
|
||||
``BaseSecureTransport`` is not fully instantiated from its abstract classes
|
||||
as it is only meant to be used in clases that derive from it.
|
||||
|
||||
Clients can provide a strategy to get cryptographically secure bytes of a given length.
|
||||
A default implementation is provided using the ``secrets`` module from the
|
||||
standard library.
|
||||
Clients can provide a strategy to get cryptographically secure bytes
|
||||
of a given length. A default implementation is provided using the
|
||||
``secrets`` module from the standard library.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
|
||||
@ -46,9 +46,7 @@ class InsecureSession(BaseSession):
|
||||
await self.conn.close()
|
||||
|
||||
async def run_handshake(self) -> None:
|
||||
"""
|
||||
Raise `HandshakeFailure` when handshake failed
|
||||
"""
|
||||
"""Raise `HandshakeFailure` when handshake failed."""
|
||||
msg = make_exchange_message(self.local_private_key.get_public_key())
|
||||
msg_bytes = msg.SerializeToString()
|
||||
encoded_msg_bytes = encode_fixedint_prefixed(msg_bytes)
|
||||
@ -104,15 +102,16 @@ class InsecureSession(BaseSession):
|
||||
|
||||
|
||||
class InsecureTransport(BaseSecureTransport):
|
||||
"""
|
||||
``InsecureTransport`` provides the "identity" upgrader for a ``IRawConnection``,
|
||||
i.e. the upgraded transport does not add any additional security.
|
||||
"""
|
||||
"""``InsecureTransport`` provides the "identity" upgrader for a
|
||||
``IRawConnection``, i.e. the upgraded transport does not add any additional
|
||||
security."""
|
||||
|
||||
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)
|
||||
Secure the connection, either locally or by communicating with opposing
|
||||
node via conn, for an inbound connection (i.e. we are not the
|
||||
initiator)
|
||||
|
||||
:return: secure connection object (that implements secure_conn_interface)
|
||||
"""
|
||||
session = InsecureSession(self.local_peer, self.local_private_key, conn, False)
|
||||
@ -121,8 +120,9 @@ class InsecureTransport(BaseSecureTransport):
|
||||
|
||||
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)
|
||||
Secure the connection, either locally or by communicating with opposing
|
||||
node via conn, for an inbound connection (i.e. we are the initiator)
|
||||
|
||||
:return: secure connection object (that implements secure_conn_interface)
|
||||
"""
|
||||
session = InsecureSession(
|
||||
|
||||
@ -6,10 +6,8 @@ class SecioException(HandshakeFailure):
|
||||
|
||||
|
||||
class SelfEncryption(SecioException):
|
||||
"""
|
||||
Raised to indicate that a host is attempting to encrypt communications
|
||||
with itself.
|
||||
"""
|
||||
"""Raised to indicate that a host is attempting to encrypt communications
|
||||
with itself."""
|
||||
|
||||
pass
|
||||
|
||||
|
||||
@ -143,10 +143,8 @@ class SecureSession(BaseSession):
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Proposal:
|
||||
"""
|
||||
A ``Proposal`` represents the set of session parameters one peer in a pair of
|
||||
peers attempting to negotiate a `secio` channel prefers.
|
||||
"""
|
||||
"""A ``Proposal`` represents the set of session parameters one peer in a
|
||||
pair of peers attempting to negotiate a `secio` channel prefers."""
|
||||
|
||||
nonce: bytes
|
||||
public_key: PublicKey
|
||||
@ -408,9 +406,9 @@ async def create_secure_session(
|
||||
) -> ISecureConn:
|
||||
"""
|
||||
Attempt the initial `secio` handshake with the remote peer.
|
||||
If successful, return an object that provides secure communication to the
|
||||
``remote_peer``.
|
||||
Raise `SecioException` when `conn` closed.
|
||||
|
||||
If successful, return an object that provides secure communication
|
||||
to the ``remote_peer``. Raise `SecioException` when `conn` closed.
|
||||
Raise `InconsistentNonce` when handshake failed
|
||||
"""
|
||||
msg_io = MsgIOReadWriter(conn)
|
||||
@ -443,18 +441,18 @@ async def create_secure_session(
|
||||
|
||||
|
||||
class Transport(BaseSecureTransport):
|
||||
"""
|
||||
``Transport`` provides a security upgrader for a ``IRawConnection``,
|
||||
following the `secio` protocol defined in the libp2p specs.
|
||||
"""
|
||||
"""``Transport`` provides a security upgrader for a ``IRawConnection``,
|
||||
following the `secio` protocol defined in the libp2p specs."""
|
||||
|
||||
def get_nonce(self) -> bytes:
|
||||
return self.secure_bytes_provider(NONCE_SIZE)
|
||||
|
||||
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)
|
||||
Secure the connection, either locally or by communicating with opposing
|
||||
node via conn, for an inbound connection (i.e. we are not the
|
||||
initiator)
|
||||
|
||||
:return: secure connection object (that implements secure_conn_interface)
|
||||
"""
|
||||
local_nonce = self.get_nonce()
|
||||
@ -469,8 +467,9 @@ class Transport(BaseSecureTransport):
|
||||
self, conn: IRawConnection, peer_id: PeerID
|
||||
) -> ISecureConn:
|
||||
"""
|
||||
Secure the connection, either locally or by communicating with opposing node via conn,
|
||||
for an inbound connection (i.e. we are the initiator)
|
||||
Secure the connection, either locally or by communicating with opposing
|
||||
node via conn, for an inbound connection (i.e. we are the initiator)
|
||||
|
||||
:return: secure connection object (that implements secure_conn_interface)
|
||||
"""
|
||||
local_nonce = self.get_nonce()
|
||||
|
||||
@ -17,15 +17,18 @@ class ISecureTransport(ABC):
|
||||
@abstractmethod
|
||||
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)
|
||||
Secure the connection, either locally or by communicating with opposing
|
||||
node via conn, for an inbound connection (i.e. we are not the
|
||||
initiator)
|
||||
|
||||
:return: secure connection object (that implements secure_conn_interface)
|
||||
"""
|
||||
|
||||
@abstractmethod
|
||||
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)
|
||||
Secure the connection, either locally or by communicating with opposing
|
||||
node via conn, for an inbound connection (i.e. we are the initiator)
|
||||
|
||||
:return: secure connection object (that implements secure_conn_interface)
|
||||
"""
|
||||
|
||||
@ -23,6 +23,7 @@ Relevant go repo: https://github.com/libp2p/go-conn-security/blob/master/interfa
|
||||
class SecurityMultistream(ABC):
|
||||
"""
|
||||
SSMuxer is a multistream stream security transport multiplexer.
|
||||
|
||||
Go implementation: github.com/libp2p/go-conn-security-multistream/ssms.go
|
||||
"""
|
||||
|
||||
@ -41,9 +42,10 @@ class SecurityMultistream(ABC):
|
||||
|
||||
def add_transport(self, protocol: TProtocol, transport: ISecureTransport) -> None:
|
||||
"""
|
||||
Add a protocol and its corresponding transport to multistream-select(multiselect).
|
||||
The order that a protocol is added is exactly the precedence it is negotiated in
|
||||
multiselect.
|
||||
Add a protocol and its corresponding transport to multistream-
|
||||
select(multiselect). The order that a protocol is added is exactly the
|
||||
precedence it is negotiated in multiselect.
|
||||
|
||||
:param protocol: the protocol name, which is negotiated in multiselect.
|
||||
:param transport: the corresponding transportation to the ``protocol``.
|
||||
"""
|
||||
@ -57,8 +59,10 @@ class SecurityMultistream(ABC):
|
||||
|
||||
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)
|
||||
Secure the connection, either locally or by communicating with opposing
|
||||
node via conn, for an inbound connection (i.e. we are not the
|
||||
initiator)
|
||||
|
||||
:return: secure connection object (that implements secure_conn_interface)
|
||||
"""
|
||||
transport = await self.select_transport(conn, False)
|
||||
@ -67,8 +71,9 @@ class SecurityMultistream(ABC):
|
||||
|
||||
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)
|
||||
Secure the connection, either locally or by communicating with opposing
|
||||
node via conn, for an inbound connection (i.e. we are the initiator)
|
||||
|
||||
:return: secure connection object (that implements secure_conn_interface)
|
||||
"""
|
||||
transport = await self.select_transport(conn, True)
|
||||
@ -79,8 +84,9 @@ class SecurityMultistream(ABC):
|
||||
self, conn: IRawConnection, is_initiator: bool
|
||||
) -> ISecureTransport:
|
||||
"""
|
||||
Select a transport that both us and the node on the
|
||||
other end of conn support and agree on
|
||||
Select a transport that both us and the node on the other end of conn
|
||||
support and agree on.
|
||||
|
||||
:param conn: conn to choose a transport over
|
||||
:param is_initiator: true if we are the initiator, false otherwise
|
||||
:return: selected secure transport
|
||||
|
||||
Reference in New Issue
Block a user