diff --git a/src/net/model/prekeys.rs b/src/net/model/prekeys.rs index 92aabff..dd7715e 100644 --- a/src/net/model/prekeys.rs +++ b/src/net/model/prekeys.rs @@ -1,7 +1,7 @@ #![allow(non_camel_case_types)] use std::collections::HashMap; -use proteus::keys::PreKeyBundle; +use proteus::keys::{IdentityKey, PreKeyBundle}; use serde::{Deserialize, Serialize}; use uuid::Uuid; @@ -10,14 +10,49 @@ pub struct PreKeyMap { pub value: HashMap>> } +impl PreKeyMap { + pub fn new() -> PreKeyMap { + PreKeyMap { + value: HashMap::new() + } + } + + pub fn get(&self, id: &Uuid) -> Option<&HashMap>> { + self.value.get(id) + } +} + #[derive(Serialize, Deserialize, Clone, Debug)] pub struct PreKey { pub key: PreKeyBundle } +impl PreKey { + pub fn last_resort(k: &IdentityKey) -> LastPreKey { + let pk = proteus::PreKey::last_resort(); + LastPreKey(PreKey { key: PreKeyBundle::new(k.clone(), &pk) }) + } + + pub fn from_str(s: &str) -> Option { + base64::decode(s).ok().and_then(|xs| { + PreKeyBundle::deserialise(xs.as_slice()).ok().map(|k| PreKey { key: k }) + }) + } +} + #[derive(Serialize, Deserialize, Clone, Debug)] pub struct LastPreKey(PreKey); +impl LastPreKey { + pub fn new(p: PreKey) -> Option { + if p.key.prekey_id == proteus::MAX_PREKEY_ID { + Some(LastPreKey(p)) + } else { + None + } + } +} + #[derive(Serialize, Deserialize, Clone, Debug)] pub struct ClientPreKey { pub id: String,