import { useWallet } from '@lazorkit/wallet'; import { SystemProgram, PublicKey, LAMPORTS_PER_SOL } from '@solana/web3.js'; import { useState, useEffect } from 'react'; export default function WalletDemo() { const { smartWalletPubkey, isConnected, isConnecting, isSigning, connect, disconnect, signAndSendTransaction, error, wallet } = useWallet(); const [recipientAddress, setRecipientAddress] = useState(''); const [amount, setAmount] = useState('0.01'); const [copied, setCopied] = useState(false); const [txStatus, setTxStatus] = useState(''); useEffect(() => { if (wallet) { // Validate wallet data integrity if (!wallet.passkeyPubkey || wallet.passkeyPubkey.length === 0) { setTxStatus('Error: Wallet data corrupted. Please disconnect and reconnect your wallet.'); return; } if (wallet.passkeyPubkey.length !== 33) { setTxStatus('Error: Invalid wallet data. Please disconnect and reconnect your wallet.'); return; } // Ensure credential ID is stored const storedCredId = localStorage.getItem('CREDENTIAL_ID'); if (wallet.credentialId && !storedCredId) { localStorage.setItem('CREDENTIAL_ID', wallet.credentialId); } setTxStatus(''); } }, [wallet, isConnected]); const handleConnect = async () => { try { await connect(); } catch (err) { setTxStatus('Connection failed. Please try again.'); } }; const handleCopyAddress = async () => { if (smartWalletPubkey) { try { await navigator.clipboard.writeText(smartWalletPubkey.toString()); setCopied(true); setTimeout(() => setCopied(false), 2000); } catch (err) { setTxStatus('Failed to copy address'); } } }; const handleSend = async (e: React.FormEvent) => { e.preventDefault(); if (!smartWalletPubkey || !recipientAddress || !amount) return; if (!wallet || !wallet.passkeyPubkey || wallet.passkeyPubkey.length !== 33) { setTxStatus('Error: Invalid wallet state. Please disconnect and reconnect your wallet.'); return; } setTxStatus(''); try { let recipientPubkey: PublicKey; try { recipientPubkey = new PublicKey(recipientAddress); } catch (err) { setTxStatus('Error: Invalid recipient address'); return; } const transferInstruction = SystemProgram.transfer({ fromPubkey: smartWalletPubkey, toPubkey: recipientPubkey, lamports: LAMPORTS_PER_SOL * parseFloat(amount), }); const sig = await signAndSendTransaction({ instructions: [transferInstruction], transactionOptions: { computeUnitLimit: 200_000 } }); setTxStatus(`Success! Transaction: ${sig}`); setRecipientAddress(''); setAmount('0.01'); setTimeout(() => setTxStatus(''), 5000); } catch (err) { const errorMsg = err instanceof Error ? err.message : 'Unknown error'; setTxStatus(`Error: ${errorMsg}`); } }; return (

Docs

Explore the LazorKit documentation to get started.

GitHub

Contribute and see the source code on our GitHub.

{isConnected && (

Send SOL

{/* Warning if wallet data is invalid */} {wallet && (!wallet.passkeyPubkey || wallet.passkeyPubkey.length !== 33) && (

Wallet data is corrupted

Please disconnect and reconnect your wallet to fix this issue.

)}
setRecipientAddress(e.target.value)} placeholder="Enter Solana address" className="w-full px-4 py-2 bg-gray-700 border border-gray-600 rounded focus:outline-none focus:border-blue-500 text-white" required />
setAmount(e.target.value)} placeholder="0.01" className="w-full px-4 py-2 bg-gray-700 border border-gray-600 rounded focus:outline-none focus:border-blue-500 text-white" required />
{txStatus && (

{txStatus}

)}
)} {error &&

Error: {error.message}

}
); }