Verify Ed25519 signatures to confirm that a message was signed by a specific Stellar address. Essential for authentication and ensuring data integrity.
Verify Signatures
import { ParaStellarSigner } from "@getpara/stellar-sdk-v14-integration";
import { Networks, StrKey } from "@stellar/stellar-sdk";
import { useClient } from "@getpara/react-sdk";
import nacl from "tweetnacl";
function VerifySignature() {
const para = useClient();
const verifyMessage = async () => {
if (!para) return;
const signer = new ParaStellarSigner(para, Networks.PUBLIC);
const message = "Hello, Stellar!";
const messageBytes = Buffer.from(new TextEncoder().encode(message));
// Sign the message
const signature = await signer.signBytes(messageBytes);
// Extract the raw Ed25519 public key from the Stellar G-address
const publicKeyBytes = StrKey.decodeEd25519PublicKey(signer.address);
// Verify the signature using tweetnacl
const isValid = nacl.sign.detached.verify(
new Uint8Array(messageBytes),
new Uint8Array(signature),
publicKeyBytes
);
console.log("Message:", message);
console.log("Signature:", signature.toString("hex"));
console.log("Signature valid:", isValid);
console.log("Signer:", signer.address);
return isValid;
};
return <button onClick={verifyMessage}>Sign & Verify Message</button>;
}
Install tweetnacl for Ed25519 signature verification: npm install tweetnacl. The StrKey.decodeEd25519PublicKey method from @stellar/stellar-sdk extracts the raw 32-byte public key from a Stellar G-address.
Next Steps