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"
}
}
Retrieve the current crypto key account associated with the user’s session
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"
}
}
Show CryptoKeyAccountResult properties
Show WebAuthnAccount properties
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"
}
}
Code | Message | Description |
---|---|---|
4001 | User denied account access | User rejected the account access request |
4100 | SDK not initialized | Base Account SDK not properly initialized |
4200 | Session expired | User session has expired, requires reconnection |
4300 | Account unavailable | Account temporarily unavailable |
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;
}
}
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
};
}
}
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;
}
Was this page helpful?