use crate::encoding::to_b64u;
use super::mnemonic::phrase_to_seed;
use super::primitives::{hmac_sha256, public_key_from, sha256};
const LABEL_PREFIX: &str = "mailcycle/v1/";
const LABEL_PREFIX_V2: &str = "mailcycle/v2/";
const MAX_EPOCH: u32 = (1 << 31) - 1;
pub fn subkey(seed: &[u8], label: &str, length: usize) -> Vec<u8> {
let first = hmac_sha256(seed, format!("{LABEL_PREFIX}{label}").as_bytes());
if length <= first.len() {
return first[..length].to_vec();
}
let second = hmac_sha256(seed, format!("{LABEL_PREFIX}{label}/2").as_bytes());
let mut out = first.to_vec();
out.extend_from_slice(&second);
out.truncate(length);
out
}
pub fn subkey_v2(key: &[u8], label: &str, length: usize) -> Option<Vec<u8>> {
if !(1..=255 * 32).contains(&length) {
return None;
}
let hkdf = hkdf::Hkdf::<sha2::Sha256>::from_prk(key).ok()?;
let mut out = vec![0u8; length];
hkdf.expand(format!("{LABEL_PREFIX_V2}{label}").as_bytes(), &mut out).ok()?;
Some(out)
}
fn is_safe_label(id: &str) -> bool {
!id.is_empty()
&& id.len() <= 64
&& id.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-')
}
#[derive(Clone)]
pub struct AccountIdentity {
pub account_id: String,
pub auth_secret: Vec<u8>,
pub auth_key: Vec<u8>,
pub auth_public_key: String,
pub vault_key: Vec<u8>,
}
pub fn identity_from_seed(seed: &[u8]) -> AccountIdentity {
let auth_key = subkey(seed, "auth-key", 32);
let id_bytes = subkey(seed, "account-id", 16);
AccountIdentity {
account_id: format!("acct_{}", crate::encoding::to_hex(&id_bytes)),
auth_secret: subkey(seed, "auth", 32),
auth_public_key: to_b64u(&public_key_from(&auth_key)),
auth_key,
vault_key: subkey(seed, "vault", 64),
}
}
pub fn identity_from_phrase(phrase: &str, passphrase: &str) -> AccountIdentity {
identity_from_seed(&phrase_to_seed(phrase, passphrase))
}
pub fn auth_verifier(auth_secret: &[u8]) -> String {
to_b64u(&sha256(auth_secret))
}
pub fn address_key(vault_key: &[u8], address_id: &str) -> Option<Vec<u8>> {
is_safe_label(address_id).then(|| subkey(vault_key, &format!("inbox/{address_id}"), 64))
}
pub fn address_key_pair(vault_key: &[u8], address_id: &str) -> Option<(Vec<u8>, [u8; 32])> {
if !is_safe_label(address_id) {
return None;
}
let private_key = subkey(vault_key, &format!("inbox-sealing/{address_id}"), 32);
let public_key = public_key_from(&private_key);
Some((private_key, public_key))
}
pub fn address_key_at(vault_key: &[u8], address_id: &str, epoch: u32) -> Option<Vec<u8>> {
match epoch {
0 => address_key(vault_key, address_id),
e if e <= MAX_EPOCH && is_safe_label(address_id) => {
subkey_v2(vault_key, &format!("inbox/{address_id}/{e}"), 64)
}
_ => None,
}
}
pub fn address_key_pair_at(vault_key: &[u8], address_id: &str, epoch: u32) -> Option<(Vec<u8>, [u8; 32])> {
match epoch {
0 => address_key_pair(vault_key, address_id),
e if e <= MAX_EPOCH && is_safe_label(address_id) => {
let private_key = subkey_v2(vault_key, &format!("inbox-sealing/{address_id}/{e}"), 32)?;
let public_key = public_key_from(&private_key);
Some((private_key, public_key))
}
_ => None,
}
}
pub fn fingerprint(account_id: &str) -> String {
let digest = crate::encoding::to_hex(&sha256(account_id.as_bytes()))[..12].to_uppercase();
format!("{}-{}-{}", &digest[0..4], &digest[4..8], &digest[8..12])
}