darkfi_contract_test_harness/
money_genesis_mint.rsuse darkfi::{
tx::{ContractCallLeaf, Transaction, TransactionBuilder},
Result,
};
use darkfi_money_contract::{
client::{genesis_mint_v1::GenesisMintCallBuilder, MoneyNote, OwnCoin},
model::MoneyGenesisMintParamsV1,
MoneyFunction, MONEY_CONTRACT_ZKAS_MINT_NS_V1,
};
use darkfi_sdk::{
crypto::{contract_id::MONEY_CONTRACT_ID, FuncId, MerkleNode},
pasta::pallas,
ContractCall,
};
use darkfi_serial::AsyncEncodable;
use log::debug;
use super::{Holder, TestHarness};
impl TestHarness {
pub async fn genesis_mint(
&mut self,
holder: &Holder,
amount: u64,
spend_hook: Option<FuncId>,
user_data: Option<pallas::Base>,
) -> Result<(Transaction, MoneyGenesisMintParamsV1)> {
let wallet = self.holders.get(holder).unwrap();
let (mint_pk, mint_zkbin) = self.proving_keys.get(MONEY_CONTRACT_ZKAS_MINT_NS_V1).unwrap();
let builder = GenesisMintCallBuilder {
signature_public: wallet.keypair.public,
amount,
recipient: None,
spend_hook,
user_data,
mint_zkbin: mint_zkbin.clone(),
mint_pk: mint_pk.clone(),
};
let debris = builder.build()?;
let mut data = vec![MoneyFunction::GenesisMintV1 as u8];
debris.params.encode_async(&mut data).await?;
let call = ContractCall { contract_id: *MONEY_CONTRACT_ID, data };
let mut tx_builder =
TransactionBuilder::new(ContractCallLeaf { call, proofs: debris.proofs }, vec![])?;
let mut tx = tx_builder.build()?;
let sigs = tx.create_sigs(&[wallet.keypair.secret])?;
tx.signatures = vec![sigs];
Ok((tx, debris.params))
}
pub async fn execute_genesis_mint_tx(
&mut self,
holder: &Holder,
tx: Transaction,
params: &MoneyGenesisMintParamsV1,
block_height: u32,
append: bool,
) -> Result<Vec<OwnCoin>> {
let wallet = self.holders.get_mut(holder).unwrap();
wallet.add_transaction("money::genesis_mint", tx, block_height).await?;
if !append {
return Ok(vec![])
}
wallet.money_merkle_tree.append(MerkleNode::from(params.output.coin.inner()));
let Ok(note) = params.output.note.decrypt::<MoneyNote>(&wallet.keypair.secret) else {
return Ok(vec![])
};
let owncoin = OwnCoin {
coin: params.output.coin,
note: note.clone(),
secret: wallet.keypair.secret,
leaf_position: wallet.money_merkle_tree.mark().unwrap(),
};
debug!("Found new OwnCoin({}) for {:?}", owncoin.coin, holder);
wallet.unspent_money_coins.push(owncoin.clone());
Ok(vec![owncoin])
}
}