Token Fundamentals

Build a token management interface using BSV SDK's PushDrop, WalletClient, and MessageBox.


What You'll Build

  • Token minting with PushDrop (BRC-48)

  • P2P transfers via MessageBox (BRC-33)

  • Balance tracking with UTXO aggregation

  • Two-phase transaction signing

Token Structure (3 fields):

[
  tokenId,    // UTF-8: '___mint___' or 'txid.vout'
  amount,     // Uint64LE (8 bytes)
  metadata    // JSON string
]

📦 Complete Code Example

The full working implementation of this token frontend is available on GitHub:

Repository: github.com/sirdeggen/demo-day/tree/main/tokenizationarrow-up-right

  • Frontend Code: /frontend folder

  • Overlay Service Code: /overlay folder

You can clone the repository and explore the complete implementation alongside this tutorial.


Setup

Create .env:


1. Wallet Context

src/context/WalletContext.tsx:

BSV SDK Pattern: WalletClient connects to BSV Desktop Wallet automatically. No API keys needed.


2. Token Minting

src/components/CreateTokens.tsx:

Critical Points:

  • '___mint___' marker creates new token → tokenId becomes txid.vout

  • customInstructions stores derivation params (required for spending)

  • randomizeOutputs: false ensures predictable output indices

Reference: BRC-48: PushDroparrow-up-right, BRC-42: Key Derivationarrow-up-right


3. Balance Viewer

src/components/TokenWallet.tsx:

Pattern: Aggregate all UTXOs by tokenId to get total balance per token type.


4. Token Transfer (Two-Phase Signing)

src/components/SendTokens.tsx:

Two-Phase Signing:

  1. createAction() - Wallet adds BSV inputs, estimates unlock length

  2. Manual signing - Create PushDrop unlock scripts using customInstructions

  3. signAction() - Submit completed unlocking scripts

Reference: BRC-62: BEEFarrow-up-right, BRC-33: MessageBoxarrow-up-right


5. Token Receiving

src/components/ReceiveTokens.tsx:

Pattern: internalizeAction imports transaction with derivation params for future spending.


Key Concepts

Privacy (BRC-42)

Every token output uses a unique derived address:

  • Random keyID per output prevents address reuse

  • Cannot link UTXOs to same owner

BEEF Format (BRC-62)

Transactions include parent txs + merkle proofs for SPV validation without full blockchain.

Critical: customInstructions

MUST store derivation params to spend tokens:

Without this, tokens are permanently unspendable.


Troubleshooting

  • "Failed to connect" - Install/unlock BSV Desktop Wallet

  • "Insufficient balance" - Need BSV for tx fees (get testnet coins from faucetarrow-up-right)

  • Tokens not appearing - Check correct basket name, verify customInstructions stored


Resources

Last updated