models.ts


Overview

The [models.ts](/projects/291/68901) file defines TypeScript interfaces and types that represent core blockchain data structures and related entities used throughout the application. These models encapsulate transaction details, unspent outputs, account and address information, network fees, and specify an API interface to interact with blockchain data sources.

Key functionalities include:

This file acts as a central schema definition for blockchain-related data and API contracts, promoting consistency and type safety across the system.


Detailed Documentation

Interfaces and Types


Vin

**Purpose:** Represents a transaction input, providing details about the source of funds in a transaction.

**Properties:**

Property

Type

Description

`txid`

`string` (opt)

The transaction ID of the previous transaction output.

`vout`

`string` (opt)

The index of the output in the previous transaction.

`sequence`

`number` (opt)

Sequence number, used for transaction replacement features.

`coinbase`

`string` (opt)

Coinbase data if this is a coinbase (mining reward) input.

`scriptSig`

`{ hex?: string }` (opt)

Script signature hex string authorizing spending of UTXO.

`addresses`

`Array` (opt)

List of addresses associated with this input.

`value`

`string` (opt)

The value of input in satoshis or coin units.

**Usage Example:**

const input: Vin = {
  txid: "abc123",
  vout: "0",
  sequence: 4294967295,
  scriptSig: { hex: "483045022100..." },
  addresses: ["1A1zP1..."],
  value: "0.001"
};

Vout

**Purpose:** Describes a single transaction output, including its value and destination.

**Properties:**

Property

Type

Description

`value`

`string`

Amount sent to this output.

`n`

`number`

Output index within the transaction.

`opReturn`

`string` (opt)

Optional OP_RETURN data (metadata).

`scriptPubKey`

`{ hex?: string }`

Script public key in hex format.

`addresses`

`Array` (opt)

Addresses receiving this output.

**Usage Example:**

const output: Vout = {
  value: "0.002",
  n: 1,
  scriptPubKey: { hex: "76a91488ac..." },
  addresses: ["1BvBMSE..."]
};

Tx

**Inheritance:** Extends `BaseTx` (imported from an external module).

**Purpose:** Encapsulates a full transaction's details, including inputs, outputs, fees, and confirmations.

**Properties:**

Property

Type

Description

`vin`

`Array`

List of transaction inputs.

`vout`

`Array`

List of transaction outputs.

`confirmations`

`number`

Number of confirmations on the blockchain.

`value`

`string`

Total value transferred by the transaction.

`fee`

`string`

Transaction fee paid.

`hex`

`string`

Raw transaction serialized in hex.

**Usage Example:**

const transaction: Tx = {
  vin: [input1, input2],
  vout: [output1, output2],
  confirmations: 10,
  value: "0.003",
  fee: "0.0001",
  hex: "0200000001..."
};

TxHistory

**Type Alias:** `BaseTxHistory`

**Purpose:** Represents a collection or history of transactions of type `Tx`. This type derives from a generic base transaction history model.


RawTx

**Purpose:** Represents the raw transaction data as returned directly by a blockchain node's API.

**Properties:** Includes complete low-level transaction details such as version, size, inputs, outputs, and block information. The structure closely matches the data returned by blockchain nodes like Bitcoin Core.

Key fields include:

**Usage Example:**

const rawTx: RawTx = {
  txid: "abc123",
  hash: "def456",
  version: 2,
  size: 225,
  vsize: 144,
  weight: 576,
  locktime: 0,
  vin: [{ txid: "prevtxid", vout: 0, scriptSig: { asm: "...", hex: "..." } }],
  vout: [{ value: 0.001, n: 0, scriptPubKey: { asm: "...", hex: "...", reqSigs: 1, type: "pubkeyhash", addresses: ["1A1zP1..."] } }],
  hex: "0200000001...",
  blockhash: "000000000000...",
  confirmations: 100,
  time: 1609459200,
  blocktime: 1609459200
};

Utxo

**Purpose:** Represents an Unspent Transaction Output (UTXO) that can be spent in new transactions.

**Properties:**

Property

Type

Description

`txid`

`string`

Transaction ID where this output exists.

`vout`

`number`

Output index in the transaction.

`value`

`string`

Value of the UTXO in satoshis or coins.

`height`

`number` (opt)

Block height where this UTXO was confirmed.

`confirmations`

`number`

Number of confirmations.

`address`

`string` (opt)

Address controlling this UTXO.

`path`

`string` (opt)

Derivation path if from a wallet.

`locktime`

`number` (opt)

Locktime for the output if any.

`coinbase`

`boolean` (opt)

Indicates if this is a coinbase UTXO.

**Usage Example:**

const utxo: Utxo = {
  txid: "abc123",
  vout: 0,
  value: "0.001",
  confirmations: 15,
  address: "1A1zP1...",
  coinbase: false
};

Address

