This section covers the client-side operations for Sign In with Algorand (SIWA), including wallet initialization, connection, message signing, and disconnection for multiple wallet providers.
Initializing Wallet Connections
We use a custom hook useWalletConnection to manage wallet connections. This hook handles multiple wallet providers and abstracts the complexity of each wallet's specific implementation.
import { useWalletConnection, WalletProvider } from"@/hooks/useWalletConnection";// In your componentconst {address,provider,isLoading,connectWallet,disconnectWallet,signMessage,} =useWalletConnection();
Connecting a Wallet
To connect a wallet, use the connectWallet function provided by the hook:
consthandleConnect=async (selectedProvider:WalletProvider) => {try {awaitconnectWallet(selectedProvider);console.log(`Connected to ${selectedProvider} wallet`); } catch (error) {console.error(`Error connecting to ${selectedProvider}:`, error); }};
Signing a Message
To sign a message with the connected wallet, use the signMessage function:
Pera Wallet uses direct message signing. No additional setup is required beyond the useWalletConnection hook.
Defly Wallet
Defly Wallet uses a transaction-based approach for signing. The hook handles creating a zero-amount payment transaction with the message in the note field.
Kibisis Wallet
Kibisis Wallet requires injection into the browser environment. The hook handles this process internally.
Lute Wallet
Lute Wallet also uses a transaction-based approach similar to Defly. The hook manages the connection and signing process.
Full Example: Client-Side Workflow
Here's a complete example of how to use the useWalletConnection hook in a React component:
This implementation provides a unified interface for connecting to different Algorand wallets, signing messages, and managing wallet connections. It abstracts away the complexities of each wallet's specific implementation, providing a consistent experience across different wallet providers.