@babylonlabs-io/ts-sdk

中文文档
8.3.3发布于 2026/11/05ISCTypeScriptESM + CJS构建来源已验证近 30 天下载 5649

TypeScript SDK for Babylon protocol integrations

原始使用文档
文档目录22 节
  1. Overview
  2. What Are Trustless Bitcoin Vaults?
  3. Key Features
  4. Platforms
  5. Installation
  6. Requirements
  7. Install
  8. loadRawTbvWasm() is removed
  9. ECC Library Initialization (Bitcoin flows only)
  10. Verify Installation
  11. Package Structure
  12. Where to start
  13. Managers (High-Level)
  14. Primitives (Low-Level, Advanced)
  15. Documentation
  16. 🏁 Start here
  17. 🚀 Quickstart
  18. 🔑 Guides
  19. 🔌 Application Integrations
  20. 🔍 API Reference
  21. 🛠️ Troubleshooting
  22. Links

@babylonlabs-io/ts-sdk

Build Status npm version

TypeScript SDK for Trustless Bitcoin Vaults

Overview

The Babylon TypeScript SDK is the toolkit for integrating Trustless Bitcoin Vaults into your applications. It provides vault lifecycle management (peg-in, activation, refund, payout signing) and application integrations built on top, starting with Aave v4.

What Are Trustless Bitcoin Vaults?

Trustless Bitcoin Vaults (TBV) let you lock Bitcoin and use it in applications on supported chains (like lending protocols) without giving up custody. The vault protocol enables:

  • Peg-in: Lock BTC in a vault on Bitcoin to use as collateral (protocol spec)
  • Peg-out: Unlock BTC from the vault back to your wallet (protocol spec)
  • DeFi Integration: Use vaulted BTC in protocols like Aave

This SDK handles the complex Bitcoin and Ethereum interactions needed to create and manage these vaults.

Key Features

  • 🔐 Vault Management - Vault creation (Pegin), vault redemption (Pegout), and vault lifecycle operations
  • 🔌 Application Integrations - Pre-built integrations starting with Aave (DeFi lending)
  • 📦 Framework Agnostic - Works with React, Vue, Angular, Node.js, or vanilla JavaScript
  • 🎯 Type-Safe - Comprehensive TypeScript types with full IDE support
  • 🧩 Modular Design - Use only what you need via subpath exports
  • 🔧 Extensible - Easy to build custom integrations

Platforms

