Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

πŸš€ BSV Monetization Plugin

A lightweight Node.js Express middleware for implementing prepaid credit monetization with BSV (Bitcoin SV) payments. Users deposit BSV once, receive credits, and pay per API callβ€”no per-request payment friction.

🎯 Core Features

βœ… ON/OFF Monetization Switch - Enable/disable monetization globally
βœ… Prepaid Credit System - Users deposit BSV, receive credits instantly
βœ… Automatic Credit Deduction - 1 API call = X credits (configurable)
βœ… Payment Verification - Validates BSV transactions on blockchain
βœ… Credit Management - Track balances, deposits, and consumption per user
βœ… Comprehensive Logging - Audit trail of all transactions
βœ… Admin Dashboard - View users, transactions, and logs
βœ… Docker Ready - Full Docker & Docker Compose support
βœ… Protected Endpoints - Enforce credit requirements per route


πŸ“‹ System Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                      API Server (Express)                    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Free Endpoints:          Protected Endpoints:               β”‚
β”‚  β€’ GET /health           β€’ GET /paid (1 credit)             β”‚
β”‚  β€’ GET /free             β€’ GET /premium (5 credits)         β”‚
β”‚  β€’ GET /config           β€’ POST /api/data (1 credit)        β”‚
β”‚                                                              β”‚
β”‚  Payment Flow:            Admin Endpoints:                   β”‚
β”‚  β€’ POST /deposit         β€’ GET /admin/users                 β”‚
β”‚  β€’ POST /verify-payment  β€’ GET /admin/transactions          β”‚
β”‚  β€’ POST /simulate-payment β€’ GET /admin/logs                  β”‚
β”‚  β€’ GET /balance          β€’ GET /admin/config                β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Credit Manager (In-Memory)                β”‚
β”‚  β€’ User balances          β€’ Transaction history              β”‚
β”‚  β€’ Deposit tracking       β€’ Usage analytics                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                              ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              Payment Verifier (BSV Blockchain)               β”‚
β”‚  β€’ Transaction verification (simulated + real)               β”‚
β”‚  β€’ Credit calculation (BSV β†’ Credits)                        β”‚
β”‚  β€’ Double-spend prevention                                   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

1. Local Development

# Install dependencies
npm install

# Start server
npm start

# In another terminal, run tests
npm test

Server runs on: http://localhost:3000

2. Docker Deployment

# Build image
npm run docker:build

# Run with Docker Compose
npm run docker:run

Exposed on: http://localhost:3000


βš™οΈ Configuration

Edit config/monetization.json:

{
  "monetization": "ON",
  "price_per_call": 0.001,
  "currency": "BSV",
  "payout_address": "REPLACE_WITH_YOUR_BSV_ADDRESS",
  "payment_mode": "prepaid",
  "credits_per_call": 1
}

Or use environment variables:

MONETIZATION=ON
PRICE_PER_CALL=0.001
CURRENCY=BSV
PAYOUT_ADDRESS=YOUR_BSV_WALLET
PAYMENT_MODE=prepaid
CREDITS_PER_CALL=1
ADMIN_TOKEN=admin_secret

πŸ“š API Endpoints

Free Endpoints (No credits required)

GET /health

Check server status

curl http://localhost:3000/health

GET /free

Free endpoint example

curl http://localhost:3000/free

GET /config

Get monetization configuration

curl http://localhost:3000/config

Payment Endpoints

POST /deposit

Request deposit instructions

curl -X POST http://localhost:3000/deposit \
  -H "Content-Type: application/json" \
  -H "x-api-key: your-api-key" \
  -d '{"amount": 0.01}'

Response:

{
  "status": "DEPOSIT_INITIATED",
  "depositInstructions": {
    "payTo": "your_payout_address",
    "amount": 0.01,
    "currency": "BSV"
  },
  "estimatedCredits": 10,
  "yourApiKey": "your-api-key"
}

POST /verify-payment

Verify payment and add credits

curl -X POST http://localhost:3000/verify-payment \
  -H "Content-Type: application/json" \
  -H "x-api-key: your-api-key" \
  -d '{"txid": "abc123...", "amount": 0.01}'

POST /simulate-payment (For Testing)

Simulate a payment in test mode

curl -X POST http://localhost:3000/simulate-payment \
  -H "Content-Type: application/json" \
  -H "x-api-key: test-user" \
  -d '{"amount": 0.01}'

Account Endpoints (Requires x-api-key)

GET /balance

Check credit balance

curl http://localhost:3000/balance \
  -H "x-api-key: your-api-key"

Response:

