use chia_protocol::Bytes32;
use crate::id::ProfileIx;
use crate::mint::{ConfirmedStore, MintedDid, PendingMint, PendingStoreLaunch};
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PendingMintRecord {
pub launcher_id: Bytes32,
pub did_coin_id: Bytes32,
pub source_coin_id: Bytes32,
pub pushed_at_height: u32,
}
impl From<&PendingMint> for PendingMintRecord {
fn from(pending: &PendingMint) -> Self {
Self {
launcher_id: pending.launcher_id(),
did_coin_id: pending.did_coin_id(),
source_coin_id: pending.source_coin_id(),
pushed_at_height: pending.pushed_at_height(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MintedDidRecord {
pub did: String,
pub launcher_id: Bytes32,
pub coin_id: Bytes32,
pub confirmed_height: u32,
}
impl From<&MintedDid> for MintedDidRecord {
fn from(minted: &MintedDid) -> Self {
Self {
did: minted.did().to_string(),
launcher_id: minted.launcher_id(),
coin_id: minted.coin_id(),
confirmed_height: minted.confirmed_height(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PendingStoreLaunchRecord {
pub launcher_id: Bytes32,
pub store_coin_id: Bytes32,
pub did_coin_id: Bytes32,
pub committed_root: [u8; 32],
pub pushed_at_height: u32,
}
impl From<&PendingStoreLaunch> for PendingStoreLaunchRecord {
fn from(pending: &PendingStoreLaunch) -> Self {
Self {
launcher_id: pending.launcher_id(),
store_coin_id: pending.store_coin_id(),
did_coin_id: pending.did_coin_id(),
committed_root: pending.committed_root(),
pushed_at_height: pending.pushed_at_height(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ConfirmedStoreRecord {
pub launcher_id: Bytes32,
pub coin_id: Bytes32,
pub confirmed_height: u32,
pub did_coin_id: Bytes32,
pub committed_root: [u8; 32],
}
impl From<&ConfirmedStore> for ConfirmedStoreRecord {
fn from(store: &ConfirmedStore) -> Self {
Self {
launcher_id: store.launcher_id(),
coin_id: store.coin_id(),
confirmed_height: store.confirmed_height(),
did_coin_id: store.did_coin_id(),
committed_root: store.committed_root(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum MintStage {
DidPushed {
pending: PendingMintRecord,
},
DidConfirmedStoreNotLaunched {
did: MintedDidRecord,
},
StorePushed {
did: MintedDidRecord,
pending_store: PendingStoreLaunchRecord,
},
}
impl MintStage {
fn progress_label(&self) -> &'static str {
match self {
Self::DidPushed { .. } => "creating your identity",
Self::DidConfirmedStoreNotLaunched { .. } => "identity created — finishing setup",
Self::StorePushed { .. } => "finishing setup",
}
}
pub(crate) fn minted_dids(&self) -> Vec<&MintedDidRecord> {
match self {
Self::DidPushed { .. } => Vec::new(),
Self::DidConfirmedStoreNotLaunched { did } => vec![did],
Self::StorePushed { did, .. } => vec![did],
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ProfileMintInProgress {
ix: ProfileIx,
stage: MintStage,
#[serde(default)]
seed_root: Option<[u8; 32]>,
store_fee: u64,
}
impl ProfileMintInProgress {
pub fn new(ix: ProfileIx, stage: MintStage, store_fee: u64) -> Self {
Self {
ix,
stage,
seed_root: None,
store_fee,
}
}
pub fn with_seed_root(
ix: ProfileIx,
stage: MintStage,
seed_root: [u8; 32],
store_fee: u64,
) -> Self {
Self {
ix,
stage,
seed_root: Some(seed_root),
store_fee,
}
}
pub fn seed_root(&self) -> Option<[u8; 32]> {
self.seed_root
}
pub fn ix(&self) -> ProfileIx {
self.ix
}
pub fn stage(&self) -> &MintStage {
&self.stage
}
pub fn store_fee(&self) -> u64 {
self.store_fee
}
pub fn progress_label(&self) -> &'static str {
self.stage.progress_label()
}
pub(crate) fn set_stage(&mut self, stage: MintStage) {
self.stage = stage;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mint::fixtures::minted_did;
fn did_record() -> MintedDidRecord {
MintedDidRecord::from(&minted_did(1))
}
fn every_stage() -> Vec<MintStage> {
vec![
MintStage::DidPushed {
pending: PendingMintRecord {
launcher_id: Bytes32::new([1; 32]),
did_coin_id: Bytes32::new([2; 32]),
source_coin_id: Bytes32::new([3; 32]),
pushed_at_height: 100,
},
},
MintStage::DidConfirmedStoreNotLaunched { did: did_record() },
MintStage::StorePushed {
did: did_record(),
pending_store: PendingStoreLaunchRecord {
launcher_id: Bytes32::new([4; 32]),
store_coin_id: Bytes32::new([5; 32]),
did_coin_id: Bytes32::new([6; 32]),
committed_root: [7; 32],
pushed_at_height: 100,
},
},
]
}
#[test]
fn a_stage_carries_a_record_not_evidence() {
let record = did_record();
let minted = minted_did(1);
assert_eq!(record.coin_id, minted.coin_id());
}
#[test]
fn each_record_mirrors_every_field_of_its_evidence() {
use crate::mint::fixtures::{confirmed_store, pending_mint, pending_store_launch};
let minted = minted_did(2);
let MintedDidRecord {
did,
launcher_id,
coin_id,
confirmed_height,
} = MintedDidRecord::from(&minted);
assert_eq!(
(did.as_str(), launcher_id, coin_id, confirmed_height),
minted.every_field()
);
let store = confirmed_store(2);
let ConfirmedStoreRecord {
launcher_id,
coin_id,
confirmed_height,
did_coin_id,
committed_root,
} = ConfirmedStoreRecord::from(&store);
assert_eq!(
(
launcher_id,
coin_id,
confirmed_height,
did_coin_id,
committed_root
),
store.every_field()
);
let pending_mint = pending_mint();
let PendingMintRecord {
launcher_id,
did_coin_id,
source_coin_id,
pushed_at_height,
} = PendingMintRecord::from(&pending_mint);
assert_eq!(
(launcher_id, did_coin_id, source_coin_id, pushed_at_height),
pending_mint.every_field()
);
let pending_launch = pending_store_launch();
let PendingStoreLaunchRecord {
launcher_id,
store_coin_id,
did_coin_id,
committed_root,
pushed_at_height,
} = PendingStoreLaunchRecord::from(&pending_launch);
assert_eq!(
(
launcher_id,
store_coin_id,
did_coin_id,
committed_root,
pushed_at_height
),
pending_launch.every_field()
);
}
#[test]
fn a_progress_label_never_claims_a_profile() {
for stage in every_stage() {
let label = stage.progress_label();
assert!(
!label.to_lowercase().contains("profile"),
"the label {label:?} names a profile that does not exist yet"
);
assert!(!label.is_empty());
}
}
#[test]
fn a_journal_entry_carries_no_did_puzzle_material() {
for stage in every_stage() {
let json = serde_json::to_string(&ProfileMintInProgress::new(
ProfileIx(1),
stage.clone(),
1_000,
))
.unwrap()
.to_lowercase();
for forbidden in ["puzzle_reveal", "solution", "proof", "puzzle"] {
assert!(
!json.contains(forbidden),
"stage {stage:?} serialized a {forbidden} key"
);
}
}
}
#[test]
fn the_profile_seed_root_survives_a_restart() {
let seeded = ProfileMintInProgress::with_seed_root(
ProfileIx(1),
MintStage::DidConfirmedStoreNotLaunched { did: did_record() },
[0xA5; 32],
7,
);
let back: ProfileMintInProgress =
serde_json::from_str(&serde_json::to_string(&seeded).unwrap()).unwrap();
assert_eq!(back.seed_root(), Some([0xA5; 32]));
let did_only = ProfileMintInProgress::new(
ProfileIx(1),
MintStage::DidPushed {
pending: PendingMintRecord {
launcher_id: Bytes32::new([1; 32]),
did_coin_id: Bytes32::new([2; 32]),
source_coin_id: Bytes32::new([3; 32]),
pushed_at_height: 100,
},
},
7,
);
let back: ProfileMintInProgress =
serde_json::from_str(&serde_json::to_string(&did_only).unwrap()).unwrap();
assert_eq!(
back.seed_root(),
None,
"a DID-only mint has no seed, and no default may be substituted for one"
);
}
#[test]
fn a_pre_seed_root_journal_entry_still_loads() {
let legacy = r#"{"ix":1,"stage":{"DidPushed":{"pending":{"launcher_id":"0x0101010101010101010101010101010101010101010101010101010101010101","did_coin_id":"0x0202020202020202020202020202020202020202020202020202020202020202","source_coin_id":"0x0303030303030303030303030303030303030303030303030303030303030303","pushed_at_height":100}}},"store_fee":5}"#;
let back: ProfileMintInProgress = serde_json::from_str(legacy).unwrap();
assert_eq!(back.seed_root(), None);
assert_eq!(back.store_fee(), 5);
}
#[test]
fn an_in_progress_mint_survives_a_restart() {
let in_progress = ProfileMintInProgress::new(
ProfileIx(1),
MintStage::DidConfirmedStoreNotLaunched { did: did_record() },
4_242,
);
let json = serde_json::to_string(&in_progress).unwrap();
let back: ProfileMintInProgress = serde_json::from_str(&json).unwrap();
assert_eq!(back, in_progress);
assert_eq!(back.store_fee(), 4_242, "the disclosed fee survives too");
let MintStage::DidConfirmedStoreNotLaunched { did } = back.stage() else {
panic!("the stage must round-trip as the same variant");
};
assert_eq!(did, &did_record());
}
}