use std::sync::Arc;
use dig_session::{UnlockedMasterSeed, SEED_LEN};
use zeroize::Zeroizing;
use crate::id::{AccountId, ProfileIx};
use crate::keys::dek::profile_dek;
use crate::signer::ProfileSigner;
use crate::wallet::authorizer::WalletOps;
pub struct UnlockedAccount {
account: AccountId,
seed: Arc<UnlockedMasterSeed>,
default_profile_ix: ProfileIx,
}
impl UnlockedAccount {
pub(crate) fn new(
account: AccountId,
seed: Arc<UnlockedMasterSeed>,
default_profile_ix: ProfileIx,
) -> Self {
Self {
account,
seed,
default_profile_ix,
}
}
pub fn account_id(&self) -> &AccountId {
&self.account
}
pub fn signer(&self) -> ProfileSigner {
self.profile_signer(self.default_profile_ix)
}
pub fn profile_signer(&self, ix: ProfileIx) -> ProfileSigner {
ProfileSigner::new(self.seed.clone(), ix)
}
pub fn wallet_ops(&self) -> WalletOps {
WalletOps::new(self.seed.clone(), self.default_profile_ix)
}
pub fn dek(&self, ix: ProfileIx) -> [u8; 32] {
profile_dek(&self.seed, ix)
}
#[allow(dead_code)] pub(crate) fn master_seed(&self) -> Zeroizing<[u8; SEED_LEN]> {
self.seed.master_seed()
}
pub fn lock(self) {
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::keys::dek::profile_dek;
use dig_ipc_protocol::signer::SessionSigner;
use dig_keystore::{BackendKey, MemoryBackend};
use dig_session::{Password, Session};
const SEED: [u8; SEED_LEN] = [0x5A; SEED_LEN];
fn unlocked(default_ix: ProfileIx) -> UnlockedAccount {
let seed = Arc::new(
Session::enroll_master_seed(
Arc::new(MemoryBackend::new()),
BackendKey::new("k".to_string()),
Password::new("pw"),
&SEED,
)
.unwrap(),
);
UnlockedAccount::new(AccountId::new("acct"), seed, default_ix)
}
#[test]
fn exposes_the_account_id() {
let acct = unlocked(ProfileIx::ROOT);
assert_eq!(acct.account_id(), &AccountId::new("acct"));
}
#[test]
fn default_signer_targets_the_default_profile() {
let acct = unlocked(ProfileIx(4));
assert_eq!(
acct.signer().signing_public_key(),
acct.profile_signer(ProfileIx(4)).signing_public_key(),
);
}
#[test]
fn profile_signers_differ_per_index() {
let acct = unlocked(ProfileIx::ROOT);
assert_ne!(
acct.profile_signer(ProfileIx::ROOT).signing_public_key(),
acct.profile_signer(ProfileIx(1)).signing_public_key(),
);
}
#[test]
fn dek_matches_the_direct_derivation() {
let acct = unlocked(ProfileIx::ROOT);
assert_eq!(
acct.dek(ProfileIx::ROOT),
profile_dek(&acct.seed, ProfileIx::ROOT)
);
assert_ne!(acct.dek(ProfileIx::ROOT), acct.dek(ProfileIx(1)));
}
#[test]
fn wallet_ops_derives_the_default_profile_key() {
let acct = unlocked(ProfileIx(2));
let via_ops = acct.wallet_ops().wallet_key();
let expected =
crate::keys::wallet_key::WalletKey::from_seed_at(&acct.master_seed()[..], ProfileIx(2));
assert_eq!(via_ops.secret_key(), expected.secret_key());
}
#[test]
fn lock_consumes_the_handle() {
unlocked(ProfileIx::ROOT).lock();
}
}