Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Andrew/aa zerodev #405

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions examples/with-zerodev-aa/.env.local.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
API_PUBLIC_KEY="<Turnkey API Public Key (that starts with 02 or 03)>"
API_PRIVATE_KEY="<Turnkey API Private Key>"
BASE_URL="https://api.turnkey.com"
ORGANIZATION_ID="<Turnkey organization ID>"
SIGN_WITH="<Turnkey Wallet Account Address, Private Key Address, or Private Key ID>" # if blank, we will create a wallet for you
ZERODEV_BUNDLER_RPC="<Zerodev Bundler RPC URL>" # see https://dashboard.zerodev.app/
ZERODEV_PAYMASTER_RPC="<Zerodev Paymaster RPC URL>" # see https://dashboard.zerodev.app/
108 changes: 108 additions & 0 deletions examples/with-zerodev-aa/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# Example: `with-biconomy-aa`

This example shows how to construct and broadcast a transaction using Turnkey with [`Ethers`](https://docs.ethers.org/v6/api/providers/#Signer) (WIP), [`Viem`](https://viem.sh/docs/clients/wallet.html), and [`Zerodev`](https://docs.zerodev.app/sdk/getting-started/quickstart).

If you want to see a demo with passkeys, it's coming 🔜™️!

## Getting started

### 1/ Cloning the example

Make sure you have `Node.js` installed locally; we recommend using Node v18+.

```bash
$ git clone https://github.com/tkhq/sdk
$ cd sdk/
$ corepack enable # Install `pnpm`
$ pnpm install -r # Install dependencies
$ pnpm run build-all # Compile source code
$ cd examples/with-zerodev-aa/
```

### 2a/ Setting up Turnkey

The first step is to set up your Turnkey organization and account. By following the [Quickstart](https://docs.turnkey.com/getting-started/quickstart) guide, you should have:

- A public/private API key pair for Turnkey
- An organization ID
- A Turnkey wallet account (address), private key address, or a private key ID

### 2b/ Setting up Zerodev

The next step is to navigate to Zerodev to create an account. Visit the [Zerodev Dashboard](https://dashboard.zerodev.app/) to create a your paymaster and find the following:

- Bundler RPC
- Paymaster RPC

Once you've gathered these values, add them to a new `.env.local` file. Notice that your private key should be securely managed and **_never_** be committed to git.

```bash
$ cp .env.local.example .env.local
```

Now open `.env.local` and add the missing environment variables:

- `API_PUBLIC_KEY`
- `API_PRIVATE_KEY`
- `BASE_URL`
- `ORGANIZATION_ID`
- `SIGN_WITH` -- a Turnkey wallet account address, private key address, or private key ID. If you leave this blank, we'll create a wallet for you.
- `ZERODEV_BUNDLER_RPC`
- `ZERODEV_PAYMASTER_RPC`

### 3/ Running the scripts

Note: there are two included — one for Viem and another for Ethers (WIP). See `package.json` for more details.

These scripts construct transactions via Turnkey and broadcast them via Infura. If the scripts exit because your account isn't funded, you can request funds on https://sepoliafaucet.com/ or https://faucet.paradigm.xyz/.

#### Viem

```bash
$ pnpm start-viem
```

This script will do the following:

1. instantiate a Turnkey Viem wallet client
2. instantiate a Viem public client (to be used to fetch onchain data)
3. create a Zerodev Kernel Client (with Paymaster support)
4. create a Zerodev Bundler Client
5. send ETH (via type 2 EIP-1559 transaction)

See the following for a sample output:

```
Network:
sepolia (chain ID 11155111)

Signer address:
0xbcb87Df08A6a4409B28a85Af41f32Da75bD442e9

Smart wallet address:
0xDbf9297467030aFf40E0D9cd43D088696596B841

Balance:
0.0099995 Ether

Transaction count:
1

Nonce:
913479994650515257524606220465835134743662536739504622017003723935449093

✔ Amount to send (wei). Default to 0.0000001 ETH … 100000000000
✔ Destination address (default to TKHQ warchest) … 0x08d2b0a37F869FF76BACB5Bab3278E26ab7067B7
Sent 0.0000001 Ether to 0x08d2b0a37F869FF76BACB5Bab3278E26ab7067B7:
https://sepolia.etherscan.io/tx/0xe70fd04de98bc47595fba282f172ff0fa75949a7b0e16647cf423e8db7fb9ed1

Bundle can be found here:
https://jiffyscan.xyz/bundle/0xe70fd04de98bc47595fba282f172ff0fa75949a7b0e16647cf423e8db7fb9ed1?network=sepolia&pageNo=0&pageSize=10

User Ops can be found here:
https://jiffyscan.xyz/userOpHash/0xe70fd04de98bc47595fba282f172ff0fa75949a7b0e16647cf423e8db7fb9ed1?network=sepolia&pageNo=0&pageSize=10
```

#### Ethers

🚧 WIP! Come back soon🔜
27 changes: 27 additions & 0 deletions examples/with-zerodev-aa/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@turnkey/example-with-biconomy-aa",
"version": "0.1.0",
"private": true,
"scripts": {
"start-viem": "tsx src/viem.ts",
"clean": "rimraf ./dist ./.cache",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@turnkey/ethers": "workspace:*",
"@turnkey/sdk-server": "workspace:*",
"@turnkey/viem": "workspace:*",
"@zerodev/ecdsa-validator": "^5.3.3",
"@zerodev/sdk": "^5.3.22",
"dotenv": "^16.0.3",
"ethers": "^6.10.0",
"permissionless": "^0.1.45",
"prompts": "^2.4.2",
"viem": "^2.18.0"
},
"devDependencies": {
"@types/node": "^22.7.7",
"@types/prompts": "^2.4.2",
"tslib": "^2.8.0"
}
}
50 changes: 50 additions & 0 deletions examples/with-zerodev-aa/src/createNewWallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Turnkey as TurnkeySDKServer } from "@turnkey/sdk-server";

import * as crypto from "crypto";
import { refineNonNull } from "./util";

export async function createNewWallet() {
console.log("creating a new wallet on Turnkey...\n");

const walletName = `ETH Wallet ${crypto.randomBytes(2).toString("hex")}`;

try {
const turnkeyClient = new TurnkeySDKServer({
apiBaseUrl: "https://api.turnkey.com",
apiPublicKey: process.env.API_PUBLIC_KEY!,
apiPrivateKey: process.env.API_PRIVATE_KEY!,
defaultOrganizationId: process.env.ORGANIZATION_ID!,
});

const { walletId, addresses } = await turnkeyClient
.apiClient()
.createWallet({
walletName,
accounts: [
{
curve: "CURVE_SECP256K1",
pathFormat: "PATH_FORMAT_BIP32",
path: "m/44'/60'/0'/0/0",
addressFormat: "ADDRESS_FORMAT_ETHEREUM",
},
],
});

const newWalletId = refineNonNull(walletId);
const address = refineNonNull(addresses[0]);

// Success!
console.log(
[
`New Ethereum wallet created!`,
`- Name: ${walletName}`,
`- Wallet ID: ${newWalletId}`,
`- Address: ${address}`,
``,
"Now you can take the address, put it in `.env.local` (`SIGN_WITH=<address>`), then re-run the script.",
].join("\n")
);
} catch (error) {
throw new Error("Failed to create a new Ethereum wallet: " + error);
}
}
24 changes: 24 additions & 0 deletions examples/with-zerodev-aa/src/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export function print(header: string, body: string): void {
console.log(`${header}\n\t${body}\n`);
}

export function assertEqual<T>(left: T, right: T) {
if (left !== right) {
throw new Error(`${JSON.stringify(left)} !== ${JSON.stringify(right)}`);
}
}

export function refineNonNull<T>(
input: T | null | undefined,
errorMessage?: string
): T {
if (input == null) {
throw new Error(errorMessage ?? `Unexpected ${JSON.stringify(input)}`);
}

return input;
}

export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
Loading
Loading