node: Implement signature verification
Signed-off-by: Alexis Sellier <self@cloudhead.io>
This commit is contained in:
parent
993d88101c
commit
35b0af9a68
|
|
@ -9,6 +9,8 @@ pub use ed25519::Signature;
|
||||||
pub trait Signer: 'static {
|
pub trait Signer: 'static {
|
||||||
/// Return this signer's public/verification key.
|
/// Return this signer's public/verification key.
|
||||||
fn public_key(&self) -> &PublicKey;
|
fn public_key(&self) -> &PublicKey;
|
||||||
|
/// Sign a message and return the signature.
|
||||||
|
fn sign(&self, msg: &[u8]) -> Signature;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The public/verification key.
|
/// The public/verification key.
|
||||||
|
|
|
||||||
|
|
@ -58,6 +58,14 @@ pub struct NodeAnnouncement {
|
||||||
addresses: Vec<Address>,
|
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.
|
/// Message payload.
|
||||||
/// These are the messages peers send to each other.
|
/// These are the messages peers send to each other.
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
#[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>
|
pub fn inventory<S, T, G>(ctx: &mut Context<S, T, G>) -> Result<Self, storage::Error>
|
||||||
where
|
where
|
||||||
T: storage::ReadStorage,
|
T: storage::ReadStorage,
|
||||||
|
|
|
||||||
|
|
@ -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!();
|
todo!();
|
||||||
}
|
}
|
||||||
(PeerState::Negotiated { .. }, Message::Hello { .. }) => {
|
(PeerState::Negotiated { .. }, Message::Hello { .. }) => {
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,11 @@
|
||||||
|
use ed25519_consensus as ed25519;
|
||||||
|
|
||||||
use crate::crypto::{PublicKey, SecretKey, Signer};
|
use crate::crypto::{PublicKey, SecretKey, Signer};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct MockSigner {
|
pub struct MockSigner {
|
||||||
key: PublicKey,
|
pk: PublicKey,
|
||||||
|
sk: SecretKey,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MockSigner {
|
impl MockSigner {
|
||||||
|
|
@ -15,7 +18,8 @@ impl MockSigner {
|
||||||
let sk = SecretKey::from(bytes);
|
let sk = SecretKey::from(bytes);
|
||||||
|
|
||||||
Self {
|
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);
|
let sk = SecretKey::from(bytes);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
key: sk.verification_key().into(),
|
pk: sk.verification_key().into(),
|
||||||
|
sk,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Signer for MockSigner {
|
impl Signer for MockSigner {
|
||||||
fn public_key(&self) -> &PublicKey {
|
fn public_key(&self) -> &PublicKey {
|
||||||
&self.key
|
&self.pk
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sign(&self, msg: &[u8]) -> ed25519::Signature {
|
||||||
|
self.sk.sign(msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue