Add strict_signing: bool and sign_key to Pubsub

This commit is contained in:
NIC619 2019-11-26 16:09:46 +08:00
parent da10fc8531
commit 683710573e
No known key found for this signature in database
GPG Key ID: 570C35F5C2D51B17

View File

@ -16,6 +16,7 @@ from typing import (
import base58 import base58
from lru import LRU from lru import LRU
from libp2p.crypto.keys import PrivateKey
from libp2p.exceptions import ParseError, ValidationError from libp2p.exceptions import ParseError, ValidationError
from libp2p.host.host_interface import IHost from libp2p.host.host_interface import IHost
from libp2p.io.exceptions import IncompleteReadError from libp2p.io.exceptions import IncompleteReadError
@ -82,8 +83,17 @@ class Pubsub:
_tasks: List["asyncio.Future[Any]"] _tasks: List["asyncio.Future[Any]"]
# Indicate if we should enforce signature verification
strict_signing: bool
sign_key: PrivateKey
def __init__( def __init__(
self, host: IHost, router: "IPubsubRouter", my_id: ID, cache_size: int = None self,
host: IHost,
router: "IPubsubRouter",
my_id: ID,
cache_size: int = None,
strict_signing: bool = True,
) -> None: ) -> None:
""" """
Construct a new Pubsub object, which is responsible for handling all Construct a new Pubsub object, which is responsible for handling all
@ -147,6 +157,12 @@ class Pubsub:
self._tasks.append(asyncio.ensure_future(self.handle_peer_queue())) self._tasks.append(asyncio.ensure_future(self.handle_peer_queue()))
self._tasks.append(asyncio.ensure_future(self.handle_dead_peer_queue())) self._tasks.append(asyncio.ensure_future(self.handle_dead_peer_queue()))
self.strict_signing = strict_signing
if strict_signing:
self.sign_key = self.host.get_private_key()
else:
self.sign_key = None
def get_hello_packet(self) -> rpc_pb2.RPC: def get_hello_packet(self) -> rpc_pb2.RPC:
"""Generate subscription message with all topics we are subscribed to """Generate subscription message with all topics we are subscribed to
only send hello packet if we have subscribed topics.""" only send hello packet if we have subscribed topics."""
@ -456,8 +472,6 @@ class Pubsub:
seqno=self._next_seqno(), seqno=self._next_seqno(),
) )
# TODO: Sign with our signing key
await self.push_msg(self.host.get_id(), msg) await self.push_msg(self.host.get_id(), msg)
logger.debug("successfully published message %s", msg) logger.debug("successfully published message %s", msg)