Security News
NVD Backlog Tops 20,000 CVEs Awaiting Analysis as NIST Prepares System Updates
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
bitcoin-p2p
Advanced tools
This is a client library for the Bitcoin P2P network, written for Node.js, using MongoDB as its back end.
The official client contains the node, wallet, GUI and miner. This library only contains the node, i.e. the P2P part of Bitcoin. Its intended use is as a server component to give lighter clients access to the data in the block chain (in real-time.)
Please refer to the wiki for detailed installation instructions.
This one-liner will install the latest version straight from NPM:
# Install node-bitcoin-p2p globally
npm install bitcoin-p2p -g
If you run into problems, please take a look at the "Troubleshooting" section below or go to the Issues tab to open a new ticket.
When upgrading node-bitcoin-p2p it is a good idea to reset its database:
mongo bitcoin --eval "db.dropDatabase()"
This won't be necessary once node-bitcoin-p2p is more stable, but for now new versions often break database compatibility and since it only takes about ten minutes to regenerate it makes sense to just reset it.
Several examples on how to start up the library are provided in the
examples/
folder. To run an example simply call it with node:
node examples/simple.js
It's highly recommended that you run bitcoind in front of bitcoin-p2p at this time (simply have bitcoind running before launching bitcoin-p2p). Forward-facing bitcoin-p2p nodes have not been well tested and may contain bugs that compromise its security.
The most basic way to start node-bitcoin-p2p in your own code goes like this:
var Bitcoin = require('bitcoin-p2p');
node = new Bitcoin.Node();
node.start();
All the other examples presuppose you've already started a node using this code.
Once the node is running it will automatically connect to the Bitcoin network and begin downloading blocks. There are two major ways to get information from the library: Events and Storage.
Get a reference to the BlockChain object to start listening to block chain changes:
var chain = node.getBlockChain();
// Log each block as it's added to the block chain
chain.addListener('blockSave', function (e) {
console.log(e.block);
});
BlockChain emits the following events:
blockAdd
- Triggered right before a block is saved to storage
block
The Block object for the block in questiontxs
The transactions attached to the blockchain
The BlockChain objectblockSave
- Triggered right after a block is saved to storage
block
The Block object for the block in questiontxs
The transactions attached to the blockchain
The BlockChain objectblockRevoke
- Triggered as the main chain is rolled back due to
a split (warning: block chain reorg is still buggy)
block
The Block object for the block in questiontxs
The transactions attached to the blockchain
The BlockChain objecttxAdd
- Triggered right before a transaction is saved to storage
block
Containing Block objectindex
The index of the transaction in questiontx
The Transaction objectchain
The BlockChain objecttxSave
- Triggered right after a transaction is saved to storage
block
Containing Block objectindex
The index of the transaction in questiontx
The Transaction objectchain
The BlockChain objecttxRevoke
- Triggered when a confirmed transaction is reverted as
the containing block is no longer in the main chain (warning: block
chain reorg is still buggy)
block
Containing Block objectindex
The index of the transaction in questiontx
The Transaction objectchain
The BlockChain objectTransactionStore emits the following events:
txNotify
- A transaction was added to the memory pool
tx
The Transaction objectstore
The TransactionStore objecttxCancel
- A transaction was removed from the memory pool
(because it was confirmed or because a conflicting transaction was
confirmed)
tx
The Transaction objecttxHash
The transaction's hash in base64store
The TransactionStore objectIf the setting feature.liveAccounting
is enabled, you can also
listen to txNotify:[pubKeyHash as base64]
and txCancel:[pubKeyHash as base64]
to get events for a specific address only.
node-bitcoin-p2p
uses the Mongoose ORM layer. You can find the
schemas for the database objects in the source code under lib/schema/.
All the models are instantiated by the Storage
class, so all you
need to do is get a reference to that from the Bitcoin Node
and
you're good to go:
var storage = node.getStorage();
storage.Transaction.findOne({hash: hash}, function (err, tx) {
// In real code, you'd handle the error of course
if (err) return;
storage.Block.findOne({_id: tx.block}, function (err, block) {
if (err) return;
// Do something fancy here...
});
});
There are also some convenience functions you can use:
var chain = node.getBlockChain();
chain.getBlockByHash(hash, function (err, block) {
if (err) return;
// Do something with the Block
console.log(block);
});
node-bitcoin-p2p
logs using the winston library. Currently, it
defaults to logging anything on the debug
log level and higher. Here
are the available log levels:
netdbg
- Networking events (sending/receiving messages)bchdbg
- Block chain events (adding blocks)rpcdbg
- JSON-RPC API events (requests/responses)scrdbg
- Script interpreter events (custom scripts, errors)debug
- Other verbose logginginfo
- General information and status messageswarn
- Something rare happened (e.g. strange pubKeyScript)error
- Something bad happenedThe XXXdbg levels can be enabled individually by editing lib/logger.js.
To run the test, please install Vows and run the following command:
vows test/* --spec
The library is currently alpha quality. Here are some things it currently lacks:
On top of that, it could use a lot more documentation, test cases and general bug fixing across the board.
You can find more information on the Issues tab on Github.
If you see this error:
Error: Cannot find module '../build-cc/default/native'
This happens when the native components of node-bitcoin-p2p are not compiled yet.
Make sure you have libgmp3-dev
installed, then go to the
node-bitcoin-p2p folder and run:
node-waf configure build
node-bitcoin-p2p - Node.js Bitcoin client
Copyright (c) 2011 Stefan Thomas justmoon@members.fsf.org.
Native extensions are
Copyright (c) 2011 Andrew Schaaf andrew@andrewschaaf.com
Parts of this software are based on BitcoinJ
Copyright (c) 2011 Google Inc.
FAQs
Implementation of Bitcoin's peer-to-peer layer for Node.js
The npm package bitcoin-p2p receives a total of 0 weekly downloads. As such, bitcoin-p2p popularity was classified as not popular.
We found that bitcoin-p2p demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
NVD’s backlog surpasses 20,000 CVEs as analysis slows and NIST announces new system updates to address ongoing delays.
Security News
Research
A malicious npm package disguised as a WhatsApp client is exploiting authentication flows with a remote kill switch to exfiltrate data and destroy files.
Security News
PyPI now supports digital attestations, enhancing security and trust by allowing package maintainers to verify the authenticity of Python packages.