darkfi_money_contract/client/
auth_token_mint_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::{note::AeadEncryptedNote, Blind, Keypair};
25use log::debug;
26use rand::rngs::OsRng;
27
28use crate::{
29    client::MoneyNote,
30    model::{CoinAttributes, MoneyAuthTokenMintParamsV1, TokenAttributes},
31};
32
33pub struct AuthTokenMintCallDebris {
34    pub params: MoneyAuthTokenMintParamsV1,
35    pub proofs: Vec<Proof>,
36}
37
38/// Struct holding necessary information to build a `Money::AuthTokenMintV1` contract call.
39pub struct AuthTokenMintCallBuilder {
40    /// Coin attributes
41    pub coin_attrs: CoinAttributes,
42    /// Token attributes
43    pub token_attrs: TokenAttributes,
44    /// Mint authority keypair
45    pub mint_keypair: Keypair,
46    /// `AuthTokenMint_V1` zkas circuit ZkBinary
47    pub auth_mint_zkbin: ZkBinary,
48    /// Proving key for the `AuthTokenMint_V1` zk circuit,
49    pub auth_mint_pk: ProvingKey,
50}
51
52impl AuthTokenMintCallBuilder {
53    pub fn build(&self) -> Result<AuthTokenMintCallDebris> {
54        debug!(target: "contract::money::client::auth_token_mint", "Building Money::AuthTokenMintV1 contract call");
55
56        // Create the proof
57        let prover_witnesses = vec![
58            // Token attributes
59            Witness::Base(Value::known(self.token_attrs.auth_parent.inner())),
60            Witness::Base(Value::known(self.token_attrs.blind.inner())),
61            // Secret key used by the mint authority
62            Witness::Base(Value::known(self.mint_keypair.secret.inner())),
63        ];
64
65        let mint_pubkey = self.mint_keypair.public;
66
67        let public_inputs =
68            vec![mint_pubkey.x(), mint_pubkey.y(), self.token_attrs.to_token_id().inner()];
69
70        //darkfi::zk::export_witness_json("proof/witness/auth_token_mint_v1.json", &prover_witnesses, &public_inputs);
71        let circuit = ZkCircuit::new(prover_witnesses, &self.auth_mint_zkbin);
72        let proof = Proof::create(&self.auth_mint_pk, &[circuit], &public_inputs, &mut OsRng)?;
73
74        // Create the note
75        let note = MoneyNote {
76            value: self.coin_attrs.value,
77            token_id: self.coin_attrs.token_id,
78            spend_hook: self.coin_attrs.spend_hook,
79            user_data: self.coin_attrs.user_data,
80            coin_blind: self.coin_attrs.blind,
81            value_blind: Blind::random(&mut OsRng),
82            token_blind: Blind::ZERO,
83            memo: vec![],
84        };
85
86        let enc_note = AeadEncryptedNote::encrypt(&note, &self.coin_attrs.public_key, &mut OsRng)?;
87
88        let params = MoneyAuthTokenMintParamsV1 {
89            token_id: self.token_attrs.to_token_id(),
90            enc_note,
91            mint_pubkey,
92        };
93        let debris = AuthTokenMintCallDebris { params, proofs: vec![proof] };
94        Ok(debris)
95    }
96}