Support for all major Bitcoin script types including Pay-to-Public-Key-Hash (P2PKH), Pay-to-Script-Hash (P2SH), Pay-to-Witness-Public-Key-Hash (P2WPKH), Pay-to-Witness-Script-Hash (P2WSH), Pay-to-Taproot (P2TR), Pay-to-Multisig (P2MS), Pay-to-Public-Key (P2PK), and Pay-to-Data (P2DATA/OP_RETURN) with automatic script generation, address creation, and validation.
interface Payment {
/** Payment name for identification */
name?: string;
/** Network configuration */
network?: Network;
/** Output script (scriptPubKey) */
output?: Buffer;
/** Script data for OP_RETURN payments */
data?: Buffer[];
/** Multisig threshold (m-of-n) */
m?: number;
/** Total number of multisig keys */
n?: number;
/** Array of public keys for multisig */
pubkeys?: Buffer[];
/** Input script (scriptSig) */
input?: Buffer;
/** Array of signatures */
signatures?: Buffer[];
/** Internal public key for Taproot */
internalPubkey?: Buffer;
/** Single public key */
pubkey?: Buffer;
/** Single signature */
signature?: Buffer;
/** Generated address */
address?: string;
/** Payment hash (hash160 or hash256) */
hash?: Buffer;
/** Redeem script payment (for P2SH/P2WSH) */
redeem?: Payment;
/** Redeem script version */
redeemVersion?: number;
/** Taproot script tree */
scriptTree?: Taptree;
/** Witness stack data */
witness?: Buffer[];
}
interface PaymentOpts {
/** Enable validation of payment data */
validate?: boolean;
/** Allow incomplete payment objects */
allowIncomplete?: boolean;
}
type PaymentCreator = (a: Payment, opts?: PaymentOpts) => Payment;
type PaymentFunction = () => Payment;Standard Bitcoin address format that pays to the hash of a public key.
/**
* Creates a Pay-to-Public-Key-Hash (P2PKH) payment object
* @param a - Payment object containing pubkey or hash
* @param opts - Optional payment options
* @returns P2PKH payment object with generated script and address
* @throws TypeError if required data is not provided or invalid
*/
function p2pkh(a: Payment, opts?: PaymentOpts): Payment;Usage Examples:
import { payments, networks } from 'bitcoinjs-lib';
// Create P2PKH from public key
const pubkey = Buffer.from('03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd', 'hex');
const p2pkh = payments.p2pkh({ pubkey, network: networks.bitcoin });
console.log(p2pkh.address); // '1F1tAaz5x1HUXrCNLbtMDqcw6o5GNn4xqX'
console.log(p2pkh.output?.toString('hex')); // '76a914a54d...'
// Create P2PKH from hash
const hash = Buffer.from('a54d...', 'hex');
const p2pkh2 = payments.p2pkh({ hash, network: networks.bitcoin });Allows sending to a script hash, enabling complex spending conditions.
/**
* Creates a Pay-to-Script-Hash (P2SH) payment object
* @param a - Payment object containing redeem script or hash
* @param opts - Optional payment options
* @returns P2SH payment object with generated script and address
* @throws TypeError if required data is not provided or invalid
*/
function p2sh(a: Payment, opts?: PaymentOpts): Payment;Usage Examples:
import { payments, networks } from 'bitcoinjs-lib';
// Create P2SH wrapping P2WPKH (for compatibility)
const pubkey = Buffer.from('03a34b...', 'hex');
const p2wpkh = payments.p2wpkh({ pubkey, network: networks.bitcoin });
const p2sh = payments.p2sh({ redeem: p2wpkh, network: networks.bitcoin });
console.log(p2sh.address); // '3...' address
console.log(p2sh.output?.toString('hex')); // 'a914...87'
// Create P2SH from script hash
const scriptHash = Buffer.from('abc123...', 'hex');
const p2sh2 = payments.p2sh({ hash: scriptHash, network: networks.bitcoin });SegWit v0 payment to a public key hash, providing transaction malleability protection and fee savings.
/**
* Creates a Pay-to-Witness-Public-Key-Hash (P2WPKH) payment object
* @param a - Payment object containing pubkey or hash
* @param opts - Optional payment options
* @returns P2WPKH payment object with generated script and address
* @throws TypeError if required data is not provided or invalid
*/
function p2wpkh(a: Payment, opts?: PaymentOpts): Payment;Usage Examples:
import { payments, networks } from 'bitcoinjs-lib';
// Create P2WPKH from public key
const pubkey = Buffer.from('03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd', 'hex');
const p2wpkh = payments.p2wpkh({ pubkey, network: networks.bitcoin });
console.log(p2wpkh.address); // 'bc1q...' address
console.log(p2wpkh.output?.toString('hex')); // '0014...'
// Create P2WPKH from hash
const hash = Buffer.from('a54d...', 'hex');
const p2wpkh2 = payments.p2wpkh({ hash, network: networks.bitcoin });SegWit v0 payment to a script hash, enabling complex scripts with witness data.
/**
* Creates a Pay-to-Witness-Script-Hash (P2WSH) payment object
* @param a - Payment object containing redeem script or hash
* @param opts - Optional payment options
* @returns P2WSH payment object with generated script and address
* @throws TypeError if required data is not provided or invalid
*/
function p2wsh(a: Payment, opts?: PaymentOpts): Payment;Usage Examples:
import { payments, networks } from 'bitcoinjs-lib';
// Create P2WSH wrapping multisig
const pubkeys = [
Buffer.from('03a34b...', 'hex'),
Buffer.from('03b45c...', 'hex'),
Buffer.from('03c56d...', 'hex')
];
const p2ms = payments.p2ms({ m: 2, pubkeys, network: networks.bitcoin });
const p2wsh = payments.p2wsh({ redeem: p2ms, network: networks.bitcoin });
console.log(p2wsh.address); // 'bc1q...' address (longer than P2WPKH)
console.log(p2wsh.output?.toString('hex')); // '0020...'SegWit v1 payment supporting both key-path and script-path spending with privacy benefits.
/**
* Creates a Pay-to-Taproot (P2TR) payment object
* @param a - Payment object containing internal pubkey and optional script tree
* @param opts - Optional payment options
* @returns P2TR payment object with generated script and address
* @throws TypeError if provided data is invalid or insufficient
*/
function p2tr(a: Payment, opts?: PaymentOpts): Payment;Usage Examples:
import { payments, networks } from 'bitcoinjs-lib';
// Create simple P2TR (key-path only)
const internalPubkey = Buffer.from('abc123...', 'hex'); // 32-byte x-only pubkey
const p2tr = payments.p2tr({ internalPubkey, network: networks.bitcoin });
console.log(p2tr.address); // 'bc1p...' address
console.log(p2tr.output?.toString('hex')); // '5120...'
// Create P2TR with script tree
const scriptTree = [/* Taptree structure */];
const p2trScript = payments.p2tr({
internalPubkey,
scriptTree,
network: networks.bitcoin
});Direct multisig payments requiring m-of-n signatures for spending.
/**
* Creates a Pay-to-Multisig (P2MS) payment object
* @param a - Payment object containing m, n, and pubkeys
* @param opts - Optional payment options
* @returns P2MS payment object with generated script
* @throws TypeError if required data is not provided or invalid
*/
function p2ms(a: Payment, opts?: PaymentOpts): Payment;Usage Examples:
import { payments, networks } from 'bitcoinjs-lib';
// Create 2-of-3 multisig
const pubkeys = [
Buffer.from('03a34b...', 'hex'),
Buffer.from('03b45c...', 'hex'),
Buffer.from('03c56d...', 'hex')
];
const p2ms = payments.p2ms({
m: 2,
pubkeys,
network: networks.bitcoin
});
console.log(p2ms.output?.toString('hex')); // '5221...53ae'
// Note: P2MS is rarely used directly; usually wrapped in P2SH or P2WSH
const p2sh = payments.p2sh({ redeem: p2ms, network: networks.bitcoin });Direct payment to a public key (legacy format, rarely used).
/**
* Creates a Pay-to-Public-Key (P2PK) payment object
* @param a - Payment object containing pubkey
* @param opts - Optional payment options
* @returns P2PK payment object with generated script
* @throws TypeError if required data is not provided or invalid
*/
function p2pk(a: Payment, opts?: PaymentOpts): Payment;Usage Examples:
import { payments, networks } from 'bitcoinjs-lib';
// Create P2PK payment
const pubkey = Buffer.from('03a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd', 'hex');
const p2pk = payments.p2pk({ pubkey, network: networks.bitcoin });
console.log(p2pk.output?.toString('hex')); // '21...ac'Embed arbitrary data in transactions using OP_RETURN (unspendable outputs).
/**
* Creates a Pay-to-Data (OP_RETURN) payment object for embedding data
* @param a - Payment object containing data array
* @param opts - Optional payment options
* @returns P2DATA payment object with OP_RETURN script
* @throws TypeError if required data is not provided or invalid
*/
function embed(a: Payment, opts?: PaymentOpts): Payment;Usage Examples:
import { payments } from 'bitcoinjs-lib';
// Embed string data
const data = [Buffer.from('Hello Bitcoin!', 'utf8')];
const opReturn = payments.embed({ data });
console.log(opReturn.output?.toString('hex')); // '6a0d48656c6c6f20426974636f696e21'
// Embed multiple data pushes
const multiData = [
Buffer.from('BTCJS', 'utf8'),
Buffer.from('12345', 'utf8')
];
const opReturn2 = payments.embed({ data: multiData });Many payment types can be nested for backward compatibility or advanced functionality:
import { payments, networks } from 'bitcoinjs-lib';
// P2SH-wrapped P2WPKH for older wallet compatibility
const pubkey = Buffer.from('03a34b...', 'hex');
const p2wpkh = payments.p2wpkh({ pubkey, network: networks.bitcoin });
const p2shP2wpkh = payments.p2sh({ redeem: p2wpkh, network: networks.bitcoin });
// P2WSH-wrapped multisig
const pubkeys = [/* public keys */];
const p2ms = payments.p2ms({ m: 2, pubkeys, network: networks.bitcoin });
const p2wshP2ms = payments.p2wsh({ redeem: p2ms, network: networks.bitcoin });All payment functions support validation options:
import { payments, networks } from 'bitcoinjs-lib';
try {
const payment = payments.p2pkh({
pubkey: Buffer.from('invalid', 'hex'),
network: networks.bitcoin
}, { validate: true });
} catch (error) {
console.log('Invalid payment data:', error.message);
}type StackElement = Buffer | number;
type Stack = StackElement[];
type StackFunction = () => Stack;
interface Tapleaf {
output: Buffer;
version?: number;
}
type Taptree = [Taptree | Tapleaf, Taptree | Tapleaf] | Tapleaf;
interface Network {
messagePrefix: string;
bech32: string;
bip32: { public: number; private: number };
pubKeyHash: number;
scriptHash: number;
wif: number;
}