Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

useGrantPermissions

Grant permissions to session keys using EIP-7715 for scoped, time-limited transactions.

Usage

import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import { useGrantPermissions } from '@openfort/react';
 
function CreateSessionKey() {
  const { grantPermissions, isLoading } = useGrantPermissions();
 
  const handleCreate = async () => {
    const sessionKey = generatePrivateKey();
    const sessionAccount = privateKeyToAccount(sessionKey);
 
    // Store the session key. You need it later to sign transactions.
    localStorage.setItem('sessionKey', sessionKey);
 
    const result = await grantPermissions({
      request: {
        signer: {
          type: 'account',
          data: { id: sessionAccount.address },
        },
        expiry: 60 * 60 * 24, // 24 hours
        permissions: [
          {
            type: 'contract-call',
            data: {
              address: '0x2522f4fc9af2e1954a3d13f7a5b2683a00a4543a',
              calls: [],
            },
            policies: [],
          },
        ],
      },
    });
 
    console.log('Permissions granted:', result.address);
  };
 
  return <button onClick={handleCreate} disabled={isLoading}>Create Session Key</button>;
}

Return type

type UseGrantPermissionsReturn = {
  grantPermissions(params: GrantPermissionsParams, options?: GrantPermissionsHookOptions): Promise<GrantPermissionsHookResult>
  data: GrantPermissionsResult | null
  reset(): void
  isLoading: boolean
  isError: boolean
  isSuccess: boolean
  error?: OpenfortError
}
 
type GrantPermissionsResult = {
  address: `0x${string}`  // Wallet that granted permissions
} & GrantPermissionsReturnType  // From viem/experimental
 
type GrantPermissionsHookResult = {
  error?: OpenfortError
} & Partial<GrantPermissionsResult>

Hook options

Configure default behavior when initializing the hook.

type GrantPermissionsHookOptions = {
  onSuccess?: (data: GrantPermissionsHookResult) => void
  onError?: (error: OpenfortError) => void
  onSettled?: (data: GrantPermissionsHookResult | undefined | null, error: OpenfortError | null) => void
  throwOnError?: boolean
}

Parameters

grantPermissions

type GrantPermissionsParams = {
  request: GrantPermissionsParameters  // From viem/experimental
}
 
// GrantPermissionsParameters includes:
type GrantPermissionsParameters = {
  signer: {
    type: 'account'
    data: { id: string }  // Session account address
  }
  expiry: number  // Duration in seconds
  permissions: Permission[]
}
 
type Permission = {
  type: 'contract-call' | 'native-token-transfer' | 'erc20-transfer' | 'erc721-transfer' | 'erc1155-transfer'
  data: {
    address: string
    calls?: []
  }
  policies?: []
}

Related

Copyright © 2023-present Alamas Labs, Inc