**Purpose:** Represents an individual address associated with an extended public key, with its balance and public key.

**Properties:**

Property

Type

Description

`balance`

`string`

Balance of the address.

`pubkey`

`string`

Public key associated with address.


Account

**Inheritance:** Extends `BaseAccount` (imported externally)

**Purpose:** Represents an account or extended public key with associated addresses and address indices for receiving and change addresses.

**Properties:**

Property

Type

Description

`addresses`

`Array

` (opt)

List of addresses belonging to the extended public key.

`nextReceiveAddressIndex`

`number` (opt)

Next unused index for receive addresses (change = 0).

`nextChangeAddressIndex`

`number` (opt)

Next unused index for change addresses (change = 1).


NetworkFee

**Purpose:** Represents fee information to get a transaction confirmed within a certain number of blocks.

**Properties:**

Property

Type

Description

`blocksUntilConfirmation`

`number`

Estimated blocks until confirmation.

`satsPerKiloByte`

`number`

Fee rate in satoshis per kilobyte.


NetworkFees

**Purpose:** Holds recommended network fees categorized by speed preference.

**Properties:**

Property

Type

Description

`fast`

`NetworkFee` (opt)

Recommended fee for fast confirmation.

`average`

`NetworkFee` (opt)

Recommended fee for average confirmation.

`slow`

`NetworkFee` (opt)

Recommended fee for slow confirmation.


Interface API

**Purpose:** Defines an asynchronous interface for coin-specific blockchain operations such as fetching UTXOs, transaction details, and network fee recommendations.


Methods


getUtxos(pubkey: string): Promise<Array<Utxo>>

Fetches all unspent transaction outputs for a given account address or extended public key.

api.getUtxos("xpub6CUGRU...").then(utxos => {
  console.log(utxos);
});

getTransaction(txid: string): Promise<Tx>

Retrieves detailed transaction information for a given transaction ID.

api.getTransaction("abc123").then(tx => {
  console.log(tx.vin, tx.vout);
});

getRawTransaction(txid: string): Promise<RawTx>

Fetches raw transaction data directly from the blockchain node.

api.getRawTransaction("abc123").then(rawTx => {
  console.log(rawTx.hex);
});

getNetworkFees(): Promise<NetworkFees>

Retrieves the current recommended network fees for different confirmation speeds.

api.getNetworkFees().then(fees => {
  console.log(fees.fast?.satsPerKiloByte);
});

Important Implementation Details


Interaction with Other System Components


Mermaid Diagram: Class/Interface Structure

classDiagram
    class Vin {
        +txid?: string
        +vout?: string
        +sequence?: number
        +coinbase?: string
        +scriptSig?: { hex?: string }
        +addresses?: string[]
        +value?: string
    }

    class Vout {
        +value: string
        +n: number
        +opReturn?: string
        +scriptPubKey: { hex?: string }
        +addresses?: string[]
    }

    class Tx {
        +vin: Vin[]
        +vout: Vout[]
        +confirmations: number
        +value: string
        +fee: string
        +hex: string
    }

    class RawTx {
        +txid: string
        +hash: string
        +version: number
        +size: number
        +vsize: number
        +weight: number
        +locktime: number
        +vin: array
        +vout: array
        +hex: string
        +blockhash: string
        +confirmations: number
        +time: number
        +blocktime: number
    }

    class Utxo {
        +txid: string
        +vout: number
        +value: string
        +height?: number
        +confirmations: number
        +address?: string
        +path?: string
        +locktime?: number
        +coinbase?: boolean
    }

    class Address {
        +balance: string
        +pubkey: string
    }

    class Account {
        +addresses?: Address[]
        +nextReceiveAddressIndex?: number
        +nextChangeAddressIndex?: number
    }

    class NetworkFee {
        +blocksUntilConfirmation: number
        +satsPerKiloByte: number
    }

    class NetworkFees {
        +fast?: NetworkFee
        +average?: NetworkFee
        +slow?: NetworkFee
    }

    class API {
        +getUtxos(pubkey: string): Promise<Array<Utxo>>
        +getTransaction(txid: string): Promise<Tx>
        +getRawTransaction(txid: string): Promise<RawTx>
        +getNetworkFees(): Promise<NetworkFees>
    }

    Tx ..|> BaseTx
    Account ..|> BaseAccount
    TxHistory --> Tx

Summary

[models.ts](/projects/291/68901) is a core schema definition file for blockchain transaction data and related entities within the system. It defines precise TypeScript interfaces for transaction inputs and outputs, full transactions, raw blockchain data, UTXOs, addresses, accounts, and network fees. The API interface defines asynchronous methods to retrieve blockchain data, enabling modular integration with different blockchain backends.

This file promotes type safety and consistent data handling across blockchain-related components, forming a foundational layer that interacts with wallet management, transaction processing, and network communication modules.