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.
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).
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.
Using dag_requestAccounts
or eth_requestAccounts
RPC methods
Calling dag_requestAccounts
or eth_requestAccounts
RPC methods, depending on the provider being used, will send an activation request for the user to accept. If the user accepts the request, the RPC method will return available accounts for the provider; if not, it will throw an error.
await dagProvider.request({ method: "dag_requestAccounts", params: [] });
// ["DAG88C9WDSKH451sisyEP3hAkgCKn5DN72fuwjfX"] provider was activated
await ethProvider.request({ method: "eth_requestAccounts", params: [] });
// ["0xAab2C30c02016585EB36b7a0d5608Db787c1e44E"] provider was activated
Read more about the different RPC methods available both for Constellation and Ethereum.
Activate method (deprecated)
Warning
This method of activation has been deprecated in favor of the EIP-1102 specification, in both Constellation and Ethereum providers.
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.
const activated = await provider.activate("A Cool App Name");
Read more about the different RPC methods available both for Constellation and Ethereum.
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:
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.