Client-side Bitcoin JavaScript library for node.js and browsers with comprehensive Bitcoin protocol support
npx @tessl/cli install tessl/npm-bitcoinjs-lib@6.1.0bitcoinjs-lib is a comprehensive client-side Bitcoin JavaScript library for node.js and browsers. It provides a complete toolkit for Bitcoin-related operations including transaction creation and manipulation, address generation and management, cryptographic operations, script creation and execution, PSBT handling, and support for various Bitcoin Improvement Proposals (BIPs) including SegWit and Taproot.
npm install bitcoinjs-libimport * as bitcoin from 'bitcoinjs-lib';
// Or specific imports
import {
Transaction,
Psbt,
Block,
address,
payments,
script,
crypto,
networks
} from 'bitcoinjs-lib';For CommonJS:
const bitcoin = require('bitcoinjs-lib');
// Or destructured
const { Transaction, Psbt, Block, address, payments } = require('bitcoinjs-lib');import { payments, networks, Psbt, Transaction } from 'bitcoinjs-lib';
// Create a P2PKH payment
const payment = payments.p2pkh({
pubkey: Buffer.from('03...', 'hex'),
network: networks.bitcoin
});
// Create a transaction
const tx = new Transaction();
tx.addInput(Buffer.from('abc123...', 'hex'), 0);
tx.addOutput(payment.output!, 5000);
// Work with PSBTs
const psbt = new Psbt({ network: networks.bitcoin });
psbt.addInput({
hash: 'abc123...',
index: 0,
nonWitnessUtxo: Buffer.from('...', 'hex')
});
psbt.addOutput({
address: '1BitcoinAddress...',
value: 5000
});bitcoinjs-lib is built around several key components:
Transaction class for creating and manipulating Bitcoin transactionspayments modulePsbt class for collaborative transaction buildinginitEccLib functionComplete address encoding and decoding for all Bitcoin address formats including Base58Check (P2PKH, P2SH) and Bech32 (P2WPKH, P2WSH, P2TR).
function fromBase58Check(address: string): { hash: Buffer; version: number };
function toBase58Check(hash: Buffer, version: number): string;
function fromBech32(address: string): { version: number; prefix: string; data: Buffer };
function toBech32(data: Buffer, version: number, prefix: string): string;
function fromOutputScript(output: Buffer, network?: Network): string;
function toOutputScript(address: string, network?: Network): Buffer;Support for all major Bitcoin script types with automatic script generation, address creation, and validation.
interface Payment {
name?: string;
network?: Network;
output?: Buffer;
address?: string;
hash?: Buffer;
pubkey?: Buffer;
pubkeys?: Buffer[];
signatures?: Buffer[];
input?: Buffer;
witness?: Buffer[];
redeem?: Payment;
scriptTree?: Taptree;
}
function p2pkh(a: Payment, opts?: PaymentOpts): Payment;
function p2sh(a: Payment, opts?: PaymentOpts): Payment;
function p2wpkh(a: Payment, opts?: PaymentOpts): Payment;
function p2wsh(a: Payment, opts?: PaymentOpts): Payment;
function p2tr(a: Payment, opts?: PaymentOpts): Payment;
function p2ms(a: Payment, opts?: PaymentOpts): Payment;
function p2pk(a: Payment, opts?: PaymentOpts): Payment;Complete transaction creation, manipulation, and serialization with support for witness data, fee calculation, and transaction signing.
class Transaction {
static fromBuffer(buffer: Buffer, _NO_STRICT?: boolean): Transaction;
static fromHex(hex: string): Transaction;
version: number;
locktime: number;
ins: Input[];
outs: Output[];
addInput(hash: Buffer, index: number, sequence?: number, scriptSig?: Buffer): number;
addOutput(scriptPubKey: Buffer, value: number): number;
hasWitnesses(): boolean;
weight(): number;
virtualSize(): number;
hashForSignature(inIndex: number, prevOutScript: Buffer, hashType: number): Buffer;
hashForWitnessV0(inIndex: number, prevOutScript: Buffer, value: number, hashType: number): Buffer;
hashForWitnessV1(inIndex: number, prevOutScripts: Buffer[], values: number[], hashType: number, leafHash?: Buffer, annex?: Buffer): Buffer;
toHex(): string;
}Complete BIP174 implementation supporting all PSBT roles: Creator, Updater, Signer, Combiner, Input Finalizer, and Transaction Extractor.
class Psbt {
static fromBase64(data: string, opts?: PsbtOptsOptional): Psbt;
static fromHex(data: string, opts?: PsbtOptsOptional): Psbt;
static fromBuffer(buffer: Buffer, opts?: PsbtOptsOptional): Psbt;
addInput(inputData: PsbtInputExtended): this;
addOutput(outputData: PsbtOutputExtended): this;
signInput(inputIndex: number, keyPair: Signer, sighashTypes?: number[]): this;
signInputAsync(inputIndex: number, keyPair: SignerAsync, sighashTypes?: number[]): Promise<void>;
finalizeAllInputs(): this;
extractTransaction(disableFeeCheck?: boolean): Transaction;
combine(...those: Psbt[]): this;
}Bitcoin-specific hash functions and cryptographic operations including SHA256, RIPEMD160, hash160, hash256, and BIP340 tagged hashes.
function sha256(buffer: Buffer): Buffer;
function ripemd160(buffer: Buffer): Buffer;
function hash160(buffer: Buffer): Buffer;
function hash256(buffer: Buffer): Buffer;
function taggedHash(prefix: TaggedHashPrefix, data: Buffer): Buffer;Complete script compilation, decompilation, and analysis utilities with support for script validation and ASM representation.
function compile(chunks: Buffer | Stack): Buffer;
function decompile(buffer: Buffer | Array<number | Buffer>): Array<number | Buffer> | null;
function toASM(chunks: Buffer | Array<number | Buffer>): string;
function fromASM(asm: string): Buffer;
function toStack(chunks: Buffer | Array<number | Buffer>): Buffer[];
function isCanonicalPubKey(buffer: Buffer): boolean;
function isCanonicalScriptSignature(buffer: Buffer): boolean;Bitcoin block parsing, validation, and Merkle root calculation with support for witness commitments and proof-of-work validation.
class Block {
static fromBuffer(buffer: Buffer): Block;
static fromHex(hex: string): Block;
static calculateMerkleRoot(transactions: Transaction[], forWitness?: boolean): Buffer;
version: number;
prevHash?: Buffer;
merkleRoot?: Buffer;
timestamp: number;
bits: number;
nonce: number;
transactions?: Transaction[];
hasWitness(): boolean;
weight(): number;
getHash(): Buffer;
toHex(headersOnly?: boolean): string;
checkTxRoots(): boolean;
checkProofOfWork(): boolean;
}Predefined network configurations for Bitcoin mainnet, testnet, and regtest with all necessary parameters for address generation and transaction creation.
interface Network {
messagePrefix: string;
bech32: string;
bip32: { public: number; private: number };
pubKeyHash: number;
scriptHash: number;
wif: number;
}
const bitcoin: Network;
const testnet: Network;
const regtest: Network;Various utility functions for buffer operations, script number encoding/decoding, signature handling, and validation helpers.
function initEccLib(eccLib: TinySecp256k1Interface | undefined): void;
// Buffer utilities
function readUInt64LE(buffer: Buffer, offset: number): number;
function writeUInt64LE(buffer: Buffer, value: number, offset: number): number;
function reverseBuffer(buffer: Buffer): Buffer;
// Script utilities
const number: {
decode(buffer: Buffer, maxLength?: number, minimal?: boolean): number;
encode(_number: number): Buffer;
};
const signature: {
decode(buffer: Buffer): { signature: Buffer; hashType: number };
encode(signature: Buffer, hashType: number): Buffer;
};interface Input {
hash: Buffer;
index: number;
script: Buffer;
sequence: number;
witness: Buffer[];
}
interface Output {
script: Buffer;
value: number;
}
type StackElement = Buffer | number;
type Stack = StackElement[];
interface Tapleaf {
output: Buffer;
version?: number;
}
type Taptree = [Taptree | Tapleaf, Taptree | Tapleaf] | Tapleaf;
interface TinySecp256k1Interface {
isXOnlyPoint(p: Uint8Array): boolean;
xOnlyPointAddTweak(p: Uint8Array, tweak: Uint8Array): XOnlyPointAddTweakResult | null;
}
interface XOnlyPointAddTweakResult {
parity: 1 | 0;
xOnlyPubkey: Uint8Array;
}