service.ts


Overview

The [service.ts](/projects/291/69094) file implements the **UTXO blockchain service layer** for the Unified API Layer architecture. It provides a concrete `Service` class that interacts with UTXO-based blockchain data sources, primarily leveraging the **Blockbook indexer** and **RPC node endpoints**, to facilitate blockchain data retrieval and transaction management.

This service abstracts the complexities of UTXO blockchain querying, including handling extended public keys (xpubs), addresses, transaction history pagination, raw transaction fetching, UTXO retrieval, fee estimation, and transaction broadcasting. It converts raw blockchain data into consistent, API-friendly models and manages error handling uniformly.

**Key responsibilities:**


Class: Service

The central class exported by this file, `Service`, implements the blockchain-specific API logic for UTXO chains and conforms to the `API` interface (an extension of [BaseAPI](/projects/291/69264) excluding `getInfo`).

Constructor

constructor(args: ServiceArgs)

Initializes a new instance of the service.


Methods


async getAccount(pubkey: string): Promise<Account>

Fetches account details for a given public key (address or xpub).


async getTxHistory(pubkey: string, cursor?: string, pageSize = 10): Promise<TxHistory>

Retrieves paginated transaction history for an address or xpub.


async getTransaction(txid: string): Promise<Tx>

Fetches detailed transaction information for a given transaction ID.


async getRawTransaction(txid: string): Promise<RawTx>

Fetches the raw transaction hex and related data for a transaction.


async getUtxos(pubkey: string): Promise<Utxo[]>

Retrieves all unspent transaction outputs (UTXOs) for the given address or xpub.


async sendTx(body: SendTxBody): Promise<string>

Broadcasts a raw transaction to the blockchain network.


async getNetworkFees(): Promise<NetworkFees>

Fetches current recommended network fees for different confirmation speeds.


async handleBlock(hash: string): Promise<BlockbookTx[]>

Fetches all transactions for a block by hash.


handleTransaction(tx: BlockbookTx): Tx

Converts a raw Blockbook transaction into the internal `Tx` model.


Important Implementation Details and Algorithms


Interactions with Other System Components


Visual Diagram

classDiagram
    class Service {
        -blockbook: Blockbook
        -rpcUrl: string
        -rpcApiKey?: string
        -formatAddress: AddressFormatter
        +isXpub(pubkey: string): boolean
        +constructor(args: ServiceArgs)
        +getAccount(pubkey: string): Promise<Account>
        +getTxHistory(pubkey: string, cursor?: string, pageSize: number): Promise<TxHistory>
        +getTransaction(txid: string): Promise<Tx>
        +getRawTransaction(txid: string): Promise<RawTx>
        +getUtxos(pubkey: string): Promise<Utxo[]>
        +sendTx(body: SendTxBody): Promise<string>
        +getNetworkFees(): Promise<NetworkFees>
        +handleBlock(hash: string): Promise<BlockbookTx[]>
        +handleTransaction(tx: BlockbookTx): Tx
    }

Summary

The [service.ts](/projects/291/69094) file provides the backbone of the UTXO blockchain support in the Unified API Layer. It encapsulates all business logic and external data source integration needed to serve blockchain data and transactions in a normalized, paginated, and consistent manner.

By abstracting Blockbook and node RPC interactions behind clean methods, it allows API controllers and consumers to operate without concern for the underlying blockchain data complexities. The service handles important UTXO-specific concerns such as address vs. xpub logic, cursor pagination, UTXO retrieval, and fee conversions, ensuring robust and reliable blockchain API functionality.

This design supports extensibility, testability, and maintainability within the modular Unified API Layer framework.