Toolkit for peer-to-peer messaging and Bitcoin SV payments via store-and-forward architecture. Provides MessageBoxClient (message inbox management) and PeerPayClient (higher-level P2P payments) leveraging BRC-103 mutual authentication and BRC-29 payment derivation.
-
Constructor:
new MessageBoxClient(options)- Options:
walletClient(BRC-100 wallet),host(MessageBox server URL),enableLogging,networkPreset('local'|'mainnet'|'testnet') - Auto-initializes on first use if needed
- Options:
-
Methods:
init(targetHost?)— manually initialize (optional, auto-runs)sendMessage({ recipient, messageBox, body, skipEncryption? })— send via HTTPsendLiveMessage(...)— send via WebSocket, fallback to HTTPlistMessages({ messageBox })— retrieve messages from inboxacknowledgeMessage({ messageIds })— delete messages after readinglistenForLiveMessages({ messageBox, onMessage })— subscribe to push updatesinitializeConnection()— establish WebSocket linkjoinRoom(messageBox)/leaveRoom(messageBox)— room subscriptionresolveHostForRecipient(identityKey)— overlay discovery for peer's MessageBox hostanointHost(url)— advertise your MessageBox host to overlay network
-
Constructor:
new PeerPayClient(options)- Options:
walletClient,messageBoxHost,enableLogging
- Options:
-
Methods:
sendPayment({ recipient, amount })— HTTP paymentsendLivePayment({ recipient, amount })— WebSocket paymentlistenForLivePayments({ onPayment })— listen for incoming paymentsacceptPayment(payment)— internalize into walletrejectPayment(payment)— send refund (minus 1000 sats)listIncomingPayments()— list pending payments
From README:
const { WalletClient } = require('@bsv/sdk')
const { MessageBoxClient } = require('@bsv/message-box-client')
const myWallet = new WalletClient()
const msgBoxClient = new MessageBoxClient({
host: 'https://message-box-us-1.bsvb.tech',
walletClient: myWallet
})
// Auto-init on first use, or manually
await msgBoxClient.init()
// Send message to John
await msgBoxClient.sendMessage({
recipient: '022600d2ef37d123fdcac7d25d7a464ada7acd3fb65a0daf85412140ee20884311',
messageBox: 'demo_inbox',
body: 'Hello John!'
})
// List John's messages
const messages = await msgBoxClient.listMessages({ messageBox: 'demo_inbox' })
console.log(messages[0].body)
// Acknowledge (delete)
await msgBoxClient.acknowledgeMessage({
messageIds: messages.map(msg => msg.messageId.toString())
})
// Listen for live messages
await msgBoxClient.listenForLiveMessages({
messageBox: 'demo_inbox',
onMessage: (msg) => console.log('Live:', msg.body)
})PeerPayClient:
import { WalletClient } from '@bsv/sdk'
import { PeerPayClient } from '@bsv/message-box-client'
const wallet = new WalletClient()
const peerPay = new PeerPayClient({ walletClient: wallet })
// Listen for incoming payments
await peerPay.listenForLivePayments({
onPayment: async (payment) => {
console.log('Received payment:', payment)
await peerPay.acceptPayment(payment)
}
})
// Send 50,000 sats
await peerPay.sendLivePayment({
recipient: '0277a2b...e3f4',
amount: 50000
})From tests:
const mockWalletClient = new WalletClient()
mockWalletClient.getPublicKey.mockResolvedValue({
publicKey: PrivateKey.fromRandom().toPublicKey().toString()
})
const peerPayClient = new PeerPayClient({
messageBoxHost: 'https://message-box-us-1.bsvb.tech',
walletClient: mockWalletClient
})
const payment = { recipient: PrivateKey.fromRandom().toPublicKey().toString(), amount: 5 }
const token = await peerPayClient.createPaymentToken(payment)
expect(token).toHaveProperty('amount', 5)- Store-and-forward: Messages posted to named "inboxes" on server; recipient polls or subscribes via WebSocket
- Ephemeral storage: Messages deleted once acknowledged by recipient
- Message encryption: AES-256-GCM by default;
skipEncryptionflag disables - BRC-103 auth: All requests signed with wallet identity; server verifies sender
- Message box naming: Arbitrary string per inbox (e.g., 'inbox', 'payment_inbox', 'notifications')
- BRC-29 derivation: Payment addresses derived from sender+recipient identity keys
- Overlay network: Can discover peers' MessageBox hosts via decentralized overlay service
- Live vs HTTP: WebSocket for push notifications; HTTP for polling
@bsv/authsocket-client^2.0.2 — WebSocket auth@bsv/sdk^2.0.14 — Wallet, crypto, auth- Dev: jest, ts-jest, ts-standard, webpack, supertest
- Auto-init now default —
init()is optional but recommended for explicit control; first use auto-initializes - Encryption on by default — message bodies are AES-256-GCM encrypted; set
skipEncryption: truefor raw data - Message ID format — treat messageId as string; don't assume numeric
- Live vs HTTP tradeoff — WebSocket faster but requires connection; HTTP more reliable for offline use
- Payment rejection — rejecting a payment sends a refund minus 1000 sats; if amount < 1000, payment is just dropped
- Overlay discovery — if no explicit host provided, client queries overlay network; this takes ~10s per peer
- Room subscription — WebSocket requires explicit
joinRoom()beforelistenForLiveMessages()
- BRC-103 (Peer-to-Peer Mutual Authentication): All messages signed/verified; handshake per-connection
- BRC-29 (Payment Derivation): Payment address derivation using sender+recipient keys
- BRC-100 (Wallet interface): Uses standard wallet methods for signing/verifying
- MessageBox server protocol: Custom HTTP + WebSocket endpoints for store-and-forward
/Users/personal/git/ts-stack/packages/messaging/message-box-client/
src/
index.ts — main exports
MessageBoxClient.ts — store-and-forward client
PeerPayClient.ts — higher-level payment client
types.ts — interfaces (PeerMessage, SendMessageParams, etc.)
authFetch.ts — HTTP request signing
tests/
PeerPayClientUnit.test.ts
integration/
integrationWS.test.ts
integrationOverlay.test.ts
- authsocket-client — underlying WebSocket with BRC-103 auth for live features
- @bsv/sdk — wallet, identity keys, crypto, AuthFetch for HTTP signing
- MessageBox server (infrastructure repo) — store-and-forward backend
- Overlay network — LARS service for peer discovery