Skip to main content
In this example, we are going to sign a personal message using a Sui wallet.
import { useDynamicContext } from '@dynamic-labs/sdk-react-core';
import { isSuiWallet } from '@dynamic-labs/sui';

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

  const onSignMessage = async () => {
    if (!primaryWallet || !isSuiWallet(primaryWallet)) {
      return;
    }

    const message = 'Hello from Dynamic!';
    const { signature } = await primaryWallet.signMessage(message);

    console.log('signature', signature);
  };

  return <button onClick={onSignMessage}>Sign message</button>;
};

Full React Native Example

Here’s a complete example component for React Native that includes message signing with UI feedback:
import { FC, useState } from 'react';
import { View, Text, Button, Alert } from 'react-native';
import { dynamicClient } from './dynamicClient';

export const SuiSignMessage: FC = () => {
  const [signature, setSignature] = useState<string | null>(null);

  const handleSignMessage = async () => {
    const wallet = dynamicClient.wallets.primary;

    if (!wallet || wallet.chain !== 'SUI') {
      Alert.alert('Error', 'No Sui wallet found');
      return;
    }

    try {
      const signer = dynamicClient.sui.getSigner({ wallet });
      const message = 'Hello from Dynamic! 🚀';
      const result = await signer.signMessage(message);

      setSignature(result.signature);
      Alert.alert('Success', 'Message signed successfully!');
    } catch (error) {
      Alert.alert(
        'Error',
        error instanceof Error ? error.message : 'Failed to sign message'
      );
    }
  };

  return (
    <View>
      <Button title="Sign Message" onPress={handleSignMessage} />
      {signature && (
        <Text selectable style={{ marginTop: 10 }}>
          Signature: {signature}
        </Text>
      )}
    </View>
  );
};

Notes

  • The signMessage method returns an object containing the signature as a base64-encoded string.
  • Message signing does not require any gas fees as it’s an off-chain operation.
  • You can use message signing for authentication, proving wallet ownership, or signing arbitrary data.