Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mappum/bitcoin-merkle-proof
:evergreen_tree: Build and verify Bitcoin Merkle proofs
https://github.com/mappum/bitcoin-merkle-proof
Last synced: 25 days ago
JSON representation
:evergreen_tree: Build and verify Bitcoin Merkle proofs
- Host: GitHub
- URL: https://github.com/mappum/bitcoin-merkle-proof
- Owner: mappum
- Created: 2016-01-27T21:20:27.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-05-11T13:23:43.000Z (over 7 years ago)
- Last Synced: 2024-10-04T01:47:21.376Z (2 months ago)
- Language: JavaScript
- Homepage:
- Size: 35.2 KB
- Stars: 12
- Watchers: 5
- Forks: 6
- Open Issues: 30
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# bitcoin-merkle-proof
[![npm version](https://img.shields.io/npm/v/bitcoin-merkle-proof.svg)](https://www.npmjs.com/package/bitcoin-merkle-proof)
[![Build Status](https://travis-ci.org/mappum/bitcoin-merkle-proof.svg?branch=master)](https://travis-ci.org/mappum/bitcoin-merkle-proof)
[![Dependency Status](https://david-dm.org/mappum/bitcoin-merkle-proof.svg)](https://david-dm.org/mappum/bitcoin-merkle-proof)**Verify Bitcoin Merkle trees**
Bitcoin [BIP37](https://github.com/bitcoin/bips/blob/master/bip-0037.mediawiki) adds support for `merkleblock` messages, which allow clients to download blocks that only include transactions relevant to them. The transactions are selected via a Bloom Filter.
This module creates and verifies the Merkle proofs in a `merkleblock` message, and lists the included transactions which match the filter.
## Usage
`npm install bitcoin-merkle-proof`
```js
var bmp = require('bitcoin-merkle-proof')// build merkle proof object (block #681135 in testnet)
var merkleProof = bmp.build({
hashes: [
new Buffer('52a893ef120d5e24aa38604ead9ada6628eea417df6d6096ef0dd7b73a89c0e9', 'hex'),
new Buffer('a76a1e1bffbbb254bd897e379298549eb8ff4aa57a4bb4c06637b36d76833207', 'hex'),
new Buffer('056b4e64697677788744a8ad23cc407cbc1c357ff889d9975edd431fb779466f', 'hex'),
new Buffer('3c51bfb4f9cdd2b8e3a5c47cb1b3bdbc8879a1c1b238d4123dcb572a00b2b80e', 'hex'),
new Buffer('d6d1f9ca0a4017050379a82ecccb050cf4218f2180087e9592110972a71e375c', 'hex')
],
include: [
new Buffer('3c51bfb4f9cdd2b8e3a5c47cb1b3bdbc8879a1c1b238d4123dcb572a00b2b80e', 'hex'),
new Buffer('d6d1f9ca0a4017050379a82ecccb050cf4218f2180087e9592110972a71e375c', 'hex')
]
})
// { flags: [ 235, 1 ],
// hashes:
// [ ,
// ,
// ,
// ],
// numTransactions: 5,
// merkleRoot: }// verify proof and return matched tx hashes
var hashes = bmp.verify(merkleProof)
console.log('Matched transactions: ', hashes)
```##### `var merkleProof = bmp.build(block)`
Construct proof object for transactions. Proof object:
```js
{
flags: number[],
hashes: Buffer[],
numTransactions: number,
merkleRoot: Buffer
}
```##### `var hashes = bmp.verify(merkleProof)`
Verifies a Merkle proof object. An error will be thrown if the tree is not valid or does not match the expected Merkle root. Returns an array of txids (as `Buffer`s) which matched the Bloom filter.