Skip to content

Commit

Permalink
final old shield flow nuke
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Nov 11, 2024
1 parent 1ba2129 commit 8b3278a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 301 deletions.
79 changes: 25 additions & 54 deletions noir-projects/noir-contracts/contracts/token_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ contract Token {
context::{PrivateCallInterface, PrivateContext},
encrypted_logs::{
encrypted_event_emission::encode_and_encrypt_event_unconstrained,
encrypted_note_emission::{
encode_and_encrypt_note, encode_and_encrypt_note_unconstrained,
},
encrypted_note_emission::encode_and_encrypt_note_unconstrained,
},
hash::compute_secret_hash,
keys::getters::get_public_keys,
macros::{
events::event,
Expand All @@ -35,11 +32,9 @@ contract Token {
},
oracle::random::random,
prelude::{
AztecAddress, FunctionSelector, Map, NoteGetterOptions, PrivateSet, PublicContext,
PublicMutable, SharedImmutable,
AztecAddress, FunctionSelector, Map, PublicContext, PublicMutable, SharedImmutable,
},
protocol_types::{point::Point, traits::Serialize},
utils::comparison::Comparator,
};

use dep::uint_note::uint_note::UintNote;
Expand All @@ -51,7 +46,7 @@ contract Token {
};
// docs:end:import_authwit

use crate::types::{balance_set::BalanceSet, transparent_note::TransparentNote};
use crate::types::balance_set::BalanceSet;

// docs:end::imports

Expand Down Expand Up @@ -85,9 +80,6 @@ contract Token {
balances: Map<AztecAddress, BalanceSet<Context>, Context>,
// docs:end:storage_balances
total_supply: PublicMutable<U128, Context>,
// docs:start:storage_pending_shields
pending_shields: PrivateSet<TransparentNote, Context>,
// docs:end:storage_pending_shields
public_balances: Map<AztecAddress, PublicMutable<U128, Context>, Context>,
symbol: SharedImmutable<FieldCompressedString, Context>,
name: SharedImmutable<FieldCompressedString, Context>,
Expand All @@ -111,6 +103,7 @@ contract Token {
// docs:end:initialize_decimals
}
// docs:end:constructor

// docs:start:set_admin
#[public]
fn set_admin(new_admin: AztecAddress) {
Expand All @@ -120,6 +113,7 @@ contract Token {
// docs:end:write_admin
}
// docs:end:set_admin

#[public]
#[view]
fn public_get_name() -> FieldCompressedString {
Expand All @@ -131,16 +125,19 @@ contract Token {
fn private_get_name() -> FieldCompressedString {
storage.name.read_private()
}

#[public]
#[view]
fn public_get_symbol() -> pub FieldCompressedString {
storage.symbol.read_public()
}

#[private]
#[view]
fn private_get_symbol() -> pub FieldCompressedString {
storage.symbol.read_private()
}

#[public]
#[view]
fn public_get_decimals() -> pub u8 {
Expand All @@ -155,34 +152,39 @@ contract Token {
storage.decimals.read_private()
// docs:end:read_decimals_private
}

// docs:start:admin
#[public]
#[view]
fn get_admin() -> Field {
storage.admin.read().to_field()
}
// docs:end:admin

// docs:start:is_minter
#[public]
#[view]
fn is_minter(minter: AztecAddress) -> bool {
storage.minters.at(minter).read()
}
// docs:end:is_minter

// docs:start:total_supply
#[public]
#[view]
fn total_supply() -> Field {
storage.total_supply.read().to_integer()
}
// docs:end:total_supply

// docs:start:balance_of_public
#[public]
#[view]
fn balance_of_public(owner: AztecAddress) -> Field {
storage.public_balances.at(owner).read().to_integer()
}
// docs:end:balance_of_public

// docs:start:set_minter
#[public]
fn set_minter(minter: AztecAddress, approve: bool) {
Expand All @@ -194,6 +196,7 @@ contract Token {
// docs:end:write_minter
}
// docs:end:set_minter

// docs:start:mint_public
#[public]
fn mint_public(to: AztecAddress, amount: Field) {
Expand All @@ -208,23 +211,6 @@ contract Token {
}
// docs:end:mint_public

// docs:start:shield
#[public]
fn shield(from: AztecAddress, amount: Field, secret_hash: Field, nonce: Field) {
if (!from.eq(context.msg_sender())) {
// The redeem is only spendable once, so we need to ensure that you cannot insert multiple shields from the same message.
assert_current_call_valid_authwit_public(&mut context, from);
} else {
assert(nonce == 0, "invalid nonce");
}
let amount = U128::from_integer(amount);
let from_balance = storage.public_balances.at(from).read().sub(amount);
let pending_shields = storage.pending_shields;
let mut note = TransparentNote::new(amount.to_field(), secret_hash);
storage.public_balances.at(from).write(from_balance);
pending_shields.insert_from_public(&mut note);
}
// docs:end:shield
// docs:start:transfer_public
#[public]
fn transfer_public(from: AztecAddress, to: AztecAddress, amount: Field, nonce: Field) {
Expand All @@ -240,6 +226,7 @@ contract Token {
storage.public_balances.at(to).write(to_balance);
}
// docs:end:transfer_public

// docs:start:burn_public
#[public]
fn burn_public(from: AztecAddress, amount: Field, nonce: Field) {
Expand All @@ -257,32 +244,7 @@ contract Token {
storage.total_supply.write(new_supply);
}
// docs:end:burn_public
// docs:start:redeem_shield
#[private]
fn redeem_shield(to: AztecAddress, amount: Field, secret: Field) {
let secret_hash = compute_secret_hash(secret);
// Pop 1 note (set_limit(1)) which has an amount stored in a field with index 0 (select(0, amount)) and
// a secret_hash stored in a field with index 1 (select(1, secret_hash)).
let mut options = NoteGetterOptions::new();
options = options
.select(TransparentNote::properties().amount, Comparator.EQ, amount)
.select(TransparentNote::properties().secret_hash, Comparator.EQ, secret_hash)
.set_limit(1);
let notes = storage.pending_shields.pop_notes(options);
assert(notes.len() == 1, "note not popped");
// Add the token note to user's balances set
// Note: Using context.msg_sender() as a sender below makes this incompatible with escrows because we send
// outgoing logs to that address and to send outgoing logs you need to get a hold of ovsk_m.
let from = context.msg_sender();
let from_ovpk_m = get_public_keys(from).ovpk_m;
storage.balances.at(to).add(to, U128::from_integer(amount)).emit(encode_and_encrypt_note(
&mut context,
from_ovpk_m,
to,
context.msg_sender(),
));
}
// docs:end:redeem_shield

// docs:start:transfer_to_public
#[private]
fn transfer_to_public(from: AztecAddress, to: AztecAddress, amount: Field, nonce: Field) {
Expand All @@ -301,6 +263,7 @@ contract Token {
Token::at(context.this_address())._increase_public_balance(to, amount).enqueue(&mut context);
}
// docs:end:transfer_to_public

// docs:start:transfer
#[private]
fn transfer(to: AztecAddress, amount: Field) {
Expand Down Expand Up @@ -343,6 +306,7 @@ contract Token {
);
}
// docs:end:transfer

#[contract_library_method]
fn subtract_balance(
context: &mut PrivateContext,
Expand All @@ -367,6 +331,7 @@ contract Token {
compute_recurse_subtract_balance_call(*context, account, remaining).call(context)
}
}

// TODO(#7729): apply no_predicates to the contract interface method directly instead of having to use a wrapper
// like we do here.
#[no_predicates]
Expand All @@ -378,6 +343,7 @@ contract Token {
) -> PrivateCallInterface<25, U128> {
Token::at(context.this_address())._recurse_subtract_balance(account, remaining.to_field())
}

// TODO(#7728): even though the amount should be a U128, we can't have that type in a contract interface due to
// serialization issues.
#[internal]
Expand All @@ -391,6 +357,7 @@ contract Token {
RECURSIVE_TRANSFER_CALL_MAX_NOTES,
)
}

/**
* Cancel a private authentication witness.
* @param inner_hash The inner hash of the authwit to cancel.
Expand All @@ -403,6 +370,7 @@ contract Token {
context.push_nullifier(nullifier);
}
// docs:end:cancel_authwit

// docs:start:transfer_from
#[private]
fn transfer_from(from: AztecAddress, to: AztecAddress, amount: Field, nonce: Field) {
Expand Down Expand Up @@ -438,6 +406,7 @@ contract Token {
));
}
// docs:end:transfer_from

// docs:start:burn
#[private]
fn burn(from: AztecAddress, amount: Field, nonce: Field) {
Expand Down Expand Up @@ -814,6 +783,7 @@ contract Token {
storage.public_balances.at(to).write(new_balance);
}
// docs:end:increase_public_balance

// docs:start:reduce_total_supply
#[public]
#[internal]
Expand All @@ -823,6 +793,7 @@ contract Token {
storage.total_supply.write(new_supply);
}
// docs:end:reduce_total_supply

/// Unconstrained ///
// docs:start:balance_of_private
pub(crate) unconstrained fn balance_of_private(owner: AztecAddress) -> pub Field {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,3 @@ mod transfer_to_public;
mod refunds;
mod minting;
mod reading_constants;
mod shielding;
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use crate::test::utils;
use crate::{Token, types::transparent_note::TransparentNote};
use dep::aztec::{oracle::random::random, test::helpers::cheatcodes};
use crate::{test::utils, Token};

#[test]
unconstrained fn mint_public_success() {
Expand Down
Loading

0 comments on commit 8b3278a

Please sign in to comment.