From e1d99657d67c1e4bfb28d14d6f6b1c98e5c5db69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20B=C3=B6ving?= Date: Fri, 11 Oct 2019 11:08:16 +0000 Subject: [PATCH] changes before split into feature branches --- src/net/model/prekeys.rs | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) 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,