Message Signing

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:

const signMessageWithPera = async (message: string, address: string): Promise<{ signature: Uint8Array; transaction: null }> => {
  const hashedMessage = hashMessage(message);
  const encodedHashedMessage = getMessageBytes(Buffer.from(hashedMessage).toString("utf8"));

  const peraSigArray = await peraWallet.signData(
    [{ data: encodedHashedMessage, message: "" }],
    address
  );
  return {
    signature: peraSigArray[0],
    transaction: null,
  };
};

Defly Wallet

Defly Wallet requires creating a transaction with the message in the note field:

const signMessageWithDefly = async (message: string, address: string): Promise<{ signature: Uint8Array; transaction: Uint8Array }> => {
  const hashedMessage = hashMessage(message);
  const encodedHashedMessage = getMessageBytes(Buffer.from(hashedMessage).toString("utf8"));

  const suggestedParams = await algodClient.getTransactionParams().do();
  const deflyTxn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({
    note: encodedHashedMessage,
    from: address,
    to: address,
    amount: 0,
    suggestedParams,
  } as any);

  const deflyTxnGroup = [{ txn: deflyTxn, signerAddress: [address] }];
  const deflySigArray = await deflyWallet.signTransaction([deflyTxnGroup]);
  const decodedDeflyTxn = algosdk.decodeSignedTransaction(deflySigArray[0]);
  return {
    signature: decodedDeflyTxn.sig as unknown as Uint8Array,
    transaction: deflySigArray[0],
  };
};

Kibisis Wallet

Kibisis Wallet uses a browser-based approach for signing:

Lute Wallet

Lute Wallet, like Defly, uses a transaction-based approach:

Unified Signing Function

Here's a unified function that handles signing for all supported wallet providers:

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.

Last updated