@bitgo/bip174

中文文档
3.0.0-rc.1发布于 2023/01/24MITTypeScriptCommonJS零依赖近 30 天下载 2422

Fork of bip174 with BitGo specific changes

此软件包已被标记为弃用

deprecate

原始使用文档
文档目录6 节
  1. Bitcoin users, use bitcoinjs-lib's Psbt class.
  2. Responsibilities Covered
  3. Responsibilities NOT Covered
  4. Static methods and addInput / addOutput require an abstract…
  5. Example
  6. LICENSE MIT

bip174

Build Status NPM

code style: prettier

A BIP174 compatible partial Transaction encoding library.

Bitcoin users, use bitcoinjs-lib's Psbt class.

This library is separate as an attempt to separate Bitcoin specific logic from the encoding format.

I apologize if this library is hard to use. Removing Bitcoin specific logic from "Partially Signed BITCOIN Transaction" format was kind of hard.

Responsibilities Covered

  1. Creator: You can create a new psbt and add inputs and outputs, then add extra info to the inputs using each individual method. (Note: Psbt.fromTransaction can create a PSBT from a serialized transaction, but all scriptSigs and witnessStacks must be empty, and the transaction should not have the segwit marker and flagbyte.)
  2. Updater: You can add one of all types of the input and output information. Each function has strict type checking at runtime.
  3. Combiner: using Psbt.combine(...Psbt[]), it will do a dumb merge gathering all the new key value pairs from the subsequent PSBTs into itself. The psbt calling combine has highest priority.

Responsibilities NOT Covered

In order to keep this library as separate from Bitcoin logic as possible, This library will not implement the following responsibilities. But rather, down the road bitcoinjs-lib will adopt this class and extend it internally to allow for the following:

  1. Signer: This class has no way to sign. You may add a PartialSig object though.
  2. Extractor: This class has minimal knowledge of Bitcoin transactions, so creating a full transaction from a PSBT must be done with a bitcoin aware extended class.
  3. Input Finalizer: This class, again, has no knowledge of whether an input is finished.

Static methods and addInput / addOutput require an abstract Transaction object

  • Static methods: You must pass a TransactionFromBuffer typed function. See ts_src/lib/interfaces.ts for info on the Transaction interface and the TransactionFromBuffer function.
  • addInput/addOutput methods: The constructor takes a Transaction abstract interface that has an addInput/addOutput method which will be called.

Example

const { Psbt } = require('bip174')
const { PsbtTransaction , pTxFromBuffer } = require('./someImplementation')
// Psbt requires a Transaction interface to create an instance, as well as
// A function that turns a buffer into that interface. See Transaction and TransactionFromBuffer
// in ts_src/lib/interfaces.ts ...

// See tests/utils/txTools file for an example of a simple Bitcoin Transaction.
// Also see BitcoinJS-lib for an extended class that uses the Transaction class internally.
// Anyone using this library for Bitcoin specifically should use bitcoinjs-lib

// Your PsbtTransaction will have a toBuffer function to allow for serialization
const tx = pTxFromBuffer(someTransactionBuffer);
const psbt = new Psbt(tx)

// OR

// This will parse the PSBT, and use the function you pass to parse the Transaction part
// the function should throw if the scriptSig section is not empty
const psbt = Psbt.fromBuffer(somePsbtBuffer, pTxFromBuffer)

psbt.addInput({
  hash: '865dce988413971fd812d0e81a3395ed916a87ea533e1a16c0f4e15df96fa7d4',
  index: 3,
})
psbt.addInput({
  hash: 'ff5dce988413971fd812d0e81a3395ed916a87ea533e1a16c0f4e15df96fa7d4',
  index: 1,
})
psbt.addOutput({
  script: Buffer.from(
    'a914e18870f2c297fbfca54c5c6f645c7745a5b66eda87',
    'hex',
  ),
  value: 1234567890,
})
psbt.addOutput({
  script: Buffer.from(
    'a914e18870f2c297fbfca54c5c6f645c7745a5b66eda87',
    'hex',
  ),
  value: 987654321,
})
psbt.addRedeemScriptToInput(0, Buffer.from(
  '00208c2353173743b595dfb4a07b72ba8e42e3797da74e87fe7d9d7497e3b2028903',
  'hex',
))
psbt.addWitnessScriptToInput(0, Buffer.from(
  '522103089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b9903' +
    '0b02dc21023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e861519268' +
    '60221f0e7352ae',
  'hex',
))
psbt.addBip32DerivationToInput(0, {
  masterFingerprint: Buffer.from('d90c6a4f', 'hex'),
  pubkey: Buffer.from(
    '023add904f3d6dcf59ddb906b0dee23529b7ffb9ed50e5e86151926860221f0e73',
    'hex',
  ),
  path: "m/0'/0'/3'",
})
psbt.addBip32DerivationToInput(0, {
  masterFingerprint: Buffer.from('d90c6a4f', 'hex'),
  pubkey: Buffer.from(
    '03089dc10c7ac6db54f91329af617333db388cead0c231f723379d1b99030b02dc',
    'hex',
  ),
  path: "m/0'/0'/2'",
})
const b64 = psbt.toBase64();

LICENSE MIT

版本与发布记录

@bitgo/bip174 共发布 2 个版本,首次发布于 2022/09/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.0.0-rc.1latest2023/01/243 年前
3.0.02022/09/014 年前

关于 @bitgo/bip174 的常见问题

@bitgo/bip174 是什么?

Fork of bip174 with BitGo specific changes注意:该包已被作者标记为弃用——deprecate

如何安装 @bitgo/bip174?

在项目目录运行 npm install @bitgo/bip174,或使用 pnpm add / yarn add / bun add。

@bitgo/bip174 支持 TypeScript 吗?

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

@bitgo/bip174 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bitgo/bip174 需要什么版本的 Node.js?

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

@bitgo/bip174 可以商用吗?

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

@bitgo/bip174 的下载量有多少?

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

@bitgo/bip174 还在维护吗?

最新版本 3.0.0-rc.1 发布于 2023/01/24(3 年前);至今共发布 2 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/09/01。作者已将其标记为弃用,新项目建议寻找替代方案。