Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@btc-stamps/tx-builder

btc-stamps444MIT0.1.7TypeScript support: included

Transaction builder for Bitcoin Stamps and SRC-20 tokens with advanced UTXO selection

bitcoin, transaction, builder, utxo, stamps, src-20, blockchain, cryptocurrency, bitcoin-stamps, counterparty, ordinals, metaprotocol, transaction-builder, utxo-selection

readme

@btc-stamps/tx-builder 🚀

npm version npm downloads npm package size JSR License: MIT Node.js CI codecov Maintenance Dependencies Status PRs Welcome

The Bitcoin transaction builder for Bitcoin Stamps and SRC-20 metaprotocols

Build Bitcoin transactions with native support for Bitcoin Stamps, SRC-20 tokens, Ordinals protection, and extensible metaprotocol support.

InstallationQuick StartExamplesDocumentationAPI Reference


🎯 Why tx-builder?

@btc-stamps/tx-builder is the only transaction builder with first-class support for Bitcoin Stamp metaprotocols. This can be extended to support other metaprotocols like Runes or BRC-20.

✨ Key Features

  • 🖼️ Bitcoin Stamps: Complete Bitcoin Stamp metaprotocol support
  • 🪙 SRC-20 Tokens: Full lifecycle support (deploy, mint, transfer)
  • 🛡️ UTXO Protection: Automatic protection of Ordinals, Inscriptions, Stamps & Counterparty assets
  • ⚡ Smart Selection: 6 UTXO selection algorithms with optimization
  • 🔌 Zero Config: Works out-of-the-box with reliable defaults
  • 🌳 Tree-Shakeable: Optimized for modern bundlers with sideEffects: false
  • 📦 Lightweight: Minimal dependencies, maximum performance
  • 🧪 Battle-Tested: Comprehensive test suite with 430+ tests
  • 🔒 Type-Safe: Full TypeScript support with detailed types

📦 Installation

Node.js / TypeScript

npm install @btc-stamps/tx-builder
# or
yarn add @btc-stamps/tx-builder
# or
pnpm add @btc-stamps/tx-builder

Deno

import { TransactionBuilder } from 'npm:@btc-stamps/tx-builder@^0.1.6';

Requirements

  • Node.js >= 18.0.0 or Bun >= 1.0.0
  • TypeScript >= 5.0.0 (for TypeScript users)
  • Deno: Partial support via npm compatibility (see guide)

🚀 Quick Start

Bitcoin Stamps with UTXO Protection

import { BitcoinStampBuilder, SelectorFactory } from '@btc-stamps/tx-builder';

// Zero-config setup with automatic UTXO protection
const selectorFactory = SelectorFactory.getInstance();
const builder = new BitcoinStampBuilder(network, selectorFactory);

// Build stamp transaction - automatically protects:
// ✅ Ordinals (sats with inscriptions or runes)
// ✅ Bitcoin Stamps (all types)
// ✅ Counterparty assets (XCP, PEPECASH, etc.)
// ✅ SRC-20 tokens
const result = await builder.buildStampTransaction(utxos, {
  stampData: {
    imageData: imageBuffer,
    filename: 'my-stamp.png',
  },
  fromAddress: 'bc1q...',
  feeRate: 20,
  algorithm: 'protection-aware', // Optional: explicitly use protection-aware selection
});

SRC-20 Tokens

import { SRC20Encoder, SRC20TokenBuilder } from '@btc-stamps/tx-builder';

const encoder = new SRC20Encoder();

// Deploy new token
const deployData = await encoder.encode({
  p: 'SRC-20',
  op: 'DEPLOY',
  tick: 'KEVIN',
  max: '21000000',
  lim: '1000',
});

// Build transaction
const psbt = await new SRC20TokenBuilder().buildSRC20Transaction({
  encodedData: deployData,
  utxos: selectedUTXOs,
  changeAddress: 'bc1q...',
  feeRate: 15,
});

🛡️ Advanced UTXO Protection

Built-in protection for Ordinals, Inscriptions, Stamps, Counterparty assets, and SRC-20 tokens is automatic in all builders.

// For custom protection configuration:
const selector = selectorFactory.createSelector('protection-aware', {
  protectionConfig: {
    enableOrdinalsDetection: true, // Detect inscriptions and runes
    enableCounterpartyDetection: true, // Detect UTXO attached assets
    enableStampsDetection: true, // Detect UTXO attached stamps
  },
});

// Use with any builder
builder.setSelector(selector);

⚡ UTXO Selection Algorithms

Optimize transaction fees with multiple selection strategies:

