use dig_social_profile::IdentityProfile;
use crate::error::{AccountError, Result};
use crate::id::{AccountId, ProfileIx};
pub struct Profile {
pub ix: ProfileIx,
identity: IdentityProfile,
}
impl Profile {
pub fn new(ix: ProfileIx, identity: IdentityProfile) -> Self {
Self { ix, identity }
}
pub fn ix(&self) -> ProfileIx {
self.ix
}
pub fn identity(&self) -> &IdentityProfile {
&self.identity
}
}
pub struct Account {
id: AccountId,
profiles: Vec<Profile>,
default_profile_ix: ProfileIx,
}
impl Account {
pub fn new(
id: AccountId,
profiles: Vec<Profile>,
default_profile_ix: ProfileIx,
) -> Result<Self> {
if profiles.is_empty() {
return Err(AccountError::DefaultProfileInvariant(
"an account must have at least one profile".to_string(),
));
}
if !profiles.iter().any(|p| p.ix == default_profile_ix) {
return Err(AccountError::ProfileNotFound(default_profile_ix));
}
Ok(Self {
id,
profiles,
default_profile_ix,
})
}
pub fn id(&self) -> &AccountId {
&self.id
}
pub fn profiles(&self) -> &[Profile] {
&self.profiles
}
pub fn default_profile(&self) -> &Profile {
self.profiles
.iter()
.find(|p| p.ix == self.default_profile_ix)
.expect("the exactly-one-default invariant guarantees the default profile is present")
}
pub fn set_default_profile(&mut self, ix: ProfileIx) -> Result<()> {
if !self.profiles.iter().any(|p| p.ix == ix) {
return Err(AccountError::ProfileNotFound(ix));
}
self.default_profile_ix = ix;
Ok(())
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct AccountRecord {
pub id: AccountId,
pub profile_indexes: Vec<u32>,
pub default_profile_ix: u32,
}
#[cfg(test)]
mod tests {
use super::*;
use chia_wallet_sdk::utils::Address;
use dig_social_profile::{
Bytes32, Coin, Did, IdentityProfile, IdentitySingleton, Profile as Metadata,
SingletonLineage, StoreRecord, DID_CHIA_PREFIX,
};
fn profile_at(ix: ProfileIx) -> Profile {
let launcher = Bytes32::new([0x42; 32]);
let did_str = Address::new(launcher, DID_CHIA_PREFIX.to_string())
.encode()
.unwrap();
let did = Did::parse(&did_str).unwrap();
let did_coin_id = Bytes32::new([0x11; 32]);
let singleton = IdentitySingleton {
did,
lineage: SingletonLineage::new(did_coin_id, [did_coin_id]),
};
let store = StoreRecord {
description: did_str,
launcher_coin: Coin {
parent_coin_info: did_coin_id,
puzzle_hash: Bytes32::new([0u8; 32]),
amount: 1,
},
};
let identity = IdentityProfile::resolve(singleton, store, Metadata::new()).unwrap();
Profile::new(ix, identity)
}
#[test]
fn set_default_profile_switches_to_a_present_profile() {
let mut account = Account::new(
AccountId::new("a"),
vec![profile_at(ProfileIx::ROOT), profile_at(ProfileIx(1))],
ProfileIx::ROOT,
)
.unwrap();
account.set_default_profile(ProfileIx(1)).unwrap();
assert_eq!(account.default_profile().ix(), ProfileIx(1));
}
#[test]
fn set_default_profile_rejects_an_absent_index_and_leaves_the_default_unchanged() {
let mut account = Account::new(
AccountId::new("a"),
vec![profile_at(ProfileIx::ROOT)],
ProfileIx::ROOT,
)
.unwrap();
let result = account.set_default_profile(ProfileIx(9));
assert!(matches!(result, Err(AccountError::ProfileNotFound(_))));
assert_eq!(
account.default_profile().ix(),
ProfileIx::ROOT,
"a rejected switch must not disturb the existing default"
);
}
#[test]
fn account_requires_at_least_one_profile() {
let result = Account::new(AccountId::new("a"), vec![], ProfileIx::ROOT);
assert!(matches!(
result,
Err(AccountError::DefaultProfileInvariant(_))
));
}
#[test]
fn account_record_serde_round_trips() {
let record = AccountRecord {
id: AccountId::new("acct"),
profile_indexes: vec![0, 1, 7],
default_profile_ix: 1,
};
let json = serde_json::to_string(&record).unwrap();
let back: AccountRecord = serde_json::from_str(&json).unwrap();
assert_eq!(record, back);
}
}