darkfi_money_contract/client/
auth_token_freeze_v1.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use darkfi::{
20    zk::{halo2::Value, Proof, ProvingKey, Witness, ZkCircuit},
21    zkas::ZkBinary,
22    Result,
23};
24use darkfi_sdk::crypto::Keypair;
25use log::debug;
26use rand::rngs::OsRng;
27
28use crate::model::{MoneyAuthTokenFreezeParamsV1, TokenAttributes};
29
30pub struct AuthTokenFreezeCallDebris {
31    pub params: MoneyAuthTokenFreezeParamsV1,
32    pub proofs: Vec<Proof>,
33}
34
35/// Struct holding necessary information to build a `Money::AuthTokenFreezeV1` contract call.
36pub struct AuthTokenFreezeCallBuilder {
37    /// Mint authority keypair
38    pub mint_keypair: Keypair,
39    pub token_attrs: TokenAttributes,
40    /// `AuthTokenMint_V1` zkas circuit ZkBinary
41    pub auth_mint_zkbin: ZkBinary,
42    /// Proving key for the `AuthTokenMint_V1` zk circuit,
43    pub auth_mint_pk: ProvingKey,
44}
45
46impl AuthTokenFreezeCallBuilder {
47    pub fn build(&self) -> Result<AuthTokenFreezeCallDebris> {
48        debug!(target: "contract::money::client::auth_token_freeze", "Building Money::AuthTokenFreezeV1 contract call");
49
50        // For the AuthTokenFreeze call, we just need to produce a valid signature,
51        // and enforce the correct derivation inside ZK.
52        let prover_witnesses = vec![
53            // Token attributes
54            Witness::Base(Value::known(self.token_attrs.auth_parent.inner())),
55            Witness::Base(Value::known(self.token_attrs.blind.inner())),
56            // Secret key used by mint
57            Witness::Base(Value::known(self.mint_keypair.secret.inner())),
58        ];
59
60        let mint_pubkey = self.mint_keypair.public;
61        let token_id = self.token_attrs.to_token_id();
62
63        let public_inputs = vec![mint_pubkey.x(), mint_pubkey.y(), token_id.inner()];
64        //darkfi::zk::export_witness_json("proof/witness/auth_token_mint_v1.json", &prover_witnesses, &public_inputs);
65        let circuit = ZkCircuit::new(prover_witnesses, &self.auth_mint_zkbin);
66        let proof = Proof::create(&self.auth_mint_pk, &[circuit], &public_inputs, &mut OsRng)?;
67
68        let params =
69            MoneyAuthTokenFreezeParamsV1 { mint_public: self.mint_keypair.public, token_id };
70        let debris = AuthTokenFreezeCallDebris { params, proofs: vec![proof] };
71        Ok(debris)
72    }
73}