NERO
React SDKHooks

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

FieldTypeDescription
walletWalletInfo | nullCurrent wallet information; null if not generated
hasWalletbooleanIndicates 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
isGeneratingbooleanLoading state during wallet generation
isSigningbooleanLoading state during signing operations
errorError | nullLast operation error; reset at operation start

State Management

Derived state (from context):

  • wallet and hasWallet derive from the shared SDK state, not local state
  • Updated automatically when the SDK emits authentication or connection events

Local state:

  • isGenerating, isSigning, and error are managed via useState

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:

  1. Reset error to null at operation start
  2. On rejection, set error and re-throw for caller handling
  3. Always reset loading flags in finally blocks

Memoization

The three operation callbacks use useCallback:

  • generateWallet depends on [sdk]
  • signMessage and signTypedData depend 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

On this page