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:

  1. Mints ('___mint___') create new supply

  2. Transfers: Ξ£ inputs = Ξ£ outputs

  3. Reject 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/tokenizationarrow-up-right

  • Overlay Service Code: /overlay folder

  • Frontend Code: /frontend folder

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: PushDroparrow-up-right, BRC-88: Overlay Servicesarrow-up-right


2. MongoDB Storage

src/TokenStorage.ts:

Index Strategy:

  • Composite {txid, outputIndex} - Fast outpoint lookups

  • Hashed {tokenId} - Efficient token searches

  • Timestamp {createdAt} - Sorted queries


3. Lookup Service (Indexer)

src/MyTokenLookupService.ts:

Lifecycle:

  1. Topic Manager admits output β†’ outputAdded() called

  2. Extract token data β†’ Store in MongoDB

  3. 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: SPVarrow-up-right


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