Aztec 백서

Aztec

원문

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 75쪽 중 20쪽

s an `overrides` option that injects values into the simulator's (ephemeral) world-state fork and contract DB before the call runs. The override is scoped to that single simulation and thrown away afterwards. Override a public-storage slot: ``` const result = await contract.methods.read_balance(account).simulate({ overrides: { publicStorage: [{ contract: contract.address, slot: BALANCE_SLOT, value: new Fr(1_000_000n) }], }, }); ``` Use this to set up state preconditions, reproduce production bugs against pinned storage, or exercise rare value branches without orchestrating the contract calls that produce them. ### Fast-forwarding a contract update[​](#fast-forwarding-a-contract-update "Direct link to Fast-forwarding a contract update") `fastForwardContractUpdate` returns a `SimulationOverrides` object that simulates a deployed instance as if it had already been upgraded to a new contract class. The new class must already be registered on chain. The cheat mirrors a real onchain upgrade followed by waiting out the upgrade delay: the override instance's `currentContractClassId` is bumped, and the `ContractInstanceRegistry`'s delayed-public-mutable storage is rewritten to look like the upgrade was scheduled in the past. ``` import { fastForwardContractUpdate } from '@aztec-labs/aztec.js'; const overrides = await fastForwardContractUpdate({ instanceAddress: contract.address, newClassId: upgradedClass.id, node, }); const result = await contract.methods.upgraded_method().simulate({ overrides }); ``` Use this to test code paths that only execute after an upgrade, without orchestrating the full delayed-mutable upgrade flow. ## Further reading[​](#further-reading "Direct link to Further reading") * [How to read contract data](/developers/testnet/docs/aztec-js/how_to_read_data.md) * [How to send transactions](/developers/testnet/docs/aztec-js/how_to_send_transaction.md) * [How to deploy a contract](/developers/testnet/docs/aztec-js/how_to_deploy_contract.md) * [How to create an account](/developers/testnet/docs/aztec-js/how_to_create_account.md) * [How to compile a contract](/developers/testnet/docs/aztec-nr/compiling_contracts.md) --- # Using Authentication Witnesses This guide shows you how to create and use authentication witnesses (authwits) to authorize other accounts to perform actions on your behalf. Automatic private authwits with EmbeddedWallet When using `EmbeddedWallet`, **private authwits are created automatically**. The wallet simulates your transaction before sending and detects which private authwits are needed, then generates them on the fly. You don't need to create them manually. Public authwits still need to be set explicitly, as they require a separate onchain transaction before use. The manual approach described below is also relevant if you're building a custom wallet implementation. aztec-nr Using AuthWitnesses is always a two-part process. This guide shows how to generate and use them, but you still need to set up your contract to accept and authenticate them. Therefore it is recommended to read the `aztec-nr` [guide on authwitnesses](/developers/testnet/docs/aztec-nr/framework-description/authentication_witnesses.md) before this one. <!-- --> ## Prerequisites[​](#prerequisites "Direct link to Prerequisites") * [Connected to a network](how_to_connect_to_local_network) with an <!-- --> `EmbeddedWallet` instance and funded accounts <!-- --> * Contract with authwit validation (see [smart contract authwits](/developers/testnet/docs/aztec-nr/framework-description/authentication_witnesses.md)) * Understanding of [authwit concepts](/developers/testnet/docs/foundational-topics/advanced/authwit.md) ## Intent types[​](#intent-types "Direct link to Intent types") The authwit system supports different intent types depending on your use case: * **`CallIntent`**: Use when authorizing a specific contract function call. Contains `{ caller, call }` where `call` is a `FunctionCall`, typically obtained with `await interaction.getFunctionCall()`. * **`ContractFunctionInteractionCallIntent`**: Convenience form that takes the interaction directly. Contains `{ caller, action }` where `action` is a `ContractFunctionInteraction`; internally resolved to a `FunctionCall` before signing. * **`IntentInnerHash`**: Use when authorizing arbitrary data. Contains `{ consumer, innerHash }` where `consumer` is the contract that will verify the authwit. ## Create private authwits[​](#create-private-authwits "Direct link to Create private authwits") note If you're using `EmbeddedWallet`, this section is handled for you automatically. See the tip above. Private authwits authorize actions in the private domain. The authorization is included directly in the transaction that uses it. Let's say Alice wants to allow Bob to transfer tokens from her account. Alice is the **authorizer** (she owns the tokens) and Bob is the **caller** (he will execute the transfer): private\_authwit ``` // Alice wants to allow Bob to transfer tokens from her account (private) const privateNonce = Fr.random(); // Define the action Bob will execute const privateAction = tokenContract.methods.transfer_in_private( aliceAddress, // from bobAddress, // to 100n, // amount privateNonce, // authwit nonce for replay protection ); // Alice creates an authwit authorizing Bob to call this function const privateWitness = await wallet.createAuthWit(aliceAddress, { caller: bobAddress, call: await privateAction.getFunctionCall(), }); // Bob executes the transfer, providing the authwit // additionalScopes lets the PXE access Alice's private state // during authwit verification await privateAction.send({ from: bobAddress, authWitnesses: [privateWitness], additionalScopes: [aliceAddress], }); ``` > [Source code: docs/examples/ts/aztecjs\_authwit/index.ts#L45-L71](https://github.com/aztec-labs-eng/aztec-node/blob/v6.0.0-rc.1/docs/examples/ts/aztecjs_authwit/index.ts#L45-L71) tip The nonce prevents replay attacks. When `from` and `msg_sender` are the same (self-transfer), set the nonce to `0`. ## Create public authwits[​](#create-public-authwits "Direct link to Create public authwits") Public authwits require a transaction to store the authorization in the `AuthRegistry` contract before the authorized action can be executed: public\_authwit ``` // Alice wants to allow Bob to transfer tokens from her account (public) const publicNonce = Fr.random(); // Define the action Bob will execute const publicAction = tokenContract.methods.transfer_in_public( aliceAddress, // from bobAddress, // to 100n, // amount publicNonce, // authwit nonce ); // Alice sets the public authwit (this requires a transaction) const authwit = await SetPublicAuthwitContractInteraction.create( wallet, aliceAddress, { caller: bobAddress, action: publicAction }, true, // authorized ); await authwit.send(); // Now Bob can execute the transfer await publicAction.send({ from: bobAddress }); ``` > [Source code: docs/examples/ts/aztecjs\_authwit/index.ts#L73-L96](https://github.com/aztec-labs-eng/aztec-node/blob/v6.0.0-rc.1/docs/examples/ts/aztecjs_authwit/index.ts#L73-L96) ## Create arbitrary message authwits[​](#create-arbitrary-message-authwits "Direct link to Create arbitrary message authwits") Use this when authorizing arbitrary data rather than a specific contract function call: arbitrary\_authwit ``` import { computeInnerAuthWitHash } from "@aztec-labs/aztec.js/authorization"; // Create hash of arbitrary data const innerHash = await computeInnerAuthWitHash([ Fr.fromHexString("0xcafe"), Fr.fromHexString("0xbeef"), ]); // Create an intent with the consumer contract address const intent = { consumer: tokenContract.address, innerHash, }; // Create the authwit for arbitrary data const arbitraryWitness = await wallet.createAuthWit(aliceAddress, intent); console.log("Arbitrary authwit created:", arbitraryWitness); ``` > [Source code: docs/examples/ts/aztecjs\_authwit/index.ts#L98-L116](https://github.com/aztec-labs-eng/aztec-node/blob/v6.0.0-rc.1/docs/examples/ts/aztecjs_authwit/index.ts#L98-L116) The `consumer` is the contract address that will verify this authwit. ## Revoke public authwits[​](#revoke-public-authwits "Direct link to Revoke public authwits") Public authwits can be revoked by setting `authorized` to `false`: revoke\_authwit ``` // Revoke a public authwit by setting authorized to false const revokeNonce = Fr.random(); const revokeAction = tokenContract.methods.transfer_in_public( aliceAddress, bobAddress, 50n, revokeNonce, ); // First, set the authwit const setAuthwit = await SetPublicAuthwitContractInteraction.create( wallet, aliceAddress, { caller: bobAddress, action: revokeAction }, true, ); await setAuthwit.send(); // Later, revoke it const revokeInteraction = await SetPublicAuthwitContractInteraction.create( wallet, aliceAddress, { caller: bobAddress, action: revokeAction }, false, // revoke authorization ); await revokeInteraction.send(); ``` > [Source code: docs/examples/ts/aztecjs\_authwit/index.ts#L118-L145](https://github.com/aztec-labs-eng/aztec-node/blob/v6.0.0-rc.1/docs/examples/ts/aztecjs_authwit/index.ts#L118-L145) ## Next steps[​](#next-steps "Direct link to Next steps") * Learn about [authwits in smart contracts](/developers/testnet/docs/aztec-nr/framework-description/authentication_witnesses.md) * Understand [authwit concepts](/developers/testnet/docs/foundational-topics/advanced/authwit.md) * Explore [account abstraction](/developers/testnet/docs/foundational-topics/accounts.md) --- # Pay Fees Privately This guide explains how private fee payment works on Aztec and walks through a concrete example. A fully private FPC can pay transaction fees without revealing the payer: it has no public functions, no owner, and no offchain agent. Because the contract is fully private, **no onchain deployment transaction is required**. Every app just derives the address deterministically from the class hash and a shared salt, and users interact with it privately. To illustrate the pattern, this guide uses [`PrivateFPC`](https://github.com/alejoamiras/ecosystem-tooling/tree/main/packages/private-fee-juice), the community-built implementation from the `private-fee-juice` package (published on npm as `@alejoamiras/private-fee-juice`). You could write your own private FPC following the same design principles. ## Prerequisites[​](#prerequisites "Direct link to Prerequisites") * [Connected to a network](how_to_connect_to_local_network) with an <!-- --> `EmbeddedWallet` instance and funded accounts <!-- --> * Familiarity with [fee concepts](/developers/testnet/docs/foundational-topics/fees.md) and [Paying Fees](/developers/testnet/docs/aztec-js/how_to_pay_fees.md) info The fee asset is only transferrable within a block to the current sequencer, as it powers the fee abstraction mechanism on Aztec. The asset is not transferable beyond this to ensure credible neutrality between all third party developer made asset portals and to ensure local compliance rules can be followed. ## Why a fully private FPC?[​](#why-a-fully-private-fpc "Direct link to Why a fully private FPC?") On Aztec, the transaction's setup phase is non-revertible, and a protocol-level allowlist controls which public function calls are permitted during it. Public token functions (like `transfer_in_public` and `_increase_public_balance`) have been removed from the default allowlist; custom FPCs may only call protocol-contract setup functions like those on `AuthRegistry` and `FeeJuice`. The reference [`FPC` contract](https://github.com/aztec-labs-eng/aztec-node/blob/v6.0.0-rc.1/noir-projects/noir-contracts/contracts/fees/fpc_contract/src/main.nr) collects user payment during setup by calling those token functions, so its flow is now rejected on public networks. The `PrivateFeePaymentMethod` shipped in `@aztec-labs/aztec.js/fee` (which targets that contract) is therefore deprecated. See the [migration note](/developers/testnet/docs/resources/migration_notes.md#custom-token-fpcs-removed-from-default-public-setup-allowlist) for details. A fully private FPC side-steps the allowlist entirely. Instead of collecting payment from the user during setup, it holds Fee Juice as its own internal private balance (funded earlier by the user through the Fee Juice portal). When a transaction runs, the FPC just verifies that a **Fee Juice claim nullifier** exists in the nullifier tree (proof that the L1 deposit was consumed on L2) and deducts from its private note-based balance. No public cross-contract token calls happen during setup, so the allowlist never blocks anything. ## How a private FPC works[​](#how-a-private-fpc-works "Direct link to How a private FPC works") This section describes the design pattern using the `private-fee-juice` package's `PrivateFPC` as an example. The contract stores an internal, note-based `BalanceSet` of Fee Juice per user. There is no constructor, no admin, and no public surface. ### Two salts, not one[​](#two-salts-not-one "Direct link to Two salts, not one") Two different salt values show up in this flow; it's worth naming them up front so they don't get confused: * **Deployment salt.** Used to derive the FPC's contract address. Once a community agrees on the bytecode and this salt, everyone can derive the same address locally without an onchain deployment tx. The convention for the `private-fee-juice` `PrivateFPC` is `Fr.ZERO` (see [Recommended salt](#recommended-salt-0)). * **Bridge salt.** A random value the user chooses per L1 deposit. Combined with the user's Aztec address, it derives the *bridge secret* (`secret = poseidon2([salt, claimer], DOM_SEP__FPC_BRIDGE_SECRET)`), whose hash is passed as the `secretHash` on the L1 deposit. Only the user knows the preimage, so only the user can later produce the `secret` that `FeeJuice.claim` requires to consume the L1-to-L2 message. `PrivateFPC.mint(amount, salt, leaf_index)` and `PrivateFPC.mint_and_pay_fee(amount, salt, leaf_index)` take the **bridge** salt (along with the leaf index and the user's claimer address, which is `msg_sender`) to reconstruct the Fee Juice claim nullifier and verify the bridge was consumed. ### Two flows[​](#two-flows "Direct link to Two flows") 1. **Bridge + mint + pay** (run once to seed the user's private Fee Juice balance inside the FPC, and run again each time that balance runs low and the user wants to add more by bridging another deposit from L1): <!-- --> 1. **L1 deposit.** Call `FeeJuicePortal.depositToAztecPublic(_to = fpcAddress, _amount = amount, _secretHash = computeSecretHash(bridgeSecret))` where `bridgeSecret = poseidon2([bridgeSalt, claimer], DOM_SEP__FPC_BRIDGE_SECRET)`. The FPC is the *recipient* of the deposit, the user is the *claimer*. 2. **L2 claim.** In a normal L2 transaction, call `FeeJuice.claim(fpcAddress, amount, bridgeSecret, leafIndex)` directly. This consumes the L1-to-L2 message, credits Fee Juice to the FPC's **public** Fee Juice balance, and emits the claim nullifier. The fee for this transaction is paid by whatever mechanism the user normally uses (their own Fee Juice, `FeeJuicePaymentMethodWithClaim` on a *separate* bridge they control, the Sponsored FPC on devnet/testnet, and so on). The `PrivateFPC` does *not* sponsor this call, because at this point the user has no balance with it yet. 3. **Mint.** In a follow-up L2 transaction, call `PrivateFPC.mint(amount, bridgeSalt, leafIndex)` (again paid by whatever mechanism the user normally uses). `mint` does **not** call `FeeJuice.claim` again, because the claim already happened in step 1.2. The contract recomputes the same nullifier value that the earlier `claim` emitted (possible because the user supplies the `bridgeSalt` that originally produced it), asserts that nullifier exists in the nullifier tree as proof the L1 deposit was consumed, emits its own FPC-scoped nullifier to prevent double-minting the same bridge credit, and credits `amount` to the claimer's private balance inside the FPC. 4. **Pay.** From that point on, the user can pass `new FPCFeePaymentMethod(fpcAddress)` as the payment method on any transaction. Under the hood, the method calls `PrivateFPC.pay_fee()` in setup, which deducts `max_gas_cost` from the user's private balance and makes the FPC the fee payer. 2. **Cold-start** (single-transaction equivalent of steps 2–4 above, for first-time users who have only done the L1 deposit): <!-- --> 1. **L1 deposit.** Same as step 1.1 above. 2. **Single L2 transaction.** Pass `new PrivateMintAndPayFeePaymentMethod(fpcAddress, amount, bridgeSecret, bridgeSalt, leafIndex)` as the payment method on the user's first real transaction. The SDK bundles two calls into the setup phase of that single transaction: * `FeeJuice.claim(fpcAddress, amount, bridgeSecret, leafIndex)`: consumes the L1-to-L2 message, crediting Fee Juice to the FPC and emitting the claim nullifier (pending within the same tx). * `PrivateFPC.mint_and_pay_fee(amount, bridgeSalt, leafIndex)`: asserts the (pending) claim nullifier, credits `amount - max_gas_cost` to the user's private balance in the FPC, and marks the FPC as fee payer. The bridged amount itself funds this transaction's fee, so the user doesn't need prior Fee Juice or a sponsor to bootstrap. Any remaining credit (`amount - max_gas_cost`) is available for subsequent transactions via `FPCFeePaymentMethod`. Cold-start exists for users who have no other way to pay fees: the bridged amount itself funds that very first transaction, but `max_gas_cost` of it is consumed in the process. For top-ups (when the user already has another fee mechanism), the three-step `claim → mint → pay` path is preferable because it credits the full `amount` rather than `amount - max_gas_cost`, and it decouples the L1 bridge from the first app transaction (useful for privacy). For protocol details and the full API surface, see the [SDK README](https://github.com/alejoamiras/ecosystem-tooling/blob/main/packages/private-fee-juice/src/ts/README.md) and [PRD](https://github.com/alejoamiras/ecosystem-tooling/blob/main/packages/private-fee-juice/docs/private-product-requirements.md). Because neither `pay_fee` nor `mint_and_pay_fee` makes public cross-contract token calls in setup (they only deduct from the FPC's internal private balance and invoke `set_as_fee_payer`), the [setup-phase allowlist](/developers/testnet/docs/foundational-topics/transactions.md#setup-phase-non-revertible) never blocks these flows. No refund `PrivateFPC.pay_fee()` deducts the full `max_gas_cost` and does not refund unused gas. Read `gasUsed` from a simulation (see [Estimate mana costs](/developers/testnet/docs/aztec-js/how_to_pay_fees.md#estimate-mana-costs)) to right-size your limits. ## Share one FPC address across the ecosystem[​](#share-one-fpc-address-across-the-ecosystem "Direct link to Share one FPC address across the ecosystem") Privacy on Aztec comes from indistinguishability. Private calldata and user identities are hidden. What an observer sees of a private call is its onchain *footprint*: the number of nullifiers and note commitments it emits, any logs, and its public gas usage. They do not learn which contract or which function produced those. Any two transactions whose footprints match are indistinguishable, even if they originated from entirely different contracts or functions, so an anonymity set at the private layer can span many unrelated contract–function pairs. Fee payments add one extra observable: the transaction's fee payer address, set via `set_as_fee_payer()`, is recorded onchain by the protocol. Every fee paid through a given FPC address is therefore publicly tagged with that address. If your app uses its own copy of the private FPC at a unique address, that tag distinguishes your users' fee payments from everyone else's. If every app derives the *same* F
프로젝트로 돌아가기