globe-pointer6.4 Wallet Adapter

In your web based decentralized applications you will need to interact with your users wallet extensions, to call program functions, sign messages, decypt records... This is the role of wallet adapters library.

While different wallets provide their own adapter SDKs, applications typically want to support multiple wallets to give users flexibility in choosing their preferred wallet. The Aleo community has developed an universal wallet adapter that is hosted herearrow-up-right, which is largely based on Demox Labs' Leo Wallet Adapterarrow-up-right.

This guide demonstrates how to implement the universal wallet adapter in React applications.

Installationarrow-up-right

First, install the required packages:

npm install --save \
    @demox-labs/aleo-wallet-adapter-base \
    @demox-labs/aleo-wallet-adapter-react \
    @demox-labs/aleo-wallet-adapter-reactui \
    aleo-adapters

To use the wallet adapter, wrap your app with both WalletProvider and WalletModalProvider components. These provide the wallet functionality and UI context respectively.

The WalletProvider accepts the following propertiesarrow-up-right (wallets is required):

export interface WalletProviderProps {
    children: ReactNode;
    wallets: Adapter[];             // Required
    decryptPermission?: DecryptPermission;
    programs?: string[];
    network?: WalletAdapterNetwork;
    autoConnect?: boolean;
    onError?: (error: WalletError) => void;
    localStorageKey?: string;
}

You can import DecryptPermission and WalletAdapterNetwork types from @demox-labs/aleo-wallet-adapter-base.

Wallet Configurationarrow-up-right

When creating wallet adapters, you can configure them with the following options:

Implementation Examplearrow-up-right

Here's a complete example showing how to set up the wallet adapter:

Using the Wallet Buttonarrow-up-right

Demox Labs provides a pre-built wallet button component that you can easily add to your application. To use it, import the WalletMultiButton component from @demox-labs/aleo-wallet-adapter-reactui along with its CSS styles.

Using the Wallet Hookarrow-up-right

Once user connected through the wallet button, you can use useWallet hook from @demox-labs/aleo-wallet-adapter-react to enable access to functions that helps connect and interact with the Aleo network. The hook provides the following interface:

For detailed examples of using these methods, please refer to the Demox Labs Leo Wallet documentationarrow-up-right.

Last updated