Crowdfunding Platform

Full-Stack BSV Application with Payment Protocol

Build a crowdfunding platform where users invest with BSV micropayments and receive PushDrop tokens when the campaign completes. This project demonstrates real-world BSV patterns including the 402 Payment Protocol (BRC-103/104), key derivation (BRC-29), and token distribution.

Repository: github.com/bsv-blockchain-demos/crowfunding-workshop-demoarrow-up-right


What You'll Build

A production-ready crowdfunding platform featuring:

  • Campaign progress tracking with live updates

  • BSV micropayments via 402 Payment Protocol

  • Automatic token distribution to investors

  • Both frontend (WalletClient) and backend (Wallet Toolbox) implementations


Learning Objectives

By completing this project, you will learn:

  • Payment Protocol - Implementing BRC-103/104 for HTTP payments

  • Key Derivation - Using BRC-29 for secure payment addressing

  • WalletClient - Frontend wallet integration with createAction

  • Wallet Toolbox - Server-side wallet management

  • PushDrop Tokens - Creating and distributing tokens to investors


Architecture Overview


Key Patterns

1. Frontend Wallet Integration

Connect to the user's BSV Desktop Wallet using a React hook:

Reference: WalletClient Integration

2. Payment Protocol (402 Flow)

The investment flow uses HTTP 402 Payment Required:

  1. Frontend sends POST to /api/invest

  2. Backend responds with 402 and derivation prefix

  3. Frontend derives payment key and creates transaction

  4. Frontend resubmits with x-bsv-payment header

  5. Backend validates and internalizes payment

Reference: BRC-29 | Transaction

3. BRC-29 Key Derivation

The protocol ID and derivation parameters are defined in the middleware:

Frontend derives the payment key for the payee:

Reference: BRC-29

4. Backend Wallet Setup

Server-side wallet using Wallet Toolbox (src/wallet.ts):

Reference: Private Keys | HD Wallets

5. Payment Middleware

Express middleware for handling 402 payments (lib/middleware.ts):

6. Token Distribution

Individual token claiming flow (pages/api/complete.ts):

When the crowdfunding goal is reached, each investor can claim their token individually:

Backend (pages/api/complete.ts):

Frontend (pages/index.tsx) internalizes the received token:

7. State Persistence

Crowdfunding state is saved to a JSON file (lib/storage.ts):

This ensures:

  • State survives server restarts

  • Multiple wallets can run on same system

  • Historical data is preserved

  • Completion transaction TXID is saved

8. Token Viewing

Investors view their tokens using listOutputs:


API Endpoints

Endpoint
Method
Purpose

/api/wallet-info

GET

Backend wallet identity key

/api/invest

POST

Submit investment (402 payment flow)

/api/status

GET

Campaign progress and investor list

/api/complete

POST

Claim individual investor token


Important Concepts

TypeScript Types

The application uses well-defined TypeScript types (src/types.ts):

Individual Token Claiming

Unlike batch distributions, this implementation uses an individual claiming pattern:

  1. Investors make investments via the 402 payment flow

  2. When goal is reached, each investor can claim their token

  3. Backend creates one token per claim request

  4. Each investor is marked as redeemed after claiming

  5. Campaign is marked complete when all investors have redeemed

This approach:

  • Reduces backend transaction fees (spread across investors)

  • Gives investors control over when they claim

  • Prevents a single large transaction failure from blocking all tokens

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

  • Cannot be found by searching address on explorer

  • Must use transaction ID to find tokens

Transaction Internalization

When the backend receives a payment, it internalizes the transaction to track the UTXO. The payment middleware handles this automatically:


Project Structure


Setup

  1. Clone the repository

  2. Setup Backend Wallet

    This creates a backend wallet and funds it with 10,000 satoshis from your local wallet:

    What this does:

    • Creates a new private key (or uses existing from .env)

    • Initializes a backend wallet using BSV Wallet Toolbox

    • Connects to your BSV Desktop Wallet

    • Sends 10,000 satoshis to the backend wallet via BRC-29 payment

    • Saves wallet configuration to .env

  3. Start the application

    Open http://localhost:3000arrow-up-right in your browser.

  4. Connect BSV Desktop Wallet and start investing


Summary

This project demonstrates:

  • 402 Payment Protocol (BRC-103/104) - HTTP-based micropayments with auth and payment middleware

  • BRC-29 Key Derivation - Secure payment addressing with protocol ID [2, '3241645161d8']

  • WalletClient - Frontend wallet integration with createAction and internalizeAction

  • Wallet Toolbox - Server-side wallet management with storage providers

  • PushDrop Tokens - Individual token claiming pattern for investors

  • State Persistence - JSON file storage keyed by wallet identity

  • Individual Token Claiming - Distributed fee model where investors claim tokens individually

These patterns form the foundation for building real-world BSV payment applications with proper state management and token distribution.


Last updated