Integrating with web3 libraries
Using web3 libraries to interact with wallets and smart contracts.
Openfort's wallet
object is fully compatible with popular web3 libraries for interfacing wallets, including ethers
, and wagmi
.
Read below to learn how to best integrate Openfort alongside these libraries.
Ethers#
Ethers represents connected wallets as a provider, which can be used to take read-only actions with the wallet, and a signer, which can be used to take write actions (signatures and transactions).
Call the wallet's getEthereumProvider
method to get a provider:
Then, call the provider's getSigner
method to get the corresponding signer:
_10const signer = web3Provider.getSigner();
You can then use the provider and signer to get information about the wallet or request signatures and transactions.
Viem#
Viem represents connected wallets as a wallet client object, which you can use to get information about the current wallet or the request signatures and transactions.
To get a viem wallet client for a user's connected wallet, first import your desired network from the viem/chains
package and import the createWalletClient
method and custom transport from viem
:
_10import {createWalletClient, custom} from 'viem';_10// Replace `sepolia` with your desired network_10import {sepolia} from 'viem/chains';
Lastly, get the wallet's EIP1193 provider using the wallet's getEthereumProvider
method and pass it to viem's createWalletClient
method like so:
You can then use the wallet client to get information about the wallet or request signatures and transactions.
Wagmi#
Wagmi is a set of React hooks for interfacing with Ethereum wallets, allowing you read wallet state, request signatures or transactions, and take read and write actions on the blockchain.
Openfort is fully compatible with wagmi, and you can use wagmi's React hooks to interface with external and embedded wallets from Openfort. Just follow the steps below!
1. Install dependencies#
Install the latest versions of wagmi
, @tanstack/react-query
and @openfort/openfort-js
:
_10npm i wagmi @tanstack/react-query @openfort/openfort-js
2. Setup TanStack Query#
To start, set up your app with the TanStack Query's React Provider. Wagmi uses TanStack Query under the hood to power its data fetching and caching of wallet and blockchain data.
_10import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
Next, create a new instance of the QueryClient
:
_10const queryClient = new QueryClient();
Then, wrap your app's components with the QueryClientProvider
. This must be rendered inside the WagmiProvider
component.
_10<WagmiProvider config={config}>_10 <QueryClientProvider client={queryClient}>_10 <Connect />_10 </QueryClientProvider>_10</WagmiProvider>
For the client
property of the QueryClientProvider
, pass the queryClient
instance you created.
3. Setup Wagmi#
To build your wagmi
config, import the createConfig
method. Next, import your app's required chains from viem/chains
and the http
transport from wagmi
. Your app's required chains should match whatever you configure as supportedChains
for Openfort.
4. Create Openfort Connector#
You'll need to create a custom connector for Openfort. Create a new file openfortConnector.ts
:
5. Connector list#
Create a new file Connect.tsx
to list the available connectors and handle the connection. When you press on Openfort, then you will be redirected to the authentication page that you should implement. You can find an example of the authentication page in the Openfort documentation.
6. Complete example#
Altogether, this should look like:
That's it! You've successfully integrated Openfort alongside wagmi
in your app! 🎉
7. Use wagmi
throughout your app#
Once you've completed the setup above, you can use wagmi's React hooks throughout your app to interface with wallets and take read and write actions on the blockchain.
To use wagmi hooks, like useAccount
, in your components, import the hook directly from wagmi
and call it as usual:
_10import {useAccount} from 'wagmi';_10_10export default const WalletAddress = () => {_10 const {address} = useAccount();_10 return <p>Wallet address: {address}</p>;_10}
Demo app with WAGMI#
Feel free to take a look at the app's source code to see an end-to-end implementation of Openfort with wagmi: