bitcoin-protocol
Bitcoin network protocol streams
This module encodes and decodes low-level network protocol data using streams.
Usage
npm install bitcoin-protocol
var net = require('net')
var bp = require('bitcoin-protocol')
var decoder = bp.createDecodeStream()
decoder.on('data', function (message) { console.log(message) })
var encoder = bp.createEncodeStream()
var socket = net.connect(8333, '127.0.0.1')
socket.pipe(decoder)
encoder.pipe(socket)
encoder.write({
magic: 0xd9b4bef9,
command: 'ping',
payload: {
nonce: new Buffer('0123456789abcdef', 'hex')
}
})
Methods
createDecodeStream([opts])
Creates a stream which parses raw network bytes written to it and outputs message objects.
Opts may contain:
{
magic: Number
}
createEncodeStream([opts])
Creates a stream which encodes message objects to raw network bytes.
Opts may contain:
{
magic: Number
}
Format
Decoder
Emitted by the decoder:
{
magic: Number,
command: String,
length: Number,
checksum: Buffer,
payload: Object
}
Encoder
Written to the encoder:
{
magic: Number,
command: String,
payload: Object
}
Payload Reference
The formats for the objects used as message payloads for the various commands are as follows. See the wiki for more information about these messages.
version
{
version: Number,
services: Buffer,
timestamp: Number,
receiverAddress: {
services: Buffer,
address: String,
port: Number
},
senderAddress: {
services: Buffer,
address: String,
port: Number
},
nonce: Buffer,
userAgent: String,
startHeight: Number,
relay: Boolean
}
addr
[
{
time: Number,
services: Buffer,
address: String,
port: Number
},
...
]
inv
, getdata
, notfound
[
{
type: Number,
hash: Buffer
},
...
]
{
version: Number,
locator: [
Buffer
],
hashStop: Buffer
}
tx
{
version: Number,
ins: [
{
hash: Buffer,
index: Number,
script: Buffer,
sequence: Number
},
...
],
outs: [
{
value: BN,
script: Buffer
},
...
],
locktime: Number
}
block
{
header: {
version: Number,
prevHash: Buffer,
merkleRoot: Buffer,
timestamp: Number,
bits: Number,
nonce: Number,
},
transactions: [
{},
...
]
}
[
{
header: {
version: Number,
prevHash: Buffer,
merkleRoot: Buffer,
timestamp: Number,
bits: Number,
nonce: Number,
},
nTransactions: Number
},
...
]
ping
, pong
{
nonce: Buffer
}
reject
{
message: String,
ccode: Number,
reason: String,
data: Buffer
}
filterload
{
data: Buffer,
nHashFuncs: Number,
nTweak: Number,
nFlags: Number
}
filteradd
{
data: Buffer
}
merkleblock
{
header: {
version: Number,
prevHash: Buffer,
merkleRoot: Buffer,
timestamp: Number,
bits: Number,
nonce: Number
},
numTransactions: Number,
hashes: [
Buffer
],
flags: Buffer
}
alert
{
payload: Buffer,
signature: Buffer
}