useSessionKeyClient

This hook initializes and returns a session key client using the provided API key, session data, and private key.

Returns:

Property

Type

Description

data

SmartSessionClient or undefined

The session key client if it was successfully created, otherwise undefined.

error

Error or null

An error object if the client creation failed, otherwise null.

isPending

boolean

A boolean indicating whether the client is currently being created.

isSuccess

boolean

A boolean indicating whether the session key client was successfully created.

isError

boolean

A boolean indicating whether an error occurred during the client creation.

Usage Example:

import { useSessionKeyClient } from "path/to/hook";

const Component = () => {
  const { data: sessionKeyClient, error, isPending, isSuccess, isError } =
    useSessionKeyClient({
      apiKey: "YOUR_API_KEY",
      sessionData: ..., 
      privateKey: ...,
    });

  if (isPending) return <p>Initializing session client...</p>;
  if (isError) return <p>Error: {error?.message}</p>;

  return (
    <div>
      {isSuccess && <p>Session Key Client Initialized!</p>}
      {/* Example usage */}
      <button
        onClick={() => {
          if (sessionKeyClient) {
            // Call methods on the session key client
            console.log("Session Key Client:", sessionKeyClient);
          }
        }}
      >
        Use Session Key Client
      </button>
    </div>
  );
};

How It Works:

This hook leverages wagmi’s useQuery to initialize the SmartSessionClient asynchronously. The process involves:

1. Using the useSessionKeySigner hook to retrieve the session key signer based on the provided session data and private key.

2. Verifying that the smartAccountClient and sessionKeySigner are available.

3. Calling createSessionSmartAccountClient to initialize the session key client.

4. Returning the query result containing the client and its state.

Error Handling:

If an error occurs during the client initialization, the error object will provide details about what went wrong. You can use this to display user-friendly error messages.

Customization:

You can customize the behavior of the query by extending it through wagmi options (e.g., setting stale time, retries, or refetching conditions if needed).

Last updated