Skip to content

Commit d315f2b

Browse files
add embedded wallet guide
1 parent 63f5d0a commit d315f2b

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

gator-sidebar.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ const sidebar = {
5858
'guides/smart-accounts/send-user-operation',
5959
'guides/smart-accounts/send-gasless-transaction',
6060
'guides/smart-accounts/generate-multisig-signature',
61+
{
62+
type: 'category',
63+
label: 'Configure signers',
64+
collapsed: true,
65+
items: [
66+
'guides/smart-accounts/signers/web3auth',
67+
],
68+
},
6169
],
6270
},
6371
{
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
description: Learn how to use MetaMask Embedded Wallets (Web3Auth) with MetaMask Smart Accounts.
3+
sidebar_label: Embedded Wallets
4+
keywords: [web3auth, smart account, signer, metamask samrt account]
5+
---
6+
7+
# Use MetaMask Embdeded 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 (Web3Auth) as a signer for MetaMask Smart Accounts.
14+
15+
View the complete code for this guide at [gator-examples repository](https://github.com/MetaMask/gator-examples/tree/main/examples/smart-accounts/signers/dynamic).
16+
17+
## Prerequisites
18+
19+
- Install [Yarn](https://yarnpkg.com/),
20+
[npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm), or another package manager
21+
- Get a [Client ID](/embedded-wallets/dashboard) from the Embedded Wallets (Web3Auth) dashboard.
22+
23+
## Steps
24+
25+
### 1. Install dependencies
26+
27+
Install the [Smart Accounts Kit](https://www.npmjs.com/package/@metamask/smart-accounts-kit) and other dependencies in your project:
28+
29+
```bash npm2yarn
30+
npm install @metamask/smart-accounts-kit @web3auth/modal wagmi @tanstack/react-query viem
31+
```
32+
33+
### 2. Create the Web3Auth provider
34+
35+
In this step, you'll configure the `Web3AuthProvider` component to provide the Embedded Wallets (Web3Auth)
36+
context to your application. You'll also use the `WagmiProvider` to integrate Embedded Wallets (Web3Auth) with Wagmi.
37+
This connector enables you to use Wagmi hooks with Embedded Wallets (Web3Auth).
38+
39+
Once you have created the `Web3AuthAppProvider`, you must wrap it at the root of your application so
40+
that the rest of your application has access to the Embedded Wallets (Web3Auth) context.
41+
42+
For the advance configuration, see [Embedded Wallets guide](https://docs.metamask.io/embedded-wallets/sdk/react/advanced/).
43+
44+
<Tabs>
45+
<TabItem value = "provider.ts">
46+
47+
```ts
48+
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
49+
import { ReactNode } from "react";
50+
import { Web3AuthProvider } from "@web3auth/modal/react";
51+
import { WagmiProvider } from "@web3auth/modal/react/wagmi";
52+
import { web3authConfig } from "./config.ts";
53+
54+
const queryClient = new QueryClient();
55+
56+
export function Web3AuthAppProvider({ children }: { children: ReactNode }) {
57+
return (
58+
<Web3AuthProvider config={web3authConfig}>
59+
<QueryClientProvider client={queryClient}>
60+
<WagmiProvider>{children}</WagmiProvider>
61+
</QueryClientProvider>
62+
</Web3AuthProvider>
63+
);
64+
}
65+
```
66+
67+
</TabItem>
68+
69+
<TabItem value = "config.ts">
70+
71+
```ts
72+
import { WEB3AUTH_NETWORK_TYPE, Web3AuthOptions } from "@web3auth/modal";
73+
74+
const web3AuthOptions: Web3AuthOptions = {
75+
clientId: process.env.NEXT_PUBLIC_WEB3AUTH_CLIENT_ID as string,
76+
web3AuthNetwork: process.env
77+
.NEXT_PUBLIC_WEB3AUTH_NETWORK as WEB3AUTH_NETWORK_TYPE,
78+
};
79+
80+
export const web3authConfig = {
81+
web3AuthOptions,
82+
};
83+
```
84+
85+
</TabItem>
86+
</Tabs>
87+
88+
### 3. Create a smart account
89+
90+
Once the user has connected their wallet, you can use the [Wallet Client](https://viem.sh/docs/clients/wallet) from Wagmi as the signer to create a
91+
MetaMask smart account.
92+
93+
```ts
94+
import { Implementation, toMetaMaskSmartAccount } from "@metamask/smart-accounts-kit";
95+
import { useAccount, usePublicClient, useWalletClient } from "wagmi";
96+
97+
const { address } = useAccount();
98+
const publicClient = usePublicClient();
99+
const { data: walletClient } = useWalletClient();
100+
101+
// Additional check to make sure the Dyanmic wallet is connected
102+
// and values are available.
103+
if (!address || !walletClient || !publicClient ) {
104+
// Handle the error case
105+
}
106+
107+
const smartAccount = await toMetaMaskSmartAccount({
108+
client: publicClient,
109+
implementation: Implementation.Hybrid,
110+
deployParams: [address, [], [], []],
111+
deploySalt: "0x",
112+
signer: { walletClient },
113+
});
114+
```
115+
116+
## Next steps
117+
- See how to [send a user operations](../send-user-operation.md).
118+
- To sponsor gas for end users, see how to [send a gasless transaction](../send-gasless-transaction.md).

0 commit comments

Comments
 (0)