mailcycle 0.7.0

Rust email API client: create addresses that receive email, read and decrypt messages on your machine, send email, verify webhooks and stream events.
Documentation
//! The account's keys, derived from its recovery phrase on this machine.
//!
//! Nothing here leaves the machine. Deriving is 120,000 PBKDF2 iterations for
//! a 64-byte seed, which takes a few tens of milliseconds natively. The app
//! computes the same bytes in its own JavaScript and takes far longer over it;
//! the iteration count is the format either way, and changing it would change
//! every account id ever derived.

use crate::crypto::identity::{
    address_key_at, address_key_pair_at, auth_verifier, identity_from_seed, AccountIdentity,
};
use crate::crypto::mnemonic::{phrase_to_seed, validate_phrase};
use crate::encoding::to_b64u;
use crate::error::{Error, Result};

/// Everything derived from one recovery phrase.
#[derive(Clone)]
pub struct Keys {
    identity: AccountIdentity,
}

impl Keys {
    pub fn from_phrase(phrase: &str) -> Result<Self> {
        if let Some(problem) = validate_phrase(phrase) {
            return Err(Error::Phrase(problem));
        }
        Ok(Keys { identity: identity_from_seed(&phrase_to_seed(phrase)) })
    }

    /// Public, safe to log, and meaningless on its own.
    pub fn account_id(&self) -> &str {
        &self.identity.account_id
    }

    pub fn auth_key(&self) -> &[u8] {
        &self.identity.auth_key
    }

    pub fn auth_public_key(&self) -> &str {
        &self.identity.auth_public_key
    }

    pub fn auth_secret(&self) -> &[u8] {
        &self.identity.auth_secret
    }

    pub fn auth_verifier(&self) -> String {
        auth_verifier(&self.identity.auth_secret)
    }

    pub fn vault_key(&self) -> &[u8] {
        &self.identity.vault_key
    }

    /// The address's public key, which lets the mail server seal to it.
    pub fn address_public_key(&self, address_id: &str) -> Result<String> {
        self.address_public_key_at(address_id, 0)
    }

    /// The address's public key at a key epoch: 0 unless its keys were rotated.
    pub fn address_public_key_at(&self, address_id: &str, epoch: u32) -> Result<String> {
        address_key_pair_at(&self.identity.vault_key, address_id, epoch)
            .map(|(_, public)| to_b64u(&public))
            .ok_or(Error::Decryption)
    }

    pub fn address_private_key(&self, address_id: &str) -> Result<Vec<u8>> {
        self.address_private_key_at(address_id, 0)
    }

    /// The address's private key at a key epoch.
    pub fn address_private_key_at(&self, address_id: &str, epoch: u32) -> Result<Vec<u8>> {
        address_key_pair_at(&self.identity.vault_key, address_id, epoch)
            .map(|(private, _)| private)
            .ok_or(Error::Decryption)
    }

    /// The key the account's own devices seal mail under.
    pub fn address_symmetric_key(&self, address_id: &str) -> Result<Vec<u8>> {
        self.address_symmetric_key_at(address_id, 0)
    }

    /// The same, at a key epoch.
    pub fn address_symmetric_key_at(&self, address_id: &str, epoch: u32) -> Result<Vec<u8>> {
        address_key_at(&self.identity.vault_key, address_id, epoch).ok_or(Error::Decryption)
    }
}

impl std::fmt::Debug for Keys {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Never the key material, however convenient that would be in a log.
        f.debug_struct("Keys").field("account_id", &self.identity.account_id).finish_non_exhaustive()
    }
}