Skip to content
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
1 change: 1 addition & 0 deletions packages/viem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ main().catch((error) => {
```

2. Start the Anvil node in one shell:

- Install [Foundry](https://book.getfoundry.sh/getting-started/installation) & Anvil if you haven't done so already
- Add Foundry to your `$PATH`
```bash
Expand Down
4 changes: 3 additions & 1 deletion packages/viem/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
"@turnkey/sdk-browser": "workspace:*",
"@turnkey/sdk-server": "workspace:*",
"@turnkey/core": "workspace:*",
"cross-fetch": "^4.0.0"
"cross-fetch": "^4.0.0",
"@zerodev/ecdsa-validator": "^5.4.8",
"@zerodev/sdk": "^5.4.32"
},
"devDependencies": {
"@types/jest": "^29.5.3",
Expand Down
96 changes: 96 additions & 0 deletions packages/viem/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
hexToBytes,
parseTransaction,
serializeTypedData,
createPublicClient,
http,
Chain,
} from "viem";
import {
SignAuthorizationReturnType,
Expand All @@ -23,6 +26,16 @@ import type {
TypedData,
} from "viem";
import { secp256k1 } from "@noble/curves/secp256k1";
import { getEntryPoint, KERNEL_V3_3 } from "@zerodev/sdk/constants";
import {
create7702KernelAccount,
create7702KernelAccountClient,
} from "@zerodev/ecdsa-validator";
import {
createZeroDevPaymasterClient,
getUserOperationGasPrice,
KernelAccountClient,
} from "@zerodev/sdk";

import {
assertNonNull,
Expand Down Expand Up @@ -718,3 +731,86 @@ export function isTurnkeyActivityError(error: any) {
})
);
}

export async function initSmartAccount(
client:
| TurnkeyClient
| TurnkeyBrowserClient
| TurnkeyServerClient
| TurnkeySDKClientBase,
organizationId: string,
signWith: string, // your Ethereum EOA
smartAccountParams: {
chain: Chain;
paymasterUrl: string;
sponsor?: boolean;
},
): Promise<KernelAccountClient> {
const { paymasterUrl, sponsor, chain } = smartAccountParams;
const entryPoint = getEntryPoint("0.7");
const kernelVersion = KERNEL_V3_3;

const publicClient = createPublicClient({
transport: http(),
chain,
});

const turnkeyAccount = await createAccount({
client,
organizationId,
signWith,
});

const account = await create7702KernelAccount(publicClient, {
signer: turnkeyAccount,
entryPoint,
kernelVersion,
});

const paymasterClient = createZeroDevPaymasterClient({
chain,
transport: http(paymasterUrl),
});

const kernelClient = create7702KernelAccountClient({
account,
chain,
bundlerTransport: http(paymasterUrl),
userOperation: {
estimateFeesPerGas: async ({ bundlerClient }) => {
return getUserOperationGasPrice(bundlerClient);
},
},
paymaster: sponsor
? {
getPaymasterData: (userOperation) => {
return paymasterClient.sponsorUserOperation({
userOperation,
});
},
}
: {},
client: publicClient,
});

return kernelClient;
}

export async function signWithSmartAccount(
kernelClient: KernelAccountClient,
calls: readonly {
to: `0x${string}`;
data?: `0x${string}` | undefined;
value?: bigint | undefined;
}[],
): Promise<KernelAccountClient> {
const userOpHash = await kernelClient.sendUserOperation({
callData: await kernelClient.account.encodeCalls(calls),
});

const { receipt } = await kernelClient.waitForUserOperationReceipt({
hash: userOpHash,
});

return receipt;
}
Loading