This SDK runs on both Node.js backends and browser/React frontends — the entire library is framework-agnostic. The only difference between the two environments is how you supply two things: a Bitcoin wallet and an Ethereum wallet. The SDK provides interfaces (BitcoinWallet, viem's WalletClient) and you adapt whichever wallet is available on your platform.

ConcernNode.js backendBrowser / React
BitcoinWalletBuild from a seed via bitcoinjs-lib + bip32 (or call KMS/HSM)Adapt an injected wallet (Unisat, OKX, Xverse, Leather)
viem WalletClientcreateWalletClient({ account: privateKeyToAccount(pk), chain, transport: http(RPC_URL) })createWalletClient({ account, chain, transport: custom(window.ethereum) }) via wagmi
Secret storageYour DB / KMS (required for activation later)Session/local storage or user-supplied
WASM loadingAutomatic (SDK reads .wasm off disk with node:fs)Automatic (SDK fetches .wasm via bundler)

If you're using a bundler (Vite, webpack, Next.js), ensure it's configured to handle .wasm assets and that Buffer is available on the global in browser targets — see the Troubleshooting Guide.

Installation

Requirements

  • Node.js ≥ 20.3.0 — the supported baseline. Works on Node 20 LTS, 22 LTS, 24 LTS.
  • Package manager: npm, yarn, or pnpm

Install

viem ^2.38.2 is the only required peer. An Ethereum-only consumer installs the SDK and viem:

pnpm add @babylonlabs-io/ts-sdk viem

The Bitcoin peers are optional. Add their exact supported versions before you import a root or Bitcoin entry:

pnpm add [email protected] @bitcoin-js/[email protected]

The WASM engine is also optional. Add it only for Bitcoin construction and signing paths:

pnpm add @babylonlabs-io/babylon-tbv-rust-wasm

The source manifest uses workspace:* for the WASM peer. The release process replaces it with the exact engine version used in the release rehearsal.

loadRawTbvWasm() is removed

Use the guarded builders in @babylonlabs-io/ts-sdk/tbv/core/primitives.

Upgrade @babylonlabs-io/babylon-tbv-rust-wasm together with this SDK. Engine releases 0.17.0 to 0.19.0 have the /raw entry and do not export the classes from the package root. With one of those releases, buildRefundPsbt() fails.

If you need the low-level classes, call loadTbvWasm() from @babylonlabs-io/ts-sdk/tbv/core/wasm. It returns the initialized engine module. Do the checks that the "WASM Classes" section of the @babylonlabs-io/babylon-tbv-rust-wasm README describes.

ECC Library Initialization (Bitcoin flows only)

ETH-only consumers do not need an ECC library. If your application uses the SDK's Bitcoin/Taproot operations, call initEccLib() once before any SDK call that builds a PSBT or derives a Bitcoin address:

import * as ecc from "@bitcoin-js/tiny-secp256k1-asmjs";
import { initEccLib } from "bitcoinjs-lib";

initEccLib(ecc);
  • Node.js: call this at the top of your entry point (index.ts / CLI entry).
  • React: call it in main.tsx / app.tsx before createRoot().

Failing to initialize produces a runtime error: "No ECC Library provided".

The WASM package used internally (@babylonlabs-io/babylon-tbv-rust-wasm) initializes itself lazily — no extra call needed.

Verify Installation

import { buildPrePeginPsbt } from "@babylonlabs-io/ts-sdk/tbv/core/primitives";

console.log("✅ SDK installed successfully!");
console.log("buildPrePeginPsbt type:", typeof buildPrePeginPsbt);

Run with: npx tsx verify-install.ts

Troubleshooting? See Troubleshooting Guide for Buffer polyfills, WASM setup, and bundler configuration.

Package Structure

The SDK uses subpath exports for hard dependency boundaries as well as tree-shaking. ETH-only code should import the dedicated /eth entries rather than a broad compatibility barrel:

// High-level managers (recommended for most users)
import { PeginManager, PayoutManager } from "@babylonlabs-io/ts-sdk/tbv/core";

// Ethereum-only readers and prepared peg-in registration (no BTC wallet,
// bitcoinjs-lib, tiny-secp256k1, or WASM required)
import {
  ViemProtocolParamsReader,
  ViemVaultRegistryReader,
  ViemPeginRegistrationClient,
} from "@babylonlabs-io/ts-sdk/tbv/core/clients/eth";

// Ethereum-only transaction receipt helper
import { waitForTransactionReceiptSmartAware } from "@babylonlabs-io/ts-sdk/tbv/core/utils/eth";

// Unauthenticated VP polling without the Bitcoin-auth implementation
import {
  VaultProviderRpcClient,
  batchPollByProvider,
  DaemonStatus,
} from "@babylonlabs-io/ts-sdk/tbv/core/clients/vault-provider/status";

// Mempool reads without bitcoinjs-lib, tiny-secp256k1, or WASM
import { getTxInfo } from "@babylonlabs-io/ts-sdk/tbv/core/clients/mempool";

// Low-level primitives (advanced use cases)
import {
  buildPrePeginPsbt,
  buildPeginTxFromFundedPrePegin,
  buildPayoutPsbt,
  buildNoPayoutPsbt,
  buildChallengeAssertPsbt,
  buildRefundPsbt,
  formatSatoshisToBtc,
} from "@babylonlabs-io/ts-sdk/tbv/core/primitives";

// Services (activation, deposit validation, protocol state)
import {
  activateVault,
  getPeginProtocolState,
  ContractStatus,
  validateDepositAmount,
  isPegoutTerminalStatus,
} from "@babylonlabs-io/ts-sdk/tbv/core/services";

// Utilities
import {
  selectUtxosForPegin,
  validateUtxosAvailable,
} from "@babylonlabs-io/ts-sdk/tbv/core";

// Shared wallet interfaces
import { BitcoinWallet } from "@babylonlabs-io/ts-sdk/shared";
// UTXO type lives in the core utils module
import { UTXO } from "@babylonlabs-io/ts-sdk/tbv/core";

// Contract ABIs (also dependency-clean for ETH consumers)
import { BTCVaultRegistryABI } from "@babylonlabs-io/ts-sdk/tbv/core/contracts";

// Protocol integrations (Aave)
import {
  buildBorrowTx,
  buildRepayTx,
  getUserAccountData,
  calculateHealthFactor,
} from "@babylonlabs-io/ts-sdk/tbv/integrations/aave";

ViemPeginRegistrationClient owns only the Ethereum submission half of a peg-in. It accepts a signed PegIn transaction, PoP, depositorPayoutScriptPubKey, and depositorBtcPubkeyRaw prepared earlier. It never asks for a BTC wallet. It validates the payout script against the raw key and the PoP key before any contract read or wallet call. PeginManager also checks the wallet address before it delegates to the ETH client.

Where to start

The SDK exposes several peer entry points — managers, services, primitives, utils, clients, and integrations — and you compose the pieces you need. The two most common starting points for a new integration are:

Managers (High-Level)

  • What: Pre-built wallet orchestration for complete vault workflows
  • Best for: Web apps, browser wallets (MetaMask, Unisat)
  • You provide: Wallet interface
  • SDK handles: Transaction building, signing coordination, contract calls

Primitives (Low-Level, Advanced)

  • What: Pure functions for building Bitcoin PSBTs
  • Best for: Backend services, custom signing (KMS/HSM), full control
  • You provide: Everything (signing logic, contract calls, broadcasting)
  • SDK handles: Only PSBT construction and utility functions

For how services, utils, clients, and integrations fit in and which layer a given task belongs to, read the Get Started guide.

Documentation

🏁 Start here

  • Get Started — five-minute orientation: what TBV is, the SDK's layer layout, prerequisites, config sourcing, glossary, vault lifecycle, and a decision table routing you to the right quickstart. Read this first if you're new.

🚀 Quickstart

Step-by-step tutorials:

🔑 Guides

  • Wallet Interfaces - Adapt any BTC/ETH wallet (browser, Node.js, KMS/HSM) to the SDK's interfaces

🔌 Application Integrations

Use BTC vaults in DeFi protocols and applications:

🔍 API Reference

Auto-generated from TSDoc comments using TypeDoc:

🛠️ Troubleshooting

版本与发布记录

@babylonlabs-io/ts-sdk 共发布 188 个版本,首次发布于 2025/11/17;过去 12 个月发布了 188 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
8.3.3latest2026/11/052 天前
8.3.22026/11/043 天前
8.3.12026/11/034 天前
8.3.02026/11/034 天前
8.2.42026/11/034 天前
8.2.32026/11/034 天前

其他发布通道:next → 0.15.4-rc-0.16.0。

关于 @babylonlabs-io/ts-sdk 的常见问题

@babylonlabs-io/ts-sdk 是什么?

TypeScript SDK for Babylon protocol integrations

如何安装 @babylonlabs-io/ts-sdk?

在项目目录运行 npm install @babylonlabs-io/ts-sdk,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:viem(^2.38.2)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@babylonlabs-io/ts-sdk 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@babylonlabs-io/ts-sdk 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@babylonlabs-io/ts-sdk 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20.3.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@babylonlabs-io/ts-sdk 可以商用吗?

@babylonlabs-io/ts-sdk 使用 ISC 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@babylonlabs-io/ts-sdk 的下载量有多少?

根据 npm 官方下载统计,@babylonlabs-io/ts-sdk 在 2026/10/08 – 2026/11/06 这 30 天内被下载 5,649 次,日均约 188 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@babylonlabs-io/ts-sdk 还在维护吗?

最新版本 8.3.3 发布于 2026/11/05(2 天前);至今共发布 188 个版本,其中过去 12 个月发布了 188 个;首次发布于 2025/11/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。