Giter Site home page Giter Site logo

keppel / blockchain-spv Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mappum/blockchain-spv

0.0 2.0 0.0 400 KB

:black_medium_small_square::black_medium_small_square::black_medium_small_square: Stores blockchain headers and verifies transactions with SPV

JavaScript 97.27% Shell 2.73%

blockchain-spv's Introduction

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
  • network (default: 'mainnet') which Bitcoin network settings to use. can be 'mainnet', 'testnet', or 'regtest'
  • 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.