controller.ts


Overview

The [controller.ts](/projects/291/68853) file serves as the primary configuration and initialization module for the Bitcoin UTXO (Unspent Transaction Output) service within a larger modular blockchain application. It establishes network connectivity to blockchain indexer and RPC services, configures API keys, sets up logging, and provides utility functions for address formatting and public key validation. The file exports a ready-to-use service instance and integrates it with the UTXO controller layer, enabling seamless blockchain data retrieval and transaction handling for Bitcoin and Bitcoin-like networks.


Detailed Explanation

Environment Configuration and Validation


Logger Initialization

export const logger = new Logger({
  namespace: ['unchained', 'coinstacks', 'bitcoin', 'api'],
  level: process.env.LOG_LEVEL,
})

API Endpoint Construction

const httpURL = INDEXER_API_KEY && IS_LIQUIFY ? `${INDEXER_URL}/api=${INDEXER_API_KEY}` : INDEXER_URL
const wsURL = INDEXER_API_KEY && IS_LIQUIFY ? `${INDEXER_WS_URL}/api=${INDEXER_API_KEY}` : INDEXER_WS_URL
const rpcUrl = RPC_API_KEY && IS_LIQUIFY ? `${RPC_URL}/api=${RPC_API_KEY}` : RPC_URL

const apiKey = INDEXER_API_KEY && IS_NOWNODES ? INDEXER_API_KEY : undefined
const rpcApiKey = RPC_API_KEY && IS_NOWNODES ? RPC_API_KEY : undefined

Blockbook Client Initialization

const blockbook = new Blockbook({ httpURL, wsURL, logger, apiKey })

Utility Functions

isXpub(pubkey: string): boolean

formatAddress(address: string): string


Service Initialization and Export

export const service = new Service({
  blockbook,
  rpcUrl,
  isXpub,
  addressFormatter: formatAddress,
  rpcApiKey,
})

Integration with UTXO Controller

UTXO.service = service

Important Implementation Details


Interaction with Other Parts of the System


Usage Example

import { UTXO } from './controller'

// This UTXO instance automatically uses the configured service
const utxoInstance = new UTXO()

// Check if a string is an extended public key
console.log(utxoInstance.service.isXpub('xpub6...')) // true

// Format a Bitcoin address
const formattedAddress = utxoInstance.service.addressFormatter('BC1QW508D6QEJXTDG4Y5R3ZARVARY0C5XW7KV8F3T4')
console.log(formattedAddress) // 'bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'

// Use UTXO methods that internally leverage the blockbook and RPC clients
utxoInstance.getTransaction('some-txid').then(tx => console.log(tx))

Mermaid Class Diagram

classDiagram
    class controller {
        <<module>>
        +logger: Logger
        +service: Service
        +isXpub(pubkey: string): boolean
        +formatAddress(address: string): string
    }

    class Blockbook {
        +constructor(config)
        +httpURL: string
        +wsURL: string
        +logger: Logger
        +apiKey?: string
        +subscribe()
        +getTransaction(txid: string)
        +getAddress(address: string)
        ...
    }

    class Service {
        +constructor(config)
        +blockbook: Blockbook
        +rpcUrl: string
        +isXpub(pubkey: string): boolean
        +addressFormatter(address: string): string
        +rpcApiKey?: string
        +getTransaction(txid: string)
        +getBalance(address: string)
        ...
    }

    class UTXO {
        +static service: Service
        +getTransaction(txid: string)
        +getBalance(address: string)
        ...
    }

    controller ..> Blockbook : instantiates
    controller ..> Service : instantiates
    controller ..> UTXO : assigns service
    Service *-- Blockbook : uses
    UTXO ..> Service : uses (static property)

Summary

The [controller.ts](/projects/291/68853) file is a critical bootstrap and configuration script for Bitcoin UTXO blockchain data services. By handling environment-based setup, logging, client instantiation, and utility functions, it provides a centralized service instance that the UTXO controller layer uses to interact with blockchain network data through indexers and RPC nodes. The file's design supports multiple provider integrations and enforces consistent data formatting and key validation, facilitating reliable and flexible Bitcoin-related operations within the overall system.