• Stars
    star
    126
  • Rank 284,543 (Top 6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

bitcoinjs-message

NPM Package Build Status Dependency status

js-standard-style

Examples (Note about Electrum support at the bottom)

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

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()

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)

// 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='

Sign a Bitcoin message using a Signer interface.

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

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

var signature = bitcoinMessage.sign(message, signer, keyPair.compressed)
console.log(signature.toString('base64'))
// => '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 = bitcoin.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(signature.toString('base64'))
})
// => 'H9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='

Sign a Bitcoin message asynchronously using SignerAsync interface

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

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

bitcoinMessage.signAsync(message, signerAsync, keyPair.compressed).then(signature => {
  console.log(signature.toString('base64'))
})
// => 'H9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk='
bitcoinMessage.signAsync(message, signer, keyPair.compressed).then(signature => {
  console.log(signature.toString('base64'))
})
// => '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

More Repositories

1

bitcoinjs-lib

A javascript Bitcoin library for node.js and browsers.
TypeScript
5,592
star
2

bip39

JavaScript implementation of Bitcoin BIP39: Mnemonic code for generating deterministic keys
JavaScript
983
star
3

bip38

BIP38 is a standard process to encrypt Bitcoin and crypto currency private keys that is less susceptible to brute force attacks thus protecting the user.
JavaScript
206
star
4

coinselect

An unspent transaction output (UTXO) selection module for bitcoin.
JavaScript
178
star
5

bip32

A BIP32 compatible library.
JavaScript
151
star
6

bolt11

A library for encoding and decoding lightning network payment requests as defined in BOLT #11.
JavaScript
93
star
7

bip44-constants

This package provides BIP44 coin constants as found here: https://github.com/satoshilabs/slips/blob/master/slip-0044.md
JavaScript
87
star
8

bs58check

A straight forward implementation of base58check extending upon bs58.
JavaScript
85
star
9

bech32

Bech32 encoding / decoding
TypeScript
84
star
10

wif

Bitcoin Wallet Import Format JS encoding/decoding module
JavaScript
75
star
11

bip32-utils

A small set of utilities for use with BIP32 HD key nodes
JavaScript
74
star
12

fast-dat-parser

Superfast blockchain parser for stats
C++
73
star
13

tiny-secp256k1

A tiny secp256k1 native/JS wrapper
JavaScript
58
star
14

bip21

A BIP21 compatible URL encoding utility library
JavaScript
55
star
15

indexd

An external bitcoind index management service module
JavaScript
53
star
16

merkle-lib

A performance conscious library for merkle root and tree calculations.
JavaScript
29
star
17

bitcoinjs.org

HTML
20
star
18

bip32-wallet

A BIP32 Wallet backed by bitcoinjs-lib, lite on features but heavily tested.
JavaScript
19
star
19

regtest-client

A client library based off of the integration tests of bitcoinjs-lib. This requires a server component.
JavaScript
18
star
20

regtest-server

A regtest server for bitcoinjs-lib testing
JavaScript
18
star
21

bip174

A BIP174 compatible partial Transaction encoding library.
TypeScript
17
star
22

electrum-mnemonic

Electrum Mnemonics (electrum v2 and greater)
TypeScript
15
star
23

payjoin-client

A Payjoin Client Library in JS with TypeScript types
TypeScript
15
star
24

blkdat-stream

A blk*.dat streaming module, useful for parsing the Bitcoin blockchain
JavaScript
14
star
25

aezeed

A package for encoding, decoding, and generating mnemonics of the aezeed specification. (WIP)
TypeScript
13
star
26

bip66

Strict DER signatures
JavaScript
12
star
27

bitcoin-ops

bitcoin OP codes
JavaScript
11
star
28

ecpair

The ECPair module for bitcoinjs-lib
TypeScript
9
star
29

varuint-bitcoin

encode/decode number as bitcoin variable length integer https://en.bitcoin.it/wiki/Protocol_documentation#Variable_length_integer
JavaScript
6
star
30

bip69

Lexicographical Indexing of Transaction Inputs and Outputs
JavaScript
4
star
31

playground

Go nuts! PRs can be useful for discussion, and won't be rejected
JavaScript
3
star
32

bip65

A BIP65 absolute lock-time encoding library.
JavaScript
2
star
33

utxo

JavaScript
2
star
34

bip68

A BIP68 relative lock-time encoding library.
JavaScript
2
star
35

minimaldata

A module to check bitcoin policy: SCRIPT_VERIFY_MINIMALDATA
JavaScript
2
star
36

pushdata-bitcoin

encode/decode number as bitcoin pushdata integer
JavaScript
2
star
37

uint8array-tools

A set of tools for Uint8Array to aide in the move from Buffers.
JavaScript
1
star
38

tif

DEPRECATED: Bitcoin Transaction Interchange Format (TIF) decoding/encoding module
JavaScript
1
star