⌨
Connector API
The Connector API is a new addition to the Altura SDK, which allows DApps to connect to and integrate with wallets easily. Using a connector, the Altura SDK automatically fills in the user's wallet information for certain SDK methods, such as get users.
export interface IConnector {
get address(): string | null
get provider(): ethers.providers.Web3Provider | null
connect: () => Promise<string | null>
sign: (message: string) => Promise<string | null>
}
Connectors can optionally be passed into the constructor for the
Altura
class. Doing so allows for easier access integration with wallets via the Altura SDK.By default, the Altura SDK provides two implementations for connectors:
First-party support for more common wallets may be added in the future.
To make a wallet compatible with the connector API, it needs a connector that follows the
IConnector
interface. The
IConnector
interface contains two functions and two fields:This function should connect the wallet to the application, usually prompting the user in the process, returning either the wallet address or
null
representing a failed state. This function is invoked in the constructor of the Altura
class.This function should have the wallet sign an arbitrary message, returning either the signed message or
null
representing a failed state.This getter should return the wallet address as a string, or
null
representing a failed state. While this is a getter for versatility, most implementations will want to use a backing field.This getter should return the wallet provider or
null
represent a failed state. While this should be implemented, the use of the provider directly is discouraged in favor of existing abstractions. While this is a getter for versatility, most implementations will want to use a backing field. import { IConnector } from './type';
import { ethers } from 'ethers';
export class MetamaskConnector implements IConnector {
private _provider: ethers.providers.Web3Provider | null = null;
private _address: string | null = null;
public get provider() {
return this._provider;
}
public get address() {
return this._address;
}
/**
* A basic constructor for connecting to MetaMask
* @param ethereum The `window.ethereum` MetaMask instance
*/
constructor(ethereum: ethers.providers.ExternalProvider | ethers.providers.JsonRpcFetchFunc) {
this._provider = new ethers.providers.Web3Provider(ethereum);
}
public async connect(): Promise<string | null> {
await this.provider?.send('eth_requestAccounts', []);
this._address = (await this.provider?.getSigner()?.getAddress()) || null;
return this.address;
}
public async sign(message: string): Promise<string | null> {
return this.provider?.getSigner()?.signMessage(message) || null;
}
}
Last modified 10mo ago