From 857cc29d272f8fac977dc5a4c5ceccdb01a9bc55 Mon Sep 17 00:00:00 2001 From: Alex Stokes Date: Thu, 27 Feb 2020 12:13:17 -0800 Subject: [PATCH] add content-addressed message id utility --- libp2p/pubsub/pubsub.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libp2p/pubsub/pubsub.py b/libp2p/pubsub/pubsub.py index f050a1b..965fc58 100644 --- a/libp2p/pubsub/pubsub.py +++ b/libp2p/pubsub/pubsub.py @@ -1,4 +1,6 @@ +import base64 import functools +import hashlib import logging import time from typing import ( @@ -52,6 +54,10 @@ def get_peer_and_seqno_msg_id(msg: rpc_pb2.Message) -> bytes: return msg.seqno + msg.from_id +def get_content_addressed_msg_id(msg: rpc_pb2.Message) -> bytes: + return base64.b64encode(hashlib.sha256(msg.data).digest()) + + class TopicValidator(NamedTuple): validator: ValidatorFn is_async: bool