use chia_protocol::Bytes32;
use crate::error::{AccountError, Result};
use crate::mint::{ConfirmedStore, MintedDid};
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ProfileAnchor {
did: String,
launcher_id: Bytes32,
did_coin_id: Bytes32,
did_confirmed_height: u32,
store_launcher_id: Bytes32,
store_confirmed_height: u32,
#[serde(default, skip_serializing_if = "Option::is_none")]
store_launched_from: Option<Bytes32>,
#[serde(default, skip_serializing_if = "Option::is_none")]
store_coin_id: Option<Bytes32>,
}
impl ProfileAnchor {
pub fn from_confirmed(did: &MintedDid, store: &ConfirmedStore) -> Result<Self> {
if store.did_coin_id() != did.coin_id() {
return Err(AccountError::MismatchedMintHalves {
did_coin_id: did.coin_id(),
store_launched_from: store.did_coin_id(),
});
}
Ok(Self {
did: did.did().to_string(),
launcher_id: did.launcher_id(),
did_coin_id: did.coin_id(),
did_confirmed_height: did.confirmed_height(),
store_launcher_id: store.launcher_id(),
store_confirmed_height: store.confirmed_height(),
store_launched_from: Some(store.did_coin_id()),
store_coin_id: Some(store.coin_id()),
})
}
pub fn did(&self) -> &str {
&self.did
}
pub fn launcher_id(&self) -> Bytes32 {
self.launcher_id
}
pub fn did_coin_id(&self) -> Bytes32 {
self.did_coin_id
}
pub fn did_confirmed_height(&self) -> u32 {
self.did_confirmed_height
}
pub fn store_launcher_id(&self) -> Bytes32 {
self.store_launcher_id
}
pub fn store_confirmed_height(&self) -> u32 {
self.store_confirmed_height
}
pub fn store_launched_from(&self) -> Option<Bytes32> {
self.store_launched_from
}
pub fn store_coin_id(&self) -> Option<Bytes32> {
self.store_coin_id
}
pub fn pairing_holds(&self) -> bool {
self.store_launched_from
.map_or(true, |launched_from| launched_from == self.did_coin_id)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mint::fixtures::{bound_mint, confirmed_store, minted_did};
#[test]
fn an_anchor_carries_every_field_of_both_evidences() {
let (did, store) = bound_mint(1);
assert_ne!(
did.launcher_id(),
store.launcher_id(),
"the fixture must distinguish the two halves, or a swapped field would pass"
);
let anchor = ProfileAnchor::from_confirmed(&did, &store)
.expect("halves of the same mint compose into an anchor");
assert_eq!(anchor.did(), did.did());
assert_eq!(anchor.launcher_id(), did.launcher_id());
assert_eq!(anchor.did_coin_id(), did.coin_id());
assert_eq!(anchor.did_confirmed_height(), did.confirmed_height());
assert_eq!(anchor.store_launcher_id(), store.launcher_id());
assert_eq!(anchor.store_confirmed_height(), store.confirmed_height());
}
#[test]
fn an_anchor_refuses_two_halves_of_different_mints() {
let did = minted_did(1);
let store = confirmed_store(2);
assert_ne!(
store.did_coin_id(),
did.coin_id(),
"the fixture must be genuinely unrelated, or this proves nothing"
);
let result = ProfileAnchor::from_confirmed(&did, &store);
assert!(
matches!(
result,
Err(AccountError::MismatchedMintHalves {
did_coin_id,
store_launched_from,
}) if did_coin_id == did.coin_id() && store_launched_from == store.did_coin_id()
),
"an unrelated pair must be refused, and the error must name both coins: {result:?}"
);
}
const ANCHOR_JSON_0_26: &str = r#"{"did":"did:chia:1qyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqscdhf6s","launcher_id":"0x0101010101010101010101010101010101010101010101010101010101010101","did_coin_id":"0x0202020202020202020202020202020202020202020202020202020202020202","did_confirmed_height":4200000,"store_launcher_id":"0x0303030303030303030303030303030303030303030303030303030303030303","store_confirmed_height":4200001}"#;
#[test]
fn an_anchor_written_before_the_chia_0_36_migration_still_round_trips_byte_identically() {
let anchor: ProfileAnchor = serde_json::from_str(ANCHOR_JSON_0_26)
.expect("an anchor written by 0.8.1 must still deserialize");
assert_eq!(anchor.launcher_id(), Bytes32::new([1; 32]));
assert_eq!(anchor.did_coin_id(), Bytes32::new([2; 32]));
assert_eq!(anchor.store_launcher_id(), Bytes32::new([3; 32]));
assert_eq!(anchor.did_confirmed_height(), 4_200_000);
assert_eq!(anchor.store_confirmed_height(), 4_200_001);
assert_eq!(
anchor.did(),
"did:chia:1qyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqszqgpqyqscdhf6s"
);
assert_eq!(
serde_json::to_string(&anchor).expect("an anchor always serializes"),
ANCHOR_JSON_0_26,
"the new chia family must re-emit the OLD encoding, or every file this host writes becomes unreadable to the version that wrote the fixture"
);
}
#[test]
fn an_anchor_round_trips_through_json() {
let (did, store) = bound_mint(1);
let anchor = ProfileAnchor::from_confirmed(&did, &store).unwrap();
let json = serde_json::to_string(&anchor).unwrap();
assert_eq!(
serde_json::from_str::<ProfileAnchor>(&json).unwrap(),
anchor
);
}
}