|
| 1 | +--- |
| 2 | +description: Learn how to use Externally Owned Account (EOA) with MetaMask Smart Accounts. |
| 3 | +sidebar_label: EOA (e.g. MetaMask) |
| 4 | +keywords: [metamask, smart account, signer, metamask smart account] |
| 5 | +--- |
| 6 | + |
| 7 | +# Use Externally Owned Account (EOA) with MetaMask Smart Accounts |
| 8 | + |
| 9 | +Externally Owned Accounts (EOAs) are accounts controlled by a user’s private key (paired with a public address) and are typically accessed through wallet apps like MetaMask. MetaMask Smart Accounts is signer-agnostic, so |
| 10 | +you can use an EOA as the signer. |
| 11 | + |
| 12 | +:::info |
| 13 | +This guide supports React and React-based frameworks. For Vue, see [Wagmi docs](https://wagmi.sh/vue/getting-started). |
| 14 | +::: |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +- Install [Node.js](https://nodejs.org/en/blog/release/v18.18.0) v18 or later |
| 19 | +- Install [Yarn](https://yarnpkg.com/), |
| 20 | + [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm), or another package manager |
| 21 | + |
| 22 | +## Steps |
| 23 | + |
| 24 | +### 1. Install dependencies |
| 25 | + |
| 26 | +Install the [Smart Accounts Kit](https://www.npmjs.com/package/@metamask/smart-accounts-kit) and other dependencies in your project: |
| 27 | + |
| 28 | +```bash npm2yarn |
| 29 | +npm install @metamask/smart-accounts-kit wagmi @tanstack/react-query viem |
| 30 | +``` |
| 31 | + |
| 32 | +### 2. Create the App provider |
| 33 | + |
| 34 | +Once you've created the `AppProvider`, wrap it at the root of your application so |
| 35 | +that the rest of your application has access to the Wagmi's and TanStack's context. |
| 36 | +This will allow every component inside the provider to use the Wagmi hooks. |
| 37 | + |
| 38 | +For the advance configuration, see [Wagmi's createConfig API reference](https://wagmi.sh/react/api/createConfig). |
| 39 | + |
| 40 | +<Tabs> |
| 41 | +<TabItem value = "provider.ts"> |
| 42 | + |
| 43 | +```ts |
| 44 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 45 | +import { ReactNode } from "react"; |
| 46 | +import { WagmiProvider } from 'wagmi' |
| 47 | +import { config } from "./config.ts"; |
| 48 | + |
| 49 | +const queryClient = new QueryClient(); |
| 50 | + |
| 51 | +export function AppProvider({ children }: { children: ReactNode }) { |
| 52 | + return ( |
| 53 | + <WagmiProvider config={config}> |
| 54 | + <QueryClientProvider client={queryClient}> |
| 55 | + {children} |
| 56 | + </QueryClientProvider> |
| 57 | + </WagmiProvider> |
| 58 | + ); |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +</TabItem> |
| 63 | + |
| 64 | +<TabItem value = "config.ts"> |
| 65 | + |
| 66 | +```ts |
| 67 | +import { createConfig, http } from "wagmi"; |
| 68 | +import { sepolia } from "viem/chains"; |
| 69 | + |
| 70 | +export const config = createConfig({ |
| 71 | + chains: [sepolia], |
| 72 | + transports: { |
| 73 | + [sepolia.id]: http(), |
| 74 | + }, |
| 75 | +}); |
| 76 | +``` |
| 77 | + |
| 78 | +</TabItem> |
| 79 | +</Tabs> |
| 80 | + |
| 81 | +### 3. Create a smart account |
| 82 | + |
| 83 | +Once the user has connected their wallet, use the [Wallet Client](https://viem.sh/docs/clients/wallet) from Wagmi as the signer to create a |
| 84 | +MetaMask smart account. |
| 85 | + |
| 86 | +```ts |
| 87 | +import { Implementation, toMetaMaskSmartAccount } from "@metamask/smart-accounts-kit"; |
| 88 | +import { useAccount, usePublicClient, useWalletClient } from "wagmi"; |
| 89 | + |
| 90 | +const { address } = useAccount(); |
| 91 | +const publicClient = usePublicClient(); |
| 92 | +const { data: walletClient } = useWalletClient(); |
| 93 | + |
| 94 | +// Additional check to make sure the EOA wallet is connected |
| 95 | +// and values are available. |
| 96 | +if (!address || !walletClient || !publicClient ) { |
| 97 | + // Handle the error case |
| 98 | + } |
| 99 | + |
| 100 | +const smartAccount = await toMetaMaskSmartAccount({ |
| 101 | + client: publicClient, |
| 102 | + implementation: Implementation.Hybrid, |
| 103 | + deployParams: [address, [], [], []], |
| 104 | + deploySalt: "0x", |
| 105 | + signer: { walletClient }, |
| 106 | +}); |
| 107 | +``` |
| 108 | + |
| 109 | +## Next steps |
| 110 | + |
| 111 | +- See how to use [MetaMask Embedded Wallets as a signer](./eoa-wallets.md) to make the user onboarding journey easier. |
| 112 | +- See how to [send a user operation](../send-user-operation.md). |
| 113 | +- To sponsor gas for end users, see how to [send a gasless transaction](../send-gasless-transaction.md). |
0 commit comments