-
Notifications
You must be signed in to change notification settings - Fork 1
/
Web3OnboardAuthenticationExample.tsx
184 lines (168 loc) · 6.1 KB
/
Web3OnboardAuthenticationExample.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import capsuleModule, { Environment, OAuthMethod } from "@web3-onboard/capsule";
import Logo from "../assets/images/capsule-logo.svg";
import {
CardContent,
CardFooter,
CardHeader,
CapsuleAuthOptions,
Alert,
Button,
} from "../components";
import { useConnectWallet, init } from "@web3-onboard/react";
import { CapsuleInitOptions } from "@web3-onboard/capsule/dist/types";
// Capsule SDK integration example with BlockNative's Web3-Onboard.
// This tutorial demonstrates how to integrate Capsule as a wallet option in Web3-Onboard.
// For additional details on Web3-Onboard, refer to: https://onboard.blocknative.com/docs/modules/react
type Web3OnboardAuthenticationExampleProps = {
setSelectedAuthOption: (option: CapsuleAuthOptions) => void;
};
// Step 1: Set up your Capsule API key
// Obtain your API key from https://usecapsule.com/beta
const CAPSULE_API_KEY = "d0b61c2c8865aaa2fb12886651627271";
// Step 2: Set the Capsule environment
// Choose between Environment.DEVELOPMENT or Environment.PRODUCTION based on your use case
const CAPSULE_ENVIRONMENT = Environment.DEVELOPMENT;
// Step 3: Initialize the Capsule module for Web3-Onboard
// Configure the Capsule module with your API key, environment, and other options
const initOptions:CapsuleInitOptions ={
environment: CAPSULE_ENVIRONMENT,
apiKey: CAPSULE_API_KEY,
modalProps: {
oAuthMethods: [
OAuthMethod.GOOGLE,
OAuthMethod.TWITTER,
OAuthMethod.APPLE,
OAuthMethod.DISCORD,
OAuthMethod.FACEBOOK,
],
logo: Logo,
},
walletLabel: "Sign in with Capsule",
walletIcon: async () =>
(await import("../assets/images/capsule-logo.svg")).default,
}
const capsule = capsuleModule(initOptions);
// Step 4: Configure the list of wallets for Web3-Onboard
// In this example, we're only including Capsule, but you can add other wallets here
const wallets = [capsule];
// Step 5: Define the blockchain networks you want to support
// This example uses Sepolia testnet, but you can add any EVM-compatible network
const chains = [
{
id: 11155111,
token: "ETH",
label: "Sepolia",
rpcUrl: "https://rpc.sepolia.org/",
},
];
// Step 6: Set the metadata for your app
// This information will be displayed in the Web3-Onboard modal
const appMetadata = {
name: "Capsule Example App",
description: "Example app for Capsule Web3-Onboard Authentication",
};
// Step 7: Initialize Web3-Onboard with your wallets, chains, and app metadata
init({
wallets: wallets,
chains: chains,
appMetadata: appMetadata,
});
export const Web3OnboardAuthenticationExample: React.FC<
Web3OnboardAuthenticationExampleProps
> = ({ setSelectedAuthOption }) => {
const handleBack = () => {
setSelectedAuthOption(CapsuleAuthOptions.None);
};
// Step 8: Use the useConnectWallet hook to manage wallet connection state
const [{ wallet, connecting }, connect, disconnect] = useConnectWallet();
// Step 9: Function to connect the wallet
// This will open the Web3-Onboard modal with Capsule as an option
const connectWallet = async () => {
await connect();
};
// Step 10: Function to disconnect the wallet
const disconnectWallet = async () => {
if (!wallet) return;
await disconnect(wallet);
};
// Note: This example focuses on connecting a Capsule wallet through Web3-Onboard
// and does not include specific signing examples. For detailed information on
// how to sign transactions or messages using a connected Capsule wallet via
// Web3-Onboard, please refer to the BlockNative Web3-Onboard documentation:
// https://onboard.blocknative.com/docs/modules/react#signing-transactions-and-messages
// Capsule wallets integrate seamlessly with Web3-Onboard's standard signing methods.
return (
<>
<CardHeader>
<h2 className="text-xl font-bold">
Capsule Integration with BlockNative Web3-Onboard
</h2>
<p className="text-sm text-muted-foreground">
This example demonstrates how to integrate Capsule with Web3-Onboard.
The Web3-Onboard modal will open with Capsule as the wallet option.
Authentication is handled seamlessly by the Capsule Modal within
Web3-Onboard.
</p>
</CardHeader>
<CardContent className="flex flex-grow flex-col items-start">
{wallet ? (
<>
<Alert>
Connected to Capsule wallet: {wallet.accounts[0].address}
</Alert>
<CardHeader>
<h2 className="text-xl font-bold">
Next Steps: Signing with Capsule
</h2>
</CardHeader>
<CardContent className="max-w-sm min-w-xs">
<Alert>
For instructions on how to sign messages or transactions with
your Capsule wallet through Web3-Onboard, please refer to the{" "}
<a
href="https://onboard.blocknative.com/docs/modules/react"
className="underline"
>
BlockNative Web3-Onboard documentation
</a>
. Capsule integrates seamlessly with Web3-Onboard's signing
methods.
</Alert>
</CardContent>
</>
) : (
<Alert>
No Capsule wallet connected. Click 'Connect Capsule Wallet' to
proceed.
</Alert>
)}
</CardContent>
<CardFooter className="flex flex-col sm:flex-row justify-between gap-2 p-4">
<Button
variant="outline"
onClick={handleBack}
className="w-full sm:w-auto text-sm"
>
Back to Options
</Button>
{wallet ? (
<Button
variant="outline"
onClick={disconnectWallet}
className="w-full sm:w-auto text-sm"
>
Disconnect Capsule Wallet
</Button>
) : (
<Button
onClick={connectWallet}
disabled={connecting}
className="w-full sm:w-auto text-sm"
>
{connecting ? "Connecting to Capsule..." : "Connect Capsule Wallet"}
</Button>
)}
</CardFooter>
</>
);
};