Wallet UI LogoWallet UI

useAuthorization

A low-level hook for managing wallet authorization.

The useAuthorization hook is a low-level hook for managing wallet authorization. It is used internally by the useMobileWallet hook and is not meant to be used directly in most cases.

Parameters

The hook accepts an object with the following properties:

NameTypeDescription
cacheCache<WalletAuthorization> | undefinedOptional. A cache instance for storing authorization data.
chainChainThe blockchain chain to connect to.
identityAppIdentityThe identity of the app connecting to the wallet.

If no cache is provided, a default cache that uses AsyncStorage will be used.

The Cache Object

The Cache object is an interface that defines the contract for a cache implementation. It has the following methods:

NameTypeDescription
clear() => Promise<void>Clears the cache.
get() => Promise<T | undefined>Retrieves an item from the cache.
set(value: T) => Promise<void>Sets an item in the cache.

You can provide your own implementation of the Cache interface to customize how authorization data is stored.

Return Value

The hook returns an object with the following properties:

NameTypeDescription
accountsAccount[] | nullAn array of all authorized accounts.
authorizeSession(wallet: AuthorizeAPI) => Promise<Account>A function to authorize a new session.
authorizeSessionWithSignIn(wallet: AuthorizeAPI, signInPayload: SignInPayload) => Promise<Account>A function to authorize a new session with sign-in.
deauthorizeSession(wallet: DeauthorizeAPI) => Promise<void>A function to deauthorize the current session.
deauthorizeSessions() => Promise<void>A function to deauthorize all sessions.
isLoadingbooleanA boolean indicating if the authorization state is loading.
selectedAccountAccount | nullThe currently selected wallet account.

The Account Object

The Account object represents a user's wallet account that has been authorized for use with the dApp. It has the following properties:

NameTypeDescription
addressstringThe base58-encoded string representation of the public key.
labelstring | undefinedAn optional, user-defined label for the account.
publicKeyPublicKeyA PublicKey object from @solana/web3.js.

Example

import { useAuthorization } from '@wallet-ui/react-native-web3js';
import { useCallback } from 'react';
import { Button, Text, View } from 'react-native';

function MyComponent() {
    const { accounts, authorizeSession, selectedAccount } = useAuthorization({
        chain: 'solana:mainnet',
        identity: {
            name: 'My App',
        },
    });

    const handleConnect = useCallback(async () => {
        // This is a simplified example. In a real app, you would
        // get the wallet object from a wallet adapter.
        const wallet = getWallet();
        await authorizeSession(wallet);
    }, [authorizeSession]);

    return (
        <View>
            {selectedAccount ? (
                <Text>Connected to {selectedAccount.address}</Text>
            ) : (
                <Button title="Connect" onPress={handleConnect} />
            )}
        </View>
    );
}