Frontend Guide

Building Browser-Based MessageBox Applications with BSV SDK

This guide covers frontend patterns for implementing MessageBox functionality in browser-based applications using WalletClient, MessageBoxClient, and PeerPayClient.

📦 Complete Code Repository: https://github.com/bsv-blockchain-demos/messagebox-platformarrow-up-right

All code examples in this guide are taken from the working implementation in the frontend-next/ directory.


Table of Contents


Setup

Installation

Environment Configuration

Create .env.local:

Note: For Next.js apps, use NEXT_PUBLIC_ prefix for client-side environment variables.


Wallet Integration

Pattern 1: Browser WalletClient

WalletClient connects to the user's BSV wallet (like MetaMask on Ethereum):

📁 See full implementation: frontend-next/lib/walletClient.tsarrow-up-right

Key Points:

  • Identity key is a 33-byte compressed public key (hex string)

  • User approves once via their BSV wallet app

  • No private keys ever sent to server

  • Same identity key works across all BSV apps

Usage in Component:

Reference: WalletClient Integration


Identity Certification

Pattern 2: Blockchain Identity Certification

Create a blockchain transaction that certifies your identity with the MessageBox host:

📁 See full implementation: frontend-next/lib/certificationClient.tsarrow-up-right

What anointHost() Does:

  1. Creates a BSV transaction with certification data

  2. Signs transaction with user's identity key

  3. Broadcasts to BSV network

  4. Returns transaction ID (TXID) - permanent blockchain proof

  5. Cost: ~1 satoshi for transaction fee

Benefits:

  • ✅ Permanent, immutable proof of registration

  • ✅ Timestamped by blockchain

  • ✅ Can verify certification independently

  • ✅ No central authority can revoke

Usage in Component:


Sending Payments

Pattern 3: PeerPay - BRC-29 Payment Tokens

Create self-contained payment tokens with transaction + derivation instructions:

📁 See full implementation: frontend-next/lib/paymentClient.tsarrow-up-right

Payment Token Structure:

Transaction Output Layout:

Key Points:

  • Transaction is broadcast immediately by createPaymentToken()

  • Payment is on-chain before message is sent

  • Recipient gets both transaction + derivation instructions

  • Enables recipient to derive the correct private key for spending

Usage in Component:


Receiving Payments

Pattern 4: Message Box Protocol & Internalization

Check inbox for encrypted payment messages and internalize them to wallet:

📁 See full implementation: frontend-next/lib/messageClient.tsarrow-up-right

Why Internalization is Required:

Usage in Component:


Complete Integration

Full Payment Flow Example


Summary

This frontend guide covered:

  • WalletClient Integration - Connecting to user wallets in the browser

  • Identity Certification - Creating blockchain proofs with anointHost()

  • PeerPay Payments - Sending BRC-29 payment tokens with createPaymentToken()

  • Message Handling - Receiving and decrypting messages with listMessages()

  • Transaction Internalization - Adding payments to wallet with internalizeAction()

These patterns form the foundation for building peer-to-peer payment and messaging applications on BSV.


Next Steps

Last updated