Skip to content

Conversation

@jpgaviria2
Copy link

feat(dm): 2-byte TLV for DMs with backward compatibility - Cashu token support

Summary

Implements backward-compatible 2-byte TLV encoding for Direct Messages to support Cashu ecash token transmission, which can exceed the 255-byte limit of the legacy 1-byte format.

Motivation

This change enables integration with bitpoints.me, a Cashu ecash wallet that uses the bitchat protocol for offline peer-to-peer Bitcoin payments over Bluetooth mesh. Cashu tokens frequently exceed 255 bytes and require the extended TLV format.

Changes

  • Backward Compatible: Receive path supports both 1B and 2B TLV formats automatically
  • Capability Negotiation: Peers advertise DM_TLV_2BYTE feature bit in announcements
  • Adaptive Encoding: Send path uses 2B only when peer supports it
  • Zero Breaking Changes: Older clients ignore new feature TLV and continue working
  • Tested: Unit tests for both encoding formats with roundtrip validation
  • Documented: Technical documentation in docs/dm_tlv.md

Technical Details

Formats

  • Legacy (1B): [type:u8][length:u8][value] (max 255 bytes)
  • New (2B): [type:u16][length:u16][value] (max 65535 bytes)

Feature Negotiation

  • New ProtocolFeatures.DM_TLV_2BYTE constant (bit 0x01)
  • IdentityAnnouncement includes optional FEATURES TLV field (type 0x04)
  • PeerManager.peerHasFeature() checks capability before encoding
  • Adaptive decode tries 2B first, falls back to 1B automatically

Implementation Flow

  1. Announcements: Peer advertises DM_TLV_2BYTE feature bit in identity packets
  2. Capability Check: peerManager.peerHasFeature() verifies support before encoding
  3. Send: privateMessage.encode2B() used when peer has feature; encode() otherwise
  4. Receive: PrivateMessagePacket.decode() tries 2B first, falls back to 1B
  5. Broadcast: Unchanged, remains 1B TLV for all peers

Security

  • Noise/AES-GCM encryption unchanged
  • Strict bounds checking on all TLV length fields
  • No impact on broadcast messages (remain 1B TLV)
  • Unknown TLV fields ignored for forward compatibility

Integration

Enables bitpoints.me - A Cashu.me + Bitchat wallet for:

  • Peer-to-peer Cashu token transfers over Bluetooth mesh
  • Offline Bitcoin payments without internet
  • Merchant rewards and point-of-sale systems
  • Interoperable with bitchat mesh networking

About bitpoints.me

bitpoints.me is a full-featured Cashu ecash wallet with:

  • Bluetooth mesh networking using bitchat protocol
  • Support for multiple mints and token swapping
  • Lightning invoices and QR code send/receive
  • Nostr integration for token claiming
  • Offline-first architecture for mesh payments

Testing

  • ✅ Unit tests for 1B and 2B encoding/decoding roundtrips
  • ✅ Backward compatibility verified with adaptive parser
  • ✅ Feature negotiation tested with mixed peer capabilities
  • ✅ All existing tests pass

Files Changed

  • BluetoothMeshService.kt - Adaptive DM encoding based on peer capabilities
  • MessageHandler.kt - Feature flag propagation from announcements
  • PeerManager.kt - Peer feature tracking and capability queries
  • IdentityAnnouncement.kt - Feature bitmask in announcements (optional TLV)
  • NoiseEncrypted.kt - Dual TLV parsers (1B/2B) with automatic fallback
  • ProtocolFeatures.kt - NEW Feature bit constants
  • PrivateMessagePacketTest.kt - NEW Unit tests for both formats
  • docs/dm_tlv.md - NEW Technical documentation
  • CHANGELOG.md - Release notes

Backward Compatibility

✅ Fully backward compatible:

  • Old clients ignore unknown FEATURES TLV in announcements
  • Old clients receive DMs in legacy 1B format
  • Old clients send DMs in legacy 1B format
  • No protocol version bump required

Checklist

References

@callebtc
Copy link
Collaborator

hi @jpgaviria2 sorry for the delay, I couldn't check this out earlier. this looks wild! could you explain a bit what bitpoints.me is and what you're trying to achieve here with bitchat?

can you also explain why you need to change the TLV field? I'd love to talk to you about this, if you could hit me up on telegram / twitter / nostr, we can chat there too.

this looks WILD
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants