# bsv-p2p

> Communicate on the Bitcoin P2P network with minimal overhead or dependencies

Latest version **3.0.2** (published 2026-04-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install bsv-p2p
pnpm add bsv-p2p
yarn add bsv-p2p
bun add bsv-p2p
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.0.2 |
| Published | 2026-04-21 |
| First published | 2020-04-16 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 195 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 24 |
| Author | Kevin Johnson |
| Maintainers | kevinejohn |
| Keywords | bitcoin, peer to peer |

## Links

- npm: https://www.npmjs.com/package/bsv-p2p
- Repository: https://github.com/kevinejohn/bsv-p2p
- Homepage: https://github.com/kevinejohn/bsv-p2p#readme
- Issues: https://github.com/kevinejohn/bsv-p2p/issues
- npm.io page: https://npm.io/package/bsv-p2p

## Dependencies (1)

- [bsv-minimal](https://npm.io/package/bsv-minimal.md) ^3.0.1

## Recent versions

- 3.0.2 (latest) — 2026-04-21
- 3.0.1 — 2026-04-21
- 3.0.0 — 2026-04-21
- 2.0.1 — 2023-10-26
- 2.0.0 — 2023-10-26
- 1.2.4 — 2023-05-09
- 1.2.3 — 2023-05-09
- 1.2.2 — 2023-05-09
- 1.2.1 — 2023-05-09
- 1.2.0 — 2023-05-09
- 1.1.7 — 2022-11-30
- 1.1.6 — 2022-11-30
- 1.1.5 — 2022-11-08
- 1.1.4 — 2022-11-02
- 1.1.3 — 2022-11-02
- … 35 more at https://npm.io/package/bsv-p2p/versions

## README

# bsv-p2p

[![NPM Package](https://img.shields.io/npm/v/bsv-p2p.svg?style=flat-square)](https://www.npmjs.org/package/bsv-p2p)

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>

### Methods

`npm i bsv-p2p`

### Docs

- [View TypeScript documentation here](docs/README.md)

### Basic use

```js
const BitcoinP2P = require("bsv-p2p").default;

const node = "95.217.42.32"; // ipv4 or ipv6 address
const ticker = "BSV"; // Also works with BTC, BCH, XEC and other bitcoin network protocols
const peer = new BitcoinP2P({ node, ticker });

const fs = require("fs");
const path = require("path");
let writeStream;
let writeDir;

peer.on("block_chunk", ({ node, chunk, blockHash, finished, started, num }) => {
  // Save blocks to disk
  if (started) {
    writeDir = path.join(__dirname, `${blockHash.toString("hex")}.bin`); // Path of final block file
    writeStream = fs.createWriteStream(`${writeDir}.tmp`);
  }

  writeStream.write(chunk);

  if (finished) {
    writeStream.end();
    writeStream = null;
    fs.renameSync(`${writeDir}.tmp`, writeDir);
  }
});

peer.on("tx_mempool", ({ tx }) => {
  console.log(`tx ${tx.getHash().toString("hex")} seen in mempool`);
});

peer.on("tx_block", ({ header, txs, finished }) => {
  for (const { index, tx } of txs) {
    console.log(
      `tx ${tx.getHash().toString("hex")} in index ${index} of block ${header
        .getHash()
        .toString("hex")}`
    );
  }
  if (finished) console.log(`Finished block ${header.getHash().toString("hex")}`);
});

await peer.connect();
await peer.getBlock("<block hash>");
peer.fetchMempoolTxs((txids) => txids); // Return filtered txids to download mempool txs
peer.fetchNewBlocks((hashes) => hashes); // Return filtered block hashes to download new blocks
```

### Other methods

```js
const BitcoinP2P = require("bsv-p2p").default;

const node = "95.217.42.32"; // ipv4 or ipv6 address
const port = 8333;
const ticker = "BSV"; // Also works with BTC, BCH, XEC and other bitcoin network protocols
const segwit = false; // Set to true for BTC and other segwit coins
const validate = true; // Perform merkle root validation
const autoReconnect = true; // Attempt reconnect after disconnects
const disableExtmsg = false; // Disable extension messages (> 4GB payloads). Set to true if ticker is not BSV
const mempoolTxs = true; // Receiving mempool tx announcements
const DEBUG_LOG = false; // console.log detailed messages on what is happening
const peer = new BitcoinP2P({
  node,
  port,
  ticker,
  segwit,
  validate,
  autoReconnect,
  disableExtmsg,
  mempoolTxs,
  DEBUG_LOG,
});

peer.on("addr", ({ addrs }) => {
  // List of connected peers
  for (const addr of addrs) {
    console.log(addr);
  }
});
peer.on("block_hashes", ({ hashes }) => {
  // New block hashes announced
  for (const hash of hashes) {
    console.log(`New block ${hash.toString("hex")} from ${node}`);
  }
});
peer.on("block_chunk", ({ chunk, blockHash, finished, started, num }) => {
  // Only needed if you want to save the block chunks
});
peer.on("block", ({ blockHash }) => {
  // Block completed downloading
});
peer.on("tx_mempool", ({ tx }) => {
  // bsv-minimal tx object
});
peer.on(
  "tx_block",
  ({
    blockHash,
    header,
    started,
    finished,
    height,
    blockSize,
    txCount,
    txs,
  }) => {
    // txs array
  }
);
peer.on("disconnected", ({ disconnects }) => {
  // Disconnected from peer
});
peer.on("connected", () => {
  // Connected to peer
});
peer.on("version", ({ version }) => {
  // Received version message
});
peer.on("message", ({ command, payload }) => {
  // Received a message
});
peer.on("error_message", ({ error }) => {
  // Error processing message
});
peer.on("error_socket", ({ error }) => {
  // Socket error
});

await peer.connect(); // Resolves when connected
await peer.getHeaders({ from: ["<hex header>"], to: "<stop hash>" }); // Returns array of Headers
peer.getMempool(); // Request node for all mempool txs. Recommend not using. Nodes usually disconnect you.
await peer.ping(); // Returns Number. Te response time in milliseconds
await peer.getAddr(); // Request nodes connected peers list
await peer.getBlock("<block hash>"); // Hex string or 32 byte Buffer
await peer.broadcastTx(transaction); // bsv-minimal Transaction object
peer.getTxs([Buffer.from("<txid>", "hex")]); // Array of txid 32 byte Buffers
peer.fetchMempoolTxs((txids) => txids); // Return filtered txids to download mempool txs
peer.fetchNewBlocks((hashes) => hashes); // Return filtered block hashes to download new blocks
peer.disconnect();
```

### Tests

`npm run test`

---
_Source: https://npm.io/package/bsv-p2p · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
