libp2p.security package

Subpackages

Submodules

libp2p.security.base_session module

class libp2p.security.base_session.BaseSession(*, local_peer: ID, local_private_key: PrivateKey, remote_peer: ID, remote_permanent_pubkey: PublicKey, is_initiator: bool)

Bases: ISecureConn

BaseSession is not fully instantiated from its abstract classes as it is only meant to be used in clases that derive from it.

get_local_peer() ID

Retrieve the local peer’s identifier.

Returns:

The local peer ID.

get_local_private_key() PrivateKey

Retrieve the local peer’s private key.

Returns:

The private key of the local peer.

get_remote_peer() ID

Retrieve the remote peer’s identifier.

Returns:

The remote peer ID.

get_remote_public_key() PublicKey

Retrieve the remote peer’s public key.

Returns:

The public key of the remote peer.

local_peer: ID
local_private_key: PrivateKey
remote_peer: ID
remote_permanent_pubkey: PublicKey

libp2p.security.base_transport module

class libp2p.security.base_transport.BaseSecureTransport(local_key_pair: ~libp2p.crypto.keys.KeyPair, secure_bytes_provider: ~collections.abc.Callable[[int], bytes] = <function default_secure_bytes_provider>)

Bases: ISecureTransport

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.

libp2p.security.base_transport.default_secure_bytes_provider(n: int) bytes

libp2p.security.exceptions module

exception libp2p.security.exceptions.HandshakeFailure

Bases: BaseLibp2pError

exception libp2p.security.exceptions.SecurityError

Bases: BaseLibp2pError

libp2p.security.secure_session module

class libp2p.security.secure_session.SecureSession(*, local_peer: ID, local_private_key: PrivateKey, remote_peer: ID, remote_permanent_pubkey: PublicKey, is_initiator: bool, conn: EncryptedMsgReadWriter)

Bases: BaseSession

buf: BytesIO
async close() None
get_remote_address() tuple[str, int] | None

Delegate to the underlying connection’s get_remote_address method.

high_watermark: int
low_watermark: int
async read(n: int | None = None) bytes
async write(data: bytes) None

libp2p.security.security_multistream module

class libp2p.security.security_multistream.SecurityMultistream(secure_transports_by_protocol: Mapping[TProtocol, object])

Bases: ABC

SSMuxer is a multistream stream security transport multiplexer.

Go implementation: github.com/libp2p/go-conn-security-multistream/ssms.go

add_transport(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.

Parameters:
  • protocol – the protocol name, which is negotiated in multiselect.

  • transport – the corresponding transportation to the protocol.

multiselect: Multiselect
multiselect_client: MultiselectClient
async secure_inbound(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)

Returns:

secure connection object (that implements secure_conn_interface)

async secure_outbound(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)

Returns:

secure connection object (that implements secure_conn_interface)

async select_transport(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.

Parameters:
  • conn – conn to choose a transport over

  • is_initiator – true if we are the initiator, false otherwise

Returns:

selected secure transport

transports: OrderedDict[TProtocol, ISecureTransport]
libp2p.security.security_multistream.logger = <Logger libp2p.security.security_multistream (DEBUG)>

Represents a secured connection object, which includes a connection and details about the security involved in the secured connection

Relevant go repo: https://github.com/libp2p/go-conn-security/blob/master/interface.go

Module contents

Security modules for libp2p.

This package provides various security implementations including: - TLS transport - Noise protocol - SECIO protocol - Insecure transport (for testing)