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 */
1819use darkfi::{
20 zk::{halo2::Value, Proof, ProvingKey, Witness, ZkCircuit},
21 zkas::ZkBinary,
22Result,
23};
24use darkfi_sdk::crypto::{note::AeadEncryptedNote, Blind, Keypair};
25use log::debug;
26use rand::rngs::OsRng;
2728use crate::{
29 client::MoneyNote,
30 model::{CoinAttributes, MoneyAuthTokenMintParamsV1, TokenAttributes},
31};
3233pub struct AuthTokenMintCallDebris {
34pub params: MoneyAuthTokenMintParamsV1,
35pub proofs: Vec<Proof>,
36}
3738/// Struct holding necessary information to build a `Money::AuthTokenMintV1` contract call.
39pub struct AuthTokenMintCallBuilder {
40/// Coin attributes
41pub coin_attrs: CoinAttributes,
42/// Token attributes
43pub token_attrs: TokenAttributes,
44/// Mint authority keypair
45pub mint_keypair: Keypair,
46/// `AuthTokenMint_V1` zkas circuit ZkBinary
47pub auth_mint_zkbin: ZkBinary,
48/// Proving key for the `AuthTokenMint_V1` zk circuit,
49pub auth_mint_pk: ProvingKey,
50}
5152impl AuthTokenMintCallBuilder {
53pub fn build(&self) -> Result<AuthTokenMintCallDebris> {
54debug!(target: "contract::money::client::auth_token_mint", "Building Money::AuthTokenMintV1 contract call");
5556// Create the proof
57let prover_witnesses = vec![
58// Token attributes
59Witness::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
62Witness::Base(Value::known(self.mint_keypair.secret.inner())),
63 ];
6465let mint_pubkey = self.mint_keypair.public;
6667let public_inputs =
68vec![mint_pubkey.x(), mint_pubkey.y(), self.token_attrs.to_token_id().inner()];
6970//darkfi::zk::export_witness_json("proof/witness/auth_token_mint_v1.json", &prover_witnesses, &public_inputs);
71let circuit = ZkCircuit::new(prover_witnesses, &self.auth_mint_zkbin);
72let proof = Proof::create(&self.auth_mint_pk, &[circuit], &public_inputs, &mut OsRng)?;
7374// Create the note
75let 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 };
8586let enc_note = AeadEncryptedNote::encrypt(¬e, &self.coin_attrs.public_key, &mut OsRng)?;
8788let params = MoneyAuthTokenMintParamsV1 {
89 token_id: self.token_attrs.to_token_id(),
90 enc_note,
91 mint_pubkey,
92 };
93let debris = AuthTokenMintCallDebris { params, proofs: vec![proof] };
94Ok(debris)
95 }
96}