# Connector API

### Overview

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](https://docs.altura.com/altura-documentation-1/javascript-sdk/broken-reference).

### Connector Interface

```typescript
export interface IConnector {
    get address(): string | null
    get provider(): ethers.providers.Web3Provider | null
    
    connect: () => Promise<string | null>
    sign: (message: string) => Promise<string | null>
}
```

### Using Connectors

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.

### Default Connectors

By default, the Altura SDK provides two implementations for connectors:&#x20;

* [`MetamaskConnector`](https://github.com/alturanft/JS-SDK/blob/master/src/connector/metamaskConnector.ts) for [Metamask](https://github.com/MetaMask/metamask-extension)
* [`TrustwalletConnector`](https://github.com/alturanft/JS-SDK/blob/master/src/connector/trustwalletConnector.ts) for [Trustwallet](https://github.com/trustwallet/wallet-core)

First-party support for more common wallets may be added in the future.

### Writing Custom Connectors

To make a wallet compatible with the connector API, it needs a connector that follows the `IConnector` interface.&#x20;

The `IConnector` interface contains two functions and two fields:

#### `connect: () => Promise<string | null>`

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.

#### `sign: (message: string) => Promise<string | null>`

This function should have the wallet sign an arbitrary message, returning either the signed message or `null` representing a failed state.

#### `get address(): string | null`

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.

#### `get provider(): ethers.providers.Web3Provider | null`

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.&#x20;

### Metamask custom connector example:

```javascript
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;
  }
}

```
