import { useConnection, useWallet } from '@solana/wallet-adapter-react'; import { WalletDisconnectButton, WalletModalProvider, WalletMultiButton } from '@solana/wallet-adapter-react-ui'; import { Payload as SIWPayload, SIWWeb3 } from '@web3auth/sign-in-with-web3'; import bs58 from 'bs58'; import React, { useState } from 'react'; import Swal from 'sweetalert2'; import styles from '../public/css/solana.module.css'; import SolanaLogo from '../public/solana-logo.png'; const Solana: React.FC = () => { const { connection } = useConnection(); // if you use anchor, use the anchor hook instead // const wallet = useAnchorWallet(); // const walletAddress = wallet?.publicKey.toString(); let walletAddress = ""; const wallet = useWallet(); if (wallet.connected && wallet.publicKey) { walletAddress = wallet.publicKey.toString() } const { publicKey, signMessage } = useWallet(); const [siwsMessage, setSiwsMessage] = useState(); const [nonce, setNonce] = useState(""); const [sign, setSignature] = useState(""); // Domain and origin const domain = window.location.host; const origin = window.location.origin; let statement = "Sign in with Solana to the app."; // Generate a message for signing // The nonce is generated on the server side function createSolanaMessage() { const payload = new SIWPayload(); payload.domain = domain; payload.uri = origin; payload.address = publicKey!.toString(); payload.statement = statement; payload.version = "1"; payload.chainId = 1; const header = { t : "sip99" }; const network = "solana"; let message = new SIWWeb3({ header, payload, network }); // we need the nonce for verification so getting it in a global variable setNonce(message.payload.nonce); setSiwsMessage(message); const messageText = message.prepareMessage(); const messageEncoded = new TextEncoder().encode(messageText); signMessage!(messageEncoded).then(resp => setSignature( bs58.encode(resp))); } return ( <> {wallet.connected && sign == "" &&

Sign Transaction

} { wallet.connected != true && sign=="" &&

Sign in With Solana

} {wallet.connected && sign == "" &&
} {wallet.connected != true && sign == "" && } { sign && <>

Verify Signature

setSignature(e.target.value)} /> } ); }; export default Solana;