|
| 1 | +--- |
| 2 | +description: Learn how to use MetaMask Embedded Wallets (Web3Auth) with MetaMask Smart Accounts. |
| 3 | +sidebar_label: MetaMask Embedded Wallets |
| 4 | +keywords: [web3auth, smart account, signer, metamask smart account] |
| 5 | +--- |
| 6 | + |
| 7 | +# Use MetaMask Embedded Wallets (Web3Auth) with MetaMask Smart Accounts |
| 8 | + |
| 9 | +[MetaMask Embedded Wallets (Web3Auth)](/embedded-wallets/) provides a pluggable embedded wallet |
| 10 | +infrastructure to simplify Web3 wallet integration and user onboarding. It supports social logins allowing |
| 11 | +users to access Web3 applications through familiar authentication methods in under a minute. |
| 12 | + |
| 13 | +MetaMask Smart Accounts is a signer agnostic implementation that allows you to use Embedded Wallets as a signer for MetaMask Smart Accounts. |
| 14 | + |
| 15 | +:::info |
| 16 | +This guide is targeted towards React and React-based frameworks. |
| 17 | +::: |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +- Install [Node.js](https://nodejs.org/en/blog/release/v18.18.0) v18 or later |
| 22 | +- Install [Yarn](https://yarnpkg.com/), |
| 23 | + [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm), or another package manager |
| 24 | +- A [Embedded Wallets (Web3Auth) Client ID](/embedded-wallets/dashboard) |
| 25 | + |
| 26 | +## Steps |
| 27 | + |
| 28 | +### 1. Install dependencies |
| 29 | + |
| 30 | +Install the [Smart Accounts Kit](https://www.npmjs.com/package/@metamask/smart-accounts-kit) and other dependencies in your project: |
| 31 | + |
| 32 | +```bash npm2yarn |
| 33 | +npm install @metamask/smart-accounts-kit @web3auth/modal wagmi @tanstack/react-query viem |
| 34 | +``` |
| 35 | + |
| 36 | +### 2. Create the Web3Auth provider |
| 37 | + |
| 38 | +Configure the `Web3AuthProvider` component to provide the Embedded Wallets context to your application. |
| 39 | +You'll also use the `WagmiProvider` to integrate Embedded Wallets with Wagmi. |
| 40 | +This connector enables you to use Wagmi hooks with Embedded Wallets. |
| 41 | + |
| 42 | +Once you've created the `Web3AuthAppProvider`, wrap it at the root of your application so |
| 43 | +that the rest of your application has access to the Embedded Wallets context. |
| 44 | + |
| 45 | +For the advance configuration, see [Embedded Wallets guide](https://docs.metamask.io/embedded-wallets/sdk/react/advanced/). |
| 46 | + |
| 47 | +<Tabs> |
| 48 | +<TabItem value = "provider.ts"> |
| 49 | + |
| 50 | +```ts |
| 51 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 52 | +import { ReactNode } from "react"; |
| 53 | +import { Web3AuthProvider } from "@web3auth/modal/react"; |
| 54 | +import { WagmiProvider } from "@web3auth/modal/react/wagmi"; |
| 55 | +import { web3authConfig } from "./config.ts"; |
| 56 | + |
| 57 | +const queryClient = new QueryClient(); |
| 58 | + |
| 59 | +export function Web3AuthAppProvider({ children }: { children: ReactNode }) { |
| 60 | + return ( |
| 61 | + <Web3AuthProvider config={web3authConfig}> |
| 62 | + <QueryClientProvider client={queryClient}> |
| 63 | + <WagmiProvider>{children}</WagmiProvider> |
| 64 | + </QueryClientProvider> |
| 65 | + </Web3AuthProvider> |
| 66 | + ); |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | +</TabItem> |
| 71 | + |
| 72 | +<TabItem value = "config.ts"> |
| 73 | + |
| 74 | +```ts |
| 75 | +import { WEB3AUTH_NETWORK_TYPE, Web3AuthOptions } from "@web3auth/modal"; |
| 76 | + |
| 77 | +const web3AuthOptions: Web3AuthOptions = { |
| 78 | + clientId: process.env.NEXT_PUBLIC_WEB3AUTH_CLIENT_ID as string, |
| 79 | + web3AuthNetwork: process.env |
| 80 | + .NEXT_PUBLIC_WEB3AUTH_NETWORK as WEB3AUTH_NETWORK_TYPE, |
| 81 | +}; |
| 82 | + |
| 83 | +export const web3authConfig = { |
| 84 | + web3AuthOptions, |
| 85 | +}; |
| 86 | +``` |
| 87 | + |
| 88 | +</TabItem> |
| 89 | +</Tabs> |
| 90 | + |
| 91 | +### 3. Create a smart account |
| 92 | + |
| 93 | +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 |
| 94 | +MetaMask smart account. |
| 95 | + |
| 96 | +```ts |
| 97 | +import { Implementation, toMetaMaskSmartAccount } from "@metamask/smart-accounts-kit"; |
| 98 | +import { useAccount, usePublicClient, useWalletClient } from "wagmi"; |
| 99 | + |
| 100 | +const { address } = useAccount(); |
| 101 | +const publicClient = usePublicClient(); |
| 102 | +const { data: walletClient } = useWalletClient(); |
| 103 | + |
| 104 | +// Additional check to make sure the Dyanmic wallet is connected |
| 105 | +// and values are available. |
| 106 | +if (!address || !walletClient || !publicClient ) { |
| 107 | + // Handle the error case |
| 108 | + } |
| 109 | + |
| 110 | +const smartAccount = await toMetaMaskSmartAccount({ |
| 111 | + client: publicClient, |
| 112 | + implementation: Implementation.Hybrid, |
| 113 | + deployParams: [address, [], [], []], |
| 114 | + deploySalt: "0x", |
| 115 | + signer: { walletClient }, |
| 116 | +}); |
| 117 | +``` |
| 118 | + |
| 119 | +## Next steps |
| 120 | + |
| 121 | +- See how to [send a user operations](../send-user-operation.md). |
| 122 | +- To sponsor gas for end users, see how to [send a gasless transaction](../send-gasless-transaction.md). |
0 commit comments