CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-bitcoinjs-lib

Client-side Bitcoin JavaScript library for node.js and browsers with comprehensive Bitcoin protocol support

Overall
score

99%

Overview
Eval results
Files

task.mdevals/scenario-8/

Transaction Weight Calculator

Build a utility that analyzes Bitcoin transactions to calculate their weight and virtual size, distinguishing between legacy and SegWit transactions based on their witness data.

Requirements

Your solution should handle both legacy transactions (without witness data) and SegWit transactions (with witness data). The system needs to:

  1. Parse transaction data from hexadecimal strings
  2. Identify whether a transaction uses SegWit by detecting the presence of witness data
  3. Calculate the transaction weight in weight units
  4. Calculate the virtual size (vsize) in bytes
  5. Extract the transaction ID
  6. Return all metrics in a structured format

Implementation

@generates

Create a module that exports a function to analyze transactions.

API

/**
 * Analyzes a Bitcoin transaction and returns weight metrics.
 *
 * @param {string} txHex - The transaction in hexadecimal format
 * @returns {Object} Analysis results containing:
 *   - isSegWit: boolean indicating if transaction uses SegWit
 *   - weight: number representing transaction weight in weight units
 *   - virtualSize: number representing vsize in bytes
 *   - txid: string transaction ID
 * @throws {Error} If the transaction hex is invalid
 */
function analyzeTransaction(txHex) {
  // IMPLEMENTATION HERE
}

module.exports = { analyzeTransaction };

Test Cases

Basic SegWit transaction analysis

  • Given a valid SegWit transaction hex, the function correctly identifies it as SegWit (isSegWit: true), calculates its weight, virtual size, and returns the transaction ID @test

Legacy transaction analysis

  • Given a valid legacy transaction hex (without witness data), the function correctly identifies it as non-SegWit (isSegWit: false), calculates weight as size × 4, and returns appropriate metrics @test

Invalid transaction handling

  • Given an invalid or malformed transaction hex string, the function throws an appropriate error @test

Dependencies { .dependencies }

bitcoinjs-lib { .dependency }

Provides Bitcoin transaction parsing and witness data handling capabilities.

Install with Tessl CLI

npx tessl i tessl/npm-bitcoinjs-lib

tile.json