Skip to content

Commit

Permalink
Updates to use Foundry keystore (#84)
Browse files Browse the repository at this point in the history
# What 💻 
* 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 ✋
* Fixes DEVRL676
* To be merged with
matter-labs/zksync-contract-templates#14

# Evidence 📷
Include screenshots, screen recordings, or `console` output here
demonstrating that your changes work as intended

<!-- All sections below are optional. You can uncomment any section
applicable to your Pull Request. -->

<!-- # Notes 📝
* Any notes/thoughts that the reviewers should know prior to reviewing
the code? -->

---------

Co-authored-by: Nicolas Villanueva <[email protected]>
  • Loading branch information
uF4No and MexicanAce authored May 28, 2024
1 parent a3976c8 commit ebf8b50
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 40 deletions.
29 changes: 5 additions & 24 deletions content/00.build/10.zksync-101/00.index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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:**
<!-- markdownlint-disable-next-line MD013 -->
- 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!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <YOUR-PRIVATE-KEY> --zksync
forge create src/CrowdfundFactory.sol:CrowdfundingFactory --factory-deps src/CrowdfundingCampaign.sol:CrowdfundingCampaign --account myKeystore --sender <KEYSTORE_ADDRESS> --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 <KEYSTORE_ADDRESS> --rpc-url inMemoryNode --broadcast --zksync
```

Upon a successfull deployment you'll receive details of the deploying address, the contract address,
Expand All @@ -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 <YOUR-PRIVATE-KEY>
cast send 0x607545Fd35ef49d7445555ddFa22938fD4Efb219 "createCampaign(uint256)" "1" --rpc-url zkSyncSepoliaTestnet --chain %%zk_testnet_chain_id%% --account myKeystore --sender <KEYSTORE_ADDRESS>
# To use the contract factory on local in-memory node:
# cast send 0x607545Fd35ef49d7445555ddFa22938fD4Efb219 "createCampaign(uint256)" "1" --rpc-url inMemoryNode --chain 260 --private-key <YOUR-PRIVATE-KEY>
# cast send 0x607545Fd35ef49d7445555ddFa22938fD4Efb219 "createCampaign(uint256)" "1" --rpc-url inMemoryNode --chain 260 --account myKeystore --sender <KEYSTORE_ADDRESS>
```

Upon a successfull deployment you'll receive details of the transaction, including the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <KEYSTORE_ADDRESS> --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 <KEYSTORE_ADDRESS> --rpc-url inMemoryNode --broadcast --zksync
```

Upon successful deployment, you'll receive output detailing the deployment process,
Expand Down
2 changes: 0 additions & 2 deletions content/00.build/10.zksync-101/_partials/_setup-wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Original file line number Diff line number Diff line change
Expand Up @@ -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"}

---

Expand Down
22 changes: 15 additions & 7 deletions content/00.build/40.tooling/30.foundry/20.getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <CONTRACT> [OPTIONS] --rpc-url <RPC-URL> --chain <CHAIN-ID> --private-key <PRIVATE-KEY> --zksync
forge create <CONTRACT> [OPTIONS] --rpc-url <RPC-URL> --chain <CHAIN-ID> --account myKeystore --sender <KEYSTORE_ADDRESS> --zksync
```

**Options:**
Expand Down Expand Up @@ -181,7 +190,7 @@ contract Greeter {
</details>

```bash
forge create src/Greeter.sol:Greeter --constructor-args "Hello zkSync" --private-key <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 <KEYSTORE_ADDRESS> --rpc-url %%zk_testnet_rpc_url%% --chain %%zk_testnet_chain_id%% --zksync
```

### Deploying Factory Contracts
Expand Down Expand Up @@ -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 <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 <KEYSTORE_ADDRESS> --rpc-url %%zk_testnet_rpc_url%% --chain %%zk_testnet_chain_id%% --zksync
```

**Deploy `Greeter.sol` via `GreeterFactory.sol`:**

```sh
cast send <FACTORY_ADDRESS> "CreateNewGreeter(string)" "zkSync Rules" --private-key <PRIVATE_KEY> --rpc-url %%zk_testnet_rpc_url%% --chain %%zk_testnet_chain_id%%
cast send <FACTORY_ADDRESS> "CreateNewGreeter(string)" "zkSync Rules" --account myKeystore --sender <KEYSTORE_ADDRESS> --rpc-url %%zk_testnet_rpc_url%% --chain %%zk_testnet_chain_id%%
```

**Interact with `Greeter.sol`**
Expand Down Expand Up @@ -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 <CONTRACT_ADDRESS> <FUNCTION_SIGNATURE> <ARGUMENTS> --rpc-url <RPC-URL> --private-key <PRIVATE-KEY> --chain <CHAIN-ID>
cast send <CONTRACT_ADDRESS> <FUNCTION_SIGNATURE> <ARGUMENTS> --rpc-url <RPC-URL> --account myKeystore --sender <KEYSTORE_ADDRESS> --chain <CHAIN-ID>
```

Example:

```sh
cast send 0xe34E488C1B0Fb372Cc4a5d39219261A5a6fc7996 "setGreeting(string)" "Hello, zkSync!" --rpc-url %%zk_testnet_rpc_url%% --private-key <PRIVATE-KEY> --chain %%zk_testnet_chain_id%%
cast send 0xe34E488C1B0Fb372Cc4a5d39219261A5a6fc7996 "setGreeting(string)" "Hello, zkSync!" --rpc-url %%zk_testnet_rpc_url%% --account myKeystore --sender <KEYSTORE_ADDRESS> --chain %%zk_testnet_chain_id%%
```

This command calls the `setGreeting` function of a contract, updating the greeting to "Hello, zkSync!".
Replace `<PRIVATE-KEY>` with your actual private key.
28 changes: 28 additions & 0 deletions content/_partials/_foundry-create-keystore.md
Original file line number Diff line number Diff line change
@@ -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 <KEYSTORE_ADDRESS>`. This will
require you to enter the keystore password you provided before.

0 comments on commit ebf8b50

Please sign in to comment.