Browse Source

IF message sending

master
ennucore 5 years ago
parent
commit
a80f7e859c
  1. 2
      src/crypto.rs
  2. 23
      src/ironforce.rs
  3. 89
      src/lib.rs
  4. 3
      src/message.rs
  5. 16
      src/transport.rs

2
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()
}
}

23
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<u8>) -> Message {
Message::new(msg_type, body, &self.key_pack)
}
fn service_msg(&self, body: Vec<u8>) {
self.transport.send(&self.new_message(MsgType::Service, body))
}
pub fn multicast(&self, body: Vec<u8>) {
self.transport.send(&self.new_message(MsgType::MultiCast, body))
}
pub fn send_message_to(&self, to: PublicKey, body: Vec<u8>) {
self.transport.send(&self.new_message(MsgType::ToTarget(to), body))
}
}

89
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::<u8>::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::<u8>::new(), &KeyPack::gen()
);
assert!(&msg.verify());
}
#[test]
fn serialization() {
let key_pack = KeyPack::gen();
let msg = Message::new(
MsgType::MultiCast, Vec::<u8>::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::<u8>::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::<u8>::new(), &KeyPack::gen(),
);
assert!(&msg.verify());
}
#[test]
fn test_invalid_sign() {
let key_pack = KeyPack::gen();
let mut msg = Message::new(
MsgType::MultiCast, Vec::<u8>::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::<u8>::new(), &crate::crypto::KeyPack::gen()
);
msg.get_hash();
#[test]
fn test_hash() {
let msg = Message::new(
MsgType::MultiCast, Vec::<u8>::new(), &crate::crypto::KeyPack::gen(),
);
msg.get_hash();
}
}
}

3
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::<u8>::new(),
sign: Vec::<u8>::new()
sign: Vec::<u8>::new(),
};
hasher.input(msg2.ser());
hasher.result().to_vec()

16
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::<u8>::new(),
&crate::crypto::KeyPack::gen())
}
}

Loading…
Cancel
Save