or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

address.mdblocks.mdcrypto.mdindex.mdnetworks.mdpayments.mdpsbt.mdscripts.mdtransactions.mdutilities.md
tile.json

networks.mddocs/

Network Configurations

Predefined network configurations for Bitcoin mainnet, testnet, and regtest with all necessary parameters for address generation, transaction creation, and network-specific operations.

Network Interface

interface Network {
  /** Message prefix for signed messages */
  messagePrefix: string;
  /** Bech32 prefix for SegWit addresses */
  bech32: string;
  /** BIP32 extended key version bytes */
  bip32: {
    /** Public extended key version */
    public: number;
    /** Private extended key version */
    private: number;
  };
  /** P2PKH address version byte */
  pubKeyHash: number;
  /** P2SH address version byte */
  scriptHash: number;
  /** WIF private key version byte */
  wif: number;
}

Capabilities

Bitcoin Mainnet

Bitcoin mainnet network configuration for production use.

/**
 * Bitcoin mainnet network configuration
 */
const bitcoin: Network;

Network Details:

const bitcoin = {
  messagePrefix: '\x18Bitcoin Signed Message:\n',
  bech32: 'bc',
  bip32: {
    public: 0x0488b21e,   // xpub
    private: 0x0488ade4   // xprv
  },
  pubKeyHash: 0x00,       // P2PKH addresses start with '1'
  scriptHash: 0x05,       // P2SH addresses start with '3'
  wif: 0x80              // WIF private keys start with '5', 'K', or 'L'
};

Usage Examples:

import { networks, payments, address } from 'bitcoinjs-lib';

// Create mainnet P2PKH address
const pubkey = Buffer.from('03a34b...', 'hex');
const p2pkh = payments.p2pkh({ pubkey, network: networks.bitcoin });
console.log('Mainnet P2PKH:', p2pkh.address); // Starts with '1'

// Create mainnet P2SH address
const p2sh = payments.p2sh({ 
  redeem: payments.p2wpkh({ pubkey, network: networks.bitcoin }),
  network: networks.bitcoin 
});
console.log('Mainnet P2SH:', p2sh.address); // Starts with '3'

// Create mainnet Bech32 address
const p2wpkh = payments.p2wpkh({ pubkey, network: networks.bitcoin });
console.log('Mainnet Bech32:', p2wpkh.address); // Starts with 'bc1'

// Convert address to script
const script = address.toOutputScript(p2pkh.address!, networks.bitcoin);
console.log('Output script:', script.toString('hex'));

Bitcoin Testnet

Bitcoin testnet network configuration for testing and development.

/**
 * Bitcoin testnet network configuration
 */
const testnet: Network;

Network Details:

const testnet = {
  messagePrefix: '\x18Bitcoin Signed Message:\n',
  bech32: 'tb',
  bip32: {
    public: 0x043587cf,   // tpub
    private: 0x04358394   // tprv
  },
  pubKeyHash: 0x6f,       // P2PKH addresses start with 'm' or 'n'
  scriptHash: 0xc4,       // P2SH addresses start with '2'
  wif: 0xef              // WIF private keys start with '9' or 'c'
};

Usage Examples:

import { networks, payments } from 'bitcoinjs-lib';

// Create testnet addresses
const pubkey = Buffer.from('03a34b...', 'hex');

const testnetP2PKH = payments.p2pkh({ pubkey, network: networks.testnet });
console.log('Testnet P2PKH:', testnetP2PKH.address); // Starts with 'm' or 'n'

const testnetP2SH = payments.p2sh({ 
  redeem: payments.p2wpkh({ pubkey, network: networks.testnet }),
  network: networks.testnet 
});
console.log('Testnet P2SH:', testnetP2SH.address); // Starts with '2'

const testnetP2WPKH = payments.p2wpkh({ pubkey, network: networks.testnet });
console.log('Testnet Bech32:', testnetP2WPKH.address); // Starts with 'tb1'

// Message signing with testnet
const messagePrefix = networks.testnet.messagePrefix;
console.log('Testnet message prefix:', messagePrefix);

Bitcoin Regtest

Bitcoin regtest network configuration for local development and testing.

/**
 * Bitcoin regtest network configuration
 */
const regtest: Network;

Network Details:

const regtest = {
  messagePrefix: '\x18Bitcoin Signed Message:\n',
  bech32: 'bcrt',
  bip32: {
    public: 0x043587cf,   // tpub (same as testnet)
    private: 0x04358394   // tprv (same as testnet)
  },
  pubKeyHash: 0x6f,       // P2PKH addresses start with 'm' or 'n'
  scriptHash: 0xc4,       // P2SH addresses start with '2'
  wif: 0xef              // WIF private keys start with '9' or 'c'
};

Usage Examples:

import { networks, payments, Psbt } from 'bitcoinjs-lib';

// Create regtest addresses (same format as testnet)
const pubkey = Buffer.from('03a34b...', 'hex');

const regtestP2PKH = payments.p2pkh({ pubkey, network: networks.regtest });
console.log('Regtest P2PKH:', regtestP2PKH.address); // Starts with 'm' or 'n'

const regtestBech32 = payments.p2wpkh({ pubkey, network: networks.regtest });
console.log('Regtest Bech32:', regtestBech32.address); // Starts with 'bcrt1'

