npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

bip388

v1.0.3

Published

bip388 utils for typescript

Downloads

2,461

Readme

bip388

TypeScript library for building Bitcoin scripts and BIP-388 wallet policies, with first-class support for Ledger hardware wallets.

Install

npm install bip388

Features

  • BIP-388 Wallet Policies - Descriptor templates with key placeholders for Ledger registration
  • BIP-379 Miniscript - Typesafe miniscript fragment builders
  • HTLCs - Hash Time-Locked Contracts for both Taproot and SegWit
  • Multisig - k-of-n multisig for Taproot and SegWit

Quick Start

Taproot HTLC

import { taprootHtlc } from "bip388";

const htlc = taprootHtlc({
  payee: {
    xpub: "tpub...",
    fingerprint: "3442193e",
    originPath: "86'/1'/0'",
  },
  payer: {
    xpub: "tpub...",
    fingerprint: "3442193e",
    originPath: "86'/1'/1'",
  },
  secretHash: "deadbeef...",
  timelock: 800000,
  network: "testnet",
});

// Address to fund
console.log(htlc.address);

// BIP-388 policy for Ledger
console.log(htlc.policy?.template);
// => tr(@0/**,{and_v(v:sha256(...),pk(@1/**)),and_v(v:after(800000),pk(@2/**))})

// Witness for spending with preimage
const witness = htlc.witness.secret(signature, preimage);

// Witness for timeout refund
const witness = htlc.witness.timeout(signature);

SegWit HTLC

import { segwitHtlc } from "bip388";

const htlc = segwitHtlc({
  payee: { xpub: "tpub...", fingerprint: "...", originPath: "..." },
  payer: { xpub: "tpub...", fingerprint: "...", originPath: "..." },
  secretHash: "deadbeef...",
  timelock: 800000,
  network: "testnet",
});

console.log(htlc.address); // tb1q...

Taproot Multisig (2-of-3)

import { taprootMultisig } from "bip388";

const multisig = taprootMultisig({
  threshold: 2,
  keys: [
    { xpub: "tpub...", fingerprint: "...", originPath: "..." },
    { xpub: "tpub...", fingerprint: "...", originPath: "..." },
    { xpub: "tpub...", fingerprint: "...", originPath: "..." },
  ],
  network: "testnet",
});

console.log(multisig.address);
console.log(multisig.policy?.template);
// => tr(@0/**,sortedmulti_a(2,@1/**,@2/**,@3/**))

// Build witness with signatures (empty buffer for non-signers)
const witness = multisig.witness([sig1, sig2, Buffer.alloc(0)]);

SegWit Multisig

import { segwitMultisig } from "bip388";

const multisig = segwitMultisig({
  threshold: 2,
  keys: [key1, key2, key3],
  network: "testnet",
});

console.log(multisig.policy?.template);
// => wsh(sortedmulti(2,@0/**,@1/**,@2/**))

Key Types

Keys can be either XpubKey (for BIP-388 policy generation) or RawKey (just a pubkey):

// XpubKey - enables BIP-388 policy generation
const xpubKey: XpubKey = {
  xpub: "tpub...",
  fingerprint: "3442193e", // master fingerprint
  originPath: "86'/1'/0'", // derivation path
  index: 0, // optional, default 0
  change: 0, // optional, 0=receive, 1=change
};

// RawKey - no policy generated
const rawKey: RawKey = {
  pubkey: "02abc...", // 33-byte compressed or 32-byte x-only
};

When all keys are XpubKey, a WalletPolicy is generated for Ledger registration. When using RawKey, policy will be null.

Miniscript Fragments

Build miniscript expressions directly:

import { pk, sha256, and_v, or_i, older, after, multi_a } from "bip388";

// pk(@0/**)
pk(keyPlaceholder(0));

// and_v(v:sha256(H),pk(@0/**))
and_v(sha256("deadbeef..."), pk(keyPlaceholder(0)));

// or_i(and_v(v:sha256(H),pk(@0/**)),and_v(v:after(144),pk(@1/**)))
or_i(
  and_v(sha256("..."), pk(keyPlaceholder(0))),
  and_v(after(144), pk(keyPlaceholder(1))),
);

// multi_a(2,@0/**,@1/**,@2/**) - Taproot
multi_a(2, [keyPlaceholder(0), keyPlaceholder(1), keyPlaceholder(2)]);

Low-Level Scripts

For direct Bitcoin Script building:

import {
  pkScript,
  pkhScript,
  multiScript,
  cltvScript,
  csvScript,
} from "bip388";

// <pubkey> OP_CHECKSIG
pkScript(pubkey);

// OP_DUP OP_HASH160 <hash> OP_EQUALVERIFY OP_CHECKSIG
pkhScript(pubkey);

// OP_2 <key1> <key2> <key3> OP_3 OP_CHECKMULTISIG
multiScript(2, [key1, key2, key3]);

// <time> OP_CHECKLOCKTIMEVERIFY OP_DROP <pubkey> OP_CHECKSIG
cltvScript(pubkey, locktime);

// <blocks> OP_CHECKSEQUENCEVERIFY OP_DROP <pubkey> OP_CHECKSIG
csvScript(pubkey, blocks);

API Reference

Pattern Builders

| Function | Description | | ------------------------- | ------------------------------------------ | | taprootHtlc(params) | Taproot HTLC with secret and timeout paths | | segwitHtlc(params) | SegWit P2WSH HTLC | | taprootMultisig(params) | Taproot k-of-n multisig | | segwitMultisig(params) | SegWit P2WSH k-of-n multisig |

BIP-388 Builders

| Function | Description | | ---------------------- | -------------------------------------- | | buildTaprootPolicy() | Build a tr() wallet policy | | buildSegwitPolicy() | Build a wsh() wallet policy | | keyPlaceholder(n) | Create @n/** key placeholder | | formatKeyInfo() | Format key as [fingerprint/path]xpub |

Miniscript Fragments

| Function | Description | | ------------------------ | ------------------------- | | pk(key) | Public key check | | sha256(hash) | SHA256 preimage check | | and_v(left, right) | Verify AND | | or_i(left, right) | IF/ELSE OR | | older(blocks) | Relative timelock (CSV) | | after(time) | Absolute timelock (CLTV) | | multi(k, keys) | k-of-n multisig (SegWit) | | multi_a(k, keys) | k-of-n multisig (Taproot) | | sortedmulti(k, keys) | Sorted k-of-n (SegWit) | | sortedmulti_a(k, keys) | Sorted k-of-n (Taproot) |

License

MIT