{
  "apiKey": "your-api-key",
  "credits": 42,
  "transactions": [...]
}

GET /transactions

Get transaction history

curl http://localhost:3000/transactions \
  -H "x-api-key: your-api-key"

Protected Endpoints (Requires credits)

GET /paid

Protected endpoint (costs 1 credit)

curl http://localhost:3000/paid \
  -H "x-api-key: your-api-key"

Response headers:

X-Credits-Remaining: 41
X-Credits-Deducted: 1

GET /premium

Premium endpoint (costs 5 credits)

curl http://localhost:3000/premium \
  -H "x-api-key: your-api-key"

POST /api/data

Data API endpoint (costs 1 credit)

curl -X POST http://localhost:3000/api/data \
  -H "Content-Type: application/json" \
  -H "x-api-key: your-api-key" \
  -d '{"query": "data"}'

Admin Endpoints (Requires token=admin_secret)

GET /admin/users?token=admin_secret

View all users

curl "http://localhost:3000/admin/users?token=admin_secret"

GET /admin/transactions?token=admin_secret

View all transactions

curl "http://localhost:3000/admin/transactions?token=admin_secret"

GET /admin/logs?token=admin_secret

View all logs

curl "http://localhost:3000/admin/logs?token=admin_secret"

GET /admin/config?token=admin_secret

Get full configuration

curl "http://localhost:3000/admin/config?token=admin_secret"

πŸ§ͺ Complete Test Scenario

Run the test suite to verify everything works:

# Terminal 1: Start server
npm start

# Terminal 2: Run tests
npm test

What the tests verify:

  1. βœ… Server health check
  2. βœ… Configuration loaded correctly
  3. βœ… Protected endpoint rejects requests without credits
  4. βœ… Payment simulation works
  5. βœ… Credits are added correctly
  6. βœ… Protected endpoint accepts requests with credits
  7. βœ… Multiple requests consume credits correctly
  8. βœ… Premium endpoint enforces 5-credit requirement
  9. βœ… Admin endpoints work
  10. βœ… Full audit trail is logged

πŸ“ Logging

Logs are stored in the logs/ directory:

  • deposits.log - All deposit transactions
  • credits.log - Credit additions and deductions
  • payments.log - Payment verification events
  • access-denied.log - Failed access attempts
  • api-calls.log - All API requests
  • errors.log - System errors

Example log entry:

[2026-04-22T10:30:45.123Z] CREDITS_DEDUCTED: apiKey=user123, credits=1, endpoint=/paid, balanceAfter=41

πŸ”„ Payment Flow Diagram

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 1. User requests deposit         β”‚
β”‚    POST /deposit                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 2. Server returns:               β”‚
β”‚    β€’ Payout address              β”‚
β”‚    β€’ Amount required             β”‚
β”‚    β€’ Estimated credits           β”‚
β”‚    β€’ API key (auto-generated)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 3. User sends BSV to payout_addr β”‚
β”‚    (BSV blockchain)              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 4. User verifies payment         β”‚
β”‚    POST /verify-payment          β”‚
β”‚    + txid + amount               β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 5. Server verifies on BSV        β”‚
β”‚    blockchain (or simulation)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 6. Credits added:                β”‚
β”‚    credits = amount / price      β”‚
β”‚    Example: 0.01 / 0.001 = 10   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
               ↓
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ 7. User can now call /paid       β”‚
β”‚    1 credit deducted per call    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ’‘ Usage Examples

Example 1: Complete User Journey

# 1. Simulate a payment (get 10 credits for 0.01 BSV)
API_KEY=$(curl -s -X POST http://localhost:3000/simulate-payment \
  -H "Content-Type: application/json" \
  -H "x-api-key: new-user" \
  -d '{"amount": 0.01}' | jq -r '.apiKey')

# 2. Check balance
curl http://localhost:3000/balance \
  -H "x-api-key: $API_KEY" | jq '.credits'
# Output: 10

# 3. Call protected endpoint 3 times
for i in {1..3}; do
  curl http://localhost:3000/paid \
    -H "x-api-key: $API_KEY" | jq '.creditsRemaining'
done
# Outputs: 9, 8, 7

# 4. Check transaction history
curl http://localhost:3000/transactions \
  -H "x-api-key: $API_KEY" | jq '.transactions'

Example 2: Disable Monetization

# Set monetization to OFF in config/monetization.json
{
  "monetization": "OFF",  # ← Change this
  ...
}

# Now all protected endpoints work without credits
curl http://localhost:3000/paid
# Response: "Protected endpoint accessed successfully"

Example 3: Custom Credit Costs

