Skip to main content
Sign arbitrary messages with a user’s embedded Ethereum wallet using the personal_sign method. Message signing is commonly used for authentication, proof of ownership, and off-chain authorization.
This method uses Ethereum’s personal_sign RPC method, which prefixes messages with "\x19Ethereum Signed Message:\n" before signing. For raw hash signatures, see Sign Raw Hash.

Overview

The useSignMessage hook from MoonKey’s React SDK provides a simple interface for signing messages. Common use cases include:
  • User authentication: Prove wallet ownership without passwords
  • Off-chain authorization: Sign permissions without gas fees
  • Proof of ownership: Verify control of an Ethereum address
  • Session tokens: Generate authenticated session credentials

React SDK

To sign a message, use the signMessage method from the useSignMessage hook:

Parameters

Message Object

The first parameter to signMessage:
string
required
The message to sign with the wallet. Can be any string.Example:

Options Object

The second parameter is an optional configuration object:
object
Configuration for the signing confirmation modal UI.
Wallet
Specific wallet to use for signing. If not provided, uses the user’s default wallet.Example:

Hook Callbacks

Configure callbacks when initializing the hook:
(result: { signature: string }) => void
Callback executed after a user successfully signs a message. Receives the signature.Example:
(error: Error) => void
Callback executed if signing fails or user cancels. Receives the error.Example:

Returns

The signMessage method returns a Promise that resolves with:
string
The hex-encoded signature produced by the wallet.Example:
'hex'
The encoding format of the signature. Currently always 'hex' for Ethereum.

Complete Examples

User Authentication

Session Authorization

Proof of Ownership

Verifying Signatures

After obtaining a signature, you can verify it using viem:

Backend Verification (Node.js)

UI Customization

Customize the signing modal to match your brand:

Hide Modal

For silent signing (ensure user has given explicit consent):
Hiding the signing modal removes the user’s ability to review the message before signing. Only use this for trusted operations or after obtaining explicit user consent.

Message Formatting Best Practices

Always make messages understandable to users:
Explain what the signature will be used for:
Include timestamps or nonces to prevent signature replay:
Include your app’s domain to prevent phishing:
For structured data, consider using EIP-712 (Sign Typed Data):

Error Handling

Handle common signing errors gracefully:

Best Practices

Never trust client-side signature verification alone:
If storing signatures, ensure proper security:
Add expiration to prevent old signatures from being reused:
Keep users informed throughout the signing process:
Don’t show errors when users deliberately cancel:

Troubleshooting

Common causes and solutions:
  • Wrong message: Ensure the exact same message is used for verification
  • Address mismatch: Verify the correct wallet address is used
  • Encoding issues: Message must be UTF-8 encoded string
  • Wrong verification method: Use verifyMessage for personal_sign
Handle cancellations without showing errors:
Ensure proper message encoding:
Ensure callbacks are properly configured:

Sign Typed Data

Sign structured EIP-712 data

Sign Transaction

Sign transactions without broadcasting

Sign Message UI

Customize the signing modal

Send Transaction

Sign and broadcast transactions

Next Steps