Skip to main content

Abstract Wallet

The base class for any wallet in the Wallet SDK, including backend wallets. It contains the functionality common to all wallets.

This wallet is not meant to be used directly, but instead be extended to build your own wallet.

Usage

export class MyCustomWallet extends AbstractWallet {
async getSigner(): Promise<Signer> {
// your implementation
}
}

Public Methods

getAddress

Get the address of the currently connected wallet.

const address = await wallet.getAddress();
Configuration

Return Value

Returns a string containing the wallet address, or undefined if the connection failed.

string | undefined;

getChainId

Get the chain ID of the network the wallet is currently connected to.

const chainId = await wallet.getChainId();
Configuration

Return Value

Returns a number containing the chain ID, or undefined if there is no connected wallet.

number | undefined;

transfer

Transfer native or ERC20 tokens from this wallet to another wallet

// for native tokens
const txResult = await wallet.transfer(recipientAddress, amount);

// for ERC20 tokens
const txResult = await wallet.transfer(recipientAddress, amount, tokenAddress);
Configuration

recipientAddress

Address of the recipient to send the tokens to

string

amount

Amount of tokens to send

string | number

Return Value

Promise<TransactionResult>

getBalance

Get the the native token balance or ERC20 token balance of the wallet.

// for native token balance
const balance = await wallet.getBalance();

// for ERC20 token balance
const balance = await wallet.getBalance(tokenAddress);
Configuration

tokenAddress

Address of the ERC20 token. If no address is provided, the native token balance is returned.

Return Value

Promise<{
symbol: string;
value: BigNumber;
name: string;
decimals: number;
displayValue: string;
}>;

getSigner

Get the signer for the currently connected wallet.

const signer = await wallet.getSigner();
Configuration

Return Value

Returns a Signer.

Signer;

signMessage

Sign a message with the currently connected wallet. This function must be implemented by the parent class.

const signature = await wallet.signMessage("Hello world!");
Configuration

message

The message to sign.

Must be a string.

Return Value

Returns a string containing the signature.

string;

verifySignature

Verify a signature is valid.

const isValid = await wallet.verifySignature(
"Message",
"Signature",
"wallet_address",
);
Configuration

message

The message that was signed.

Must be a string.

signature

The signature to verify.

Must be a string.

address

The address of the wallet that signed the message.

Must be a string.

chainId

The chain ID to check the signature on.

Optional, must be a number.

Events

Abstract Wallet implements EventEmitter, and emits useful events to track the connection lifecycle.

wallet.on("connect", {
console.log("connected");
});
Configuration

eventName

The name of the event to listen for. Each event returns a different data object, or no data object at all.

Possible options:

change

User changes the wallet they are connected with, or the chain they are connected to.

Data available:

{
address: string;
chainId: number;
}
connect

User connects their wallet to your app.

Data available:

{
address: string;
chainId: number;
}
disconnect

User disconnects their wallet from your app.

No data is available.

error

An error occurs in the wallet.

Data available:

{
cause: unknown;
message: string;
name: string;
stack: string | undefined;
}
message

Wallet receives some message that the consumer should be notified of. The kind of message is identified by the type field.

Data available:

{
type: string;
data: unknown;
}
open_wallet

User opens the wallet app.

data is a string | undefined.

request

Wallet receives some request that the consumer should be notified of. The kind of request is identified by the type field.

No data is available.

callback

The function to run when the event is emitted.