# bitcoinjs-message

> bitcoinjs-message

Latest version **2.2.0** (published 2021-01-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install bitcoinjs-message
pnpm add bitcoinjs-message
yarn add bitcoinjs-message
bun add bitcoinjs-message
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2021-01-28 |
| First published | 2015-09-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=0.10 |
| Dependencies | 6 |
| Unpacked size | 13.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 134 |
| Maintainers | fanatid, jprichardson, junderw |
| Keywords | bitcoinjs-message, bitcoin |

## Links

- npm: https://www.npmjs.com/package/bitcoinjs-message
- Repository: https://github.com/bitcoinjs/bitcoinjs-message
- Issues: https://github.com/bitcoinjs/bitcoinjs-message/issues
- npm.io page: https://npm.io/package/bitcoinjs-message

## Dependencies (6)

- [bech32](https://npm.io/package/bech32.md) ^1.1.3
- [bs58check](https://npm.io/package/bs58check.md) ^2.1.2
- [secp256k1](https://npm.io/package/secp256k1.md) ^3.0.1
- [create-hash](https://npm.io/package/create-hash.md) ^1.1.2
- [buffer-equals](https://npm.io/package/buffer-equals.md) ^1.0.3
- [varuint-bitcoin](https://npm.io/package/varuint-bitcoin.md) ^1.0.1

## Recent versions

- 2.2.0 (latest) — 2021-01-28
- 2.1.4 — 2020-12-23
- 2.1.3 — 2020-09-04
- 2.1.2 — 2020-09-04
- 2.1.1 — 2020-06-17
- 2.1.0 — 2019-06-16
- 2.0.0 — 2017-09-30
- 1.0.1 — 2016-10-08
- 1.0.0 — 2015-09-08

## README

# bitcoinjs-message
[![NPM Package](https://img.shields.io/npm/v/bitcoinjs-message.svg?style=flat-square)](https://www.npmjs.org/package/bitcoinjs-message)
[![Build Status](https://img.shields.io/travis/bitcoinjs/bitcoinjs-message.svg?branch=master&style=flat-square)](https://travis-ci.org/bitcoinjs/bitcoinjs-message)
[![Dependency status](https://img.shields.io/david/bitcoinjs/bitcoinjs-message.svg?style=flat-square)](https://david-dm.org/bitcoinjs/bitcoinjs-message#info=dependencies)

[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)

## Examples (Note about Electrum support at the bottom)

``` javascript
var bitcoin = require('bitcoinjs-lib') // v4.x.x
var bitcoinMessage = require('bitcoinjs-message')
```

> 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
``` javascript
var keyPair = bitcoin.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(signature.toString('base64'))
// => 'H9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='
```

To produce non-deterministic signatures you can pass an extra option to sign()
``` javascript
var { randomBytes } = require('crypto')
var keyPair = bitcoin.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(signature.toString('base64'))
// => different (but valid) signature each time
```

Sign a Bitcoin message (with segwit addresses)
``` javascript
// P2SH(P2WPKH) address 'p2sh(p2wpkh)'
var signature = bitcoinMessage.sign(message, privateKey, keyPair.compressed, { segwitType: 'p2sh(p2wpkh)' })
console.log(signature.toString('base64'))
// => 'I9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='

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

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

Verify a Bitcoin message
``` javascript
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](LICENSE)

---
_Source: https://npm.io/package/bitcoinjs-message · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
