useNeroWallet
Wallet generation, message signing, and EIP-712 typed data signing
useNeroWallet
The useNeroWallet React hook provides wallet generation and signing capabilities to components wrapped by NeroMpcAuthProvider.
Usage
import { useNeroWallet } from "@nerochain/mpc-sdk/react";
function WalletView() {
const {
wallet,
hasWallet,
generateWallet,
signMessage,
signTypedData,
isGenerating,
isSigning,
error,
} = useNeroWallet();
if (!hasWallet) {
return (
<button onClick={generateWallet} disabled={isGenerating}>
{isGenerating ? "Generating..." : "Generate Wallet"}
</button>
);
}
return (
<div>
<p>EOA: {wallet?.eoaAddress}</p>
<button
onClick={() => signMessage("Hello, NERO!")}
disabled={isSigning}
>
Sign Message
</button>
{error && <p>{error.message}</p>}
</div>
);
}Return Value
| Field | Type | Description |
|---|---|---|
wallet | WalletInfo | null | Current wallet information; null if not generated |
hasWallet | boolean | Indicates existing key share for current user |
generateWallet | () => Promise<WalletInfo> | Triggers MPC key generation protocol |
signMessage | (message: string) => Promise<string> | Signs Ethereum personal messages (EIP-191) |
signTypedData | (domain, types, primaryType, value) => Promise<string> | Signs EIP-712 typed data |
isGenerating | boolean | Loading state during wallet generation |
isSigning | boolean | Loading state during signing operations |
error | Error | null | Last operation error; reset at operation start |
State Management
Derived state (from context):
walletandhasWalletderive from the shared SDK state, not local state- Updated automatically when the SDK emits authentication or connection events
Local state:
isGenerating,isSigning, anderrorare managed viauseState
generateWallet()
Initiates MPC key generation. Throws synchronously if the SDK is uninitialized. Sets isGenerating to true, resets error to null, and always clears the loading flag in the finally block.
const { generateWallet, isGenerating, error } = useNeroWallet();
try {
const walletInfo = await generateWallet();
console.log("Address:", walletInfo.eoaAddress);
} catch (err) {
console.error("Generation failed:", err);
}signMessage(message)
Signs arbitrary strings using the Ethereum personal signing prefix. Requires both the SDK and an existing wallet. Throws "Wallet not available" if preconditions are unmet, without entering the loading state.
const { signMessage, isSigning } = useNeroWallet();
const signature = await signMessage("Hello, NERO!");
console.log("Signature:", signature);signTypedData(domain, types, primaryType, value)
Signs EIP-712 typed data. Accepts a domain struct, type definitions map, primary type name, and message data. Returns a hex signature string.
const { signTypedData, isSigning } = useNeroWallet();
const signature = await signTypedData(
{ name: "MyApp", version: "1", chainId: 689 },
{ Transfer: [{ name: "to", type: "address" }, { name: "amount", type: "uint256" }] },
"Transfer",
{ to: "0x1234...", amount: "1000000000000000000" }
);Error Handling Pattern
All operations follow consistent error semantics:
- Reset
errortonullat operation start - On rejection, set
errorand re-throw for caller handling - Always reset loading flags in
finallyblocks
Memoization
The three operation callbacks use useCallback:
generateWalletdepends on[sdk]signMessageandsignTypedDatadepend on[sdk, state.hasWallet]
This ensures stable references across renders.
Context Requirement
The hook requires a parent NeroMpcAuthProvider. If used outside this provider, it throws:
useNeroMpcAuthContext must be used within a NeroMpcAuthProvider