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

Taproot Script Hash Calculator

Build a utility that computes tagged hash commitments for Taproot script trees used in Bitcoin Taproot transactions.

Overview

Your task is to create functions that compute the cryptographic hash commitments used in Taproot script paths. These tagged hashes are a core part of how Bitcoin's Taproot upgrade works, allowing multiple spending conditions to be committed to in a Merkle tree structure.

Requirements

Leaf Hash Computation

Create a function that:

  • Takes a Bitcoin script (Buffer) and a leaf version byte
  • Computes the TapLeaf tagged hash for this script
  • Returns the hash as a Buffer

The leaf version should default to 0xc0 (standard Tapscript leaf version).

Branch Hash Computation

Create a function that:

  • Takes two child node hashes (Buffers)
  • Sorts them lexicographically before hashing (smaller hash first)
  • Computes the TapBranch tagged hash
  • Returns the combined hash as a Buffer

Merkle Root Calculation

Create a function that:

  • Takes an array of Bitcoin scripts
  • Builds a Merkle tree using the leaf and branch hash functions
  • Returns the root hash of the tree

For an odd number of leaves, the unpaired leaf should be promoted to the next level.

Test Cases

  • Computing a leaf hash for a simple script Buffer returns a 32-byte hash. @test
  • Computing a branch hash from two different 32-byte hashes returns a 32-byte hash. @test
  • Branch hash computation with hashes in different orders produces the same result (due to sorting). @test
  • Computing the Merkle root for a single script returns the leaf hash. @test
  • Computing the Merkle root for two scripts produces the correct branch hash. @test

Implementation

@generates

API

/**
 * Computes the TapLeaf tagged hash for a script
 *
 * @param script - The script Buffer to hash
 * @param leafVersion - The leaf version byte (defaults to 0xc0)
 * @returns The TapLeaf hash as a 32-byte Buffer
 */
export function computeLeafHash(script: Buffer, leafVersion?: number): Buffer;

/**
 * Computes the TapBranch tagged hash for two child hashes
 *
 * @param hash1 - First child hash
 * @param hash2 - Second child hash
 * @returns The TapBranch hash as a 32-byte Buffer
 */
export function computeBranchHash(hash1: Buffer, hash2: Buffer): Buffer;

/**
 * Computes the Merkle root for an array of scripts
 *
 * @param scripts - Array of script Buffers
 * @param leafVersion - The leaf version byte (defaults to 0xc0)
 * @returns The Merkle root hash as a 32-byte Buffer
 */
export function computeMerkleRoot(scripts: Buffer[], leafVersion?: number): Buffer;

Dependencies { .dependencies }

bitcoinjs-lib { .dependency }

Provides Bitcoin transaction primitives and Taproot tagged hashing functions.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-bitcoinjs-lib

tile.json