Example for Solana embedded Wallet

import { IEmbeddedWalletSolanaSigner, isSignedMessage } from '@dynamic-labs/solana';


const App = () => {
  const { primaryWallet } = useDynamicContext();

  const signMessage = async () => {
    if (!primaryWallet) return;

    const signer = await primaryWallet.connector.getSigner<IEmbeddedWalletSolanaSigner>();

    if (!signer) return;

    const signedMessage = await signer.signMessage(
      new TextEncoder().encode('example'),
    );

    if (!signedMessage) return;

    const signature: Uint8Array = isSignedMessage(signedMessage)
      ? signedMessage.signature
      : signedMessage;
  }

  ...
}

Example for any external Solana wallet and Backpack wallet

import { ISolana, isSignedMessage } from '@dynamic-labs/solana';

const App = () => {
  const { primaryWallet } = useDynamicContext();

  const signMessage = async () => {
    if (!primaryWallet) return;

    const signer = await primaryWallet.connector.getSigner<ISolana>();

    if (!signer) return;

    const signedMessage = await signer.signMessage(
      new TextEncoder().encode('example'),
    );

    if (!signedMessage) return;

    const signature: Uint8Array = isSignedMessage(signedMessage)
      ? signedMessage.signature
      : signedMessage;
  }

  ...
}

Example for Solana common wallets

import { ISolanaSigner } from '@dynamic-labs/solana';

const App = () => {
  const { primaryWallet } = useDynamicContext();

  const signMessage = async () => {
    if (!primaryWallet) return;

    const signer = await primaryWallet.connector.getSigner<ISolanaSigner>();

    if (!signer) return;

    const signedMessage = await signer.signMessage(
      new TextEncoder().encode('example'),
    );

    if (!signedMessage) return;

    const signature: Uint8Array = signedMessage.signature
  }

  ...
}

Example for Backpack wallet only

import { IBackpackSolanaSigner } from '@dynamic-labs/solana';

const App = () => {
  const { primaryWallet } = useDynamicContext();

  const signMessage = async () => {
    if (!primaryWallet) return;

    const signer = await primaryWallet.connector.getSigner<IBackpackSolanaSigner>();

    if (!signer) return;

    const signature: Uint8Array | void = await signer.signMessage(
      new TextEncoder().encode('example'),
    );
  }

  ...
}

Example using isBackpackSolanaSigner type guard function

import { ISolana, isBackpackSolanaSigner } from '@dynamic-labs/solana';

const App = () => {
  const { primaryWallet } = useDynamicContext();

  const signMessage = async () => {
    if (!primaryWallet) return;

    const signer = await primaryWallet.connector.getSigner();

    if (isBackpackSolanaSigner(signer)) {
      const signature: Uint8Array | void = await signer.signMessage(
        new TextEncoder().encode('example'),
      );
    }
  }

  ...
}