From 1d94b0fad7ba587bd081bcf0a776f84709f22407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Silva?= <123550+andresilva@users.noreply.github.com> Date: Thu, 8 Apr 2021 13:46:22 +0100 Subject: [PATCH] gadget: verify SignedCommitment message signature (#142) * gadget: verify SignedCommitment message signature * gadget: log messages with bad sigs * gadget: move todo comment --- client/beefy/src/worker.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/client/beefy/src/worker.rs b/client/beefy/src/worker.rs index 2b1c369ee1287..841f710385dbb 100644 --- a/client/beefy/src/worker.rs +++ b/client/beefy/src/worker.rs @@ -109,20 +109,25 @@ impl GossipValidator for BeefyGossipValidator where B: Block, P: Pair, - P::Public: Decode, - P::Signature: Decode, + P::Public: Debug + Decode, + P::Signature: Debug + Decode, { fn validate( &self, _context: &mut dyn GossipValidatorContext, - _sender: &sc_network::PeerId, + sender: &sc_network::PeerId, mut data: &[u8], ) -> GossipValidationResult { - if VoteMessage::, P::Public, P::Signature>::decode(&mut data).is_ok() { - GossipValidationResult::ProcessAndKeep(self.topic) - } else { - GossipValidationResult::Discard + if let Ok(msg) = VoteMessage::, P::Public, P::Signature>::decode(&mut data) { + if P::verify(&msg.signature, &msg.commitment.encode(), &msg.id) { + return GossipValidationResult::ProcessAndKeep(self.topic); + } else { + // TODO: report peer + debug!(target: "beefy", "🥩 Bad signature on message: {:?}, from: {:?}", msg, sender); + } } + + GossipValidationResult::Discard } fn message_expired<'a>(&'a self) -> Box bool + 'a> { @@ -406,7 +411,6 @@ where fn handle_vote(&mut self, round: (MmrRootHash, NumberFor), vote: (P::Public, P::Signature)) { self.gossip_validator.note_round(round.1); - // TODO: validate signature let vote_added = self.rounds.add_vote(round, vote); if vote_added && self.rounds.is_done(&round) {