@bitcoinerlab/btcmessage

中文文档
4.0.2发布于 2026/11/05MITTypeScriptCommonJS近 30 天下载 4032

Fork of bitcoinjs-message

原始使用文档
文档目录3 节
  1. Examples (Note about Electrum support at the bottom)
  2. About Electrum segwit signature support
  3. LICENSE MIT

bitcoinjs-message

js-standard-style

Forked from bitcoinjs-message. Published to npm as @bitcoinerlab/btcmessage.

Examples (Note about Electrum support at the bottom)

var tinysecp = require('tiny-secp256k1');
var MessageFactory = require('@bitcoinerlab/btcmessage').MessageFactory;
var toBase64 = require('uint8array-tools').toBase64;
var bitcoinMessage = MessageFactory(tinysecp);

When using tiny-secp256k1 or @bitcoin-js/tiny-secp256k1-asmjs, use version 2.2.5 or later. MessageFactory uses the backend supplied by your application, so updating this package alone does not update that backend.

Breaking change in v4: this library is Uint8Array-first and does not return Buffer instances.

sign(message, privateKey, compressed[, network.messagePrefix, sigOptions])

  • If you pass the sigOptions arg instead of messagePrefix it will dynamically replace.
  • sigOptions contains two attributes
    • segwitType should be one of 'p2sh(p2wpkh)' or 'p2wpkh'
    • extraEntropy will be used to create non-deterministic signatures using the RFC6979 extra entropy parameter. R value reuse is not an issue.

Sign a Bitcoin message

var keyPair = ECPair.fromWIF(
  'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1',
);
var privateKey = keyPair.privateKey;
var message = 'This is an example of a signed message.';

var signature = bitcoinMessage.sign(message, privateKey, keyPair.compressed);
console.log(toBase64(signature));
// => 'H9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='

To produce non-deterministic signatures you can pass an extra option to sign()

var { randomBytes } = require('crypto');
var keyPair = ECPair.fromWIF(
  'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1',
);
var privateKey = keyPair.privateKey;
var message = 'This is an example of a signed message.';

var signature = bitcoinMessage.sign(message, privateKey, keyPair.compressed, {
  extraEntropy: randomBytes(32),
});
console.log(toBase64(signature));
// => different (but valid) signature each time

Sign a Bitcoin message (with segwit addresses)

// P2SH(P2WPKH) address 'p2sh(p2wpkh)'
var signature = bitcoinMessage.sign(message, privateKey, keyPair.compressed, {
  segwitType: 'p2sh(p2wpkh)',
});
console.log(toBase64(signature));
// => 'I9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='

// P2WPKH address 'p2wpkh'
var signature = bitcoinMessage.sign(message, privateKey, keyPair.compressed, {
  segwitType: 'p2wpkh',
});
console.log(toBase64(signature));
// => 'J9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='

Sign a Bitcoin message using a Signer interface.

var keyPair = ECPair.fromWIF(
  'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1',
);
var privateKey = keyPair.privateKey;
var message = 'This is an example of a signed message.';

var secp256k1 = require('tiny-secp256k1');
// Notice we are using the privateKey var from the outer scope inside the sign function.
var signer = {
  signRecoverable: (hash, extraData) =>
    secp256k1.signRecoverable(hash, privateKey, extraData),
};

var signature = bitcoinMessage.sign(message, signer, keyPair.compressed);
console.log(toBase64(signature));
// => 'H9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='

signAsync(message, privateKey, compressed[, network.messagePrefix, sigOptions]) Same as sign, except returns a promise, and can accept a SignerAsync interface instead of privateKey

Sign a Bitcoin message asynchronously

var keyPair = ECPair.fromWIF(
  'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1',
);
var privateKey = keyPair.privateKey;
var message = 'This is an example of a signed message.';

bitcoinMessage
  .signAsync(message, privateKey, keyPair.compressed)
  .then(signature => {
    console.log(toBase64(signature));
  });
// => 'H9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='

Sign a Bitcoin message asynchronously using SignerAsync interface

var keyPair = ECPair.fromWIF(
  'L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1',
);
var privateKey = keyPair.privateKey;
var message = 'This is an example of a signed message.';

var secp256k1 = require('tiny-secp256k1');
// Note that a Signer will also work
var signerAsync = {
  signRecoverable: (hash, extraData) =>
    Promise.resolve(secp256k1.signRecoverable(hash, privateKey, extraData)),
};
var signer = {
  signRecoverable: (hash, extraData) =>
    secp256k1.signRecoverable(hash, privateKey, extraData),
};

bitcoinMessage
  .signAsync(message, signerAsync, keyPair.compressed)
  .then(signature => {
    console.log(toBase64(signature));
  });
// => 'H9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='
bitcoinMessage
  .signAsync(message, signer, keyPair.compressed)
  .then(signature => {
    console.log(toBase64(signature));
  });
// => 'H9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='

verify(message, address, signature[, network.messagePrefix, checkSegwitAlways])

Verify a Bitcoin message

var address = '1F3sAm6ZtwLAUnj7d38pGFxtP3RVEvtsbV';

console.log(bitcoinMessage.verify(message, address, signature));
// => true

About Electrum segwit signature support

  • For Signing: Use the non-segwit compressed signing parameters for both segwit types (p2sh-p2wpkh and p2wpkh)
  • For Verifying: Pass the checkSegwitAlways argument as true. (messagePrefix should be set to null to default to Bitcoin messagePrefix)

LICENSE MIT

版本与发布记录

@bitcoinerlab/btcmessage 共发布 3 个版本,首次发布于 2026/02/19;过去 12 个月发布了 3 个版本。

版本发布日期距今
4.0.2latest2026/11/052 天前
4.0.12026/02/198 个月前
4.0.02026/02/198 个月前

关于 @bitcoinerlab/btcmessage 的常见问题

@bitcoinerlab/btcmessage 是什么?

Fork of bitcoinjs-message

如何安装 @bitcoinerlab/btcmessage?

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

@bitcoinerlab/btcmessage 支持 TypeScript 吗?

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

@bitcoinerlab/btcmessage 支持 ESM 还是 CommonJS?

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

@bitcoinerlab/btcmessage 需要什么版本的 Node.js?

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

@bitcoinerlab/btcmessage 可以商用吗?

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

@bitcoinerlab/btcmessage 的下载量有多少?

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

@bitcoinerlab/btcmessage 还在维护吗?

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