Solana Wallet Adapter

repository·master·Indexed 24 days ago

https://github.com/anza-xyz/wallet-adapter

A collection of modular TypeScript packages providing standardized interfaces and UI components for integrating Solana wallets into decentralized applications (dApps). It includes core React hooks like useWallet and useWalletMultiButton, context providers for connection management, and specific adapters for wallets such as Coinbase, Fractal, Saifu, and WalletConnect.

Tokens
37.6K
Snippets
16
Records
234
Agent score
83%

What's inside @solana/wallet-adapter

  1. Overview of Wallet Adapter

    master
    Wallet Adapter provides modular TypeScript wallet adapters and UI components designed for Solana applications. It allows developers to integrate various Solana wallets into their applications using a standardized interface, ensuring compatibility across different wallet providers.
  2. Understand `useWalletMultiButton` states

    master

    The useWalletMultiButton hook returns a buttonState string that indicates the current status of the wallet connection. You should use this state to determine which UI elements to render and which actions are valid:

    • no-wallet: No wallet is selected. You should trigger the wallet selection UI and call onSelectWallet() with the chosen wallet name.
    • has-wallet: A wallet is selected, but the app is not yet connected to it. Render a button that calls onConnect().
    • connecting: The wallet is currently in the process of connecting.
    • connected: The app is connected to a wallet. You have access to the publicKey and can call onDisconnect() to sign out, or onSelectWallet() to switch wallets.
    • disconnecting: The wallet is currently in the process of disconnecting.
  3. Use @solana/wallet-adapter-unsafe-burner for testing

    master

    The @solana/wallet-adapter-unsafe-burner is a specialized wallet adapter designed for starter packs and development environments. It implements the standard Wallet Adapter interface, allowing it to be used in place of real wallets, but it uses an unsafe local keypair to sign messages.

    Warning: This adapter is intended for development and testing only. Because it uses a local keypair, it does not provide the security of a hardware or browser extension wallet. Do not use this in a production environment.

  4. Prerequisites for building Wallet Adapter

    master

    To build the Wallet Adapter from source, ensure you have the following installed:

    • Node.js: version 18 or higher.
    • PNPM: version 9 or higher.

    If you are using Node 16+, you can activate PNPM using Corepack. If you do not have jq installed to automate the version detection, you can manually activate a specific version (e.g., 9.1.0).

    # Using Corepack with jq to activate the latest pnpm
    corepack enable
    corepack prepare pnpm@`npm info pnpm --json | jq -r .version` --activate
    
    # OR manually activating a specific version
    corepack prepare pnpm@9.1.0 --activate
  5. Build Wallet Adapter from source

    master

    Follow these steps to clone, install, and build the repository:

    1. Clone the repository:
      git clone https://github.com/anza-xyz/wallet-adapter.git
      cd wallet-adapter
    2. Install dependencies using pnpm:
      pnpm install
    3. Build all packages:
      pnpm run build
      Note: The first build may take some time. Subsequent builds are incremental and faster.

    To enable hot module reloading and run incremental builds as you modify source files, use:

    pnpm watch
    git clone https://github.com/anza-xyz/wallet-adapter.git
    cd wallet-adapter
    pnpm install
    pnpm run build
  6. Set up Wallet Context Providers in React

    master

    To enable wallet functionality, wrap your application components in a hierarchy of context providers. You must include ConnectionProvider, WalletProvider, and WalletModalProvider.

    Key configuration details:

    • Network: Use WalletAdapterNetwork (e.g., Devnet, Testnet, or MainnetBeta) to define the network.
    • Endpoint: Provide an RPC endpoint, typically generated via clusterApiUrl(network) from @solana/web3.js.
    • Wallets: Pass an array of wallet instances to WalletProvider. Wallets following the Solana Wallet Standard or Mobile Wallet Adapter Protocol are available automatically. For legacy wallets, instantiate them from @solana/wallet-adapter-wallets.
    • Styles: Import @solana/wallet-adapter-react-ui/styles.css to ensure the UI components render correctly.

    Common UI components provided by @solana/wallet-adapter-react-ui include:

    • WalletMultiButton: A button to trigger the wallet selection modal.
    • WalletDisconnectButton: A button to disconnect the current wallet.
    import React, { FC, useMemo } from 'react';
    import { ConnectionProvider, WalletProvider } from '@solana/wallet-adapter-react';
    import { WalletAdapterNetwork } from '@solana/wallet-adapter-base';
    import { UnsafeBurnerWalletAdapter } from '@solana/wallet-adapter-wallets';
    import {
        WalletModalProvider,
        WalletDisconnectButton,
        WalletMultiButton
    } from '@solana/wallet-adapter-react-ui';
    import { clusterApiUrl } from '@solana/web3.js';
    
    // Default styles that can be overridden by your app
    import '@solana/wallet-adapter-react-ui/styles.css';
    
    export const Wallet: FC = () => {
        // The network can be set to 'devnet', 'testnet', or 'mainnet-beta'.
        const network = WalletAdapterNetwork.Devnet;
    
        // You can also provide a custom RPC endpoint.
        const endpoint = useMemo(() => clusterApiUrl(network), [network]);
    
        const wallets = useMemo(
            () => [
                /**
                 * Wallets that implement either of these standards will be available automatically.
                 *
                 *   - Solana Mobile Stack Mobile Wallet Adapter Protocol
                 *     (https://github.com/solana-mobile/mobile-wallet-adapter)
                 *   - Solana Wallet Standard
                 *     (https://github.com/anza-xyz/wallet-standard)
                 *
                 * If you wish to support a wallet that supports neither of those standards, 
                 * instantiate its legacy wallet adapter here. Common legacy adapters can be found
                 * in the npm package `@solana/wallet-adapter-wallets`.
                 */
                new UnsafeBurnerWalletAdapter(),
            ],
            // eslint-disable-next-line react-hooks/exhaustive-deps
            [network]
        );
    
        return (
            <ConnectionProvider endpoint={endpoint}>
                <WalletProvider wallets={wallets} autoConnect>
                    <WalletModalProvider>
                        <WalletMultiButton />
                        <WalletDisconnectButton />
                        { /* Your app's components go here, nested within the context providers. */ }
                    </WalletModalProvider>
                </WalletProvider>
            </ConnectionProvider>
        );
    };
  7. Use Community Wallet Adapter implementations

    master

    For frameworks other than React, several community-maintained packages are available:

    • Vue: solana-wallets-vue
    • Angular: platform/libs/wallet-adapter
    • Svelte: svelte-on-solana-wallet-adapter
    • TypeScript: solana-connect
    • Ant Design Web3: web3.ant.design/components/solana
  8. Install Wallet Adapter for React

    master

    To use the Wallet Adapter in a React-based Solana application, install the following core dependencies via npm:

    npm install --save \
        @solana/wallet-adapter-base \
        @solana/wallet-adapter-react \
        @solana/wallet-adapter-react-ui \
        @solana/wallet-adapter-wallets \
        @solana/web3.js \
        react