2019-08-22 23:55:05 +08:00
|
|
|
import secrets
|
|
|
|
from typing import Callable
|
|
|
|
|
2019-08-16 09:36:50 +08:00
|
|
|
from libp2p.crypto.keys import KeyPair
|
2019-08-03 07:50:44 +08:00
|
|
|
from libp2p.peer.id import ID
|
|
|
|
from libp2p.security.secure_transport_interface import ISecureTransport
|
|
|
|
|
|
|
|
|
2019-08-22 23:55:05 +08:00
|
|
|
def default_secure_bytes_provider(n: int) -> bytes:
|
|
|
|
return secrets.token_bytes(n)
|
|
|
|
|
|
|
|
|
2019-08-03 07:50:44 +08:00
|
|
|
class BaseSecureTransport(ISecureTransport):
|
2019-10-24 14:41:10 +08:00
|
|
|
"""``BaseSecureTransport`` is not fully instantiated from its abstract
|
|
|
|
classes as it is only meant to be used in clases that derive from it.
|
2019-08-22 23:55:05 +08:00
|
|
|
|
2019-10-24 14:41:10 +08:00
|
|
|
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.
|
2019-08-03 07:50:44 +08:00
|
|
|
"""
|
|
|
|
|
2019-08-22 23:55:05 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
local_key_pair: KeyPair,
|
|
|
|
secure_bytes_provider: Callable[[int], bytes] = default_secure_bytes_provider,
|
|
|
|
) -> None:
|
2019-08-16 09:36:50 +08:00
|
|
|
self.local_private_key = local_key_pair.private_key
|
|
|
|
self.local_peer = ID.from_pubkey(local_key_pair.public_key)
|
2019-08-22 23:55:05 +08:00
|
|
|
self.secure_bytes_provider = secure_bytes_provider
|