npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

blockchain-spv

v3.3.0

Published

Stores blockchain headers and verifies transactions with SPV

Downloads

44

Readme

blockchain-spv

npm version Build Status Dependency Status

SPV Bitcoin blockchain verifier

Usage

npm install blockchain-spv

let Blockchain = require('blockchain-spv')

let bitcoinGenesis = {
  height: 0,
  version: 1,
  prevHash: Buffer.alloc(32),
  merkleRoot: Buffer.from('4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b', 'hex').reverse(),
  timestamp: 1231006505,
  bits: 0x1d00ffff,
  nonce: 2083236893
}

let chain = new Blockchain({
  start: bitcoinGenesis
})

// add arrays of headers, throws if invalid
chain.add(header)

Blockchain stores and verifies block headers, and does SPV (light client) verification. It is compatible with Bitcoin and Bitcoin-derived blockchains.


new Blockchain(opts)

Creates an SPV Blockchain which stores and verifies block headers.

opts can contain:

  • start - a header to be used as the starting point (e.g. the genesis or a checkpoint)
  • store (optional) an array where the verified chain of headers should be stored
  • indexed (default: false) whether or not to index headers by their hash, enabling the getByHash method
  • maxTarget (default: Bitcoin maxTarget) a maximum difficulty target value as a 32-byte Buffer, should only be changed to make mining easier for writing tests

chain.add(headers, now = Date.now())

Adds block headers to the chain. headers should be an array of contiguous, ascending block headers. now is an optional argument that should be the current time in milliseconds. The headers will be verified (checked to make sure the expected amount of work was done, the difficulty was correct, etc.), then added to the store if valid. An error will be thrown if there is a validation error.

The headers can contain a reorg (e.g. they don't connect to chain's current tip).


chain.getByHeight(height)

Gets a header in the chain with height height. If the header isn't found, an error is thrown. This runs in O(1).


chain.getByHash(hash)

Gets a header in the chain with hash hash (either a Buffer or a hex string). If the header isn't found or indexing was not enabled (see the Blockchain constructor), an error is thrown. This runs in O(1).


chain.height()

Returns the height of the highest block added to the chain.