Skip to main content

Active Account

The Active Account feature in the Beacon SDK is crucial for your dApp to interact effectively with the user's wallet. Here's how it works:

  • getActiveAccount: This function returns the current active account. Use it to check the user's active account when they first start using your dApp.
  • Active Account Subscription: Beyond just checking the current account, Beacon SDK allows your dApp to subscribe to any changes in the active account (BeaconEvent.ACTIVE_ACCOUNT_SET): This includes when the user switches to a different account or disconnects an account from their wallet.

It's important to implement this subscription so your dApp can react to account changes in real-time. This ensures your application always interacts with the correct account.

Example
import { BeaconEvent, DAppClient } from "@airgap/beacon-sdk";

const dAppClient = new DAppClient({ name: "Beacon Docs" });

// Listen for all the active account changes
dAppClient.subscribeToEvent(BeaconEvent.ACTIVE_ACCOUNT_SET, async (account) => {
  // An active account has been set, update the dApp UI
  console.log(`${BeaconEvent.ACTIVE_ACCOUNT_SET} triggered: `, account);
});

try {
  console.log("Requesting permissions...");
  const permissions = await dAppClient.requestPermissions();
  console.log("Got permissions:", permissions.address);
} catch (error) {
  console.error("Got error:", error.message);
}
Loading...