From c43430f4c03c292c719e7801e425d887cbdf7464 Mon Sep 17 00:00:00 2001 From: junderw Date: Fri, 29 Jan 2021 08:45:26 +0900 Subject: [PATCH] Update README and CHANGELOG --- CHANGELOG.md | 26 ++++++++++++++++++++++++++ README.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00a5ffd..e7c0110 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,29 @@ +# 2.2.0 +__added__ +- Signer and SignerAsync interfaces +- sign functions can accept Signer | SignerAsync interfaces in place of privateKey +- Added an async signAsync function (needed if you use SignerAsync interface) that returns a promise. + +# 2.1.4 +__fixed__ +- Fixed TypeScript types + +# 2.1.3 +__added__ +- TypeScript types + +# 2.1.2 +__added__ +- Support for Segwit signatures compatible with Electrum. (See README) + +# 2.1.1 +__fixed__ +- Fix UTF8 handling of message. + +# 2.1.0 +__added__ +- Segwit support for P2WPKH and P2SH-P2WPKH addresses. This is based on Trezor implementation. + # 2.0.0 __breaking__ - `messagePrefix` is now the last parameter for the `sign`, `verify` and `magicHash` functions diff --git a/README.md b/README.md index dd1c3b2..ab9b03b 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,57 @@ console.log(signature.toString('base64')) // => 'J9L5yLFjti0QTHhPyFrZCT1V/MMnBtXKmoiKDZ78NDBjERki6ZTQZdSMCtkgoNmp17By9ItJr8o7ChX0XxY91nk=' ``` +Sign a Bitcoin message using a Signer interface. +``` javascript +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 +``` javascript +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 +``` javascript +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