Overlay Services
Build a token validation overlay using BSV SDK's overlay architecture, Topic Managers, and Lookup Services.
What You'll Build
Transaction validation with balance conservation
BEEF parsing (BRC-62) and PushDrop decoding (BRC-48)
MongoDB UTXO indexing
REST API for token queries
Validation Rules:
Mints (
'___mint___') create new supplyTransfers:
Ξ£ inputs = Ξ£ outputsReject any balance mismatch
π¦ Complete Code Example
The full working implementation of this overlay service is available on GitHub:
Repository: github.com/sirdeggen/demo-day/tree/main/tokenization
Overlay Service Code:
/overlayfolderFrontend Code:
/frontendfolder
You can clone the repository and explore the complete implementation alongside this tutorial.
Setup
Create .env:
Create tsconfig.json:
BSV SDK Requirement: Use ES2022+ for BigInt support in amount fields.
1. Topic Manager (Validator)
src/MyTokenTopicManager.ts:
Balance Accounting:
Reference: BRC-48: PushDrop, BRC-88: Overlay Services
2. MongoDB Storage
src/TokenStorage.ts:
Index Strategy:
Composite
{txid, outputIndex}- Fast outpoint lookupsHashed
{tokenId}- Efficient token searchesTimestamp
{createdAt}- Sorted queries
3. Lookup Service (Indexer)
src/MyTokenLookupService.ts:
Lifecycle:
Topic Manager admits output β
outputAdded()calledExtract token data β Store in MongoDB
Client queries β
lookup()returns results
4. Main Server
src/index.ts:
5. SPV Verification (Optional)
Add ChainTracker for merkle proof validation:
Purpose: Validates merkle paths in BEEF packages without full node.
Reference: BRC-9: SPV
Testing
Start MongoDB:
Start Overlay:
Submit Transaction (from client):
Query Tokens:
Key Concepts
Topic Manager vs Lookup Service:
Topic Manager - Validates before admission (gatekeeper)
Lookup Service - Indexes after admission (librarian)
Balance Conservation:
Enhancements
1. Spend Tracking:
2. Metadata Validation:
3. Token Registry:
Resources
Last updated
