node: Implement signature verification

Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
Alexis Sellier
2022-09-13 10:16:05 +02:00
parent 993d88101c
commit 35b0af9a68
4 changed files with 43 additions and 5 deletions
+18
View File
@@ -58,6 +58,14 @@ pub struct NodeAnnouncement {
addresses: Vec<Address>,
}
impl NodeAnnouncement {
/// Verify a signature on this message.
pub fn verify(&self, signature: &crypto::Signature) -> bool {
let msg = serde_json::to_vec(self).unwrap();
self.id.verify(signature, &msg).is_ok()
}
}
/// Message payload.
/// These are the messages peers send to each other.
#[derive(Debug, Serialize, Deserialize, Clone)]
@@ -110,6 +118,16 @@ impl Message {
}
}
pub fn node<S: crypto::Signer>(announcement: NodeAnnouncement, signer: S) -> Self {
let msg = serde_json::to_vec(&announcement).unwrap();
let signature = signer.sign(&msg);
Self::Node {
signature,
announcement,
}
}
pub fn inventory<S, T, G>(ctx: &mut Context<S, T, G>) -> Result<Self, storage::Error>
where
T: storage::ReadStorage,
+10 -1
View File
@@ -194,7 +194,16 @@ impl Peer {
}));
}
}
(PeerState::Negotiated { .. }, Message::Node { .. }) => {
(
PeerState::Negotiated { .. },
Message::Node {
announcement,
signature,
},
) => {
if !announcement.verify(&signature) {
return Err(PeerError::Misbehavior);
}
todo!();
}
(PeerState::Negotiated { .. }, Message::Hello { .. }) => {