Establish a Connection

After detecting the provider, an application can request to connect to Starkey. This connection request will prompt the user to grant permission to share their public key, signaling their willingness to engage further. Users must approve the connection request before the app can proceed with additional actions, such as signing a message or sending a transaction.

Once permission is granted for the first time, the web application's domain will be whitelisted for future connection requests. After a connection is established, either the application or the user can choose to terminate the connection.

Connecting


To connect to Starkey by default, you can call the window.starkey function.

const provider = getProvider(); // see "Detecting the Provider"
try {
    const accounts = await provider.connect();
    console.log(accounts[0]);
    // 0x534583cd8cE0ac1af4Ce01Ae4f294d52b4Cd305F
} catch (err) {
    // { code: 4001, message: 'User rejected the request.' }
}

The connect method will return a Promise. If it resolves, it returns an array where the connected address is in the 0th index. If the user declines the request or closes the pop-up, it will reject (throw when awaited).

Once the web application is connected to StarKey, it will be able to read the connected account's address and prompt the user for additional transactions. It also exposes a convenience account method.

const accounts = await provider.account();
console.log(accounts[0]);
// 0x534583cd8cE0ac1af4Ce01Ae4f294d52b4Cd305F

Disconnecting

Once a user has established a connection, StarKey will add the website they opened a connection with to a list of apps. The user can then revoke access through the UI at any time, and will need to reconnect.

To disconnect StarKey from dApp, you can call the disconnect method.

const provider = getProvider();
await provider.disconnect();

Changing Accounts

StarKey allows users to seamlessly manage multiple accounts (i.e., addresses) from within a single extension or mobile app. Whenever a user switches accounts, StarKey will emit an accountChanged event.

If a user changes accounts while already connected to an application, and the new account had already whitelisted that application, then the user will stay connected and StarKey will pass the public key of the new account.

provider.on('accountChanged', (accounts: string[]) => {
    if (accounts.length > 0) {
        // Set new public key and continue as usual
        console.log(`Switched to account ${accounts[0]}`);
    } 
});

Last updated