SPV Bitcoin blockchain verifier
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.
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 storednetwork
(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 thegetByHash
methodmaxTarget
(default: Bitcoin maxTarget) a maximum difficulty target value as a 32-byteBuffer
, should only be changed to make mining easier for writing tests
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).
Gets a header in the chain with height height
. If the header isn't found, an error is thrown. This runs in O(1)
.
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)
.
Returns the height of the highest block added to the chain.