controller.ts


Overview

`controller.ts` is a configuration and initialization module for the Litecoin UTXO (Unspent Transaction Output) service within a broader blockchain infrastructure. This file sets up key service components to interact with Litecoin blockchain data through the Blockbook API and an RPC endpoint. It also provides utility functions to identify extended public keys (xpubs) and format Litecoin addresses consistently.

The main purpose of this file is to:

This setup enables efficient querying, formatting, and management of Litecoin UTXOs, crucial for wallet operations, blockchain explorers, or other blockchain data services.


Detailed Explanation

Imports and Constants

Constants fetch environment variables:

The file throws errors if critical environment variables are missing, preventing runtime misconfiguration.


Logger Initialization

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

Blockbook Client Initialization

const blockbook = new Blockbook({ httpURL: INDEXER_URL, wsURL: INDEXER_WS_URL, apiKey: INDEXER_API_KEY, logger })

Function: isXpub

const isXpub = (pubkey: string): boolean => {
  return pubkey.startsWith('Ltub') || pubkey.startsWith('Mtub') || pubkey.startsWith('zpub')
}

Function: formatAddress

export const formatAddress = (address: string): string => {
  if (bech32.decodeUnsafe(address.toLowerCase())?.prefix === 'ltc') return address.toLowerCase()
  return address
}

Service Instantiation

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

Assigning Service to UTXO

UTXO.service = service

Interaction with Other Parts of the System


Implementation Details and Algorithms


Usage Example

import { UTXO } from '../../../common/api/src/utxo/controller'

// Assuming environment variables are set and controller.ts is imported somewhere in the app

async function getUtxosForAddress(address: string) {
  const formatted = formatAddress(address)
  const utxoInstance = new UTXO(formatted)
  const utxos = await utxoInstance.getUtxos() // method from UTXO class (not shown here)
  return utxos
}

This example demonstrates how the configured service and formatting utilities enable seamless UTXO retrieval for a given Litecoin address.


Diagram: Class and Utility Structure

classDiagram
    class Service {
        +constructor(options)
        +blockbook
        +rpcUrl
        +rpcApiKey
        +isXpub(pubkey: string): boolean
        +addressFormatter(address: string): string
        ...
    }

    class UTXO {
        +static service: Service
        +constructor(address: string)
        +getUtxos()
        ...
    }

    class Logger {
        +constructor(options)
        +namespace: string[]
        +level: string
        ...
    }

    class Blockbook {
        +constructor(options)
        +httpURL: string
        +wsURL: string
        +apiKey: string
        +logger: Logger
        ...
    }

    controller.ts ..> Service : instantiates
    controller.ts ..> UTXO : assigns .service
    controller.ts ..> Logger : instantiates
    controller.ts ..> Blockbook : instantiates
    controller.ts ..> isXpub : function
    controller.ts ..> formatAddress : function

Summary

`controller.ts` is a lightweight but critical configuration file that initializes the Litecoin blockchain interaction layer, providing:

This file acts as the integration point connecting generic UTXO infrastructure with Litecoin-specific blockchain access and formatting rules, enabling other components to transparently interact with Litecoin UTXOs.