import {
  AccumulativeSelector, // Fast selection
  BlackjackSelector, // Target exact amounts
  BranchAndBoundSelector, // Optimal selection
  WasteOptimizedSelector, // Long-term optimization
} from '@btc-stamps/tx-builder';

🌐 Network Support

Zero-configuration with reliable defaults:

// Works immediately - no setup required
const provider = new ElectrumXProvider(); // Uses blockstream.info, fortress.qtornado.com, etc.

Networks: Mainnet, Testnet, Regtest with automatic server selection


📚 Learn More


🧪 Testing

npm test              # Run all tests
npm run test:unit     # Unit tests only
npm run test:coverage # Coverage report

🤝 Contributing

Contributions welcome! See Contributing Guide for details.

Development Setup

# Clone the repository
git clone https://github.com/btc-stamps/tx-builder.git
cd tx-builder

# Install dependencies
npm install

# Run tests
npm test

# Build the package
npm run build

📄 License

MIT License - see LICENSE file.

🙏 Acknowledgments

Built on top of excellent libraries:

💬 Support


GitHub: btc-stamps/tx-builderNPM: @btc-stamps/tx-builderJSR: @btc-stamps/tx-builderTelegram: @BitcoinStamps

Built with ❤️ by the Stampchain team

Star on GitHub

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

0.1.3 - 2025-08-17

Added

  • JSR (Deno) registry support with full compatibility
  • Codecov integration for test coverage reporting
  • JSR badge in README
  • Cross-platform timer utilities for Deno/Node compatibility

Fixed

  • Timer compatibility issues between Deno and Node.js environments
  • TypeScript override modifiers for JSR compatibility
  • GitHub Pages documentation links now properly point to GitHub for TypeScript source files
  • README links now use absolute GitHub URLs for JSR/npm compatibility

Changed

  • Documentation links updated to work across all platforms (GitHub, npm, JSR)
  • Improved Codecov configuration with proper ignore patterns
  • Documentation structure cleaned up with outdated docs archived

0.1.2 - 2025-08-17

Fixed

  • Import extensions changed from .js to .ts for Deno compatibility
  • Timer utility functions for cross-platform support
  • TypeScript type casting for timer returns

Changed

  • Version sync between package.json and deno.json

0.1.1 - 2025-08-17

Fixed

  • Corrected import extensions from .js to .ts for module resolution
  • Updated all relative imports to include .ts extension
  • Fixed version synchronization between package.json and deno.json

Changed

  • Squashed commits for cleaner release history
  • Initial GitHub Pages deployment
  • Examples moved under docs/ directory

0.1.0 - 2025-08-16

Added

  • Initial public release of @btc-stamps/tx-builder
  • Core transaction building functionality with PSBT support
  • Multiple UTXO selection algorithms:
    • Branch and Bound (optimal selection)
    • Knapsack (combination-based selection)
    • Single Random Draw (SRD)
    • Consolidation (UTXO consolidation)
    • Output Group (privacy-preserving)
    • Tax-optimized (FIFO/LIFO strategies)
    • Protection-aware (ordinals/inscriptions/stamps protection)
  • Provider abstractions:
    • ElectrumX provider with real-time fee estimation
    • Mempool.space provider
    • Blockstream provider
    • Fallback provider with automatic failover
  • Data encoders:
    • Bitcoin Stamps encoder (full protocol support)
    • SRC-20 encoder (deploy, mint, transfer operations)
    • OP_RETURN encoder
    • P2WSH encoder for witness scripts
    • Counterparty encoding support
  • Asset validation service:
    • CPID validation and generation
    • Counterparty API integration
    • Collision-free asset name generation
  • Enhanced error handling:
    • Structured selection results
    • Detailed failure reasons
    • Recovery suggestions
  • Comprehensive TypeScript types and interfaces
  • Support for both Node.js and Deno runtimes
  • Extensive test coverage (448+ tests)
  • Documentation and examples

Features

  • UTXO Protection: Built-in protection for valuable ordinals, inscriptions, and stamps
  • Fee Optimization: Multiple fee estimation providers with fallback support
  • SRC-20 Support: Full support for SRC-20 token operations
  • Asset Validation: Automatic CPID validation and generation
  • Performance: Parallel UTXO selection with algorithm comparison
  • Type Safety: Full TypeScript support with strict typing
  • Cross-Platform: Works in Node.js, Deno, and browsers

Architecture

  • Clean architecture with dependency injection
  • Separation of concerns between core logic, providers, selectors, and encoders
  • Extensible design for custom implementations
  • Performance monitoring and optimization
  • Robust error handling and recovery

Documentation

  • Comprehensive README with quick start guide
  • API documentation with TypeDoc
  • Multiple usage examples for all features
  • Architecture documentation in docs/
  • Migration guides and best practices