From ebf8b503cab4d7047acb44579b19141b32d8facd Mon Sep 17 00:00:00 2001 From: Antonio Date: Tue, 28 May 2024 17:18:35 +0100 Subject: [PATCH] Updates to use Foundry keystore (#84) # What :computer: * Updates 101 getting started to setup a keystore * Updates all 101 Foundry tutorials to use keystore in commands to deploy/send txs * Update all commands in Foundry guide with examples of keystore use and PK. * Additionally, fixes a partial not correctly displayed in paymaster page # Why :hand: * Fixes DEVRL676 * To be merged with https://github.com/matter-labs/zksync-contract-templates/pull/14 # Evidence :camera: Include screenshots, screen recordings, or `console` output here demonstrating that your changes work as intended --------- Co-authored-by: Nicolas Villanueva --- content/00.build/10.zksync-101/00.index.md | 29 ++++--------------- .../_foundry_deploy_contract_factory.md | 8 ++--- .../_hello-zksync/_foundry_deploy_contract.md | 4 +-- .../10.zksync-101/_partials/_setup-wallet.md | 2 -- .../_paymasters/_general_paymaster_flow.md | 2 +- .../30.foundry/20.getting-started.md | 22 +++++++++----- content/_partials/_foundry-create-keystore.md | 28 ++++++++++++++++++ 7 files changed, 55 insertions(+), 40 deletions(-) create mode 100644 content/_partials/_foundry-create-keystore.md diff --git a/content/00.build/10.zksync-101/00.index.md b/content/00.build/10.zksync-101/00.index.md index 8239a61f..9e21de0e 100644 --- a/content/00.build/10.zksync-101/00.index.md +++ b/content/00.build/10.zksync-101/00.index.md @@ -64,7 +64,7 @@ zksync-cli dev start Choose "In memory node" to deploy a local zkSync Era node in a Docker container. -The local era node will also include pre-configured rich wallets for use, visit [era-test-node rich wallets](/build/test-and-debug/in-memory-node#pre-configured-rich-wallets) +The local era node will also include pre-configured rich wallets for use, visit [era-test-node rich wallets](/build/test-and-debug/in-memory-node#pre-configured-rich-wallets). Your local zkSync Era node is accessible at **[http://127.0.0.1:8011](http://127.0.0.1:8011/)**, ready for deployment or testing purposes. Leave this terminal open and running as you build your projects. @@ -109,6 +109,10 @@ Quickly set up `foundry-zksync` by following these steps: ./install-foundry-zksync ``` +### Private key setup with Foundry keystore + +:display-partial{ path="_partials/_foundry-create-keystore" } + --- ## Fund your wallet @@ -128,29 +132,6 @@ If you did not set up a local era node for development and plan to use %%zk_test --- -## Configure your wallet in a project - -To deploy contracts, you'll need to securely add your wallet's private key to the project environment. Follow these steps when you set up a new project: - -1. **Extract Your Private Key:** - - - If you are using the local era node, use a private key from the available rich accounts. Otherwise, find your personal wallet's private key. For MetaMask users, here's how to [export your wallet's private key](https://support.metamask.io/hc/en-us/articles/360015289632-How-to-export-an-account-s-private-key). - -2. **Prepare the Environment File:** - - Locate the **`.env-example`** file in your project directory. - - Rename this file to **`.env`**. - -3. **Add Your Private Key:** - - Open the `.env` file and add your private key in the following format: - - ```sh - WALLET_PRIVATE_KEY=your_private_key_here - ``` - - - Replace **`your_private_key_here`** with your actual private key. **If you're using Foundry, make sure your private key starts with `0x`**. - ---- - ## Next Steps You should now have a fully working local environment to build new projects on zkSync! diff --git a/content/00.build/10.zksync-101/_deploy_factory/_foundry_deploy_contract_factory.md b/content/00.build/10.zksync-101/_deploy_factory/_foundry_deploy_contract_factory.md index 916a50f9..6d75699d 100644 --- a/content/00.build/10.zksync-101/_deploy_factory/_foundry_deploy_contract_factory.md +++ b/content/00.build/10.zksync-101/_deploy_factory/_foundry_deploy_contract_factory.md @@ -105,9 +105,9 @@ our new `CrowdfundingFactory`. command: ```bash - forge create src/CrowdfundFactory.sol:CrowdfundingFactory --factory-deps src/CrowdfundingCampaign.sol:CrowdfundingCampaign --rpc-url zkSyncSepoliaTestnet --chain %%zk_testnet_chain_id%% --private-key --zksync + forge create src/CrowdfundFactory.sol:CrowdfundingFactory --factory-deps src/CrowdfundingCampaign.sol:CrowdfundingCampaign --account myKeystore --sender --rpc-url zkSyncSepoliaTestnet --chain %%zk_testnet_chain_id%% --zksync # To deploy the contract on local in-memory node: - # forge script script/DeployFactory.s.sol:DeployFactoryAndCreateCampaign --rpc-url inMemoryNode --broadcast --zksync + # forge script script/DeployFactory.s.sol:DeployFactoryAndCreateCampaign --account myKeystore --sender --rpc-url inMemoryNode --broadcast --zksync ``` Upon a successfull deployment you'll receive details of the deploying address, the contract address, @@ -122,9 +122,9 @@ command: 1. Using the `CrowdfundingFactory` contract address let's deploy our `CrowdfundingCampaign`: ```bash - cast send 0x607545Fd35ef49d7445555ddFa22938fD4Efb219 "createCampaign(uint256)" "1" --rpc-url zkSyncSepoliaTestnet --chain %%zk_testnet_chain_id%% --private-key + cast send 0x607545Fd35ef49d7445555ddFa22938fD4Efb219 "createCampaign(uint256)" "1" --rpc-url zkSyncSepoliaTestnet --chain %%zk_testnet_chain_id%% --account myKeystore --sender # To use the contract factory on local in-memory node: - # cast send 0x607545Fd35ef49d7445555ddFa22938fD4Efb219 "createCampaign(uint256)" "1" --rpc-url inMemoryNode --chain 260 --private-key + # cast send 0x607545Fd35ef49d7445555ddFa22938fD4Efb219 "createCampaign(uint256)" "1" --rpc-url inMemoryNode --chain 260 --account myKeystore --sender ``` Upon a successfull deployment you'll receive details of the transaction, including the diff --git a/content/00.build/10.zksync-101/_hello-zksync/_foundry_deploy_contract.md b/content/00.build/10.zksync-101/_hello-zksync/_foundry_deploy_contract.md index dfdff8fd..cca854a0 100644 --- a/content/00.build/10.zksync-101/_hello-zksync/_foundry_deploy_contract.md +++ b/content/00.build/10.zksync-101/_hello-zksync/_foundry_deploy_contract.md @@ -142,9 +142,9 @@ the transaction broadcast and `vm.stopBroadcast()` to end it, facilitating the a Execute the deployment command. ```bash -forge script script/Deploy.s.sol:DeployCrowdfundContract --rpc-url zkSyncSepoliaTestnet --broadcast --zksync +forge script script/Deploy.s.sol:DeployCrowdfundContract --account myKeystore --sender --rpc-url zkSyncSepoliaTestnet --broadcast --zksync # To deploy the contract on local in-memory node: -# forge script script/Deploy.s.sol:DeployCrowdfundContract --rpc-url inMemoryNode --broadcast --zksync +# forge script script/Deploy.s.sol:DeployCrowdfundContract --account myKeystore --sender --rpc-url inMemoryNode --broadcast --zksync ``` Upon successful deployment, you'll receive output detailing the deployment process, diff --git a/content/00.build/10.zksync-101/_partials/_setup-wallet.md b/content/00.build/10.zksync-101/_partials/_setup-wallet.md index 468fe308..94f55e1b 100644 --- a/content/00.build/10.zksync-101/_partials/_setup-wallet.md +++ b/content/00.build/10.zksync-101/_partials/_setup-wallet.md @@ -6,5 +6,3 @@ Deploying contracts on the %%zk_testnet_name%% requires having testnet ETH. If you're working within the local development environment, you can utilize pre-configured rich wallets and skip this step. For testnet deployments, you should have your wallet funded from the [previous step](/build/zksync-101#fund-your-wallet). - -Ensure your project is configured to [use your wallet via the `.env` file](/build/zksync-101#configure-your-wallet-in-a-project). diff --git a/content/00.build/10.zksync-101/_paymasters/_general_paymaster_flow.md b/content/00.build/10.zksync-101/_paymasters/_general_paymaster_flow.md index a916061e..ccedace9 100644 --- a/content/00.build/10.zksync-101/_paymasters/_general_paymaster_flow.md +++ b/content/00.build/10.zksync-101/_paymasters/_general_paymaster_flow.md @@ -12,7 +12,7 @@ cd contract-paymaster-quickstart ## Set up your wallet -:display-partial{path = /build/zksync-101/_partials/_setup-wallet"} +:display-partial{path = "/build/zksync-101/_partials/_setup-wallet"} --- diff --git a/content/00.build/40.tooling/30.foundry/20.getting-started.md b/content/00.build/40.tooling/30.foundry/20.getting-started.md index d29ed460..2e0d7fd2 100644 --- a/content/00.build/40.tooling/30.foundry/20.getting-started.md +++ b/content/00.build/40.tooling/30.foundry/20.getting-started.md @@ -96,6 +96,10 @@ is_system = false mode = "3" ``` +### Private key setup with Foundry keystore + +:display-partial{ path="_partials/_foundry-create-keystore" } + ## Basic Usage ### Running Tests @@ -136,12 +140,17 @@ forge build --zksync ### Deployment with `forge create --zksync` +::callout{icon="i-heroicons-information-circle" color="blue"} +The following commands make use of Foundry keystore instead of private keys. +[Learn how to create a keystore](#private-key-setup-with-foundry-keystore). +:: + `forge create --zksync` deploys smart contracts to zkSync. **Usage:** ```sh -forge create [OPTIONS] --rpc-url --chain --private-key --zksync +forge create [OPTIONS] --rpc-url --chain --account myKeystore --sender --zksync ``` **Options:** @@ -181,7 +190,7 @@ contract Greeter { ```bash -forge create src/Greeter.sol:Greeter --constructor-args "Hello zkSync" --private-key --rpc-url %%zk_testnet_rpc_url%% --chain %%zk_testnet_chain_id%% --zksync +forge create src/Greeter.sol:Greeter --constructor-args "Hello zkSync" --account myKeystore --sender --rpc-url %%zk_testnet_rpc_url%% --chain %%zk_testnet_chain_id%% --zksync ``` ### Deploying Factory Contracts @@ -226,13 +235,13 @@ forge build --is-system=true --zksync **Deploy `GreeterFactory.sol`:** ```sh -forge create src/GreeterFactory.sol:Factory --factory-deps src/Greeter.sol:Greeter --private-key --rpc-url %%zk_testnet_rpc_url%% --chain %%zk_testnet_chain_id%% --zksync +forge create src/GreeterFactory.sol:Factory --factory-deps src/Greeter.sol:Greeter --account myKeystore --sender --rpc-url %%zk_testnet_rpc_url%% --chain %%zk_testnet_chain_id%% --zksync ``` **Deploy `Greeter.sol` via `GreeterFactory.sol`:** ```sh -cast send "CreateNewGreeter(string)" "zkSync Rules" --private-key --rpc-url %%zk_testnet_rpc_url%% --chain %%zk_testnet_chain_id%% +cast send "CreateNewGreeter(string)" "zkSync Rules" --account myKeystore --sender --rpc-url %%zk_testnet_rpc_url%% --chain %%zk_testnet_chain_id%% ``` **Interact with `Greeter.sol`** @@ -333,14 +342,13 @@ Expected Output: Detailed information about the latest block, including base fee Initiate transactions, such as contract function calls, using `cast`: ```sh -cast send --rpc-url --private-key --chain +cast send --rpc-url --account myKeystore --sender --chain ``` Example: ```sh -cast send 0xe34E488C1B0Fb372Cc4a5d39219261A5a6fc7996 "setGreeting(string)" "Hello, zkSync!" --rpc-url %%zk_testnet_rpc_url%% --private-key --chain %%zk_testnet_chain_id%% +cast send 0xe34E488C1B0Fb372Cc4a5d39219261A5a6fc7996 "setGreeting(string)" "Hello, zkSync!" --rpc-url %%zk_testnet_rpc_url%% --account myKeystore --sender --chain %%zk_testnet_chain_id%% ``` This command calls the `setGreeting` function of a contract, updating the greeting to "Hello, zkSync!". -Replace `` with your actual private key. diff --git a/content/_partials/_foundry-create-keystore.md b/content/_partials/_foundry-create-keystore.md new file mode 100644 index 00000000..e74b5b83 --- /dev/null +++ b/content/_partials/_foundry-create-keystore.md @@ -0,0 +1,28 @@ +--- +title: foundry create keystore +--- + +Follow these steps to securely store your wallet's private key to use it in Foundry projects: + +1. **Extract Your Private Key:** If you are using the local era node, use a private key from the available rich + accounts. Otherwise, find your personal wallet's private key. For MetaMask users, here's how to [export your wallet's + private key](https://support.metamask.io/hc/en-us/articles/360015289632-How-to-export-an-account-s-private-key). + +2. **Create a Foundry keystore:** Create a keystore and import your private key by running + +```bash +cast wallet import myKeystore --interactive +# enter your PK when prompted, provide a password, and copy the returned address +``` + +It'll return an address (keystore address). + +::callout{icon="i-heroicons-information-circle" color="blue"} +Note that the name `myKeystore` is arbitrary and can be updated. For our docs, we've chosen this name for consistency. +If you decide to use another name, be sure to reference it when using `cast`. +:: + +#### Using the keystore + +When running commands that require a private key, like `forge create` or `cast send`, use `--account myKeystore --sender `. This will +require you to enter the keystore password you provided before.