This guide explains how to sign SIWA (Sign-In with Algorand) messages using different wallet providers: Pera, Defly, Kibisis, and Lute. Each wallet has its own method for signing messages.
Pera Wallet
Pera Wallet supports signing arbitrary data directly:
Here's a unified function that handles signing for all supported wallet providers:
const signMessage = async (message: string): Promise<{ signature: Uint8Array; transaction?: any | null }> => {
if (!address) {
throw new Error("No address connected");
}
const hashedMessage = hashMessage(message);
const encodedHashedMessage = getMessageBytes(Buffer.from(hashedMessage).toString("utf8"));
const suggestedParams = ["Defly", "Lute"].includes(provider)
? await algodClient.getTransactionParams().do()
: null;
switch (provider) {
case "Pera":
return await signMessageWithPera(message, address);
case "Defly":
if (!suggestedParams) {
throw new Error("Suggested params are not available");
}
return await signMessageWithDefly(message, address);
case "Kibisis":
return await signMessageWithKibisis(message);
case "Lute":
if (!suggestedParams) {
throw new Error("Suggested params are not available");
}
return await signMessageWithLute(message, address);
default:
throw new Error("Unsupported wallet provider");
}
};
This unified signMessage function determines the correct signing method based on the current wallet provider. It handles the differences in signing approaches between the wallets, ensuring a consistent interface for your application.
Remember to import and initialize the necessary wallet SDKs and utility functions (like hashMessage, getMessageBytes, and algodClient) before using these signing functions.