Server-side operations
This guide explains how to implement server-side verification for Sign-In with Algorand (SIWA) messages. The server-side verification is crucial for maintaining security and integrity.
Verifying SIWA Messages
import { SiwaMessage } from "@avmkit/siwa";
import { verifySignature } from "./utils";
/**
* Verifies the SIWA message on the server
* @param {Object} payload - The payload from the client containing credentials
* @returns {Object} - Verification result
*/
const verifySIWAMessageOnServer = async (payload) => {
try {
// Extract message and credentials from the payload
const { message, signature, address, provider, encodedTransaction, nfd } = payload;
// Initialize the SiwaMessage instance
const siwaMessageInstance = new SiwaMessage(JSON.parse(message));
// Perform the verification
const isValid = await verifySignature(
siwaMessageInstance,
signature,
provider,
encodedTransaction,
nfd
);
if (isValid) {
console.log("SIWA message verified successfully");
return { success: true, user: { address, provider } }; // Attach verified user data
} else {
throw new Error("Failed to verify SIWA message");
}
} catch (error) {
console.error("Verification error:", error);
return { success: false, error: error.message };
}Integration in an API Endpoint
Client-side Verification for Demonstration
Last updated