use std::sync::Arc;
use dig_session::{UnlockedMasterSeed, MASTER_SEED_LEN};
use zeroize::Zeroizing;
use crate::edit::ProfileEditor;
use crate::id::{AccountId, ProfileIx};
use crate::keys::dek::profile_dek;
use crate::keys::sealing::{profile_sealing_public_key, profile_sealing_secret};
use crate::melt::ProfileMelter;
use crate::profile_mint::ProfileMinter;
use crate::session_residency::Residency;
use crate::signer::ProfileSigner;
use crate::wallet::authorizer::WalletOps;
pub struct UnlockedAccount {
account: AccountId,
seed: Arc<UnlockedMasterSeed>,
default_profile_ix: ProfileIx,
residency: Arc<Residency>,
}
impl UnlockedAccount {
pub(crate) fn new(
account: AccountId,
seed: Arc<UnlockedMasterSeed>,
default_profile_ix: ProfileIx,
) -> Self {
Self::with_residency(
account,
seed,
default_profile_ix,
Arc::new(Residency::new()),
)
}
pub(crate) fn with_residency(
account: AccountId,
seed: Arc<UnlockedMasterSeed>,
default_profile_ix: ProfileIx,
residency: Arc<Residency>,
) -> Self {
Self {
account,
seed,
default_profile_ix,
residency,
}
}
pub fn residency(&self) -> Arc<Residency> {
self.residency.clone()
}
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 {
self.wallet_ops_at(self.default_profile_ix)
}
pub fn wallet_ops_at(&self, ix: ProfileIx) -> WalletOps {
WalletOps::new(self.seed.clone(), ix, self.residency.clone())
}
pub fn profile_minter(&self) -> ProfileMinter {
ProfileMinter::new(self.seed.clone(), self.residency.clone())
}
pub fn profile_editor(&self) -> ProfileEditor {
ProfileEditor::new(self.seed.clone(), self.residency.clone())
}
pub fn profile_melter(&self) -> ProfileMelter {
ProfileMelter::new(self.seed.clone(), self.residency.clone())
}
pub fn dek(&self, ix: ProfileIx) -> [u8; 32] {
profile_dek(&self.seed, ix)
}
pub fn profile_sealing_key(&self, ix: ProfileIx) -> x25519_dalek::StaticSecret {
profile_sealing_secret(&self.seed, ix)
}
pub fn profile_sealing_public_key(&self, ix: ProfileIx) -> [u8; 32] {
profile_sealing_public_key(&self.seed, ix)
}
pub fn recovery_phrase(&self) -> Zeroizing<String> {
self.seed.recovery_phrase()
}
#[allow(dead_code)] pub(crate) fn master_seed(&self) -> Zeroizing<[u8; MASTER_SEED_LEN]> {
self.seed.master_seed()
}
pub fn lock(self) {
self.residency.revoke();
}
}
#[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::ENTROPY_LEN;
use dig_session::{Password, Session};
const ENTROPY: [u8; ENTROPY_LEN] = [0x5A; ENTROPY_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"),
&ENTROPY,
)
.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_at_derives_at_the_requested_index_not_the_default() {
const DEFAULT: ProfileIx = ProfileIx(2);
const OTHER: ProfileIx = ProfileIx(7);
let acct = unlocked(DEFAULT);
let expected =
crate::keys::wallet_key::WalletKey::from_seed_at(&acct.master_seed()[..], OTHER);
assert_eq!(
acct.wallet_ops_at(OTHER).puzzle_hash(),
expected.puzzle_hash(),
"the handle must derive at the index it was asked for"
);
assert_eq!(
acct.wallet_ops_at(OTHER).public_key(),
expected.public_key()
);
assert_ne!(
acct.wallet_ops_at(OTHER).puzzle_hash(),
acct.wallet_ops().puzzle_hash(),
"a profile's money lives at its OWN index; these must not collapse together"
);
assert_eq!(
acct.wallet_ops_at(DEFAULT).puzzle_hash(),
acct.wallet_ops().puzzle_hash()
);
}
#[test]
fn each_profile_has_its_own_receive_address() {
let acct = unlocked(ProfileIx::ROOT);
let addresses: Vec<String> = [ProfileIx::ROOT, ProfileIx(1), ProfileIx(2)]
.into_iter()
.map(|ix| acct.wallet_ops_at(ix).address().expect("derives"))
.collect();
assert!(
addresses.iter().all(|a| a.starts_with("xch1")),
"{addresses:?}"
);
let unique: std::collections::HashSet<&String> = addresses.iter().collect();
assert_eq!(
unique.len(),
addresses.len(),
"three profiles must have three receive addresses, not one repeated: {addresses:?}"
);
}
#[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());
}
const TEST_PHRASE: &str = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon art";
const TEST_ADDRESS_0: &str = "xch16grurcglcwcv6arjarr720yd9wqhp9gkx3k8h25lhwg8pl7vl6ysuax0gy";
const OTHER_PHRASE: &str =
"fog spot notable regret pizza coffee harvest ensure fog spot notable regret pizza coffee harvest ensure fog spot notable regret pizza coffee harvest equal";
const OTHER_ADDRESS_0: &str = "xch1vpxzuu6aqfu790qcrcppcr2gmju4f5tpuuznuv2lx3g79v2jxc7qxttpzt";
fn restore(phrase: &str, id: &str) -> UnlockedAccount {
crate::session::AccountSession::enroll_from_recovery_phrase(
Arc::new(crate::store::AccountStore::new(Arc::new(
dig_keystore::MemoryBackend::new(),
))),
AccountId::new(id),
dig_session::Password::new("pw"),
phrase,
ProfileIx::ROOT,
)
.expect("a valid phrase must restore")
}
fn wallet_address(acct: &UnlockedAccount) -> String {
crate::keys::wallet_key::WalletKey::from_seed_at(&acct.master_seed()[..], ProfileIx::ROOT)
.address()
.expect("a derived wallet key always encodes to an address")
}
#[test]
fn account_sits_at_the_standard_chia_address_for_its_phrase() {
assert_eq!(wallet_address(&restore(TEST_PHRASE, "a")), TEST_ADDRESS_0);
}
#[test]
fn recovery_phrase_does_not_consume_the_account() {
let acct = restore(TEST_PHRASE, "a");
let first = acct.recovery_phrase();
assert_eq!(first.split_whitespace().count(), 24);
assert_eq!(&*first, &*acct.recovery_phrase());
assert_eq!(wallet_address(&acct), TEST_ADDRESS_0);
}
#[test]
fn each_accounts_phrase_restores_that_account_and_not_another() {
let a = restore(TEST_PHRASE, "a");
let b = restore(OTHER_PHRASE, "b");
assert_ne!(&*a.recovery_phrase(), &*b.recovery_phrase());
for (acct, expected) in [(&a, TEST_ADDRESS_0), (&b, OTHER_ADDRESS_0)] {
assert_eq!(wallet_address(acct), expected);
let again = restore(&acct.recovery_phrase(), "restored");
assert_eq!(wallet_address(&again), expected);
assert_eq!(again.dek(ProfileIx::ROOT), acct.dek(ProfileIx::ROOT));
}
}
#[test]
fn lock_consumes_the_handle() {
unlocked(ProfileIx::ROOT).lock();
}
}