mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2026-02-12 16:10:57 +00:00
Sign and verify in Pubsub
This commit is contained in:
@ -17,6 +17,7 @@ import base58
|
|||||||
from lru import LRU
|
from lru import LRU
|
||||||
|
|
||||||
from libp2p.crypto.keys import PrivateKey
|
from libp2p.crypto.keys import PrivateKey
|
||||||
|
from libp2p.crypto.serialization import deserialize_public_key
|
||||||
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
|
||||||
@ -472,6 +473,12 @@ class Pubsub:
|
|||||||
seqno=self._next_seqno(),
|
seqno=self._next_seqno(),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if self.strict_signing:
|
||||||
|
priv_key = self.sign_key
|
||||||
|
signature = priv_key.sign(msg.SerializeToString())
|
||||||
|
msg.key = self.host.get_public_key().serialize()
|
||||||
|
msg.signature = signature
|
||||||
|
|
||||||
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)
|
||||||
@ -519,18 +526,20 @@ class Pubsub:
|
|||||||
|
|
||||||
# TODO: Check if the `from` is in the blacklist. If yes, reject.
|
# TODO: Check if the `from` is in the blacklist. If yes, reject.
|
||||||
|
|
||||||
# TODO: Check if signing is required and if so signature should be attached.
|
|
||||||
|
|
||||||
# If the message is processed before, return(i.e., don't further process the message).
|
# If the message is processed before, return(i.e., don't further process the message).
|
||||||
if self._is_msg_seen(msg):
|
if self._is_msg_seen(msg):
|
||||||
return
|
return
|
||||||
|
|
||||||
# TODO: - Validate the message. If failed, reject it.
|
# Check if signing is required and if so signature should be attached.
|
||||||
# Validate the signature of the message
|
if self.strict_signing:
|
||||||
# FIXME: `signature_validator` is currently a stub.
|
if msg.signature == b'':
|
||||||
if not signature_validator(msg.key, msg.SerializeToString()):
|
logger.debug("Reject because no signature attached for msg: %s", msg)
|
||||||
logger.debug("Signature validation failed for msg: %s", msg)
|
return
|
||||||
return
|
# Validate the signature of the message
|
||||||
|
if not signature_validator(deserialize_public_key(msg.key), msg):
|
||||||
|
logger.debug("Signature validation failed for msg: %s", msg)
|
||||||
|
return
|
||||||
|
|
||||||
# Validate the message with registered topic validators.
|
# Validate the message with registered topic validators.
|
||||||
# If the validation failed, return(i.e., don't further process the message).
|
# If the validation failed, return(i.e., don't further process the message).
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user