diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/test/minting.nr b/noir-projects/noir-contracts/contracts/token_contract/src/test/minting.nr index 6d2b21207ae6..bc4f877d2a3a 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/test/minting.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/test/minting.nr @@ -1,6 +1,6 @@ use crate::test::utils; use crate::{Token, types::transparent_note::TransparentNote}; -use dep::aztec::{hash::compute_secret_hash, oracle::random::random, test::helpers::cheatcodes}; +use dep::aztec::{oracle::random::random, test::helpers::cheatcodes}; #[test] unconstrained fn mint_public_success() { @@ -56,185 +56,3 @@ unconstrained fn mint_public_failures() { utils::check_public_balance(token_contract_address, recipient, mint_for_recipient_amount); utils::check_public_balance(token_contract_address, owner, 0); } - -#[test] -unconstrained fn mint_private_success() { - // Setup without account contracts. We are not using authwits here, so dummy accounts are enough - let (env, token_contract_address, owner, _) = utils::setup(/* with_account_contracts */ false); - let mint_amount = 10000; - // Mint some tokens - let secret = random(); - let secret_hash = compute_secret_hash(secret); - Token::at(token_contract_address).mint_private(mint_amount, secret_hash).call(&mut env.public()); - - Token::at(token_contract_address).mint_public(owner, mint_amount).call(&mut env.public()); - - // Time travel so we can read keys from the registry - env.advance_block_by(6); - - // We need to manually add the note to TXE because `TransparentNote` does not support automatic note log delivery. - env.add_note( - &mut TransparentNote::new(mint_amount, secret_hash), - Token::storage_layout().pending_shields.slot, - token_contract_address, - ); - - // Redeem our shielded tokens - Token::at(token_contract_address).redeem_shield(owner, mint_amount, secret).call( - &mut env.private(), - ); - - utils::check_private_balance(token_contract_address, owner, mint_amount); -} - -#[test(should_fail_with = "note not popped")] -unconstrained fn mint_private_failure_double_spend() { - // Setup without account contracts. We are not using authwits here, so dummy accounts are enough - let (env, token_contract_address, owner, recipient) = - utils::setup(/* with_account_contracts */ false); - let mint_amount = 10000; - // Mint some tokens - let secret = random(); - let secret_hash = compute_secret_hash(secret); - Token::at(token_contract_address).mint_private(mint_amount, secret_hash).call(&mut env.public()); - - Token::at(token_contract_address).mint_public(owner, mint_amount).call(&mut env.public()); - - // Time travel so we can read keys from the registry - env.advance_block_by(6); - - // We need to manually add the note to TXE because `TransparentNote` does not support automatic note log delivery. - env.add_note( - &mut TransparentNote::new(mint_amount, secret_hash), - Token::storage_layout().pending_shields.slot, - token_contract_address, - ); - - // Redeem our shielded tokens - Token::at(token_contract_address).redeem_shield(owner, mint_amount, secret).call( - &mut env.private(), - ); - - utils::check_private_balance(token_contract_address, owner, mint_amount); - - // Attempt to double spend - Token::at(token_contract_address).redeem_shield(recipient, mint_amount, secret).call( - &mut env.private(), - ); -} - -#[test(should_fail_with = "caller is not minter")] -unconstrained fn mint_private_failure_non_minter() { - // Setup without account contracts. We are not using authwits here, so dummy accounts are enough - let (env, token_contract_address, _, recipient) = - utils::setup(/* with_account_contracts */ false); - let mint_amount = 10000; - // Try to mint some tokens impersonating recipient - env.impersonate(recipient); - - let secret = random(); - let secret_hash = compute_secret_hash(secret); - Token::at(token_contract_address).mint_private(mint_amount, secret_hash).call(&mut env.public()); -} - -#[test(should_fail_with = "call to assert_max_bit_size")] -unconstrained fn mint_private_failure_overflow() { - // Setup without account contracts. We are not using authwits here, so dummy accounts are enough - let (env, token_contract_address, _, _) = utils::setup(/* with_account_contracts */ false); - - // Overflow recipient - let mint_amount = 2.pow_32(128); - let secret = random(); - let secret_hash = compute_secret_hash(secret); - Token::at(token_contract_address).mint_private(mint_amount, secret_hash).call(&mut env.public()); -} - -#[test(should_fail_with = "attempt to add with overflow")] -unconstrained fn mint_private_failure_overflow_recipient() { - // Setup without account contracts. We are not using authwits here, so dummy accounts are enough - let (env, token_contract_address, owner, _) = utils::setup(/* with_account_contracts */ false); - let mint_amount = 10000; - // Mint some tokens - let secret = random(); - let secret_hash = compute_secret_hash(secret); - Token::at(token_contract_address).mint_private(mint_amount, secret_hash).call(&mut env.public()); - // Time travel so we can read keys from the registry - env.advance_block_by(6); - - // We need to manually add the note to TXE because `TransparentNote` does not support automatic note log delivery. - env.add_note( - &mut TransparentNote::new(mint_amount, secret_hash), - Token::storage_layout().pending_shields.slot, - token_contract_address, - ); - - // Redeem our shielded tokens - Token::at(token_contract_address).redeem_shield(owner, mint_amount, secret).call( - &mut env.private(), - ); - - utils::check_private_balance(token_contract_address, owner, mint_amount); - - let mint_amount = 2.pow_32(128) - mint_amount; - // Mint some tokens - let secret = random(); - let secret_hash = compute_secret_hash(secret); - Token::at(token_contract_address).mint_private(mint_amount, secret_hash).call(&mut env.public()); -} - -#[test(should_fail_with = "attempt to add with overflow")] -unconstrained fn mint_private_failure_overflow_total_supply() { - // Setup without account contracts. We are not using authwits here, so dummy accounts are enough - let (env, token_contract_address, owner, recipient) = - utils::setup(/* with_account_contracts */ false); - let mint_amount = 10000; - // Mint some tokens - let secret_owner = random(); - let secret_recipient = random(); - let secret_hash_owner = compute_secret_hash(secret_owner); - let secret_hash_recipient = compute_secret_hash(secret_recipient); - - Token::at(token_contract_address).mint_private(mint_amount, secret_hash_owner).call( - &mut env.public(), - ); - Token::at(token_contract_address).mint_private(mint_amount, secret_hash_recipient).call( - &mut env.public(), - ); - - // Time travel so we can read keys from the registry - env.advance_block_by(6); - - // Store 2 notes in the cache so we can redeem it for owner and recipient - env.add_note( - &mut TransparentNote::new(mint_amount, secret_hash_owner), - Token::storage_layout().pending_shields.slot, - token_contract_address, - ); - env.add_note( - &mut TransparentNote::new(mint_amount, secret_hash_recipient), - Token::storage_layout().pending_shields.slot, - token_contract_address, - ); - - // Redeem owner's shielded tokens - env.impersonate(owner); - Token::at(token_contract_address).redeem_shield(owner, mint_amount, secret_owner).call( - &mut env.private(), - ); - - // Redeem recipient's shielded tokens - env.impersonate(recipient); - Token::at(token_contract_address).redeem_shield(recipient, mint_amount, secret_recipient).call( - &mut env.private(), - ); - - utils::check_private_balance(token_contract_address, owner, mint_amount); - utils::check_private_balance(token_contract_address, recipient, mint_amount); - - env.impersonate(owner); - let mint_amount = 2.pow_32(128) - 2 * mint_amount; - // Try to mint some tokens - let secret = random(); - let secret_hash = compute_secret_hash(secret); - Token::at(token_contract_address).mint_private(mint_amount, secret_hash).call(&mut env.public()); -} diff --git a/yarn-project/end-to-end/src/e2e_prover/e2e_prover_test.ts b/yarn-project/end-to-end/src/e2e_prover/e2e_prover_test.ts index 1b7b29c6d9d7..8d1bfb5b4566 100644 --- a/yarn-project/end-to-end/src/e2e_prover/e2e_prover_test.ts +++ b/yarn-project/end-to-end/src/e2e_prover/e2e_prover_test.ts @@ -14,7 +14,6 @@ import { Note, type PXE, type TxHash, - computeSecretHash, createDebugLogger, deployL1Contract, } from '@aztec/aztec.js'; @@ -346,24 +345,23 @@ export class FullProverTest { 'mint', async () => { const { fakeProofsAsset: asset, accounts } = this; - const amount = 10000n; + const privateAmount = 10000n; + const publicAmount = 10000n; const waitOpts = { proven: false }; - this.logger.verbose(`Minting ${amount} publicly...`); - await asset.methods.mint_public(accounts[0].address, amount).send().wait(waitOpts); + this.logger.verbose(`Minting ${privateAmount + publicAmount} publicly...`); + await asset.methods + .mint_public(accounts[0].address, privateAmount + publicAmount) + .send() + .wait(waitOpts); - this.logger.verbose(`Minting ${amount} privately...`); - const secret = Fr.random(); - const secretHash = computeSecretHash(secret); - const receipt = await asset.methods.mint_private(amount, secretHash).send().wait(waitOpts); + this.logger.verbose(`Transferring ${privateAmount} to private...`); + await asset.methods.transfer_to_private(accounts[0].address, privateAmount).send().wait(waitOpts); - await this.addPendingShieldNoteToPXE(0, amount, secretHash, receipt.txHash); - const txClaim = asset.methods.redeem_shield(accounts[0].address, amount, secret).send(); - await txClaim.wait({ ...waitOpts, debug: true }); this.logger.verbose(`Minting complete.`); - return { amount }; + return { amount: publicAmount }; }, async ({ amount }) => { const { diff --git a/yarn-project/end-to-end/src/guides/dapp_testing.test.ts b/yarn-project/end-to-end/src/guides/dapp_testing.test.ts index 4e326a207681..1fea34910177 100644 --- a/yarn-project/end-to-end/src/guides/dapp_testing.test.ts +++ b/yarn-project/end-to-end/src/guides/dapp_testing.test.ts @@ -1,17 +1,6 @@ // docs:start:imports import { createAccount, getDeployedTestAccountsWallets } from '@aztec/accounts/testing'; -import { - type AccountWallet, - CheatCodes, - ExtendedNote, - Fr, - Note, - type PXE, - TxStatus, - computeSecretHash, - createPXEClient, - waitForPXE, -} from '@aztec/aztec.js'; +import { type AccountWallet, CheatCodes, Fr, type PXE, TxStatus, createPXEClient, waitForPXE } from '@aztec/aztec.js'; // docs:end:imports // docs:start:import_contract import { TestContract } from '@aztec/noir-contracts.js/Test'; @@ -19,6 +8,7 @@ import { TestContract } from '@aztec/noir-contracts.js/Test'; import { TokenContract } from '@aztec/noir-contracts.js/Token'; import { U128_UNDERFLOW_ERROR } from '../fixtures/fixtures.js'; +import { mintTokensToPrivate } from '../fixtures/token_utils.js'; const { PXE_URL = 'http://localhost:8080', ETHEREUM_HOST = 'http://localhost:8545' } = process.env; @@ -51,22 +41,8 @@ describe('guides/dapp/testing', () => { expect(await token.methods.balance_of_private(recipientAddress).simulate()).toEqual(0n); const mintAmount = 20n; - const secret = Fr.random(); - const secretHash = computeSecretHash(secret); - const receipt = await token.methods.mint_private(mintAmount, secretHash).send().wait(); - - const note = new Note([new Fr(mintAmount), secretHash]); - const extendedNote = new ExtendedNote( - note, - recipientAddress, - token.address, - TokenContract.storage.pending_shields.slot, - TokenContract.notes.TransparentNote.id, - receipt.txHash, - ); - await owner.addNote(extendedNote); - - await token.methods.redeem_shield(recipientAddress, mintAmount, secret).send().wait(); + await mintTokensToPrivate(token, owner, recipientAddress, mintAmount); + expect(await token.withWallet(recipient).methods.balance_of_private(recipientAddress).simulate()).toEqual(20n); }); }); @@ -91,22 +67,9 @@ describe('guides/dapp/testing', () => { expect(await token.methods.balance_of_private(recipient.getAddress()).simulate()).toEqual(0n); const recipientAddress = recipient.getAddress(); const mintAmount = 20n; - const secret = Fr.random(); - const secretHash = computeSecretHash(secret); - const receipt = await token.methods.mint_private(mintAmount, secretHash).send().wait(); - - const note = new Note([new Fr(mintAmount), secretHash]); - const extendedNote = new ExtendedNote( - note, - recipientAddress, - token.address, - TokenContract.storage.pending_shields.slot, - TokenContract.notes.TransparentNote.id, - receipt.txHash, - ); - await owner.addNote(extendedNote); - - await token.methods.redeem_shield(recipientAddress, mintAmount, secret).send().wait(); + + await mintTokensToPrivate(token, owner, recipientAddress, mintAmount); + expect(await token.withWallet(recipient).methods.balance_of_private(recipientAddress).simulate()).toEqual(20n); }); }); @@ -131,22 +94,8 @@ describe('guides/dapp/testing', () => { const ownerAddress = owner.getAddress(); const mintAmount = 100n; - const secret = Fr.random(); - const secretHash = computeSecretHash(secret); - const receipt = await token.methods.mint_private(100n, secretHash).send().wait(); - - const note = new Note([new Fr(mintAmount), secretHash]); - const extendedNote = new ExtendedNote( - note, - ownerAddress, - token.address, - TokenContract.storage.pending_shields.slot, - TokenContract.notes.TransparentNote.id, - receipt.txHash, - ); - await owner.addNote(extendedNote); - - await token.methods.redeem_shield(ownerAddress, 100n, secret).send().wait(); + + await mintTokensToPrivate(token, owner, ownerAddress, mintAmount); // docs:start:calc-slot cheats = await CheatCodes.create(ETHEREUM_HOST, pxe); diff --git a/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts b/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts index 6030d9cf9113..a73e8fbd0b87 100644 --- a/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts +++ b/yarn-project/end-to-end/src/guides/writing_an_account_contract.test.ts @@ -4,8 +4,10 @@ import { AuthWitness, type AuthWitnessProvider, BatchCall, - type CompleteAddress, Fr, - GrumpkinScalar, Schnorr + type CompleteAddress, + Fr, + GrumpkinScalar, + Schnorr, } from '@aztec/aztec.js'; import { SchnorrHardcodedAccountContractArtifact } from '@aztec/noir-contracts.js/SchnorrHardcodedAccount'; import { TokenContract } from '@aztec/noir-contracts.js/Token';