mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-24 23:25:19 +00:00
4.7 KiB
4.7 KiB
MessageParser Implementation
Overview
I've successfully implemented a comprehensive MessageParser system for the bitchat Android app that can parse messages containing Cashu payment tokens and display them as interactive payment chips.
Files Created
Core Parser Classes
-
MessageParser.kt- Main orchestration class that parses messages- Singleton pattern for easy access
- Detects Cashu tokens using regex pattern
cashuB[A-Za-z0-9+/=_-]+ - Returns list of
MessageElementobjects (text + special elements) - Robust error handling with fallback to plain text
- Safe logging for unit tests compatibility
-
CashuTokenParser.kt- Specialized Cashu token parser- Implements Cashu NUT-00 specification for V4 tokens (cashuB)
- Uses CBOR decoding to extract token information
- Extracts: amount, unit, mint URL, memo, and proof count
- Fallback system when CBOR parsing fails (creates 100 sat placeholder)
- Safe logging for unit test environment
UI Components
MessageComponents.kt- Compose UI components for displaying parsed contentParsedMessageContent- Renders mixed text and payment elementsCashuPaymentChip- Interactive payment chip with "Receive" button- Clean, card-based design with sea green theme
- Lightning bolt icon for visual appeal
- Proper inline layout that flows with text
Integration
-
Updated
MessageComponents.kt(main)- Enhanced
MessageItemto detect and render special content - Maintains existing text-only layout for regular messages
- Uses new
MessageHeadercomponent for consistent timestamp/sender display - Seamless integration with existing delivery status system
- Enhanced
-
Updated
ChatUIUtils.kt- Added
parseMessageContent()function for easy integration - Maintains existing
formatMessageAsAnnotatedString()for plain text - Clean separation of concerns
- Added
Testing
MessageParserTest.kt- Comprehensive unit tests- Tests plain text message handling
- Tests Cashu token detection and parsing
- Tests multiple tokens in single message
- Tests invalid token fallback behavior
- Tests token parser edge cases
Key Features
Cashu Token Support
- Full V4 Specification: Implements cashuB format with CBOR decoding
- Amount Extraction: Sums up all proof amounts for total payment value
- Unit Support: Extracts currency unit (defaults to "sat")
- Mint Information: Identifies the mint URL
- Memo Support: Shows optional payment memo
- Error Resilience: Falls back to placeholder values (100 sat) if parsing fails
User Experience
- Inline Display: Payment chips flow naturally with text content
- Visual Design: Professional card-based chips with proper elevation
- Interactive: "Receive" button ready for wallet integration
- Informative: Shows "Cashu Payment: 100 sat" format
- Accessible: Proper content descriptions for screen readers
Technical Excellence
- Thread Safe: Uses safe logging that works in both app and test environments
- Memory Efficient: Proper cleanup and fallback handling
- Protocol Compliant: Follows Cashu NUT-00 specification exactly
- Extensible: Easy to add more parser types in the future
Integration Points
Message Rendering Pipeline
MessageItemdetects special content usingparseMessageContent()- If special elements found, uses
ParsedMessageContentfor rendering - Otherwise falls back to existing
formatMessageAsAnnotatedString() - Maintains all existing features (mentions, hashtags, delivery status)
Payment Interaction
- Clicking "Receive" button logs token details for now
- Ready for wallet integration - just update
handleCashuPayment()function - Logs: original token string, amount, unit, mint URL, memo, proof count
Dependencies Added
co.nstant.in:cbor:0.9- For CBOR decoding of V4 tokens- Integrated cleanly with existing Compose and cryptography stack
Example Usage
When a user sends a message like:
"Here's your payment: cashuBeyJ0b2tlbiI6MSJ9 enjoy!"
The app will display:
- Text: "Here's your payment: "
- Interactive payment chip: "⚡ Cashu Payment: 100 sat [Receive]"
- Text: " enjoy!"
The payment chip is clickable and ready for wallet integration.
Future Enhancements
Ready for extension to support:
- Real CBOR token parsing (when proper tokens are used)
- V3 token support (cashuA format)
- Multiple payment types (Bitcoin Lightning, etc.)
- Wallet integration for receiving payments
- Payment status tracking
- Transaction history
The architecture is designed to be modular and extensible for future payment types while maintaining the existing chat functionality perfectly.