Giter Club home page Giter Club logo

bsv-p2p-1's Introduction

bsv-p2p

NPM Package

Communicate on the Bitcoin P2P network with minimal overhead or dependencies

Built to follow the protocol definition here: https://en.bitcoin.it/wiki/Protocol_documentation

Use

npm install --save bsv-p2p

const BitcoinP2P = require('bsv-p2p')

const nodes = [`seed.bitcoinsv.io`, `seed.cascharia.com`, `seed.satoshisvision.network`]
const stream = true // Parse txs while block is downloading. No block size memory constraints
const validate = true // Perform merkle root validation. Disable to save processing time
const autoReconnect = true // Attempt reconnect after disconnects
const DEBUG_LOG = false // console.log detailed messages on what is happening
const peer = new BitcoinP2P({ nodes, stream, validate, autoReconnect, DEBUG_LOG })

peer.on('addr', ({ node, addr }) => {
    // List of connected peers
})
peer.on('block_hashes', ({ node, blocks }) => {
    // New block hashes announced
})
peer.on('block_chunk', ({ node, chunk, blockHash, finished, started, num }) => {
    // Only needed if you want to save the block chunks
})
peer.on('block', ({ node, block }) => {
    // Only called if `stream = false`
})
peer.on('transactions', ({ node, header, finished, transactions }) => {
    // `header` if transaction is confirmed in a block. Otherwise it is a mempool tx
    // `finished` if these are the last transactions in a block
    for (const [index, transaction] of transactions) {
        // Filter and store transaction information here
    }
})
peer.on('disconnected', ({ node, disconnects }) => {
    // Disconnected from peer
})
peer.on('connected', ({ node }) => {
    // Connected to peer
})
peer.on('version', ({ node, version }) => {
    // Both nodes sent and acknowledged version messages
})
peer.on('message', ({ node, command, payload }) => {
    // Received a message
})
peer.on('error_message', ({ node, error }) => {
    // Error processing message
})
peer.on('error_socket', ({ node, error }) => {
    // Socket error
})

await peer.connect()
await peer.getHeaders({ from: [<hashes>...], to: <stop hash> })
peer.isConnected()
peer.getMempool()
await peer.ping()
peer.getAddr()
await peer.getBlock(<block hash>)
await peer.broadcastTx(<tx buffer>)
peer.getTxs([<txid>...])
peer.getBlocks([<block hash>...])
peer.listenForTxs()
peer.listenForBlocks()
peer.disconnect()

Tests

TODO

bsv-p2p-1's People

Contributors

kevinejohn avatar

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.