Skip to main content

Enabling A Client

Overview

Before we can start interacting with a provider, we need ask the provider to enable the client. This will achieve two things:

  • if necessary, it will authorize your client with the provider; and
  • it will get a list of the authorized accounts available to your client.
note

It should be safe to call enable(). as many times as your client needs; the provider should assume this.

caution

The definition of "enabling" for a provider can mean different things to different providers, but it is highly recommended that you first run enable() before attempting any signing/post methods.

Enabling the client with all providers

After initialization, you can simply call:

// initialized client
client.onEnable(({ error, result }) => {
if (error) {
console.error('error:', error);

return;
}

console.log(result);
/*
{
accounts: [
{
address: 'P3AIQVDJ2CTH54KSJE63YWB7IZGS4W4JGC53I6GK72BGZ5BXO2B2PS4M4U',
name: 'Wallet-1',
},
{
address: '6GT6EXFDAHZDZYUOPT725ZRWYBZDCEGYT7SYYXGJKRFUAG5B7JMI7DQRNQ',
name: 'Wallet-2',
},
],
genesisHash: 'SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=',
genesisId: 'testnet-v1.0',
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
sessionId: 'ab192498-0c63-4028-80fd-f148710611d8',
}
*/
});

// send a enable request
client.enable();
caution

If any providers do not support the enable method, then a MethodNotSupportedError will be returned.

Enabling the client with a specific provider

If you want to target a specific provider, you can simply pass the ID of the provider in the params:

const providerId = '02657eaf-be17-4efc-b0a4-19d654b2448e';

// initialized client
client.onEnable(({ error, result }) => {
if (error) {
console.error('error:', error);

return;
}

console.log(result);
/*
{
accounts: [
{
address: 'P3AIQVDJ2CTH54KSJE63YWB7IZGS4W4JGC53I6GK72BGZ5BXO2B2PS4M4U',
name: 'Wallet-1',
},
{
address: '6GT6EXFDAHZDZYUOPT725ZRWYBZDCEGYT7SYYXGJKRFUAG5B7JMI7DQRNQ',
name: 'Wallet-2',
},
],
genesisHash: 'SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=',
genesisId: 'testnet-v1.0',
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
sessionId: 'ab192498-0c63-4028-80fd-f148710611d8',
}
*/
});

// send a enable request
client.enable({ providerId });

Enabling a client on a specific network

If you want to target a specific network, and any providers support it, you can simply pass the genesis hash of the network in the params:

const genesisHash = 'SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=';

// initialized client
client.onEnable(({ error, result }) => {
if (error) {
console.error('error:', error);

return;
}

console.log(result);
/*
{
accounts: [
{
address: 'P3AIQVDJ2CTH54KSJE63YWB7IZGS4W4JGC53I6GK72BGZ5BXO2B2PS4M4U',
name: 'Wallet-1',
},
{
address: '6GT6EXFDAHZDZYUOPT725ZRWYBZDCEGYT7SYYXGJKRFUAG5B7JMI7DQRNQ',
name: 'Wallet-2',
},
],
genesisHash: 'SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=',
genesisId: 'testnet-v1.0',
providerId: '02657eaf-be17-4efc-b0a4-19d654b2448e',
sessionId: 'ab192498-0c63-4028-80fd-f148710611d8',
}
*/
});

// send a enable request
client.enable({ genesisHash });
caution

If the network and the provider ID is specified, and the provider does not support the network, then a NetworkNotSupportedError should be thrown.