Connect 4337
Cometh ConnectCometh MarketplaceCometh Checkout
  • 🚀Quick start
    • What is Connect 4337
    • Getting started
    • Supported Networks
  • 🛠️CORE FEATURES
    • Create a Wallet
    • Send transactions
    • Go Gasless
    • Sign/Verify a message
    • Retrieve a wallet address
    • Handle owners
    • Import a safe into connect
  • 🥷ADVANCED
    • Session Keys
      • Tutorial
      • Policies
        • Sudo policy
        • Action policy
    • Social recovery
    • Add a passkey signer on a different OS
    • Capabilities
      • sendCalls
      • getCallsStatus
      • getCapabilities
      • grantPermissions
    • Other signers (Auth Providers)
      • EOA wallets (Metamask, Phantom...)
      • Magic signer
      • Web3Auth signer
      • Turnkey signer
      • Privy signer
  • 🔌Integrations
    • React hooks
      • ConnectProvider
      • useAccount
      • useConnect
      • useDisconnect
      • useGetGasPrice
      • useSendTransaction
      • useSignMessage
      • useVerifyMessage
      • useWriteContract
      • Handle owners
        • useRemoveOwner
        • useValidateAddDevice
        • useCreateNewSigner
        • useAddOwner
        • useGetOwners/EnrichedOwners
      • Session Keys
        • useGrantPermission
        • useSendPermission
        • useSessionKeyClient
        • useSessionKeySigner
      • Recovery
        • useIsRecoveryActive
        • useSetUpRecovery
        • useGetRecoveryRequest
        • useCancelRecoveryRequest
    • Mobile SDKs
      • IOS
      • Android
      • React Native
    • Wagmi
  • SDK Core
    • Signers (Auth Providers)
      • EOA wallets (Metamask, Phantom...)
      • Magic signer
      • Web3Auth signer
      • Turnkey signer
      • Privy signer
    • Handle owners
    • Capabilities
      • sendCalls
      • getCallsStatus
      • getCapabilities
  • SDK Session Keys
    • Setup Smart Account Client
    • Manage session keys
    • Policies
      • Sudo policy
      • Action policy
  • 📦Bundler
    • Bundler API
      • eth_sendUserOperation
      • eth_estimateUserOperationGas
      • eth_getUserOperationByHash
      • eth_getUserOperationReceipt
      • eth_supportedEntryPoints
  • 💳Paymaster
    • Paymaster API
  • 📖RESOURCES
    • Migrate from the connect legacy SDK
    • Connect Legacy SDKs (Unity, JS)
    • FAQ
Powered by GitBook
On this page
  • Returns:
  • Explanation:
  • Usage Example:
  • How It Works:
  • Example of Expected Response:
  • Error Handling:
  • Mutation Props (mutationProps):
  1. Integrations
  2. React hooks
  3. Session Keys

useGrantPermission

This hook provides functionality to asynchronously grant permissions and monitor the status of the transaction.

Returns:

Property

Type

Description

data

GrantPermissionMutateResponse or undefined

The response object containing the transaction hash and session details if the mutation is successful, otherwise undefined.

error

Error or null

An error object if the transaction failed, otherwise null.

isPending

boolean

A boolean indicating whether the transaction is currently pending.

isSuccess

boolean

A boolean indicating whether the transaction was successfully sent.

isError

boolean

A boolean indicating whether an error occurred during the transaction process.

grantPermission

(variables: GrantPermissionParameters<ComethSafeSmartAccount>) => void

A function to trigger the grant permission for a sessionKey.

grantPermissionAsync

(variables: GrantPermissionParameters<ComethSafeSmartAccount>) => Promise<GrantPermissionMutateResponse>

A function to trigger the grant permission for a sessionKey and return a promise resolving to the transaction details.

GrantPermissionMutateResponse Structure:

type GrantPermissionMutateResponse = {
  txHash: Hash;
  createSessionsResponse: GrantPermissionResponse;
};

Explanation:

• txHash: The hash of the transaction that was sent to the blockchain.

• createSessionsResponse: The response from the grantPermission operation, including details about the session and its status.

Usage Example:

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

const Component = () => {
  const {
    data,
    error,
    isPending,
    isSuccess,
    isError,
    grantPermission,
    grantPermissionAsync,
  } = useGrantPermission();

  const handleGrantPermission = () => {
    grantPermission(permission);
  };

  const handleAsyncGrantPermission = async () => {
    try {
      const result = await grantPermissionAsync(permission);
      console.log("Transaction hash:", result.txHash);
    } catch (err) {
      console.error("Permission grant failed:", err);
    }
  };

  return (
    <div>
      {isPending && <p>Granting permission...</p>}
      {isSuccess && <p>Permission granted successfully!</p>}
      {isError && <p>Error: {error?.message}</p>}
      <button onClick={handleGrantPermission}>Grant Permission</button>
      <button onClick={handleAsyncGrantPermission}>Grant Permission (Async)</button>
    </div>
  );
};

How It Works:

The hook relies on the smartAccountClient to extend actions from the cometh/connect-sdk-4337 package. It executes the following steps:

1. Extends the smart account with necessary actions (erc7579Actions, smartSessionActions).

2. Calls the grantPermission method with the provided parameters.

3. Waits for confirmation using waitForUserOperationReceipt.

4. Returns the transaction hash and the session response.

Example of Expected Response:

{
  "txHash": "0x123abc456def...",
  "createSessionsResponse": {
    "userOpHash": "0x789xyz...",
    "session": ...,
    "permissionIds": ...;
    "action": ...;
  }
}

Error Handling:

If the mutation fails, the error object provides details about the failure, which can be used to display custom error messages to users.

Mutation Props (mutationProps):

The hook accepts optional custom mutation parameters through the MutationOptionsWithoutMutationFn object, allowing you to adjust its behavior if needed.

PreviousSession KeysNextuseSendPermission

Last updated 3 months ago

🔌