Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
node: Implement signature verification
Alexis Sellier committed 3 years ago
commit 35b0af9a6833dbaf9089bd97ed98d386a8fd1256
parent 993d88101cdc829fab7a48f1fa1f98c23b7807af
4 files changed +43 -5
modified node/src/crypto.rs
@@ -9,6 +9,8 @@ pub use ed25519::Signature;
pub trait Signer: 'static {
    /// Return this signer's public/verification key.
    fn public_key(&self) -> &PublicKey;
+
    /// Sign a message and return the signature.
+
    fn sign(&self, msg: &[u8]) -> Signature;
}

/// The public/verification key.
modified node/src/protocol/message.rs
@@ -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,
modified node/src/protocol/peer.rs
@@ -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 { .. }) => {
modified node/src/test/crypto.rs
@@ -1,8 +1,11 @@
+
use ed25519_consensus as ed25519;
+

use crate::crypto::{PublicKey, SecretKey, Signer};

#[derive(Debug)]
pub struct MockSigner {
-
    key: PublicKey,
+
    pk: PublicKey,
+
    sk: SecretKey,
}

impl MockSigner {
@@ -15,7 +18,8 @@ impl MockSigner {
        let sk = SecretKey::from(bytes);

        Self {
-
            key: sk.verification_key().into(),
+
            pk: sk.verification_key().into(),
+
            sk,
        }
    }
}
@@ -26,13 +30,18 @@ impl Default for MockSigner {
        let sk = SecretKey::from(bytes);

        Self {
-
            key: sk.verification_key().into(),
+
            pk: sk.verification_key().into(),
+
            sk,
        }
    }
}

impl Signer for MockSigner {
    fn public_key(&self) -> &PublicKey {
-
        &self.key
+
        &self.pk
+
    }
+

+
    fn sign(&self, msg: &[u8]) -> ed25519::Signature {
+
        self.sk.sign(msg)
    }
}