// Use regtest for local development
const psbt = new Psbt({ network: networks.regtest });
psbt.addInput({
  hash: 'abc123...',
  index: 0,
  witnessUtxo: {
    script: regtestBech32.output!,
    value: 100000
  }
});

Advanced Usage

Network Detection

Detect network from address format:

import { networks, address } from 'bitcoinjs-lib';

function detectNetwork(addr: string): Network | null {
  try {
    // Try mainnet first
    address.toOutputScript(addr, networks.bitcoin);
    return networks.bitcoin;
  } catch (e1) {
    try {
      // Try testnet
      address.toOutputScript(addr, networks.testnet);
      return networks.testnet;
    } catch (e2) {
      try {
        // Try regtest
        address.toOutputScript(addr, networks.regtest);
        return networks.regtest;
      } catch (e3) {
        return null;
      }
    }
  }
}

// Detect network from various address formats
console.log(detectNetwork('1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa')); // bitcoin
console.log(detectNetwork('tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx')); // testnet
console.log(detectNetwork('bcrt1qw508d6qejxtdg4y5r3zarvary0c5xw7kyuewjs')); // regtest

Address Format Validation

Validate address formats for specific networks:

import { networks, address } from 'bitcoinjs-lib';

function isValidAddress(addr: string, network: Network): boolean {
  try {
    address.toOutputScript(addr, network);
    return true;
  } catch {
    return false;
  }
}

// Validate addresses
const mainnetAddr = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa';
const testnetAddr = 'tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx';

console.log('Mainnet addr on mainnet:', isValidAddress(mainnetAddr, networks.bitcoin)); // true
console.log('Mainnet addr on testnet:', isValidAddress(mainnetAddr, networks.testnet)); // false
console.log('Testnet addr on testnet:', isValidAddress(testnetAddr, networks.testnet)); // true
console.log('Testnet addr on mainnet:', isValidAddress(testnetAddr, networks.bitcoin)); // false

Network-Specific Transaction Building

Build transactions for specific networks:

import { networks, payments, Psbt, Transaction } from 'bitcoinjs-lib';

function buildTransaction(network: Network) {
  const psbt = new Psbt({ network });
  
  // Add network-appropriate input
  psbt.addInput({
    hash: 'abc123...',
    index: 0,
    witnessUtxo: {
      script: Buffer.from('0014...', 'hex'),
      value: 100000
    }
  });
  
  // Create network-appropriate output address
  const pubkey = Buffer.from('03a34b...', 'hex');
  const payment = payments.p2wpkh({ pubkey, network });
  
  psbt.addOutput({
    address: payment.address!,
    value: 90000 // 10000 sat fee
  });
  
  return psbt;
}

// Build transactions for different networks
const mainnetPsbt = buildTransaction(networks.bitcoin);
const testnetPsbt = buildTransaction(networks.testnet);
const regtestPsbt = buildTransaction(networks.regtest);

console.log('Mainnet PSBT network:', mainnetPsbt.txOutputs[0].address?.startsWith('bc1'));
console.log('Testnet PSBT network:', testnetPsbt.txOutputs[0].address?.startsWith('tb1'));
console.log('Regtest PSBT network:', regtestPsbt.txOutputs[0].address?.startsWith('bcrt1'));

Custom Network Configuration

Create custom network configurations for alternative chains:

interface Network {
  messagePrefix: string;
  bech32: string;
  bip32: { public: number; private: number };
  pubKeyHash: number;
  scriptHash: number;
  wif: number;
}

// Example custom network (not for production use)
const customNetwork: Network = {
  messagePrefix: '\x18CustomCoin Signed Message:\n',
  bech32: 'cc',
  bip32: {
    public: 0x0488b21e,   // Same as Bitcoin
    private: 0x0488ade4
  },
  pubKeyHash: 0x1e,       // Different version byte
  scriptHash: 0x22,
  wif: 0x9e
};

// Use custom network
const pubkey = Buffer.from('03a34b...', 'hex');
const customPayment = payments.p2pkh({ pubkey, network: customNetwork });
console.log('Custom network address:', customPayment.address);

Network Constants Reference

Quick reference for network-specific values:

import { networks } from 'bitcoinjs-lib';

function printNetworkInfo(network: Network, name: string) {
  console.log(`=== ${name} Network ===`);
  console.log('Message prefix:', JSON.stringify(network.messagePrefix));
  console.log('Bech32 prefix:', network.bech32);
  console.log('BIP32 public:', '0x' + network.bip32.public.toString(16));
  console.log('BIP32 private:', '0x' + network.bip32.private.toString(16));
  console.log('P2PKH version:', '0x' + network.pubKeyHash.toString(16));
  console.log('P2SH version:', '0x' + network.scriptHash.toString(16));
  console.log('WIF version:', '0x' + network.wif.toString(16));
  console.log('');
}

printNetworkInfo(networks.bitcoin, 'Bitcoin Mainnet');
printNetworkInfo(networks.testnet, 'Bitcoin Testnet');
printNetworkInfo(networks.regtest, 'Bitcoin Regtest');

Types

interface Network {
  messagePrefix: string;
  bech32: string;
  bip32: {
    public: number;
    private: number;
  };
  pubKeyHash: number;
  scriptHash: number;
  wif: number;
}

interface Bip32 {
  public: number;
  private: number;
}