Overview
TheuseSignTransaction hook from MoonKey’s React SDK provides an interface for signing transactions without immediately broadcasting them. This is useful for scenarios where you need to:
- Batch multiple transactions for later submission
- Implement custom transaction broadcasting logic
- Store signed transactions for delayed execution
- Create transactions that will be submitted by a relayer
Signing vs Sending:
signTransaction only signs the transaction and returns the signature, while sendTransaction both signs and broadcasts the transaction to the network.React SDK
- Basic Usage
- With UI Customization
To sign a transaction, use the
signTransaction method from the useSignTransaction hook:Parameters
Transaction Object
The first parameter tosignTransaction is the transaction object:
string
required
The recipient address for the transaction.Example:
string
The amount of ETH to send in wei, as a hexadecimal string. Omit for contract interactions with no ETH transfer.Example:
string
The encoded function call data for contract interactions, as a hexadecimal string.Example:
number
required
The chain ID for the transaction.Example:
string
Optional gas limit as a hexadecimal string. MoonKey estimates gas automatically if not provided.Example:
string
Optional maximum fee per gas for EIP-1559 transactions, as a hexadecimal string.Example:
string
Optional maximum priority fee per gas for EIP-1559 transactions, as a hexadecimal string.Example:
number
Optional nonce for the transaction. MoonKey determines this automatically if not provided.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:
Returns
ThesignTransaction method returns a Promise that resolves with:
string
The RLP-encoded signed transaction, ready to be broadcast.Example:
string
The transaction signature.Example:
'rlp'
The encoding format of the signed transaction. Currently always
'rlp' for Ethereum.Complete Examples
Contract Interaction Signing
Broadcasting Signed Transactions
After signing a transaction, you can broadcast it usingviem’s public client:
UI Customization
Customize the signing confirmation modal:Hide Modal
For automated flows, you can sign without showing the modal:Signing vs Sending
Understanding the difference between signing and sending transactions:When to use signTransaction:
- Batch operations: Sign multiple transactions and submit together
- Relayer services: Submit through a third-party relayer for gasless transactions
- Delayed execution: Sign now, broadcast later
- Custom broadcasting: Implement your own transaction submission logic
- Multi-sig workflows: Collect signatures from multiple parties
When to use sendTransaction:
- Immediate execution: Transaction should execute right away
- Simple workflows: Standard transaction flow without special handling
- User-paid gas: User pays gas fees directly
- Real-time updates: Need immediate confirmation
Error Handling
Handle common signing errors:Best Practices
Store signed transactions securely
Store signed transactions securely
If storing signed transactions, use secure storage:
Set appropriate nonce values
Set appropriate nonce values
When batching transactions, manage nonces carefully:
Validate before signing
Validate before signing
Always validate transaction parameters before signing:
Handle transaction expiration
Handle transaction expiration
Signed transactions can become stale. Implement expiration logic:
Provide clear user feedback
Provide clear user feedback
Keep users informed about signing status:
Troubleshooting
Signed transaction won't broadcast
Signed transaction won't broadcast
Common causes when broadcasting fails:
- Stale nonce: The nonce used when signing is outdated
- Expired transaction: Too much time passed since signing
- Insufficient gas: Gas parameters were underestimated
- Invalid format: Signed transaction is malformed
User cancelled signing
User cancelled signing
Handle user cancellations gracefully:
Invalid chain ID
Invalid chain ID
Ensure correct chain ID for the target network:
Related Documentation
Send Transaction
Sign and broadcast transactions immediately
Sign Message
Sign arbitrary messages
Sign Transaction UI
Customize the signing confirmation UI
Interfacing Libraries
Use with viem and ethers
Next Steps
- Learn about sending transactions for immediate execution
- Explore message signing for authentication
- Understand contract interactions with web3 libraries
- Customize the signing UI to match your brand