Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ node_modules
#Buidler files
cache
artifacts

# env variables
.env
52 changes: 42 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
# Example of onchain smart contract wallet generation that is not deployed until needed.
# zkSync Create2 example

Example of onchain smart contract wallet generation that is not deployed until needed.


## Demo steps

1. Create a `.env` file and set 2 environment variables:

```
ETH_PRIVATE_KEY=0x020......... (Make sure it's prefixed with 0x)

INFURA_ID=25...........
```

* `ETH_PRIVATE_KEY` env variable to private key of the account with ropsten ethereum.
* `INFURA_ID` the Project ID of an eth project: from [Infura](https://infura.io/dashboard/ethereum)

2. Run:

```sh
$ yarn install
$ yarn run-example
```

Which runs `index.js` that:

* Generates wallet for `contract/Counter.sol` contract with custom salt arguments and deployer deployed at address stored in `factoryAddress.json`
* Deposits ETH to this account
* Sets signing key for this account
* Makes transfer to check if account is unlocked

3. To deploy contract using CREATE2 run:

```sh
$ yarn deploy-with-create2 $SALT
```

where `$SALT` is salt that was printed on step 2


## Built with

* zkSync branch: https://github.com/matter-labs/zksync/tree/create2-deployment
* deployed to: https://ropsten-beta.zkscan.io/
* zksync.js: 0.8.2-beta


1. Set `ETH_PRIVATE_KEY` env variable to private key of the account with ropsten ethereum.
2. Run `yarn run-example` to run `index.js` script that.
* Generates wallet for `contract/Counter.sol` contract with custom salt arguments and deployer deployed at address stored in `factoryAddress.json`
* Deposits ETH to this account
* Sets signing key for this account
* Makes transfer to check if account is unlocked
3. To deploy contract using CREATE2 run `yarn deploy-with-create2 $SALT` where `$SALT` is salt that was printed on step 2
4 changes: 3 additions & 1 deletion buidler.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const fs = require("fs");

require('dotenv').config();

usePlugin("@nomiclabs/buidler-waffle");

task("deploy-create2", "Deploys contract with the given salt")
Expand All @@ -25,7 +27,7 @@ module.exports = {
defaultNetwork: "ropsten",
networks: {
ropsten: {
url: "https://ropsten.infura.io/v3/84842078b09946638c03157f83405213",
url: `https://ropsten.infura.io/v3/${process.env.INFURA_ID}`,
accounts: [process.env.ETH_PRIVATE_KEY],
}
},
Expand Down
21 changes: 11 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const ethers = require("ethers");
const zksync = require("zksync");

const crypto = require("crypto");

require('dotenv').config();

async function createDepositWallet() {
const ethProvider = new ethers.providers.getDefaultProvider("ropsten");
const ethSigner = new ethers.Wallet(process.env.ETH_PRIVATE_KEY).connect(ethProvider);
const zksyncProvider = await zksync.getDefaultProvider("ropsten-beta")
const zksyncProvider = await zksync.getDefaultProvider("ropsten-beta");
return zksync.wallet.Wallet.fromEthSigner(ethSigner, zksyncProvider);
}

Expand All @@ -30,15 +31,15 @@ async function main() {
saltArg,
};

const zksyncProvider = await zksync.getDefaultProvider("ropsten-beta")
const zksyncProvider = await zksync.getDefaultProvider("ropsten-beta");
const ethSigner = new zksync.create2wallet.Create2WalletSigner(signer.pubKeyHash(), create2Auth);
console.log("Generated new account");
console.log("address:",ethSigner.address);
console.log("salt:",ethSigner.salt);
console.log("============ Generated new account ============");
console.log("address: ", ethSigner.address);
console.log("salt: ", ethSigner.salt);

const zksWallet = await zksync.wallet.Wallet.fromEthSigner(ethSigner, zksyncProvider, signer, null, { verificationMethod: "ERC-1271", isSignedMsgPrefixed: true});

console.log("Depositing to new account");
console.log("============ Depositing to new account ============");
const depositWallet = await createDepositWallet();
const depositHandle = await depositWallet.depositToSyncFromEthereum({
depositTo: zksWallet.address(),
Expand All @@ -48,14 +49,14 @@ async function main() {
});
await depositHandle.awaitReceipt();

console.log("Setting ChangePubkey for Create2Walelt");
console.log("============ Setting ChangePubkey for Create2Wallet ============");
const chpk = await zksWallet.setSigningKey({
feeToken: "ETH",
changePubkeyType: "Create2Contract"
});
await chpk.awaitReceipt();

console.log("Making transfer");
console.log("============ Making transfer ============");
const transfer = await zksWallet.syncTransfer({
to: zksWallet.address(),
token: "ETH",
Expand All @@ -69,6 +70,6 @@ async function main() {
main()
.then(() => process.exit(0))
.catch((e) => {
console.error("Error: ",e);
console.error("Error: ", e);
process.exit(1)
})
Loading