Verify message and transaction signatures using Solana Web3.js or Anchor
import { useParaSolana } from './hooks/useParaSolana';
import { PublicKey } from '@solana/web3.js';
import nacl from 'tweetnacl';
import bs58 from 'bs58';
function VerifySignature() {
const { signer } = useParaSolana();
const verifyMessage = async () => {
if (!signer) {
console.error("No signer available. Connect wallet first.");
return;
}
const message = "Hello, Solana!";
const messageBytes = new TextEncoder().encode(message);
// Sign the message first
const signature = await signer.signBytes(Buffer.from(messageBytes));
const signatureBase58 = bs58.encode(signature);
// Verify the signature
try {
const publicKeyBytes = new PublicKey(signer.address).toBytes();
const isValid = nacl.sign.detached.verify(
messageBytes,
signature,
publicKeyBytes
);
console.log("Message:", message);
console.log("Signature:", signatureBase58);
console.log("Signature valid:", isValid);
console.log("Signer:", signer.address);
return isValid;
} catch (error) {
console.error("Verification failed:", error);
return false;
}
};
return <button onClick={verifyMessage}>Sign & Verify Message</button>;
}