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
TheuseSignMessage 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
- Basic Usage
- With Callbacks
- With UI Customization
To sign a message, use the
signMessage method from the useSignMessage hook:Parameters
Message Object
The first parameter tosignMessage:
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
ThesignMessage 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 usingviem:
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):Message Formatting Best Practices
Use clear, human-readable messages
Use clear, human-readable messages
Always make messages understandable to users:
Include context and purpose
Include context and purpose
Explain what the signature will be used for:
Add timestamps to prevent replay attacks
Add timestamps to prevent replay attacks
Include timestamps or nonces to prevent signature replay:
Specify the domain or app
Specify the domain or app
Include your app’s domain to prevent phishing:
Use structured data for complex operations
Use structured data for complex operations
For structured data, consider using EIP-712 (Sign Typed Data):
Error Handling
Handle common signing errors gracefully:Best Practices
Always validate on the backend
Always validate on the backend
Never trust client-side signature verification alone:
Store signatures securely
Store signatures securely
If storing signatures, ensure proper security:
Implement signature expiration
Implement signature expiration
Add expiration to prevent old signatures from being reused:
Provide clear user feedback
Provide clear user feedback
Keep users informed throughout the signing process:
Handle user cancellations gracefully
Handle user cancellations gracefully
Don’t show errors when users deliberately cancel:
Troubleshooting
Signature verification fails
Signature verification fails
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
verifyMessageforpersonal_sign
User cancelled signing
User cancelled signing
Handle cancellations without showing errors:
Message encoding issues
Message encoding issues
Ensure proper message encoding:
Callback not firing
Callback not firing
Ensure callbacks are properly configured:
Related Documentation
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
- Learn about signing typed data for structured messages
- Explore signature verification with viem
- Understand authentication patterns with MoonKey
- Customize the signing UI to match your brand