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-9/

Bitcoin Transaction Builder

Build a utility module that can create, serialize, parse, and analyze Bitcoin transactions. The module should handle transaction input/output management and provide serialization capabilities for storing and transmitting transaction data.

Requirements

Your implementation should provide functionality to:

  1. Create new transactions with configurable version and locktime parameters
  2. Add transaction inputs with the following fields:
    • Transaction hash (previous output reference)
    • Output index (which output from the previous transaction)
    • Script signature (unlocking script)
    • Sequence number
  3. Add transaction outputs with:
    • Value (amount in satoshis)
    • Script pubkey (locking script)
  4. Serialize transactions to both hexadecimal string and buffer formats
  5. Parse transactions from hexadecimal string or buffer back into transaction objects
  6. Calculate transaction identifiers (transaction hash and ID)

Implementation Details

  • Create a module src/transaction-utils.ts that exports the necessary functions
  • Support both transaction creation from scratch and parsing from serialized formats
  • Handle proper byte ordering for transaction fields
  • Ensure serialization produces valid Bitcoin transaction format

Test Cases { @test }

Create test file src/transaction-utils.test.ts with the following test cases:

Test 1: Create and serialize a simple transaction { @test }

Create a transaction with:

  • Version: 2
  • One input:
    • Previous tx hash: fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f
    • Output index: 0
    • Script signature: empty buffer
    • Sequence: 0xffffffee
  • One output:
    • Value: 150000 satoshis
    • Script pubkey: hex 76a914389ffce9cd9ae88dcc0631e88a821ffdbe9bfe2688ac
  • Locktime: 0

Verify that:

  1. The transaction can be serialized to hex format
  2. The hex output is a valid non-empty string
  3. The transaction has exactly 1 input and 1 output

Test 2: Parse transaction from hex { @test }

Parse the following transaction hex:

01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000

Verify that:

  1. The parsed transaction has version 1
  2. The transaction has 1 input and 1 output
  3. The output value is 5000000000 satoshis
  4. The transaction locktime is 0

Test 3: Transaction ID calculation { @test }

Create a simple transaction with:

  • Version: 1
  • One input with all zeros (coinbase-like for testing)
  • One output with value 5000000000 and a simple pubkey script
  • Locktime: 0

Verify that:

  1. A transaction ID can be calculated
  2. The transaction ID is a 64-character hex string
  3. The transaction hash is also available as a buffer

Test 4: Round-trip serialization { @test }

Create a transaction with multiple inputs and outputs:

  • Version: 2
  • Two inputs (any valid configuration)
  • Two outputs with different values
  • Locktime: 500000

Verify that:

  1. The transaction can be serialized to hex
  2. The hex can be parsed back to a transaction object
  3. Re-serializing produces identical hex output
  4. All transaction properties match after round-trip (version, input count, output count, locktime)

Dependencies { .dependencies }

bitcoinjs-lib { .dependency }

Bitcoin transaction creation and manipulation library for JavaScript.

Install with Tessl CLI

npx tessl i tessl/npm-bitcoinjs-lib

tile.json