Solana Wallet Adapter
repository·master·Indexed 24 days ago
https://github.com/anza-xyz/wallet-adapterA 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.
What's inside @solana/wallet-adapter
- 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.
Integrate Saifu Wallet using `@solana/wallet-adapter-saifu`
masterUse the@solana/wallet-adapter-saifupackage to integrate Saifu Wallet into your Solana application. This adapter allows your application to interact with users who use the Saifu Wallet interface.Understand `useWalletMultiButton` states
masterThe
useWalletMultiButtonhook returns abuttonStatestring 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 callonSelectWallet()with the chosen wallet name.has-wallet: A wallet is selected, but the app is not yet connected to it. Render a button that callsonConnect().connecting: The wallet is currently in the process of connecting.connected: The app is connected to a wallet. You have access to thepublicKeyand can callonDisconnect()to sign out, oronSelectWallet()to switch wallets.disconnecting: The wallet is currently in the process of disconnecting.
Use @solana/wallet-adapter-unsafe-burner for testing
masterThe
@solana/wallet-adapter-unsafe-burneris 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.
How to use Wallet Adapter for Solana Apps
masterIf you are building a Solana application, refer to the dedicated guide: Wallet Adapter for Solana Apps.How to use Wallet Adapter for Solana Wallets
masterIf you are building a wallet provider, refer to the dedicated guide: Wallet Adapter for Solana Wallets.Prerequisites for building Wallet Adapter
masterTo 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
jqinstalled 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 --activateBuild Wallet Adapter from source
masterFollow these steps to clone, install, and build the repository:
- Clone the repository:
git clone https://github.com/anza-xyz/wallet-adapter.git cd wallet-adapter - Install dependencies using pnpm:
pnpm install - Build all packages:
Note: The first build may take some time. Subsequent builds are incremental and faster.pnpm run build
To enable hot module reloading and run incremental builds as you modify source files, use:
pnpm watchgit clone https://github.com/anza-xyz/wallet-adapter.git cd wallet-adapter pnpm install pnpm run build- Clone the repository:
Set up Wallet Context Providers in React
masterTo enable wallet functionality, wrap your application components in a hierarchy of context providers. You must include
ConnectionProvider,WalletProvider, andWalletModalProvider.Key configuration details:
- Network: Use
WalletAdapterNetwork(e.g.,Devnet,Testnet, orMainnetBeta) 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.cssto ensure the UI components render correctly.
Common UI components provided by
@solana/wallet-adapter-react-uiinclude: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> ); };- Network: Use
Use Community Wallet Adapter implementations
masterFor 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
- Vue:
Use the Coinbase Wallet adapter
masterThe@solana/wallet-adapter-coinbasepackage allows Solana applications to connect to the Coinbase Wallet browser extension. To integrate this into your application, follow the standard wallet adapter setup patterns (typically using@solana/wallet-adapter-reactand@solana/wallet-adapter-react-ui).Install Wallet Adapter for React
masterTo 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