Skip to main content
useSessionKey provides programmatic access to session key lifecycle management: loading, setup, and revocation.

Import

import { useSessionKey } from '@taprails/tap-to-pay';

Usage

const {
  sessionKey,
  setupSessionKey,
  revokeSessionKey,
  isLoading,
  error,
  refresh,
} = useSessionKey();

Return Value

sessionKey
SessionKeyData | null
The current session key data loaded from the device Keychain, or null if no key exists. Loaded automatically on mount.
setupSessionKey
(walletAddress?: string) => Promise<SessionKeyData>
Triggers the full session key setup flow: generates a keypair, calls onSignTransaction for user approval, and registers the public key with the backend. Returns the created key.
revokeSessionKey
() => Promise<void>
Revokes the current session key on the backend and deletes it from the Keychain.
isLoading
boolean
true during setupSessionKey or revokeSessionKey operations.
error
Error | null
Error from the most recent operation.
refresh
() => Promise<void>
Re-reads the session key from the Keychain. Useful after external changes.

SessionKeyData

interface SessionKeyData {
  publicKey: string;   // ECDSA public key (PEM format)
  expiresAt: number;   // Unix timestamp (ms) when the key expires
  walletAddress: string;
}

Example

import { useSessionKey } from '@taprails/tap-to-pay';

export function SessionKeySettings() {
  const { sessionKey, revokeSessionKey, isLoading } = useSessionKey();

  const expiresAt = sessionKey ? new Date(sessionKey.expiresAt) : null;
  const isExpired = expiresAt ? expiresAt < new Date() : false;

  return (
    <View>
      {sessionKey ? (
        <>
          <Text>
            Status: {isExpired ? '⚠️ Expired' : '✅ Active'}
          </Text>
          <Text>
            Expires: {expiresAt?.toLocaleDateString()}
          </Text>
          <Button
            title="Revoke Tap-to-Pay Access"
            onPress={revokeSessionKey}
            disabled={isLoading}
          />
        </>
      ) : (
        <Text>No session key — tap-to-pay not set up.</Text>
      )}
    </View>
  );
}