{
  "monetization": "ON",
  "price_per_call": 0.002,  # More expensive
  "credits_per_call": 1,
  ...
}

// Now: 0.01 BSV = 5 credits (0.01 / 0.002 = 5)
// Instead of: 0.01 BSV = 10 credits

πŸ” Security Notes

⚠️ For Production:

  1. Change admin token: Set ADMIN_TOKEN to a strong random value
  2. Use real BSV address: Replace payout_address with your wallet
  3. Enable HTTPS: Deploy behind nginx/apache with SSL
  4. Validate txid: Connect to real BSV blockchain API (not simulation)
  5. Use database: Replace in-memory storage with PostgreSQL/MongoDB
  6. Rate limiting: Add rate limiting middleware
  7. API key validation: Use JWT or similar for API key generation

πŸ“¦ Project Structure

β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ index.js                 # Main Express app & endpoints
β”‚   β”œβ”€β”€ creditManager.js         # Credit balance tracking
β”‚   β”œβ”€β”€ monetizationMiddleware.js # Credit enforcement middleware
β”‚   β”œβ”€β”€ paymentVerifier.js       # Transaction verification
β”‚   β”œβ”€β”€ config.js                # Configuration manager
β”‚   └── logger.js                # Event logging
β”œβ”€β”€ config/
β”‚   └── monetization.json        # Configuration file
β”œβ”€β”€ tests/
β”‚   └── test-scenario.js         # Complete test suite
β”œβ”€β”€ docker-compose.yml           # Docker Compose setup
β”œβ”€β”€ Dockerfile                   # Docker image definition
β”œβ”€β”€ package.json                 # Dependencies
└── README.md                    # This file

πŸ› οΈ Development Commands

# Install dependencies
npm install

# Start server (development with nodemon)
npm run dev

# Start server (production)
npm start

# Run tests
npm test

# Build Docker image
npm run docker:build

# Run with Docker
npm run docker:run

# View logs (after running app)
tail -f logs/*.log

πŸ“Š Environment Variables

Variable Default Purpose
PORT 3000 Server port
NODE_ENV development Environment mode
MONETIZATION ON Enable/disable monetization
PRICE_PER_CALL 0.001 BSV per API call
CURRENCY BSV Payment currency
PAYOUT_ADDRESS REPLACE_... BSV wallet address
PAYMENT_MODE prepaid Payment model
CREDITS_PER_CALL 1 Credits per endpoint call
ADMIN_TOKEN admin_secret Admin authentication

πŸš€ Production Deployment

AWS EC2 Example

# SSH into instance
ssh ec2-user@your-instance.compute.amazonaws.com

# Clone repo
git clone <repo> && cd monetization-switch

# Install Node.js
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash
sudo yum install -y nodejs

# Install dependencies & run with PM2
npm install
sudo npm install -g pm2
pm2 start npm --name "monetization" -- start
pm2 startup
pm2 save

Docker Hub Deployment

# Build and tag
docker build -t your-username/bsv-monetization:latest .

# Push to Docker Hub
docker push your-username/bsv-monetization:latest

# Pull and run anywhere
docker pull your-username/bsv-monetization
docker run -p 3000:3000 your-username/bsv-monetization

πŸ”— Real BSV Integration

To use real BSV payments instead of simulation:

  1. Configure BSV API endpoint in paymentVerifier.js:
async verifyTransaction(txid, expectedAmount, payoutAddress) {
  // Instead of simulation:
  const response = await axios.get(
    `https://api.whiteonion.io/v3/tx/${txid}`
  );
  
  // Parse and validate response
  const tx = response.data;
  const verified = tx.outputs.some(output => 
    output.address === payoutAddress && 
    output.amount === expectedAmount
  );
  
  return { verified, ...tx };
}
  1. Test with testnet BSV first
  2. Switch to mainnet when ready

πŸ“ž Support

  • πŸ“– Documentation: See README.md
  • πŸ§ͺ Examples: Run npm test
  • πŸ› Issues: Check logs in logs/ directory
  • πŸ’¬ Questions: Review comments in source code

πŸ“„ License

MIT License - Free to use and modify


✨ What's Included

βœ… Complete Node.js Express server
βœ… Pre-built middleware for credit enforcement
βœ… In-memory credit manager (ready for database upgrade)
βœ… Payment verification layer (simulation + BSV ready)
βœ… Comprehensive logging system
βœ… Docker & Docker Compose setup
βœ… Full test suite
βœ… Admin dashboard endpoints
βœ… Configuration management
βœ… Error handling & validation


Ready to monetize? Deploy with npm run docker:run πŸš€

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages