|
|
|
@ -3,36 +3,49 @@ use sha2::Digest;
|
|
|
|
|
use serde::{Serialize, Deserialize}; |
|
|
|
|
use alloc::vec::Vec; |
|
|
|
|
use pinecone::{from_bytes, to_vec}; |
|
|
|
|
use serde::private::de::size_hint::from_bounds; |
|
|
|
|
use ed25519_dalek::Signature; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)] |
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
|
|
|
|
pub enum MsgType { |
|
|
|
|
MultiCast, |
|
|
|
|
ToTarget(PublicKey), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)] |
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
|
|
|
|
pub struct Message { |
|
|
|
|
pub msg_type: MsgType, |
|
|
|
|
pub body: Vec<u8>, |
|
|
|
|
pub source: PublicKey, |
|
|
|
|
hash: u64, |
|
|
|
|
sign: u64, |
|
|
|
|
pub hash: Vec<u8>, |
|
|
|
|
pub sign: Vec<u8>, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Message { |
|
|
|
|
pub fn get_hash(self) -> Vec<u8> { |
|
|
|
|
pub fn get_hash(&self) -> Vec<u8> { |
|
|
|
|
let mut hasher = sha2::Sha256::new(); |
|
|
|
|
hasher.input(self.ser()); |
|
|
|
|
let msg2 = Message { |
|
|
|
|
msg_type: self.msg_type.clone(), |
|
|
|
|
body: self.body.clone(), |
|
|
|
|
source: self.source.clone(), |
|
|
|
|
hash: Vec::<u8>::new(), |
|
|
|
|
sign: Vec::<u8>::new() |
|
|
|
|
}; |
|
|
|
|
hasher.input(msg2.ser()); |
|
|
|
|
hasher.result().to_vec() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn new(msg_type: MsgType, body: Vec<u8>, key_pack: KeyPack) -> Message { |
|
|
|
|
let msg = Message { msg_type, body, source: key_pack.get_public(), hash: 0, sign: 0 }; |
|
|
|
|
pub fn new(msg_type: MsgType, body: Vec<u8>, key_pack: &KeyPack) -> Message { |
|
|
|
|
let mut msg = Message { msg_type, body, source: key_pack.get_public(), hash: Vec::<u8>::new(), sign: Vec::<u8>::new() }; |
|
|
|
|
msg.hash = msg.get_hash(); |
|
|
|
|
msg.sign = key_pack.sign(&msg); |
|
|
|
|
msg |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn verify(self) -> bool { |
|
|
|
|
self.hash == self.get_hash() && self.source.verify_sign(&self) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn ser(self) -> Vec<u8> { |
|
|
|
|
to_vec(&self).expect("Serialization failed") |
|
|
|
|
} |
|
|
|
|