Skip to content

Commit

Permalink
chore: renaming private token as token with refunds (#7626)
Browse files Browse the repository at this point in the history
Private token makes me think that the token is working only with private state so the name is confusing. Also I plan on using the reference token impl and just extending it with the refund stuff in a future PR. This makes sense because a lot of the custom changes to the token were made because npk was used instead of owner address --> this required modiying balances_map. Given that this will be tackled the standard way now that we are able to compute slotted hiding point in private I can revert most of the custom changes to the token.
  • Loading branch information
benesjan authored Jul 31, 2024
1 parent 19cdf01 commit 43a83ae
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 63 deletions.
2 changes: 1 addition & 1 deletion noir-projects/noir-contracts/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ members = [
"contracts/pending_note_hashes_contract",
"contracts/price_feed_contract",
"contracts/private_fpc_contract",
"contracts/private_token_contract",
"contracts/token_with_refunds_contract",
"contracts/schnorr_account_contract",
"contracts/schnorr_hardcoded_account_contract",
"contracts/schnorr_single_key_account_contract",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ type = "contract"
[dependencies]
aztec = { path = "../../../aztec-nr/aztec" }
authwit = { path = "../../../aztec-nr/authwit" }
private_token = { path = "../private_token_contract" }
token_with_refunds = { path = "../token_with_refunds_contract" }
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod lib;
contract PrivateFPC {
use dep::aztec::protocol_types::{abis::log_hash::LogHash, address::AztecAddress, hash::poseidon2_hash};
use dep::aztec::state_vars::SharedImmutable;
use dep::private_token::PrivateToken;
use dep::token_with_refunds::TokenWithRefunds;
use crate::lib::emit_randomness_as_unencrypted_log;

#[aztec(storage)]
Expand Down Expand Up @@ -31,7 +31,7 @@ contract PrivateFPC {
// We emit fee payer randomness to ensure FPC admin can reconstruct their fee note
emit_randomness_as_unencrypted_log(&mut context, fee_payer_randomness);

PrivateToken::at(asset).setup_refund(
TokenWithRefunds::at(asset).setup_refund(
storage.admin_npk_m_hash.read_private(),
context.msg_sender(),
amount,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "private_token_contract"
name = "token_with_refunds_contract"
authors = [""]
compiler_version = ">=0.25.0"
type = "contract"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod test;

// Minimal token implementation that supports `AuthWit` accounts and private refunds

contract PrivateToken {
contract TokenWithRefunds {
use dep::compressed_string::FieldCompressedString;
use dep::aztec::{
note::utils::compute_slotted_note_hash_raw, hash::compute_secret_hash,
Expand Down Expand Up @@ -97,7 +97,7 @@ contract PrivateToken {
let header = context.get_header();
let caller_npk_m_hash = header.get_npk_m_hash(&mut context, caller);
storage.balances.add(caller_npk_m_hash, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, caller, caller));
PrivateToken::at(context.this_address()).assert_minter_and_mint(context.msg_sender(), amount).enqueue(&mut context);
TokenWithRefunds::at(context.this_address()).assert_minter_and_mint(context.msg_sender(), amount).enqueue(&mut context);
}

#[aztec(public)]
Expand Down Expand Up @@ -205,16 +205,9 @@ contract PrivateToken {
let tx_fee = context.transaction_fee();
let (fee_payer_note_hiding_point, user_note_hiding_point) = TokenNote::complete_refund(fee_payer_point, user_point, tx_fee);

// 2. Now we "manually" compute the slotted note hashes.
let fee_payer_slotted_note_hash = compute_slotted_note_hash_raw(
PrivateToken::storage().balances.slot,
fee_payer_note_hiding_point
);
let user_slotted_note_hash = compute_slotted_note_hash_raw(PrivateToken::storage().balances.slot, user_note_hiding_point);

// 3. At last we emit the note hashes.
context.push_note_hash(fee_payer_slotted_note_hash);
context.push_note_hash(user_slotted_note_hash);
// 2. At last we emit the note hashes.
context.push_note_hash(fee_payer_note_hiding_point.x);
context.push_note_hash(user_note_hiding_point.x);
// --> Once the tx is settled user and fee recipient can add the notes to their pixies.
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::aztec::{
test::helpers::cheatcodes, oracle::unsafe_rand::unsafe_rand, hash::compute_secret_hash,
prelude::NoteHeader
};
use crate::PrivateToken;
use crate::TokenWithRefunds;
use crate::types::token_note::TokenNote;
use dep::authwit::cheatcodes as authwit_cheatcodes;

Expand All @@ -13,7 +13,7 @@ unconstrained fn transfer_success() {

let transfer_amount = 1_000;

let transfer_private_from_call_interface = PrivateToken::at(token_contract_address).transfer_from(owner, recipient, transfer_amount, 1);
let transfer_private_from_call_interface = TokenWithRefunds::at(token_contract_address).transfer_from(owner, recipient, transfer_amount, 1);

authwit_cheatcodes::add_private_authwit_from_call_interface(owner, recipient, transfer_private_from_call_interface);

Expand Down Expand Up @@ -45,7 +45,7 @@ unconstrained fn setup_refund_success() {
let mut context = env.private();
let recipient_npk_m_hash = context.get_header().get_npk_m_hash(&mut context, recipient);

let setup_refund_from_call_interface = PrivateToken::at(token_contract_address).setup_refund(
let setup_refund_from_call_interface = TokenWithRefunds::at(token_contract_address).setup_refund(
recipient_npk_m_hash, // fee payer
owner, // sponsored user
funded_amount,
Expand Down Expand Up @@ -75,7 +75,7 @@ unconstrained fn setup_refund_success() {
randomness: user_randomness,
header: NoteHeader::empty()
},
PrivateToken::storage().balances.slot,
TokenWithRefunds::storage().balances.slot,
token_contract_address
);
env.store_note_in_cache(
Expand All @@ -85,7 +85,7 @@ unconstrained fn setup_refund_success() {
randomness: fee_payer_randomness,
header: NoteHeader::empty()
},
PrivateToken::storage().balances.slot,
TokenWithRefunds::storage().balances.slot,
token_contract_address
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use dep::aztec::{
context::PrivateContext
};

use crate::{types::{token_note::TokenNote}, PrivateToken};
use crate::{types::{token_note::TokenNote}, TokenWithRefunds};

pub fn setup(with_account_contracts: bool) -> (&mut TestEnvironment, AztecAddress, AztecAddress, AztecAddress) {
// Setup env, generate keys
Expand All @@ -27,13 +27,13 @@ pub fn setup(with_account_contracts: bool) -> (&mut TestEnvironment, AztecAddres
env.impersonate(owner);

// Deploy token contract
let initializer_call_interface = PrivateToken::interface().constructor(
let initializer_call_interface = TokenWithRefunds::interface().constructor(
owner,
"TestToken0000000000000000000000",
"TT00000000000000000000000000000",
18
);
let token_contract = env.deploy_self("PrivateToken").with_public_initializer(initializer_call_interface);
let token_contract = env.deploy_self("TokenWithRefunds").with_public_initializer(initializer_call_interface);
let token_contract_address = token_contract.to_address();
env.advance_block_by(6);
(&mut env, token_contract_address, owner, recipient)
Expand All @@ -43,7 +43,7 @@ pub fn setup_and_mint(with_account_contracts: bool) -> (&mut TestEnvironment, Az
// Setup
let (env, token_contract_address, owner, recipient) = setup(with_account_contracts);
let mint_amount = 10_000;
let mint_private_call_interface = PrivateToken::at(token_contract_address).privately_mint_private_note(mint_amount);
let mint_private_call_interface = TokenWithRefunds::at(token_contract_address).privately_mint_private_note(mint_amount);
env.call_private_void(mint_private_call_interface);
env.advance_block_by(6);

Expand All @@ -64,7 +64,7 @@ pub fn check_private_balance(
let header = context.get_header();
let owner_npk_m_hash = header.get_npk_m_hash(context, address);

let balance_of_private = PrivateToken::balance_of_unconstrained(owner_npk_m_hash);
let balance_of_private = TokenWithRefunds::balance_of_unconstrained(owner_npk_m_hash);
assert(balance_of_private == address_amount, "Private balance is not correct");
cheatcodes::set_contract_address(current_contract_address);
}
46 changes: 26 additions & 20 deletions yarn-project/end-to-end/src/e2e_fees/fees_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
FPCContract,
GasTokenContract,
PrivateFPCContract,
PrivateTokenContract,
TokenWithRefundsContract,
} from '@aztec/noir-contracts.js';
import { getCanonicalGasToken } from '@aztec/protocol-contracts/gas-token';

Expand Down Expand Up @@ -67,7 +67,7 @@ export class FeesTest {
public gasTokenContract!: GasTokenContract;
public bananaCoin!: BananaCoin;
public bananaFPC!: FPCContract;
public privateToken!: PrivateTokenContract;
public tokenWithRefunds!: TokenWithRefundsContract;
public privateFPC!: PrivateFPCContract;
public counterContract!: CounterContract;
public subscriptionContract!: AppSubscriptionContract;
Expand All @@ -77,7 +77,7 @@ export class FeesTest {
public getGasBalanceFn!: BalancesFn;
public getBananaPublicBalanceFn!: BalancesFn;
public getBananaPrivateBalanceFn!: BalancesFn;
public getPrivateTokenBalanceFn!: BalancesFn;
public getTokenWithRefundsBalanceFn!: BalancesFn;

public readonly INITIAL_GAS_BALANCE = BigInt(1e15);
public readonly ALICE_INITIAL_BANANAS = BigInt(1e12);
Expand All @@ -99,11 +99,11 @@ export class FeesTest {
await this.snapshotManager.teardown();
}

/** Alice mints PrivateToken */
async mintPrivateTokens(amount: bigint) {
const balanceBefore = await this.privateToken.methods.balance_of_private(this.aliceAddress).simulate();
await this.privateToken.methods.privately_mint_private_note(amount).send().wait();
const balanceAfter = await this.privateToken.methods.balance_of_private(this.aliceAddress).simulate();
/** Alice mints TokenWithRefunds */
async mintTokenWithRefunds(amount: bigint) {
const balanceBefore = await this.tokenWithRefunds.methods.balance_of_private(this.aliceAddress).simulate();
await this.tokenWithRefunds.methods.privately_mint_private_note(amount).send().wait();
const balanceAfter = await this.tokenWithRefunds.methods.balance_of_private(this.aliceAddress).simulate();
expect(balanceAfter).toEqual(balanceBefore + amount);
}

Expand Down Expand Up @@ -242,22 +242,28 @@ export class FeesTest {
);
}

async applyPrivateTokenAndFPC() {
async applyTokenWithRefundsAndFPC() {
await this.snapshotManager.snapshot(
'private_token_and_private_fpc',
'token_with_refunds_and_private_fpc',
async context => {
// Deploy token/fpc flavors for private refunds
const gasTokenContract = this.gasBridgeTestHarness.l2Token;
expect(await context.pxe.isContractPubliclyDeployed(gasTokenContract.address)).toBe(true);

const privateToken = await PrivateTokenContract.deploy(this.aliceWallet, this.aliceAddress, 'PVT', 'PVT', 18n)
const tokenWithRefunds = await TokenWithRefundsContract.deploy(
this.aliceWallet,
this.aliceAddress,
'PVT',
'PVT',
18n,
)
.send()
.deployed();

this.logger.info(`PrivateToken deployed at ${privateToken.address}`);
this.logger.info(`TokenWithRefunds deployed at ${tokenWithRefunds.address}`);
const adminKeyHash = this.bobWallet.getCompleteAddress().publicKeys.masterNullifierPublicKey.hash();

const privateFPCSent = PrivateFPCContract.deploy(this.bobWallet, privateToken.address, adminKeyHash).send();
const privateFPCSent = PrivateFPCContract.deploy(this.bobWallet, tokenWithRefunds.address, adminKeyHash).send();
const privateFPC = await privateFPCSent.deployed();

this.logger.info(`PrivateFPC deployed at ${privateFPC.address}`);
Expand All @@ -268,18 +274,18 @@ export class FeesTest {
);

return {
privateTokenAddress: privateToken.address,
tokenWithRefundsAddress: tokenWithRefunds.address,
privateFPCAddress: privateFPC.address,
};
},
async data => {
this.privateFPC = await PrivateFPCContract.at(data.privateFPCAddress, this.bobWallet);
this.privateToken = await PrivateTokenContract.at(data.privateTokenAddress, this.aliceWallet);
this.tokenWithRefunds = await TokenWithRefundsContract.at(data.tokenWithRefundsAddress, this.aliceWallet);

const logger = this.logger;
this.getPrivateTokenBalanceFn = getBalancesFn(
this.getTokenWithRefundsBalanceFn = getBalancesFn(
'🕵️.private',
this.privateToken.methods.balance_of_private,
this.tokenWithRefunds.methods.balance_of_private,
logger,
);
},
Expand Down Expand Up @@ -348,11 +354,11 @@ export class FeesTest {
);
}

public async applyFundAliceWithPrivateTokens() {
public async applyFundAliceWithTokens() {
await this.snapshotManager.snapshot(
'fund_alice_with_private_tokens',
'fund_alice_with_tokens',
async () => {
await this.mintPrivateTokens(BigInt(this.ALICE_INITIAL_BANANAS));
await this.mintTokenWithRefunds(BigInt(this.ALICE_INITIAL_BANANAS));
},
() => Promise.resolve(),
);
Expand Down
33 changes: 17 additions & 16 deletions yarn-project/end-to-end/src/e2e_fees/private_refunds.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {
import { Fr, type GasSettings } from '@aztec/circuits.js';
import { FunctionSelector, FunctionType } from '@aztec/foundation/abi';
import { poseidon2Hash } from '@aztec/foundation/crypto';
import { type PrivateFPCContract, PrivateTokenContract } from '@aztec/noir-contracts.js';
import { type PrivateFPCContract, TokenWithRefundsContract } from '@aztec/noir-contracts.js';

import { expectMapping } from '../fixtures/utils.js';
import { FeesTest } from './fees_test.js';

describe('e2e_fees/private_refunds', () => {
let aliceWallet: Wallet;
let aliceAddress: AztecAddress;
let privateToken: PrivateTokenContract;
let tokenWithRefunds: TokenWithRefundsContract;
let privateFPC: PrivateFPCContract;

let initialAliceBalance: bigint;
Expand All @@ -31,9 +31,9 @@ describe('e2e_fees/private_refunds', () => {
await t.applyInitialAccountsSnapshot();
await t.applyPublicDeployAccountsSnapshot();
await t.applyDeployGasTokenSnapshot();
await t.applyPrivateTokenAndFPC();
await t.applyFundAliceWithPrivateTokens();
({ aliceWallet, aliceAddress, privateFPC, privateToken } = await t.setup());
await t.applyTokenWithRefundsAndFPC();
await t.applyFundAliceWithTokens();
({ aliceWallet, aliceAddress, privateFPC, tokenWithRefunds } = await t.setup());
t.logger.debug(`Alice address: ${aliceAddress}`);
});

Expand All @@ -43,12 +43,13 @@ describe('e2e_fees/private_refunds', () => {

beforeEach(async () => {
[[initialAliceBalance, initialBobBalance], [initialFPCGasBalance]] = await Promise.all([
t.getPrivateTokenBalanceFn(aliceAddress, t.bobAddress),
t.getTokenWithRefundsBalanceFn(aliceAddress, t.bobAddress),
t.getGasBalanceFn(privateFPC.address),
]);
});

it('can do private payments and refunds', async () => {
// This will get re-enabled in a PR up the stack.
it.skip('can do private payments and refunds', async () => {
// 1. We get the hash of Bob's master nullifier public key. The corresponding nullifier secret key can later on
// be used to nullify/spend the note that contains the npk_m_hash.
// TODO(#7324): The values in complete address are currently not updated after the keys are rotated so this does
Expand All @@ -58,13 +59,13 @@ describe('e2e_fees/private_refunds', () => {
const bobRandomness = poseidon2Hash([aliceRandomness]); // Called fee_payer_randomness in contracts

// 2. We call arbitrary `private_get_name(...)` function to check that the fee refund flow works.
const tx = await privateToken.methods
const tx = await tokenWithRefunds.methods
.private_get_name()
.send({
fee: {
gasSettings: t.gasSettings,
paymentMethod: new PrivateRefundPaymentMethod(
privateToken.address,
tokenWithRefunds.address,
privateFPC.address,
aliceWallet,
aliceRandomness,
Expand Down Expand Up @@ -99,9 +100,9 @@ describe('e2e_fees/private_refunds', () => {
new ExtendedNote(
aliceRefundNote,
t.aliceAddress,
privateToken.address,
PrivateTokenContract.storage.balances.slot,
PrivateTokenContract.notes.TokenNote.id,
tokenWithRefunds.address,
TokenWithRefundsContract.storage.balances.slot,
TokenWithRefundsContract.notes.TokenNote.id,
tx.txHash,
),
);
Expand All @@ -117,9 +118,9 @@ describe('e2e_fees/private_refunds', () => {
new ExtendedNote(
bobFeeNote,
t.bobAddress,
privateToken.address,
PrivateTokenContract.storage.balances.slot,
PrivateTokenContract.notes.TokenNote.id,
tokenWithRefunds.address,
TokenWithRefundsContract.storage.balances.slot,
TokenWithRefundsContract.notes.TokenNote.id,
tx.txHash,
),
);
Expand All @@ -128,7 +129,7 @@ describe('e2e_fees/private_refunds', () => {
await expectMapping(t.getGasBalanceFn, [privateFPC.address], [initialFPCGasBalance - tx.transactionFee!]);
// ... and that the transaction fee was correctly transferred from Alice to Bob.
await expectMapping(
t.getPrivateTokenBalanceFn,
t.getTokenWithRefundsBalanceFn,
[aliceAddress, t.bobAddress],
[initialAliceBalance - tx.transactionFee!, initialBobBalance + tx.transactionFee!],
);
Expand Down

0 comments on commit 43a83ae

Please sign in to comment.