Solana CookbookWallets
How to Sign and Verify a Message
The primary function of a keypair is to sign messages, transactions and enable verification of the signature. Verification of a signature allows the recipient to be sure that the data was signed by the owner of a specific private key.
import {generateKeyPair,signBytes,verifySignature,getUtf8Encoder,getBase58Decoder,} from "@solana/web3.js";const keys = await generateKeyPair();const message = getUtf8Encoder().encode("Hello, World!");const signedBytes = await signBytes(keys.privateKey, message);const decoded = getBase58Decoder().decode(signedBytes);console.log("Signature:", decoded);const verified = await verifySignature(keys.publicKey, signedBytes, message);console.log("Verified:", verified);