Stellar Wallet Kit - Quick Integration

Building a Stellar Wallet Management System

This guide will walk you through creating a comprehensive wallet management system for Stellar blockchain integration in your React/Next.js application. The system provides secure wallet connection, disconnection, and state management with persistence.

Overview

The system consists of three main components:

  1. Wallet Provider - Global state management for wallet information

  2. Wallet Hook - Business logic for wallet operations

  3. Wallet Kit Configuration - Stellar blockchain integration setup

Step 1: Install Dependencies

First, install the required Stellar Wallet Kit package:

npm install @creit.tech/stellar-wallets-kit

Step 2: Configure the Stellar Wallet Kit

Create a configuration file that sets up the Stellar Wallet Kit with support for multiple wallet types:

import {
  StellarWalletsKit,
  WalletNetwork,
  FREIGHTER_ID,
  AlbedoModule,
  FreighterModule,
} from "@creit.tech/stellar-wallets-kit";

/**
 * Main configuration for Stellar Wallet Kit
 * This kit supports multiple wallet types including Freighter and Albedo
 * Configure for TESTNET during development and MAINNET for production
 */
export const kit: StellarWalletsKit = new StellarWalletsKit({
  network: WalletNetwork.TESTNET,
  selectedWalletId: FREIGHTER_ID,
  modules: [new FreighterModule(), new AlbedoModule()],
});

/**
 * Interface for transaction signing parameters
 */
interface signTransactionProps {
  unsignedTransaction: string;
  address: string;
}

/**
 * Sign a Stellar transaction using the connected wallet
 * This function handles the signing process and returns the signed transaction
 * 
 * @param unsignedTransaction - The XDR string of the unsigned transaction
 * @param address - The wallet address that will sign the transaction
 * @returns Promise<string> - The signed transaction XDR
 */
export const signTransaction = async ({
  unsignedTransaction,
  address,
}: signTransactionProps): Promise<string> => {
  const { signedTxXdr } = await kit.signTransaction(unsignedTransaction, {
    address,
    networkPassphrase: WalletNetwork.TESTNET,
  });

  return signedTxXdr;
};

Step 3: Create the Wallet Context Provider

The Wallet Provider manages global wallet state and persists information in localStorage for better user experienc

Step 4: Create the Wallet Hook

The wallet hook encapsulates all the business logic for wallet operations, providing a clean interface for components:

Step 5: Integrate the Provider in Your App

Wrap your application with the WalletProvider to enable wallet state management throughout your app:

Step 6: Use the Wallet Hook in Components

Now you can use the wallet functionality in any component. Here's an example of a wallet connection button:

Key Features and Benefits

State Persistence

  • Wallet information is automatically saved to localStorage

  • Users don't need to reconnect their wallet every time they visit your app

  • Seamless user experience across browser sessions

Multi-Wallet Support

  • Supports multiple Stellar wallet types (Freighter, Albedo, etc.)

  • Easy to add new wallet modules as they become available

  • Users can choose their preferred wallet

Error Handling

  • Comprehensive error handling for wallet operations

  • Graceful fallbacks when wallet operations fail

  • Easy to extend with custom error notifications

Type Safety

  • Full TypeScript support with proper type definitions

  • IntelliSense support for better development experience

  • Compile-time error checking for wallet operations

Context-Based Architecture

  • Clean separation of concerns between state management and business logic

  • Easy to access wallet state from any component

  • Centralized wallet state management

Configuration Options

Network Selection

  • TESTNET: Use during development and testing

  • MAINNET: Use for production applications

Last updated

Was this helpful?