|
|
@@ -24,7 +24,8 @@ use darkfi::{
|
|
|
zkas::ZkBinary,
|
|
|
};
|
|
|
use darkfi_money_contract::{
|
|
|
- client::mint_v1::MintCallBuilder, MoneyFunction, MONEY_CONTRACT_ZKAS_TOKEN_MINT_NS_V1,
|
|
|
+ client::{freeze_v1::FreezeCallBuilder, mint_v1::MintCallBuilder},
|
|
|
+ MoneyFunction, MONEY_CONTRACT_ZKAS_TOKEN_FRZ_NS_V1, MONEY_CONTRACT_ZKAS_TOKEN_MINT_NS_V1,
|
|
|
};
|
|
|
use darkfi_sdk::{
|
|
|
crypto::{contract_id::MONEY_CONTRACT_ID, Keypair, PublicKey, TokenId},
|
|
|
@@ -37,7 +38,7 @@ use rand::rngs::OsRng;
|
|
|
use super::Drk;
|
|
|
|
|
|
impl Drk {
|
|
|
- /// Create a payment transaction. Returns the transaction object on success.
|
|
|
+ /// Create a token mint transaction. Returns the transaction object on success.
|
|
|
pub async fn mint_token(
|
|
|
&self,
|
|
|
amount: &str,
|
|
|
@@ -103,4 +104,53 @@ impl Drk {
|
|
|
|
|
|
Ok(tx)
|
|
|
}
|
|
|
+
|
|
|
+ /// Create a token freeze transaction. Returns the transaction object on success.
|
|
|
+ pub async fn freeze_token(&self, token_id: TokenId) -> Result<Transaction> {
|
|
|
+ let mut tokens = self.list_tokens().await?;
|
|
|
+ tokens.retain(|x| x.0 == token_id);
|
|
|
+ if tokens.is_empty() {
|
|
|
+ return Err(anyhow!("Did not find mint authority for token ID {}", token_id))
|
|
|
+ }
|
|
|
+ assert!(tokens.len() == 1);
|
|
|
+
|
|
|
+ let mint_authority = Keypair::new(tokens[0].1);
|
|
|
+
|
|
|
+ if tokens[0].2 {
|
|
|
+ return Err(anyhow!("This token is already marked as frozen in the wallet"))
|
|
|
+ }
|
|
|
+
|
|
|
+ let zkas_bins = self.lookup_zkas(&MONEY_CONTRACT_ID).await?;
|
|
|
+ let zkas_ns = MONEY_CONTRACT_ZKAS_TOKEN_FRZ_NS_V1;
|
|
|
+
|
|
|
+ let Some(token_freeze_zkbin) = zkas_bins.iter().find(|x| x.0 == zkas_ns) else {
|
|
|
+ return Err(anyhow!("Token freeze circuit not found"))
|
|
|
+ };
|
|
|
+
|
|
|
+ let k = 13;
|
|
|
+ let token_freeze_zkbin = ZkBinary::decode(&token_freeze_zkbin.1)?;
|
|
|
+ let token_freeze_circuit =
|
|
|
+ ZkCircuit::new(empty_witnesses(&token_freeze_zkbin), token_freeze_zkbin.clone());
|
|
|
+
|
|
|
+ eprintln!("Creating token freeze circuit proving keys");
|
|
|
+ let freeze_builder = FreezeCallBuilder {
|
|
|
+ mint_authority,
|
|
|
+ token_freeze_zkbin,
|
|
|
+ token_freeze_pk: ProvingKey::build(k, &token_freeze_circuit),
|
|
|
+ };
|
|
|
+
|
|
|
+ eprintln!("Building transaction parameters");
|
|
|
+ let debris = freeze_builder.build()?;
|
|
|
+
|
|
|
+ // Encode and sign the transaction
|
|
|
+ let mut data = vec![MoneyFunction::FreezeV1 as u8];
|
|
|
+ debris.params.encode(&mut data)?;
|
|
|
+ let calls = vec![ContractCall { contract_id: *MONEY_CONTRACT_ID, data }];
|
|
|
+ let proofs = vec![debris.proofs];
|
|
|
+ let mut tx = Transaction { calls, proofs, signatures: vec![] };
|
|
|
+ let sigs = tx.create_sigs(&mut OsRng, &[mint_authority.secret])?;
|
|
|
+ tx.signatures = vec![sigs];
|
|
|
+
|
|
|
+ Ok(tx)
|
|
|
+ }
|
|
|
}
|