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

Bitcoin Address Validator and Generator

Overview

Build a Bitcoin address utility that can generate P2PKH (Pay-to-Public-Key-Hash) addresses from public keys and validate whether given addresses are valid P2PKH addresses. The utility should support multiple Bitcoin networks (mainnet, testnet, and regtest).

Requirements

Address Generation

Create a function generateP2PKHAddress(publicKeyBuffer, network) that:

  • Accepts a public key as a Buffer
  • Accepts a network identifier string ('mainnet', 'testnet', or 'regtest')
  • Returns the corresponding P2PKH address string for the specified network
  • Throws an error if the public key is invalid

Address Validation

Create a function isValidP2PKHAddress(address, network) that:

  • Accepts an address string
  • Accepts a network identifier string ('mainnet', 'testnet', or 'regtest')
  • Returns true if the address is a valid P2PKH address for that network
  • Returns false otherwise

Output Script Extraction

Create a function getP2PKHOutputScript(address) that:

  • Accepts a P2PKH address string
  • Returns the corresponding output script as a Buffer
  • Throws an error if the address is not a valid P2PKH address

Test Cases { .test }

Create a test file address-utils.test.js with the following test cases:

Test 1: Generate mainnet P2PKH address @test

const publicKey = Buffer.from('03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd', 'hex');
const address = generateP2PKHAddress(publicKey, 'mainnet');
// Expected address should start with '1' (mainnet P2PKH prefix)
// Verify it's a valid mainnet address

Test 2: Generate testnet P2PKH address @test

const publicKey = Buffer.from('02d0de0aaeaefad02b8bdc8a01a1b8b11c696bd3d66a2c5f10780d95b7df42645c', 'hex');
const address = generateP2PKHAddress(publicKey, 'testnet');
// Expected address should start with 'm' or 'n' (testnet P2PKH prefix)
// Verify it's a valid testnet address

Test 3: Validate P2PKH addresses @test

// Mainnet P2PKH address
assert(isValidP2PKHAddress('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', 'mainnet') === true);

// Invalid address (Bech32, not P2PKH)
assert(isValidP2PKHAddress('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4', 'mainnet') === false);

// Wrong network
assert(isValidP2PKHAddress('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa', 'testnet') === false);

Test 4: Extract output script @test

const address = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa';
const script = getP2PKHOutputScript(address);
// Verify the script is a Buffer
// Verify the script length is appropriate for P2PKH (25 bytes)

Implementation Notes

  • Use appropriate error handling for invalid inputs
  • Ensure network configuration is correctly applied
  • The public key should be in compressed or uncompressed format
  • P2PKH addresses on mainnet start with '1'
  • P2PKH addresses on testnet start with 'm' or 'n'

Dependencies { .dependencies }

bitcoinjs-lib { .dependency }

Provides Bitcoin protocol support including address generation, script creation, and network configuration.

Install with Tessl CLI

npx tessl i tessl/npm-bitcoinjs-lib

tile.json