import { getCryptoKeyAccount } from '@base-org/account';

const cryptoAccount = await getCryptoKeyAccount();
if (cryptoAccount?.account) {
  console.log('Account address:', cryptoAccount.account.address);
}
{
  account: {
    address: "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
    publicKey: "0x04a1b2c3d4e5f6...",
    type: "webauthn"
  }
}
Defined in the Base Account SDK
Retrieves the current crypto key account associated with the user’s session. Crypto keys are used as the default local signer implementation for sub accounts.

Parameters

This function takes no parameters.

Returns

result
CryptoKeyAccountResult
An object containing the user’s crypto key account information or null if none exists.
import { getCryptoKeyAccount } from '@base-org/account';

const cryptoAccount = await getCryptoKeyAccount();
if (cryptoAccount?.account) {
  console.log('Account address:', cryptoAccount.account.address);
}
{
  account: {
    address: "0xd46e8dd67c5d32be8058bb8eb970870f07244567",
    publicKey: "0x04a1b2c3d4e5f6...",
    type: "webauthn"
  }
}

Error Handling

CodeMessageDescription
4001User denied account accessUser rejected the account access request
4100SDK not initializedBase Account SDK not properly initialized
4200Session expiredUser session has expired, requires reconnection
4300Account unavailableAccount temporarily unavailable
Always check if the account exists before using it, as users may not be connected or may have disconnected.

Account State Management

import { getCryptoKeyAccount } from '@base-org/account';

class AccountManager {
  private currentAccount: WebAuthnAccount | LocalAccount | null = null;
  
  async initialize() {
    const cryptoAccount = await getCryptoKeyAccount();
    this.currentAccount = cryptoAccount?.account || null;
    
    return this.isConnected();
  }
  
  isConnected(): boolean {
    return !!this.currentAccount;
  }
  
  getAddress(): string | null {
    return this.currentAccount?.address || null;
  }
  
  getAccountType(): 'webauthn' | 'local' | null {
    return this.currentAccount?.type || null;
  }
  
  async refresh() {
    const cryptoAccount = await getCryptoKeyAccount();
    const newAccount = cryptoAccount?.account;
    
    // Check if account changed
    if (newAccount?.address !== this.currentAccount?.address) {
      console.log('Account changed:', newAccount?.address);
      this.currentAccount = newAccount;
      return true;
    }
    
    return false;
  }
}

Integration with Provider

import { getCryptoKeyAccount, createBaseAccountSDK } from '@base-org/account';

async function initializeApp() {
  const sdk = createBaseAccountSDK({
    appName: 'My App',
    appLogoUrl: 'https://example.com/logo.png',
    appChainIds: [8453], // Base mainnet
  });
  
  // Check current account status
  const cryptoAccount = await getCryptoKeyAccount();
  
  if (cryptoAccount?.account) {
    console.log('User is already connected:', cryptoAccount.account.address);
    
    // Get provider for transactions
    const provider = sdk.getProvider();
    
    return {
      sdk,
      provider,
      account: cryptoAccount.account,
      isConnected: true
    };
  } else {
    console.log('User needs to connect');
    
    return {
      sdk,
      provider: null,
      account: null,
      isConnected: false
    };
  }
}

Account Verification

async function verifyAccountAccess() {
  const cryptoAccount = await getCryptoKeyAccount();
  
  if (!cryptoAccount?.account) {
    throw new Error('No account available');
  }
  
  const { account } = cryptoAccount;
  
  // Verify account has required properties
  if (!account.address || !account.publicKey) {
    throw new Error('Invalid account data');
  }
  
  // Verify address format
  if (!/^0x[a-fA-F0-9]{40}$/.test(account.address)) {
    throw new Error('Invalid address format');
  }
  
  return account;
}