did-btcr2-js

@did-btcr2/bitcoin

Sans-I/O Bitcoin client for did-btcr2-js. Speaks Esplora REST (mempool.space, blockstream.info, or any compatible indexer) and Bitcoin Core JSON-RPC.

Part of the did-btcr2-js monorepo.

Summary

The DID method needs to read transactions from beacon addresses, fetch block metadata, and broadcast signed updates. This package provides those operations as a pluggable, browser-compatible client.

Install

npm install @did-btcr2/bitcoin

Or with pnpm:

pnpm add @did-btcr2/bitcoin

Requires Node >= 22. Ships both ESM and CJS; pick whichever your bundler needs.

Key Exports

Concern Entry point
Per-network connection BitcoinConnection, BitcoinConnection.forNetwork(name, overrides?)
REST client (Esplora) BitcoinRestClient, sub-clients BitcoinAddress, BitcoinBlock, BitcoinTransaction
RPC client (Bitcoin Core) BitcoinCoreRpcClient, JsonRpcTransport, RpcMethodMap, TypedRpcMethod
Sans-I/O protocol layer EsploraProtocol, JsonRpcProtocol, HttpRequest, HttpExecutor, defaultHttpExecutor
Network params getNetwork(name), BTCNetwork, NetworkName
Errors BitcoinRpcError, BitcoinRestError, RpcErrorType
Defaults DEFAULT_BITCOIN_NETWORK_CONFIG
Bitcoin constants INITIAL_BLOCK_REWARD, HALVING_INTERVAL, COINBASE_MATURITY_DELAY, DEFAULT_BLOCK_CONFIRMATIONS, GENESIS_TX_ID

Quick Start

import { BitcoinConnection } from '@did-btcr2/bitcoin';

// Public network: REST only, defaults to mutinynet.com/api.
const btc = BitcoinConnection.forNetwork('mutinynet');

const height = await btc.rest.block.count();
const utxos  = await btc.rest.address.getUtxos('tb1q...');
const txHex  = await btc.rest.transaction.getHex('abc123...');

// Regtest with explicit RPC credentials (host/port default; username/password do not).
const regtest = BitcoinConnection.forNetwork('regtest', {
  rpc: { username: 'polaruser', password: 'polarpass' },
});
await regtest.rpc!.generateToAddress(6, await regtest.rpc!.getNewAddress('bech32'));

Injecting a custom executor

For tests, sandboxes, or rate-limited fetchers, pass your own HttpExecutor:

const btc = BitcoinConnection.forNetwork('mutinynet', {
  executor: (req) => fetch(req.url, {
    method  : req.method,
    headers : req.headers,
    body    : req.body,
    signal  : AbortSignal.timeout(5_000),
  }),
});

Architecture Principles

Build & Test

# From packages/bitcoin/
pnpm build              # Compile ESM + CJS + type declarations
pnpm build:tests        # Compile tests to tests/compiled/
pnpm test               # Run the test suite with coverage
pnpm lint               # ESLint (zero warnings tolerated)

Documentation

License

MPL-2.0