use chia_wallet_sdk::driver::{DataStore, SpendContext};
use crate::metadata::DigDataStoreMetadata;
use crate::types::LineageProof;
use crate::MerkleResult;
pub fn child_lineage_proof(store: &DataStore<DigDataStoreMetadata>) -> MerkleResult<LineageProof> {
let mut ctx = SpendContext::new();
Ok(store.child_lineage_proof(&mut ctx)?)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::mint::mint_datastore;
use crate::types::{Bytes32, Owner};
use chia_puzzle_types::standard::StandardArgs;
use chia_wallet_sdk::test::Simulator;
#[test]
fn lineage_proof_binds_to_the_store_coin() -> anyhow::Result<()> {
let mut sim = Simulator::new();
let owner = sim.bls(1_000_000);
let owner_ph: Bytes32 = StandardArgs::curry_tree_hash(owner.pk).into();
let built = mint_datastore(
owner.coin,
Owner::Standard(owner.pk),
Bytes32::new([0x5a; 32]),
None,
None,
None,
None,
None,
owner_ph,
vec![],
0,
)?;
sim.spend_coins(built.coin_spends.clone(), std::slice::from_ref(&owner.sk))?;
let store = built.child.expect("mint yields a child");
let proof = child_lineage_proof(&store)?;
assert_eq!(proof.parent_amount, store.coin.amount);
assert_eq!(
proof.parent_parent_coin_info, store.coin.parent_coin_info,
"the proof references the store coin's parent"
);
Ok(())
}
#[test]
fn lineage_proof_lets_a_child_spend_validate() -> anyhow::Result<()> {
let mut sim = Simulator::new();
let owner = sim.bls(1_000_000);
let owner_ph: Bytes32 = StandardArgs::curry_tree_hash(owner.pk).into();
let built = mint_datastore(
owner.coin,
Owner::Standard(owner.pk),
Bytes32::new([0x5a; 32]),
None,
None,
None,
None,
None,
owner_ph,
vec![],
0,
)?;
sim.spend_coins(built.coin_spends.clone(), std::slice::from_ref(&owner.sk))?;
let store = built.child.expect("mint yields a child");
let child = crate::update::update_root(
&store,
Owner::Standard(owner.pk),
DigDataStoreMetadata {
root_hash: Bytes32::new([0x77; 32]),
..Default::default()
},
)?;
sim.spend_coins(child.coin_spends.clone(), std::slice::from_ref(&owner.sk))?;
Ok(())
}
}