Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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.