-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72cf03e
commit a0ccac3
Showing
4 changed files
with
48 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,17 +19,25 @@ We can start by creating the contracts for the smart account, paymaster, and NFT | |
zksync-cli create contracts --template hardhat_solidity --project contracts | ||
``` | ||
|
||
Enter the private key for a pre-configured rich wallet: | ||
The CLI will prompt you to enter a private key for deploying. | ||
Enter the private key below for a pre-configured rich wallet: | ||
|
||
```shell | ||
? Private key of the wallet responsible for deploying contracts (optional) | ||
0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110 | ||
``` | ||
|
||
Once that is done, move into the `contracts` folders and delete the template contracts, scripts, and tests: | ||
Once that is done, move into the `contracts` folders and | ||
install the dependency below: | ||
|
||
```shell | ||
cd contracts | ||
npm install -D @matterlabs/[email protected] | ||
``` | ||
|
||
Then, delete the template contracts, scripts, and tests: | ||
|
||
```shell | ||
rm -rf ./contracts/* | ||
rm -rf ./deploy/* | ||
rm -rf ./test/* | ||
|
@@ -68,7 +76,7 @@ This is a basic paymaster contract that allows us to sponsor transactions for us | |
|
||
### NFT Contract | ||
|
||
Create a file in the `contracts/contracts` folder called `MyNFT.sol`. | ||
Create another file in the `contracts/contracts` folder called `MyNFT.sol`. | ||
|
||
```shell | ||
touch contracts/MyNFT.sol | ||
|
@@ -129,17 +137,15 @@ The smart account contract is where we perform the WebAuthn signature validation | |
|
||
### Understanding WebAuthn Signature Validation | ||
|
||
The smart contract account implements a way to validate signatures created with WebAuthn. | ||
|
||
The `_validateTransaction` function first checks to see if there is a valid secp256k1 signature from the account owner, | ||
In the `Account.sol` contract, the `_validateTransaction` function checks to see if there is a valid secp256k1 signature from the account owner, | ||
just like the example in the [Native AA Multisig](https://code.zksync.io/tutorials/native-aa-multisig) tutorial. | ||
If the transaction is not signed by the standard account owner, the next check is to see if the transaction contains a valid WebAuthn signature. | ||
|
||
```solidity [contracts/Account.sol] | ||
:code-import{filePath="webauthn/contracts/contracts/Account.sol:_validateTransaction"} | ||
``` | ||
|
||
The contract contains a state variable `r1Owner` to store the public key of the WebAuthn account that the owner wants to use. | ||
The contract contains a state variable `r1Owner` to store the public key of the WebAuthn account that the owner wants to authorize. | ||
The `updateR1Owner` function can update the value of the `r1Owner`. | ||
|
||
```solidity [contracts/Account.sol] | ||
|
@@ -150,14 +156,16 @@ The `updateR1Owner` function can update the value of the `r1Owner`. | |
:code-import{filePath="webauthn/contracts/contracts/Account.sol:updateR1Owner"} | ||
``` | ||
|
||
To validate the signature, the `_validateWebAuthnSignature` first extracts the `authenticatorData`, `clientData`, and `rs` values from the decoded signature. | ||
To validate the signature, the `_validateWebAuthnSignature` function | ||
first extracts the `authenticatorData`, `clientData`, and `rs` values from the decoded signature. | ||
|
||
It's important to note that the WebAuthn authentication process doesn't just sign the transaction data we send as a challenge. | ||
The WebAuthn authentication process returns three pieces of information that we need for the validation process: | ||
the WebAuthn signature, the `authenticatorData`, and the `clientData`. | ||
|
||
The `extractChallengeFromClientData` extracts the WebAuthn challenge from the `clientData`, and checks to see if it matches the transaction hash. | ||
This means that the exact transaction data that is submitted to the contract matches the transaction data signed by the WebAuthn account. | ||
The `extractChallengeFromClientData` function extracts the WebAuthn challenge from the `clientData`, and checks to see if it matches the transaction hash. | ||
If the values are equal, | ||
this means that the exact transaction data that is submitted to the contract matches the transaction data signed by the WebAuthn account. | ||
|
||
Next, the `_validateWebAuthnSignature` function checks for the malleability of the signature, | ||
and checks to see if the WebAuthn user flags are set. | ||
|
@@ -189,18 +197,18 @@ Open a new terminal and start a local in-memory node with `era_test_node`: | |
era_test_node run | ||
``` | ||
|
||
Next, modify the `defaultNetwork` in the `hardhat.config.ts` file to deploy to the local in-memory node, and change the version of Solidity to `0.8.20`: | ||
Next, replace your `hardhat.config.ts` file with the file below: | ||
|
||
```ts | ||
defaultNetwork: "inMemoryNode", | ||
``` | ||
::drop-panel | ||
::panel{label="hardhat.config.ts"} | ||
|
||
```ts | ||
solidity: { | ||
version: "0.8.20", | ||
}, | ||
```solidity [hardhat.config.ts] | ||
:code-import{filePath="webauthn/contracts/hardhat.config.ts"} | ||
``` | ||
|
||
:: | ||
:: | ||
|
||
### Adding the Deploy Script | ||
|
||
Create a new file inside the `deploy` folder called `deploy.ts`: | ||
|
@@ -209,11 +217,7 @@ Create a new file inside the `deploy` folder called `deploy.ts`: | |
touch deploy/deploy.ts | ||
``` | ||
|
||
Copy and paste the code below. This script will: | ||
|
||
- deploy the `AAFactory` contract | ||
- deploy the NFT contract and mint an NFT | ||
- deploy the paymaster contract and send funds to it from the preconfigured rich account. | ||
Copy and paste the code below. | ||
|
||
::drop-panel | ||
::panel{label="deploy.ts"} | ||
|
@@ -225,6 +229,12 @@ Copy and paste the code below. This script will: | |
:: | ||
:: | ||
|
||
This script will: | ||
|
||
- deploy the `AAFactory` contract | ||
- deploy the NFT contract and mint an NFT | ||
- deploy the paymaster contract and send funds to it from the pre-configured rich account. | ||
|
||
### Running the Deploy Script | ||
|
||
Finally, compile and deploy the contracts with: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters