Build a Wallet interface

Building a Wallet interface is done in two steps:

  • Create a Connector class
  • Create a Wallet class and use the Connector class in it

1. Create Connector class

Extend the abstract Connector interface and implement the all the required methods.

import { Connector, ConnectParams } from "@thirdweb-dev/wallets";
export class MyConnector extends Connector {
connect(options?: ConnectParams<TConnectParams>): Promise<string> {
// ...
}
disconnect(): Promise<void> {
// ...
}
getAddress(): Promise<string> {
// ...
}
getSigner(): Promise<Signer> {
// ...
}
getProvider(): Promise<providers.Provider> {
// ...
}
switchChain(chainId: number): Promise<void> {
// ...
}
isConnected(): Promise<boolean> {
// ...
}
setupListeners(): Promise<void> {
// ...
}
updateChains(chains: Chain[]): void {
// ...
}
}

Required methods

Optional methods

You can throw an exception or leave the implementation empty if you don't want to implement these methods.

2. Create Wallet class

Now that the hard part is done, the rest is easy, we just need to wrap our connector in a AbstractClientWallet class.

The AbstractClientWallet class is the base class that provides an interface for interacting with your connector on one side and with applications on the other.

The main method that needs to be overridden is the getConnector method. This method should return a Promise that resolves to the Connector class that you implemented.

export class MyWallet extends AbstractClientWallet {
async getConnector(): Promise<Connector> {
return new MyConnector();
}
}

You can expose any custom logic here as the public API for your wallet.

Required Methods

Using your new wallet

At this point, you should be able to instantiate your new wallet and call connect on it.

const wallet = new MyWallet();
wallet.connect();

Examples

You can look at how the built-in wallets in @thirdweb-dev/wallets package are created for reference