From a80f7e859c2f15f18c665677077cae074944d4e4 Mon Sep 17 00:00:00 2001 From: ennucore Date: Thu, 19 Mar 2020 23:21:25 +0300 Subject: [PATCH] IF message sending --- src/crypto.rs | 2 +- src/ironforce.rs | 23 +++++++++++-- src/lib.rs | 89 +++++++++++++++++++++++++----------------------- src/message.rs | 3 +- src/transport.rs | 16 ++++++++- 5 files changed, 86 insertions(+), 47 deletions(-) diff --git a/src/crypto.rs b/src/crypto.rs index a339826..d74ce05 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -33,7 +33,7 @@ impl PublicKey { PK::from_bytes(&self.key).unwrap() .verify( msg.get_hash().as_ref(), - &Signature::from_bytes(&msg.clone().sign.into_boxed_slice()).unwrap() + &Signature::from_bytes(&msg.clone().sign.into_boxed_slice()).unwrap(), ).is_ok() } } diff --git a/src/ironforce.rs b/src/ironforce.rs index 79768b7..4fc5204 100644 --- a/src/ironforce.rs +++ b/src/ironforce.rs @@ -1,14 +1,33 @@ use crate::transport::Transport; use crate::crypto::{PublicKey, KeyPack}; +use crate::message::{Message, MsgType}; + +use alloc::vec::Vec; pub struct IronForce { transport: Transport, - key_pack: KeyPack + key_pack: KeyPack, } impl IronForce { - pub fn gen() -> IronForce { + pub fn new() -> IronForce { IronForce { transport: Transport {}, key_pack: KeyPack::gen() } } + + fn new_message(&self, msg_type: MsgType, body: Vec) -> Message { + Message::new(msg_type, body, &self.key_pack) + } + + fn service_msg(&self, body: Vec) { + self.transport.send(&self.new_message(MsgType::Service, body)) + } + + pub fn multicast(&self, body: Vec) { + self.transport.send(&self.new_message(MsgType::MultiCast, body)) + } + + pub fn send_message_to(&self, to: PublicKey, body: Vec) { + self.transport.send(&self.new_message(MsgType::ToTarget(to), body)) + } } diff --git a/src/lib.rs b/src/lib.rs index 401b1c6..057039f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,53 +12,58 @@ mod error; #[cfg(test)] mod tests { - use crate::ironforce::IronForce; - use crate::message::{MsgType, Message}; - use crate::crypto::KeyPack; - use alloc::vec::Vec; - #[test] - fn creation_works() { - IronForce::gen(); + mod iron_force { + #[test] + fn creation_works() { + use crate::ironforce::IronForce; + IronForce::new(); + } } - #[test] - fn serialization() { - let key_pack = KeyPack::gen(); - let msg = Message::new( - MsgType::MultiCast, Vec::::new(), &key_pack - ); - let serialized = &msg.ser(); - let msg2 = Message::deserialize(serialized.to_vec()); - assert_eq!(msg2.msg_type, MsgType::MultiCast); - } + mod message { + use crate::message::{MsgType, Message}; + use crate::crypto::KeyPack; + use alloc::vec::Vec; - #[test] - fn valid_message_creation() { - let msg = Message::new( - MsgType::MultiCast, Vec::::new(), &KeyPack::gen() - ); - assert!(&msg.verify()); - } + #[test] + fn serialization() { + let key_pack = KeyPack::gen(); + let msg = Message::new( + MsgType::MultiCast, Vec::::new(), &key_pack, + ); + let serialized = &msg.ser(); + let msg2 = Message::deserialize(serialized.to_vec()); + assert_eq!(msg2.msg_type, MsgType::MultiCast); + } - #[test] - fn test_invalid_sign() { - let key_pack = KeyPack::gen(); - let mut msg = Message::new( - MsgType::MultiCast, Vec::::new(), &key_pack - ); - msg.sign = [0;64].to_vec(); - assert!(!&msg.verify()); - msg.hash = [0;64].to_vec(); - msg.sign = key_pack.sign(&msg); - assert!(!&msg.verify()); - } + #[test] + fn valid_message_creation() { + let msg = Message::new( + MsgType::MultiCast, Vec::::new(), &KeyPack::gen(), + ); + assert!(&msg.verify()); + } + + #[test] + fn test_invalid_sign() { + let key_pack = KeyPack::gen(); + let mut msg = Message::new( + MsgType::MultiCast, Vec::::new(), &key_pack, + ); + msg.sign = [0; 64].to_vec(); + assert!(!&msg.verify()); + msg.hash = [0; 64].to_vec(); + msg.sign = key_pack.sign(&msg); + assert!(!&msg.verify()); + } - #[test] - fn test_hash() { - let msg = Message::new( - MsgType::MultiCast, Vec::::new(), &crate::crypto::KeyPack::gen() - ); - msg.get_hash(); + #[test] + fn test_hash() { + let msg = Message::new( + MsgType::MultiCast, Vec::::new(), &crate::crypto::KeyPack::gen(), + ); + msg.get_hash(); + } } } diff --git a/src/message.rs b/src/message.rs index 1d35cf9..e34bf16 100644 --- a/src/message.rs +++ b/src/message.rs @@ -10,6 +10,7 @@ use ed25519_dalek::Signature; pub enum MsgType { MultiCast, ToTarget(PublicKey), + Service, } #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] @@ -29,7 +30,7 @@ impl Message { body: self.body.clone(), source: self.source.clone(), hash: Vec::::new(), - sign: Vec::::new() + sign: Vec::::new(), }; hasher.input(msg2.ser()); hasher.result().to_vec() diff --git a/src/transport.rs b/src/transport.rs index e3f5577..9c281fc 100644 --- a/src/transport.rs +++ b/src/transport.rs @@ -1,2 +1,16 @@ +use crate::message::{Message, MsgType}; +use alloc::vec::Vec; -pub(crate) struct Transport {} +pub struct Transport {} + +impl Transport { + pub fn send(&self, msg: &Message) { + + } + + pub fn receive(&self) -> Message { + Message::new(MsgType::Service, + Vec::::new(), + &crate::crypto::KeyPack::gen()) + } +}