Skip to main content
Version: v2

Connecting wallet

You can convert Passport's native EIP-1193 Provider into an EthersJS compatible provider such as a BrowserProvider.

To create the BrowserProvider first fetch the Passport provider by calling await passportInstance.connectEvm() after initialising Passport. Then create a new EtherJS BrowserProvider and pass the Passport provider into the constructor.

// fetch the Passport provider from the Passport instance
const [passportProvider, setPassportProvider] = useState<Provider>();

useEffect(() => {
const fetchPassportProvider = async () => {
const passportProvider = await passportInstance.connectEvm();
setPassportProvider(passportProvider);
};
fetchPassportProvider();
}, []);

// create the BrowserProvider using the Passport provider
const browserProvider = useMemo(() => passportProvider ? new BrowserProvider(passportProvider) : undefined, [passportProvider]);

Once the BrowserProvider is created, you can call various methods on it via the send function (which is protocol-agnostic), for example:

// calling eth_requestAccounts triggers the Passport login flow
const accounts = await browserProvider.send('eth_requestAccounts', []);

Calling the eth_requestAccounts request method will trigger the login flow for Passport if the user is not signed in and will connect the BrowserProvider to the Passport instance.

Checkout the full working example here: Passport with NextJS Connect with EtherJS.

Provider Events

As per the EIP-1193 specification, the zkEVM provider exposes an on and removeListener function, which can be used to subscribe to and unsubscribe from events emitted by the zkEVM provider

Supported Events

Accounts Changed

The zkEVM provider will emit an accountsChanged event when the accounts available to the provider changes (for example, when eth_requestAccounts is called and the users wallet is initialised):

// listen to the ACCOUNTS_CHANGED event and update the accounts state when it changes
passportProvider?.on(ProviderEvent.ACCOUNTS_CHANGED, (accounts: string[]) => {
setAccountsState(accounts);
});