> ## Documentation Index
> Fetch the complete documentation index at: https://docs.presschain.io/llms.txt
> Use this file to discover all available pages before exploring further.

# PressKey Events

> Keep account, chain and lock state synchronized with the live PressKey session.

# PressKey events

A connected browser identity is not static. Users can change accounts, lock PressKey or encounter a network change while an application remains open. PressKey exposes provider events so the application can respond without relying on stale state.

## Accounts changed

```ts theme={null}
const provider = window.presschain;

function handleAccountsChanged(accounts: string[]) {
  sessionStore.setState({
    account: accounts[0] ?? null,
  });
}

provider.on("accountsChanged", handleAccountsChanged);
```

When the account changes, invalidate role, bond, outlet and Capsule-relationship caches tied to the previous address.

Do not update only the avatar while leaving authorization state untouched.

## Chain changed

```ts theme={null}
function handleChainChanged(chainId: string) {
  sessionStore.setState({ chainId });

  if (String(chainId).toLowerCase() !== "0x498b64a") {
    disablePressChainWrites();
  }
}

provider.on("chainChanged", handleChainChanged);
```

A network mismatch should disable writes and clearly explain the required network rather than allowing a transaction to fail later.

## Lock changed

Current provider behavior includes a `lockChanged` event. This is useful for flows that prepare an action over several minutes.

```ts theme={null}
provider.on("lockChanged", ({ locked }: { locked: boolean }) => {
  sessionStore.setState({ signerLocked: locked });
});
```

If PressKey locks after metadata upload but before signing, keep the prepared action and prompt for unlock rather than forcing the user to rebuild the publication.

## Remove listeners

Single-page applications can accidentally subscribe several times during navigation or hot reload.

```ts theme={null}
provider.removeListener("accountsChanged", handleAccountsChanged);
provider.removeListener("chainChanged", handleChainChanged);
```

Clean up listeners when the component or session manager that owns them is disposed.

## Invalidate derived authorization state

An account change should invalidate data such as:

* protocol role
* active bond
* outlet permissions
* contributor relationship
* existing evidence vote
* bounty assignment
* pending unsigned transaction intent

The last item is especially important. A transaction prepared for one account should not silently be signed by a newly selected account.

## Pending transaction state is separate

Do not delete a submitted transaction from local state simply because the wallet disconnects. Once the transaction hash exists, confirmation belongs to network observation, not wallet session state.

A robust state model distinguishes:

```ts theme={null}
type SessionState = {
  account: string | null;
  chainId: string | null;
  locked: boolean;
};

type PendingTransaction = {
  hash: string;
  action: string;
  submittedBy: string;
  submittedAt: string;
};
```

That lets the application reconnect and continue tracking correctly.

## UX principle

Provider events are not merely technical callbacks. They tell the application that the user’s authorization context changed. Make that change visible before the next irreversible action.
