From 72cf03efcfe8e58b0806c4b9050aa5bbea808bb5 Mon Sep 17 00:00:00 2001 From: Sarah Schwartz <58856580+sarahschwartz@users.noreply.github.com> Date: Thu, 12 Sep 2024 18:27:52 -0600 Subject: [PATCH] docs: update webauthn tutorial draft --- code/webauthn/contracts/contracts/Account.sol | 2 + .../frontend/src/pages/create-account.tsx | 4 +- code/webauthn/frontend/src/pages/mint.tsx | 2 +- code/webauthn/frontend/utils/tx.ts | 17 +- code/webauthn/frontend/utils/webauthn.ts | 2 +- content/tutorials/erc20-paymaster/10.index.md | 4 +- .../10.index.md | 17 +- ...tracts.md => 20.building-the-contracts.md} | 99 +++++--- .../30.building-the-frontend.md | 238 ++++++++++++++++++ .../30.frontend.md | 227 ----------------- .../40.completing-the-frontend.md | 211 ++++++++++++++++ .../_dir.yml | 4 +- 12 files changed, 533 insertions(+), 294 deletions(-) rename content/tutorials/signing-transactions-with-webauthn/{20.contracts.md => 20.building-the-contracts.md} (70%) create mode 100644 content/tutorials/signing-transactions-with-webauthn/30.building-the-frontend.md delete mode 100644 content/tutorials/signing-transactions-with-webauthn/30.frontend.md create mode 100644 content/tutorials/signing-transactions-with-webauthn/40.completing-the-frontend.md diff --git a/code/webauthn/contracts/contracts/Account.sol b/code/webauthn/contracts/contracts/Account.sol index c7701ea3..b3dedfff 100644 --- a/code/webauthn/contracts/contracts/Account.sol +++ b/code/webauthn/contracts/contracts/Account.sol @@ -203,9 +203,11 @@ contract Account is IAccount, IERC1271 { _transaction.processPaymasterInput(); } + // ANCHOR: updateR1Owner function updateR1Owner(bytes memory _r1Owner) external onlySelf { r1Owner = _r1Owner; } + // ANCHOR_END: updateR1Owner // ANCHOR: validateWebAuthnSignature function validateWebAuthnSignature(bytes memory webauthnSignature, bytes32 txHash) private view returns (bool valid) { diff --git a/code/webauthn/frontend/src/pages/create-account.tsx b/code/webauthn/frontend/src/pages/create-account.tsx index 41ed1c7f..c080505b 100644 --- a/code/webauthn/frontend/src/pages/create-account.tsx +++ b/code/webauthn/frontend/src/pages/create-account.tsx @@ -4,7 +4,7 @@ import { buttonStyles, AA_FACTORY_ADDRESS, BUTTON_COLORS } from './index'; import { containerStyles, headerStyles } from './register'; import { type Provider, utils, Wallet } from 'zksync-ethers'; import { ethers } from 'ethers'; -import * as factoryAbiJSON from '../../../contracts/artifacts-zk/contracts/AAFactory.sol/AAFactory.json'; +import factoryAbiJSON from '../../../contracts/artifacts-zk/contracts/AAFactory.sol/AAFactory.json'; import { getPaymasterOverrides } from '../../utils/tx'; import { useAccount, useSetAccount } from '@/hooks/useAccount'; import { @@ -36,7 +36,7 @@ export default function CreateAccount({ provider }: { provider: Provider }) { async function deployNewAccount() { const owner = Wallet.createRandom().connect(provider); const aaFactory = new ethers.Contract(AA_FACTORY_ADDRESS, factoryAbiJSON.abi, owner); - const salt = '0x0000000000000000000000000000000000000000000000000000000000000000'; + const salt = ethers.utils.hexlify(ethers.utils.randomBytes(32)); const overrides = await getPaymasterOverrides(); const tx = await aaFactory.deployAccount(salt, owner.address, overrides); await tx.wait(); diff --git a/code/webauthn/frontend/src/pages/mint.tsx b/code/webauthn/frontend/src/pages/mint.tsx index a3b33609..4ef1b531 100644 --- a/code/webauthn/frontend/src/pages/mint.tsx +++ b/code/webauthn/frontend/src/pages/mint.tsx @@ -7,7 +7,7 @@ import { Layout } from '../components/Layout'; import { buttonStyles } from '.'; import { containerStyles } from './register'; import { ethers } from 'ethers'; -import * as NFT_ABI_JSON from '../../../contracts/artifacts-zk/contracts/MyNFT.sol/MyNFT.json'; +import NFT_ABI_JSON from '../../../contracts/artifacts-zk/contracts/MyNFT.sol/MyNFT.json'; import { BUTTON_COLORS, NFT_CONTRACT_ADDRESS } from '@/pages/index'; import { useAccount } from '@/hooks/useAccount'; diff --git a/code/webauthn/frontend/utils/tx.ts b/code/webauthn/frontend/utils/tx.ts index 5c664ebb..46209003 100644 --- a/code/webauthn/frontend/utils/tx.ts +++ b/code/webauthn/frontend/utils/tx.ts @@ -1,7 +1,7 @@ import { DEFAULT_GAS_PER_PUBDATA_LIMIT, getPaymasterParams } from 'zksync-ethers/build/utils'; import { EIP712Signer, type Wallet, type Provider, utils, type types } from 'zksync-ethers'; import { ethers } from 'ethers'; -import * as accountAbiJSON from '../../contracts/artifacts-zk/contracts/Account.sol/Account.json'; +import accountAbiJSON from '../../contracts/artifacts-zk/contracts/Account.sol/Account.json'; import { PAYMASTER_ADDRESS } from '@/pages/index'; export async function getTransaction(to: string, from: string, value: string, data: string, provider: Provider) { @@ -42,20 +42,7 @@ export async function registerKeyInAccount(pubKey: string, account: string, prov const contract = new ethers.Contract(account, accountAbiJSON.abi, provider); const data = contract.interface.encodeFunctionData('updateR1Owner', [pubKey]); const transferAmount = '0'; - const overrides = await getPaymasterOverrides(); - - const registerTx = { - to: account, - from: account, - chainId: (await provider.getNetwork()).chainId, - nonce: await provider.getTransactionCount(account), - type: 113, - customData: overrides.customData, - value: ethers.utils.parseEther(transferAmount), - gasPrice: await provider.getGasPrice(), - gasLimit: BigInt(2000000000), - data, - }; + const registerTx = await getTransaction(account, account, transferAmount, data, provider); const signedTxHash = EIP712Signer.getSignedDigest(registerTx); const signingKey: ethers.utils.SigningKey = new ethers.utils.SigningKey(wallet.privateKey); const walletSignature = signingKey.signDigest(signedTxHash); diff --git a/code/webauthn/frontend/utils/webauthn.ts b/code/webauthn/frontend/utils/webauthn.ts index 4ec9234b..1672826c 100644 --- a/code/webauthn/frontend/utils/webauthn.ts +++ b/code/webauthn/frontend/utils/webauthn.ts @@ -4,7 +4,7 @@ import { defaultAbiCoder } from 'ethers/lib/utils'; import { EIP712Signer, type Provider, utils } from 'zksync-ethers'; import type { AuthenticatorAssertionResponseJSON } from '@simplewebauthn/types'; import { BigNumber } from 'ethers'; -import type { TransactionRequest } from 'zksync-ethers/build/types'; +import type { TransactionRequest } from 'zksync-ethers/src/types'; import * as cbor from 'cbor'; export async function authenticate(challenge: string) { diff --git a/content/tutorials/erc20-paymaster/10.index.md b/content/tutorials/erc20-paymaster/10.index.md index 67a4d5e1..c40dbb25 100644 --- a/content/tutorials/erc20-paymaster/10.index.md +++ b/content/tutorials/erc20-paymaster/10.index.md @@ -596,7 +596,7 @@ Make sure you delete the `artifacts-zk` and `cache-zk` folders before recompilin paymasterBalance = await provider.getBalance(PAYMASTER_ADDRESS); console.log(`Paymaster ETH balance is now ${paymasterBalance.toString()}`); - console.log(`ERC20 token balance of the the wallet after mint: ${await wallet.getBalance(TOKEN_ADDRESS)}`); + console.log(`ERC20 token balance of the wallet after mint: ${await wallet.getBalance(TOKEN_ADDRESS)}`); } ``` @@ -628,7 +628,7 @@ Make sure you delete the `artifacts-zk` and `cache-zk` folders before recompilin Minting 5 tokens for the wallet via paymaster... Paymaster ERC20 token balance is now 1 Paymaster ETH balance is now 59650952750000000 - ERC20 token balance of the the wallet after mint: 7 + ERC20 token balance of the wallet after mint: 7 ``` The wallet had 3 tokens after running the deployment script and, after sending the transaction to `mint` 5 more tokens, diff --git a/content/tutorials/signing-transactions-with-webauthn/10.index.md b/content/tutorials/signing-transactions-with-webauthn/10.index.md index 4450082e..fb248b23 100644 --- a/content/tutorials/signing-transactions-with-webauthn/10.index.md +++ b/content/tutorials/signing-transactions-with-webauthn/10.index.md @@ -7,6 +7,7 @@ description: Learn how to sign transactions with WebAuthn - Make sure your machine satisfies the [system requirements](https://github.com/matter-labs/era-compiler-solidity/tree/main#system-requirements). +- You should have the latest version of Google Chrome installed. - You should have [Node.js](https://nodejs.org/en/download) installed. - You should have [`era_test_node`](https://docs.zksync.io/build/test-and-debug/in-memory-node#install-and-set-up-era_test_node) installed. - If you aren't familiar with paymasters and smart accounts on ZKsync Era, check out the @@ -22,13 +23,12 @@ To break this down, the app will allow users to: - Deploy a new instance of a smart account. - Create a new WebAuthn passkey. - Register the WebAuthn passkey as an authorized user of the smart account. -- Use WebAuthn to Transfer ETH. +- Use WebAuthn to transfer ETH. - Use WebAuthn to mint an NFT. - Have gasless transactions. -