|
|
|
@ -15,21 +15,23 @@ fn get_hash(b: &Vec<u8>) -> Vec<u8> {
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
|
|
|
|
pub enum MsgType { |
|
|
|
|
MultiCast, // No source, sign and tunnel
|
|
|
|
|
UniCast(PublicKey /* destination */), // has source, sign and tunnel
|
|
|
|
|
MultiCast, |
|
|
|
|
// No source, sign and tunnel
|
|
|
|
|
UniCast(PublicKey /* destination */), |
|
|
|
|
// has source, sign and tunnel
|
|
|
|
|
Service, // Has source and sign but no tunnel
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
|
|
|
|
pub enum Sign { |
|
|
|
|
NoSign, |
|
|
|
|
Signed(PublicKey /* source */, Vec<u8> /* sign */) |
|
|
|
|
Signed(PublicKey /* source */, Vec<u8> /* sign */), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
|
|
|
|
pub enum MsgTunnel { |
|
|
|
|
NoTunnel, |
|
|
|
|
Tunnel(Tunnel) |
|
|
|
|
Tunnel(Tunnel), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
|
|
|
@ -60,10 +62,12 @@ impl MessageContent {
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] |
|
|
|
|
pub struct Message { |
|
|
|
|
pub content: MessageContent, |
|
|
|
|
pub content_hash: Vec<u8>, // hash for signing
|
|
|
|
|
pub hash: Vec<u8>, // hash for checking integrity
|
|
|
|
|
pub content_hash: Vec<u8>, |
|
|
|
|
// hash for signing
|
|
|
|
|
pub hash: Vec<u8>, |
|
|
|
|
// hash for checking integrity
|
|
|
|
|
pub sign: Sign, |
|
|
|
|
pub tunnel: MsgTunnel |
|
|
|
|
pub tunnel: MsgTunnel, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl Message { |
|
|
|
@ -74,19 +78,19 @@ impl Message {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn new(msg_type: MsgType, body: Vec<u8>, key_pack: &Keys) -> Message { |
|
|
|
|
let content= MessageContent::new(msg_type.clone(), body); |
|
|
|
|
let content = MessageContent::new(msg_type.clone(), body); |
|
|
|
|
let content_hash = content.get_hash(); |
|
|
|
|
let mut msg = Message { |
|
|
|
|
content, |
|
|
|
|
content_hash, |
|
|
|
|
hash: Vec::<u8>::new(), |
|
|
|
|
sign: Sign::NoSign, |
|
|
|
|
tunnel: MsgTunnel::NoTunnel |
|
|
|
|
tunnel: MsgTunnel::NoTunnel, |
|
|
|
|
}; |
|
|
|
|
msg.sign = match msg_type { |
|
|
|
|
MsgType::UniCast(_) | MsgType::Service => { |
|
|
|
|
Sign::Signed(key_pack.get_public(), key_pack.sign(&msg.content_hash)) |
|
|
|
|
}, |
|
|
|
|
} |
|
|
|
|
MsgType::MultiCast => { Sign::NoSign } |
|
|
|
|
}; |
|
|
|
|
msg.hash = msg.get_hash(); |
|
|
|
|