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.
It should be safe to call enable()
. as many times as your client needs; the provider should assume this.
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:
- Javascript
- TypeScript
// 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();
import { IAVMWebClientCallbackOptions } from '@agoralabs-sh/avm-web-provider';
// initialized client
client.onEnable(({ error, result }: IAVMWebClientCallbackOptions) => {
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();
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:
- Javascript
- TypeScript
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 });
import { IAVMWebClientCallbackOptions } from '@agoralabs-sh/avm-web-provider';
const providerId: string = '02657eaf-be17-4efc-b0a4-19d654b2448e';
// initialized client
client.onEnable(({ error, result }: IAVMWebClientCallbackOptions) => {
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:
- Javascript
- TypeScript
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 });
import { IAVMWebClientCallbackOptions } from '@agoralabs-sh/avm-web-provider';
const genesisHash: string = 'SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=';
// initialized client
client.onEnable(({ error, result }: IAVMWebClientCallbackOptions) => {
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 });
If the network and the provider ID is specified, and the provider does not support the network, then a NetworkNotSupportedError
should be thrown.