#[macro_use]
extern crate serde_derive;
#[macro_use]
extern crate base64_serde;
#[macro_use]
extern crate quick_error;
#[macro_use]
extern crate lazy_static;
#[cfg(test)]
#[macro_use]
extern crate galvanic_assert;
mod crypto;
mod internal;
pub mod document;
pub mod group;
pub mod prelude;
pub mod user;
pub use crate::internal::{
DeviceContext, DeviceSigningKeyPair, IronOxideErr, KeyPair, PrivateKey, PublicKey,
};
use rand::rngs::ThreadRng;
use recrypt::api::{Ed25519, RandomBytes, Recrypt, Sha256};
use tokio::runtime::current_thread::Runtime;
pub type Result<T> = std::result::Result<T, IronOxideErr>;
pub struct IronOxide {
pub(crate) recrypt: Recrypt<Sha256, Ed25519, RandomBytes<ThreadRng>>,
pub(crate) user_master_pub_key: PublicKey,
pub(crate) device: DeviceContext,
pub(crate) rng: ThreadRng,
}
pub fn initialize(device_context: &DeviceContext) -> Result<IronOxide> {
let mut rt = Runtime::new().unwrap();
let account_id = device_context.account_id();
rt.block_on(crate::internal::user_api::user_key_list(
&device_context.auth(),
&vec![account_id.clone()],
))
.and_then(|mut users| {
users
.remove(&device_context.account_id())
.map(|current_user_public_key| IronOxide {
recrypt: Recrypt::new(),
device: device_context.clone(),
user_master_pub_key: current_user_public_key,
rng: rand::thread_rng(),
})
.ok_or(IronOxideErr::InitializeError)
})
}
impl IronOxide {
pub fn device(&self) -> &DeviceContext {
&self.device
}
}
impl From<IronOxideErr> for String {
fn from(err: IronOxideErr) -> Self {
format!("{}", err)
}
}