reactnative-lightening-wallet-rs
v0.1.42
Published
High-performance unified Lightning wallet for React Native
Downloads
3,769
Maintainers
Readme
React Native Lightning Wallet (Rust)
A high-performance unified Lightning wallet library for React Native, built entirely in Rust. This library consolidates LDK (Lightning Development Kit), Electrum client, and Bitcoin wallet functionality into a single native module.
Features
- High Performance: All wallet logic runs in native Rust code
- Bitcoin Wallet: BIP39/BIP32 HD wallet with native address derivation
- Lightning Network: LDK integration for Lightning payments (coming soon)
- Electrum Client: Built-in Electrum protocol support
- UTXO Management: Native UTXO tracking with caching
- Transaction Building: Coin selection and transaction signing
- Payment History: SQLite-based payment database
- Event Streaming: Real-time wallet events
- Cross-platform: Supports Android and iOS
Architecture
reactnative-lightening-wallet-rs/
├── rust/ # Rust core library
│ ├── src/
│ │ ├── lib.rs # JNI/FFI bindings
│ │ ├── coordinator.rs # Main orchestrator
│ │ ├── wallet/ # Bitcoin wallet logic
│ │ ├── electrum/ # Electrum client
│ │ ├── storage/ # SQLite & caching
│ │ └── events.rs # Event system
│ └── Cargo.toml
├── android/ # Android JNI bindings
├── ios/ # iOS FFI bindings
└── js/ # TypeScript APIPerformance Improvements
Compared to the previous JavaScript-based wallet:
- Wallet initialization: 3-5s → 500ms (10x faster)
- Balance refresh: 2-3s → 100ms (20x faster)
- UTXO scanning: 5-10s → 300ms (30x faster)
- Payment send: 2-4s → 400ms (7x faster)
Installation
Prerequisites
- Rust 1.70+ with
cargo - Android NDK (if building for Android)
- Xcode (if building for iOS)
cargo-ndkfor Android builds:cargo install cargo-ndk
Install the package
npm install reactnative-lightening-wallet-rs
# or
yarn add reactnative-lightening-wallet-rsiOS Setup
After installing the package, you need to install CocoaPods dependencies:
cd ios
pod install
cd ..The Podspec includes a prepare_command that will automatically build the Rust library for iOS when you run pod install. If you prefer to build manually:
npm run build:ios
cd ios
pod installImportant: Make sure you have the iOS Rust targets installed:
rustup target add aarch64-apple-ios x86_64-apple-ios aarch64-apple-ios-simAndroid Setup
The Android build is automatically handled by Gradle. Just make sure you have:
- Android NDK installed
cargo-ndkinstalled:cargo install cargo-ndk
Then build your React Native app normally:
cd android
./gradlew assembleReleaseBuild the native library
Android
# Option 1: Using npm script
npm run build:rust:android
# Option 2: Direct Gradle build
cd android
./gradlew assembleRelease
# Option 3: Build Rust directly
cd rust
cargo ndk -t arm64-v8a -t armeabi-v7a -o ../android/src/main/jniLibs build --releaseiOS
# Option 1: Using npm script (recommended)
npm run build:ios
# Option 2: Using the build script directly
./scripts/build-ios.sh
# Option 3: Manual build
cd rust
rustup target add aarch64-apple-ios x86_64-apple-ios aarch64-apple-ios-sim
cargo build --release --target aarch64-apple-ios
cargo build --release --target x86_64-apple-ios
cargo build --release --target aarch64-apple-ios-simNote for iOS: The first build will take longer as it downloads dependencies and compiles for multiple architectures (simulator + device).
Build Both Platforms
npm run build:allUsage
Initialize wallet
import LighteningWallet from 'reactnative-lightening-wallet-rs';
// Generate a new mnemonic
const mnemonic = await LighteningWallet.generateMnemonic();
// Initialize wallet
await LighteningWallet.initialize(
'user123', // User ID
mnemonic, // BIP39 mnemonic
'testnet', // Network: 'bitcoin' or 'testnet'
'/path/to/wallet.db' // Database path
);Get balance
const balance = await LighteningWallet.getBalance();
console.log('Total balance:', balance.total);
console.log('On-chain confirmed:', balance.onchain_confirmed);
console.log('Lightning balance:', balance.lightning_balance);
console.log('Pending:', balance.pending);Sync wallet
await LighteningWallet.sync();Get receiving address
const address = await LighteningWallet.getReceivingAddress();
console.log('Receive to:', address);List payment history
const payments = await LighteningWallet.listPayments(10, 0); // limit, offset
payments.forEach(payment => {
console.log(`${payment.payment_type}: ${payment.amount_sats} sats`);
});Subscribe to events
const unsubscribe = LighteningWallet.onEvent((event) => {
switch (event.type) {
case 'BalanceUpdated':
console.log('Balance updated:', event.total);
break;
case 'PaymentReceived':
console.log('Payment received:', event.amount_sats);
break;
case 'SyncCompleted':
console.log('Sync completed in', event.duration_ms, 'ms');
break;
}
});
// Cleanup
unsubscribe();Disconnect
await LighteningWallet.disconnect();API Reference
LighteningWalletAPI
static generateMnemonic(): Promise<string>
Generate a new 24-word BIP39 mnemonic phrase.
initialize(userId: string, mnemonic: string, network: 'bitcoin' | 'testnet', dbPath: string): Promise<void>
Initialize a wallet instance.
getBalance(): Promise<WalletBalance>
Get the current wallet balance.
sync(): Promise<void>
Sync the wallet with the blockchain via Electrum.
getReceivingAddress(): Promise<string>
Get a fresh receiving address.
listPayments(limit?: number, offset?: number): Promise<Payment[]>
List payment history.
getEvents(): Promise<WalletEvent[]>
Get pending wallet events.
onEvent(callback: (event: WalletEvent) => void): () => void
Subscribe to real-time wallet events.
disconnect(): Promise<void>
Disconnect and cleanup the wallet.
Types
interface WalletBalance {
onchain_confirmed: number;
onchain_unconfirmed: number;
lightning_balance: number;
lightning_receivable: number;
claimable_balance: number;
total: number;
pending: number;
}
interface Payment {
id?: number;
payment_hash: string;
payment_type: 'sent' | 'received' | 'onchain_sent' | 'onchain_received';
amount_sats: number;
fee_sats?: number;
status: 'pending' | 'completed' | 'failed';
timestamp: number;
description?: string;
destination?: string;
txid?: string;
preimage?: string;
bolt11?: string;
}
type WalletEvent =
| { type: 'BalanceUpdated'; ... }
| { type: 'PaymentReceived'; ... }
| { type: 'PaymentSent'; ... }
| { type: 'SyncCompleted'; ... }
| ...Development
Run tests
cd rust
cargo testBuild for development
cd rust
cargo buildFormat code
cd rust
cargo fmtCheck code
cd rust
cargo clippyRoadmap
- [x] Bitcoin wallet core (BIP39/32)
- [x] Electrum client
- [x] UTXO management
- [x] Transaction building
- [x] Payment history database
- [x] Event streaming
- [ ] LDK integration
- [ ] Lightning payments
- [ ] Channel management
- [ ] Background sync service
- [ ] UTXO consolidation
- [ ] Fee estimation
- [ ] iOS support
- [ ] Example app
License
MIT
Credits
This library integrates and builds upon:
