Skip to main content

Provider Activation

A chain provider allows you to interact with any available network. In this guide, you will learn how to obtain a chain provider and activate it.

Tip

With the Stargazer Extension installed you can test the following examples in the browser console (devtools).

Detect Stargazer

The Stargazer browser extension injects a WalletProvider instance under window.stargazer each time a page loads. You can check the existence of this property using the following snippet.

TypeScript
if (window.stargazer) {
console.log("Stargazer version " + window.stargazer.version + " detected");
} else {
console.log("Stargazer not detected");
}

Obtain a ChainProvider

Once you've verified your app has access to a WalletProvider instance you can obtain a ChainProvider to interact with a network of your choice (Constellation or Ethereum).

TypeScript
const provider = window.stargazer.getProvider("constellation");

Read more about the WalletProvider API and the ChainProvider API.

Activate your provider

Activating the provider is required before it can be used to interact with the user's wallet. When activation is triggered, a popup is triggered for the user to allow your site access to their wallet. The user may choose a subset of their wallets to share if they have multiple. Activation can be achieved with one of the following methods.

Activate method

You can send an activation request to the user using the provider's activate() method. Once the user accepts the request you'll be able to use the provider's RPC interface and methods for the selected chain.

TypeScript
const activated = await provider.activate("A Cool App Name");

Read more about the different RPC methods available both for Constellation and Ethereum.

Other RPC methods

You can also call any RPC related method such as request() or on() to activate the provider. It will send an internal activation request for the user to accept. If the user accepts the request, the RPC-related method is executed; if not, it will throw an error.

Warning

If the user denies an activation request, the next calls to any RPC related method will immediately throw an error. No further internal activation requests will be sent but you can trigger activation again using the .activate method.

TypeScript
await provider.request({ method: "dag_accounts", params: [] });
// ["DAG88C9WDSKH451sisyEP3hAkgCKn5DN72fuwjfX"] provider was activated

Scope of the activation

Activations are issued for the page origin and cover all chains available (currently Constellation and Ethereum) and on all providers given. If you have been granted activation in the past the user will not be asked to grant it again. If the user is logged out, they will be prompted to log in again.

ChainProvider identity

All chain providers are instantiated once per page, and per chain with the following setup:

TypeScript
const constellationProviderA = window.stargazer.getProvider("constellation");
const constellationProviderB = window.stargazer.getProvider("constellation");

const ethereumProviderA = window.stargazer.getProvider("ethereum");
const ethereumProviderB = window.stargazer.getProvider("ethereum");

Two chain providers from the same network and page will share the same underlying reference:

  • Object.is(constellationProviderA, constellationProviderB) will be true.

  • Object.is(ethereumProviderA, ethereumProviderB) will be true.

  • Object.is(constellationProviderA, ethereumProviderB) will be false.

  • Object.is(constellationProviderB, ethereumProviderA) will be false.