@trezor/blockchain-link package provides a unified client interface for multiple blockchain backend implementations. It abstracts the differences between various blockchain APIs, allowing Trezor Suite to interact with different networks through a consistent interface.
Overview
blockchain-link is the bridge between Trezor Suite and various blockchain backends. It handles:
- Blockchain connectivity and communication
- Transaction fetching and broadcasting
- Account balance and history queries
- Block and mempool monitoring
- WebSocket connections for real-time updates
Supported Backends
Blockbook
Maintained by: SatoshiLabsNetworks: Bitcoin-like and Ethereum-likeFeatures: Full history, UTXO tracking, mempool monitoringBlockbook on GitHub
Electrum
Maintained by: Third partiesNetworks: Bitcoin networkFeatures: Lightweight, wide server availabilityElectrum Protocol Docs
Ripple
Maintained by: Ripple/XRP Ledger FoundationNetworks: Ripple (XRP)Features: Native XRP Ledger supportXRP Ledger Docs
Blockfrost
Maintained by: Blockfrost.ioNetworks: Cardano (ADA)Features: Cardano-specific functionalityBlockfrost Docs
Installation
Install the package in your project:Basic Usage
Initializing a Connection
Fetching Blockchain Info
Getting Account Information
Subscribing to Block Updates
Architecture
Worker-Based Design
Blockchain-link uses Web Workers to handle network communication without blocking the main thread:Key Components
BlockchainLink Class
BlockchainLink Class
The main class that:
- Manages worker lifecycle
- Handles message passing between main thread and worker
- Provides the public API
- Manages event subscriptions
- Implements request throttling
Workers
Workers
Each backend has a dedicated worker implementation:
workers/blockbook/index.ts- Blockbook backendworkers/electrum/index.ts- Electrum backendworkers/ripple/index.ts- Ripple backendworkers/blockfrost/index.ts- Blockfrost backend
- Network communication
- Response parsing
- WebSocket management
- Error handling
Type Definitions
Type Definitions
Shared types in
@trezor/blockchain-link-types:- Request/response types
- Event types
- Backend-specific types
- Error definitions
Utilities
Utilities
Helper functions in
@trezor/blockchain-link-utils:- Response transformers
- Data validators
- Common utilities
Worker Implementation
Using Workers in Different Environments
Web (with Webpack)
Web (with URL)
Node.js (without Workers)
API Reference
Methods
connect()
connect()
Establishes connection to the blockchain backend.
disconnect()
disconnect()
Closes the connection and cleans up resources.
getInfo()
getInfo()
Gets current blockchain info (block height, hash, etc.).
getAccountInfo(request)
getAccountInfo(request)
Fetches account information including balance and history.
getTransaction(txid)
getTransaction(txid)
Gets details of a specific transaction.
pushTransaction(tx)
pushTransaction(tx)
Broadcasts a signed transaction to the network.
estimateFee(blocks)
estimateFee(blocks)
Estimates transaction fee for confirmation within N blocks.
subscribe(subscription)
subscribe(subscription)
Subscribes to blockchain events (blocks, addresses, etc.).
unsubscribe(subscription)
unsubscribe(subscription)
Unsubscribes from blockchain events.
Events
Listen to events using the EventEmitter pattern:Development
Running the Development UI
Blockchain-link includes a testing UI:Running Tests
Integration Tests
Configuration Options
Name used in debug logs to identify this instance
Worker implementation or path to worker script
Backend server URL(s). Multiple URLs provide fallback support
Enable debug logging
Connection timeout in milliseconds
Throttle block events to prevent overwhelming the UI (in ms)
Related Packages
blockchain-link-types
TypeScript type definitions
blockchain-link-utils
Utility functions and helpers
websocket-client
WebSocket client implementation
Best Practices
Always Handle Errors
Always Handle Errors
Network requests can fail. Always wrap API calls in try-catch blocks:
Use Multiple Servers
Use Multiple Servers
Provide fallback servers for reliability:
Clean Up Subscriptions
Clean Up Subscriptions
Unsubscribe when no longer needed to prevent memory leaks:
Throttle Block Events
Throttle Block Events
Use the
throttleBlockEvent option to prevent UI performance issues during rapid block updates:Next Steps
Package Overview
Explore all packages in the monorepo
Utilities
Learn about utility packages
Connect Package
Explore the Connect package documentation
Common Tasks
Development workflows and troubleshooting