Technical Documentation
Demo ID: demo-2025-014 Version: 1.0.0 Last Updated: December 2025
Architecture Overview
System Architecture
┌─────────────────────────────────────────┐
│ Frontend (Next.js/React) │
│ - WalletClient for user payments │
│ - createAction for transactions │
│ - internalizeAction for token claiming │
│ - listOutputs for token viewing │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ Backend (Next.js API) │
│ - Auth middleware (BRC-103) │
│ - Payment middleware (BRC-103/104) │
│ - Wallet Toolbox for server wallet │
│ - PushDrop token creation │
│ - JSON file state persistence │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ BSV Blockchain │
│ - Investment transactions │
│ - Individual token claim transactions │
└─────────────────────────────────────────┘Technology Stack
Frontend: Next.js, React, TypeScript, @bsv/sdk (WalletClient)
Backend: Next.js API Routes, TypeScript, @bsv/sdk (Wallet Toolbox)
Blockchain: BSV Blockchain
Middleware: @bsv/payment-express-middleware
State Persistence: JSON file storage
Infrastructure: Node.js, npm
Key Components
Frontend Wallet Integration: React hooks for WalletClient connection and transaction management
402 Payment Protocol: HTTP payment flow with auth and payment middleware
BRC-29 Key Derivation: Secure payment addressing with protocol ID [2, '3241645161d8']
Backend Wallet: Wallet Toolbox implementation for server-side operations
Token Distribution: Individual PushDrop token claiming pattern
State Management: JSON file persistence keyed by wallet identity
Integration & APIs
External Dependencies
API Endpoints
GET /api/wallet-info
Returns backend wallet identity key for BRC-29 derivation.
POST /api/invest
Submit investment using 402 payment flow. First request returns 402 with derivation parameters. Second request with x-bsv-payment header completes the transaction.
GET /api/status
Returns campaign progress, goal, raised amount, and investor list.
POST /api/complete
Individual token claiming endpoint. Creates and sends PushDrop token to investor when goal is reached.
Implementation Guide
Prerequisites
Node.js (v18 or higher)
npm package manager
BSV Desktop Wallet installed
Environment variables configured in .env
Setup Instructions
Open http://localhost:3000 in your browser.
Configuration
Key configuration parameters in .env:
Key Implementation Patterns
1. Frontend Wallet Integration
2. 402 Payment Flow
Frontend sends POST to
/api/investBackend responds with 402 and derivation prefix
Frontend derives payment key using BRC-29
Frontend creates transaction with
createActionFrontend resubmits with
x-bsv-paymentheaderBackend validates and internalizes payment
3. BRC-29 Key Derivation
4. Payment Middleware
5. Individual Token Claiming
6. State Persistence
Testing & Validation
Validation Criteria
Performance & Scalability
Current Metrics
Transaction Cost: ~1-2 satoshis per investment (BSV network fees)
Token Distribution: Individual claiming pattern distributes costs
State Storage: JSON file persistence for lightweight operation
Response Time: Sub-second for API endpoints
Scalability Considerations
Individual token claiming reduces backend transaction size
JSON file storage suitable for single-server deployments
Can migrate to database for multi-server environments
BRC-29 derivation enables deterministic payment addressing
Project Structure
Maintenance & Support
Troubleshooting
402 payment fails
Incorrect BRC-29 derivation
Verify protocol ID matches middleware
Token claiming fails
Goal not reached
Check campaign status endpoint
State not persisting
File permissions
Verify write access to project directory
Wallet connection fails
BSV Desktop Wallet not running
Start BSV Desktop Wallet application
Important Concepts
randomizeOutputs: false
Critical for payment middleware. The middleware needs to know the exact output index for transaction internalization.
P2PK vs P2PKH
PushDrop tokens use P2PK (Pay-to-Public-Key), not P2PKH. Script contains raw public key and cannot be found by searching address on explorer.
Individual Token Claiming Pattern
Unlike batch distributions, each investor claims their token individually:
Reduces backend transaction fees (spread across investors)
Gives investors control over when they claim
Prevents single large transaction failure from blocking all tokens
Resources
Repository: GitHub - crowfunding-workshop-demo
Business Documentation: business-crowdfunding-platform.md
BSV SDK Documentation: docs.bsvblockchain.org
Wallet Toolbox: fast.brc.dev
For business context and use cases, see: Business Documentation
Last updated
