Connecting Wallets
SwapKit provides multiple ways to connect wallets for different blockchain networks. This guide demonstrates how to connect various wallet types using both granular and all-in-one approaches.
Two Approaches to Using SwapKit
Section titled “Two Approaches to Using SwapKit”You can use SwapKit in two ways:
-
Granular approach - Import only what you need from individual packages:
import { SwapKit, keystoreWallet } from "@swapkit/sdk"; -
All-in-one approach - Import everything from the SDK (better for backend/Node.js):
import { createSwapKit } from "@swapkit/sdk";
The granular approach is recommended for frontend applications as it results in smaller bundle sizes.
Initialize SwapKit
Section titled “Initialize SwapKit”Granular Approach (Frontend Recommended)
Section titled “Granular Approach (Frontend Recommended)”// @noErrorValidation
import { SwapKit, keystoreWallet, ledgerWallet, evmWallet } from "@swapkit/sdk";
// Initialize the SwapKit client with specific walletsconst swapKit = SwapKit({ config: { apiKeys: { swapKit: "your-swapkit-api-key", walletConnectProjectId: "your-walletconnect-project-id", }, }, wallets: { ...keystoreWallet, ...ledgerWallet, ...evmWallet },});
All-in-one Approach (Backend/Node.js)
Section titled “All-in-one Approach (Backend/Node.js)”// @noErrorValidation
import { createSwapKit } from "@swapkit/sdk";
// Initialize the SwapKit client with all wallets includedconst swapKit = createSwapKit({ config: { apiKeys: { swapKit: "your-swapkit-api-key", walletConnectProjectId: "your-walletconnect-project-id", }, },});
Connect with Keystore Wallet
Section titled “Connect with Keystore Wallet”Granular Approach
Section titled “Granular Approach”// @noErrorValidationimport { Chain, SwapKit, keystoreWallet, decryptFromKeystore, type Keystore } from "@swapkit/sdk";
// Initialize SwapKitconst swapKit = SwapKit({ wallets: { ...keystoreWallet },});
// Connect with keystoreasync function connectWithKeystore( keystoreFile: Keystore, password: string, chains: Chain[]) { try { // Decrypt the keystore file const phrase = await decryptFromKeystore(keystoreFile, password);
// Connect the chains with the decrypted phrase await swapKit.connectKeystore(chains, phrase);
// Get wallet balances const balances = await Promise.all( chains.map(async (chain) => { const wallet = await swapKit.getWalletWithBalance(chain); return wallet.balance; }) );
return { success: true, balances }; } catch (error) { return { success: false, error }; }}
// Example: Connect multiple chains including Rippleconst chains = [Chain.Ethereum, Chain.Bitcoin, Chain.Ripple];await connectWithKeystore(keystoreFile, password, chains);
All-in-one Approach
Section titled “All-in-one Approach”// @noErrorValidation
import { Chain, createSwapKit, decryptFromKeystore, type Keystore } from "@swapkit/sdk";
// Initialize SwapKitconst swapKit = createSwapKit();
// Connect with keystoreasync function connectWithKeystore( keystoreFile: Keystore, password: string, chains: Chain[]) { try { // Decrypt the keystore file const phrase = await decryptFromKeystore(keystoreFile, password);
// Connect the chains with the decrypted phrase await swapKit.connectKeystore(chains, phrase);
return { success: true }; } catch (error) { return { success: false, error }; }}
Using Hardware Wallets
Section titled “Using Hardware Wallets”Ledger Wallet
Section titled “Ledger Wallet”// @noErrorValidation
import { Chain, SwapKit, ledgerWallet } from "@swapkit/sdk";
// Initialize SwapKit with Ledger walletconst swapKit = SwapKit({ wallets: { ...ledgerWallet },});
// Connect Ledger walletasync function connectLedger(chains: Chain[]) { try { // Connect to Ledger await swapKit.connectLedger(chains);
// Get the wallet address for a specific chain const ethAddress = swapKit.getAddress(Chain.Ethereum);
return { success: true }; } catch (error) { return { success: false, error }; }}
Trezor Wallet
Section titled “Trezor Wallet”// @noErrorValidation
import { Chain, NetworkDerivationPath, SwapKit, trezorWallet } from "@swapkit/sdk";
// Initialize SwapKit with Trezor walletconst swapKit = SwapKit({ wallets: { ...trezorWallet },});
// Connect Trezor walletasync function connectTrezor(chains: Chain[]) { try { // Connect to Trezor await swapKit.connectTrezor([Chain.Bitcoin], NetworkDerivationPath.BTC);
// Get wallet balances for Bitcoin const btcWallet = await swapKit.getWalletWithBalance(Chain.Bitcoin);
return { success: true, balance: btcWallet.balance }; } catch (error) { return { success: false, error }; }}
Using Browser Extension Wallets
Section titled “Using Browser Extension Wallets”MetaMask / EVM Wallets
Section titled “MetaMask / EVM Wallets”// @noErrorValidation
import { Chain, SwapKit, evmWallet } from "@swapkit/sdk";
// Initialize SwapKitconst swapKit = SwapKit({ wallets: { ...evmWallet },});
// Connect to MetaMask or other EVM walletsasync function connectEVMWallet() { try { // This will prompt the user to connect with their browser wallet await swapKit.connectEVMWallet([Chain.Ethereum, Chain.BinanceSmartChain]);
// Get the Ethereum address const address = swapKit.getAddress(Chain.Ethereum);
return { success: true, address }; } catch (error) { return { success: false, error }; }}
Keplr (Cosmos) Wallet
Section titled “Keplr (Cosmos) Wallet”// @noErrorValidation
import { Chain, SwapKit, keplrWallet } from "@swapkit/sdk";
// Initialize SwapKitconst swapKit = SwapKit({ wallets: { ...keplrWallet },});
// Connect to Keplrasync function connectKeplr() { try { await swapKit.connectKeplr([Chain.Cosmos]);
// Get the Cosmos address const address = swapKit.getAddress(Chain.Cosmos);
return { success: true, address }; } catch (error) { return { success: false, error }; }}
Cosmostation (Cosmos) Wallet
Section titled “Cosmostation (Cosmos) Wallet”// @noErrorValidation
import { Chain, SwapKit, cosmostationWallet } from "@swapkit/sdk";
// Initialize SwapKitconst swapKit = SwapKit({ wallets: { ...cosmostationWallet },});
// Connect to Cosmostationasync function connectCosmostation() { try { await swapKit.connectCosmostation([Chain.Cosmos, Chain.THORChain]);
// Get the Cosmos address const address = swapKit.getAddress(Chain.Cosmos);
return { success: true, address }; } catch (error) { return { success: false, error }; }}
Phantom (Solana) Wallet
Section titled “Phantom (Solana) Wallet”// @noErrorValidation
import { Chain, SwapKit, phantomWallet } from "@swapkit/sdk";
// Initialize SwapKitconst swapKit = SwapKit({ wallets: { ...phantomWallet },});
// Connect to Phantomasync function connectPhantom() { try { await swapKit.connectPhantom([Chain.Solana]);
// Get the Solana address const address = swapKit.getAddress(Chain.Solana);
return { success: true, address }; } catch (error) { return { success: false, error }; }}
Managing Wallet Connections
Section titled “Managing Wallet Connections”// @noErrorValidation
import { Chain, SwapKit, evmWallet } from "@swapkit/sdk";
// Initialize SwapKitconst swapKit = SwapKit({ wallets: { ...evmWallet },});
// Get all connected walletsfunction getAllWallets() { const wallets = swapKit.getAllWallets(); return wallets;}
// Disconnect a specific chainfunction disconnectChain(chain: Chain) { swapKit.disconnectChain(chain);}
// Disconnect all walletsfunction disconnectAll() { swapKit.disconnectAll();}
Getting Wallet Balances
Section titled “Getting Wallet Balances”// @noErrorValidation
import { Chain, SwapKit, evmWallet } from "@swapkit/sdk";
// Initialize SwapKitconst swapKit = SwapKit({ wallets: { ...evmWallet },});
// Connect a wallet firstawait swapKit.connectEVMWallet([Chain.Ethereum]);
// Get balance for a specific chainasync function getChainBalance(chain: Chain) { // Get the balance (true parameter refreshes the balance from the network) const balance = await swapKit.getBalance(chain, true); return balance;}
// Get balances for multiple chainsasync function getAllBalances(chains: Chain[]) { const balances = await Promise.all( chains.map(async (chain) => { try { const wallet = await swapKit.getWalletWithBalance(chain); return { chain, balance: wallet.balance }; } catch (error) { return { chain, balance: [], error }; } }) );
return balances;}
Using WalletConnect
Section titled “Using WalletConnect”// @noErrorValidation
import { Chain, SwapKit, walletconnectWallet } from "@swapkit/sdk";
// Initialize SwapKit with WalletConnectconst swapKit = SwapKit({ config: { apiKeys: { walletConnectProjectId: "YOUR_WALLETCONNECT_PROJECT_ID", // Required }, }, wallets: { ...walletconnectWallet },});
// Connect using WalletConnectasync function connectWalletConnect() { try { await swapKit.connectWalletconnect([ Chain.Ethereum, Chain.BinanceSmartChain, Chain.Cosmos, ]);
// Check connected addresses const ethAddress = swapKit.getAddress(Chain.Ethereum);
return { success: true }; } catch (error) { return { success: false, error }; }}
Advanced: Connecting Multiple Wallets
Section titled “Advanced: Connecting Multiple Wallets”// @noErrorValidation
import { Chain, SwapKit, evmWallet, keplrWallet, keystoreWallet } from "@swapkit/sdk";
// Initialize SwapKit with multiple wallet typesconst swapKit = SwapKit({ wallets: { ...evmWallet, ...keplrWallet, ...keystoreWallet },});
// Connect multiple wallet typesasync function connectMultipleWallets(phrase: string) { try { // Connect EVM wallet for Ethereum await swapKit.connectEVMWallet([Chain.Ethereum]);
// Connect Keplr for Cosmos await swapKit.connectKeplr([Chain.Cosmos]);
// Connect keystore for Bitcoin await swapKit.connectKeystore([Chain.Bitcoin], phrase);
// Get all connected wallets const wallets = swapKit.getAllWallets();
// Get addresses for each chain const addresses = { [Chain.Ethereum]: swapKit.getAddress(Chain.Ethereum), [Chain.Cosmos]: swapKit.getAddress(Chain.Cosmos), [Chain.Bitcoin]: swapKit.getAddress(Chain.Bitcoin), };
return { success: true, addresses }; } catch (error) { return { success: false, error }; }}