index.ts
Overview
`index.ts` serves as an aggregation and utility entry point within the project’s blockchain-related modules. Its primary purpose is to:
Re-export core components and models (
Blockbook, transaction-related types, and websocket utilities) to provide a consolidated import source for consumers.Define TypeScript interfaces that model various blockchain events and websocket responses used throughout the system.
Provide a utility function,
getAddresses, which extracts a unique list of all blockchain addresses involved in a given transaction (Tx).
This file acts as a bridge between raw blockchain data structures and higher-level application logic, simplifying the integration and handling of blockchain events and transaction data.
Exported Entities
Re-exports
Blockbookfrom./controllerAll exports from
./modelsAll exports from
./websocket
These re-exports allow external modules to import blockchain controller logic, data models, and websocket utilities from a single entry point.
Interfaces
1. NewBlock
Represents a new block event in the blockchain.
Property | Type | Description |
|---|---|---|
height | number | The height (index) of the block in the blockchain. |
hash | string | The unique hash identifier of the block. |
2. NewTx
Represents a new blockchain transaction event related to a specific address.
Property | Type | Description |
|---|---|---|
address | string | The blockchain address involved in the transaction. |
tx | `Tx` | The transaction object (imported from `./models`). |
3. SubscriptionResponse
Represents the response status for a subscription request over a websocket or API.
Property | Type | Description |
|---|---|---|
subscribed | boolean | Indicates if the subscription was successful (`true`) or not (`false`). |
4. WebsocketRepsonse
Represents the structure of a message received via websocket.
Property | Type | Description |
|---|---|---|
id | string | Unique identifier of the websocket message. |
data | `NewBlock` \ | `NewTx` \ |
**Note:** There is a typographical error in the interface name: `WebsocketRepsonse` should likely be `WebsocketResponse`.
Function: getAddresses
function getAddresses(tx: Tx): Array<string>
Description
Extracts a unique set of all blockchain addresses referenced in a given transaction (`Tx`). It scans through the transaction’s inputs (`vin`), outputs (`vout`), and token transfers to collect all associated addresses.
Parameters
tx(Tx): The transaction object to analyze.Txis imported from./modelsand represents the blockchain transaction structure.
Returns
Array<string>: An array of unique blockchain addresses found in the transaction.
Detailed Behavior
Iterates over all inputs (
vin) in the transaction:Checks if the input is associated with an address (
isAddress).If yes, collects all addresses in
vin.addresses.
Iterates over all outputs (
vout):Checks if the output is associated with an address (
isAddress).If yes, collects all addresses in
vout.addresses.
Iterates over all token transfers (
tokenTransfers):Adds the
fromandtoaddresses, if present.
Eliminates duplicate addresses by converting the collected list to a
Setand back to an array.
Usage Example
import { getAddresses } from './index'
import { Tx } from './models'
const transaction: Tx = /* fetched or constructed transaction */
const addresses = getAddresses(transaction)
console.log(addresses)
// Output: ['addr1...', 'addr2...', ...] - unique addresses involved in the transaction
Important Implementation Details
The function safely navigates optional arrays (
vin?,vout?,tokenTransfers?) to avoid runtime exceptions.Uses the
isAddressboolean flag to determine if a particular input or output actually corresponds to address data.The use of
new Set()ensures that the returned list contains no duplicate addresses, which is important for downstream processing or display.By including token transfers alongside standard inputs and outputs, the function provides comprehensive coverage of all address activity within complex transactions.
Interaction with Other Modules
Models (
./models): This file relies on theTxtype which encapsulates the structure of blockchain transactions. The models define the shape of inputs (vin), outputs (vout), and token transfers.Controller (
./controller): ExposesBlockbook, presumably a controller class or module to manage blockchain data retrieval and subscription.Websocket (
./websocket): Re-exports websocket related types and utilities used for real-time blockchain event streaming.Consumers: Other parts of the application import this file to get unified access to blockchain event types, transaction utilities, and controller classes.
Mermaid Diagram: Structure of index.ts
classDiagram
class index {
<<interface>>
+NewBlock
+NewTx
+SubscriptionResponse
+WebsocketRepsonse
--
+getAddresses(tx: Tx): Array~string~
}
class Tx {
<<imported>>
}
class Blockbook {
<<exported>>
}
index ..> Tx : uses
index ..> Blockbook : exports
Summary
`index.ts` is a critical integration file within the blockchain module, offering:
Centralized exports for blockchain controllers, models, and websocket utilities.
Typed interfaces for representing blockchain event payloads and websocket messages.
A utility function for extracting all relevant addresses from complex blockchain transactions.
This design promotes modularity, type safety, and ease of use for other parts of the system interacting with blockchain data and events.