bitcore-lib-zcash/lib/transport/message.js

525 lines
12 KiB
JavaScript
Raw Normal View History

'use strict';
2014-12-09 13:02:27 -08:00
var Buffers = require('buffers');
var buffertools = require('buffertools');
var Put = require('bufferput');
2014-12-09 13:02:27 -08:00
var util = require('util');
var Block = require('../block');
2014-12-09 06:21:11 -08:00
var BufferReader = require('../encoding/bufferreader');
var BufferUtil = require('../util/buffer');
2014-12-09 13:02:27 -08:00
var Hash = require('../crypto/hash');
2014-12-09 06:21:11 -08:00
var Random = require('../crypto/random');
var CONNECTION_NONCE = Random.getPseudoRandomBuffer(8);
var PROTOCOL_VERSION = 70000;
2014-12-09 13:02:27 -08:00
/**
* Static helper for consuming a data buffer until the next message.
*
* @param{Network} network - the network object
* @param{Buffer} dataBuffer - the buffer to read from
* @returns{Message|undefined} A message or undefined if there is nothing to read.
*/
var parseMessage = function(network, dataBuffer) {
if (dataBuffer.length < 20) return;
// Search the next magic number
if (!discardUntilNextMessage(network, dataBuffer)) return;
var PAYLOAD_START = 16;
var payloadLen = (dataBuffer.get(PAYLOAD_START)) +
(dataBuffer.get(PAYLOAD_START + 1) << 8) +
(dataBuffer.get(PAYLOAD_START + 2) << 16) +
(dataBuffer.get(PAYLOAD_START + 3) << 24);
var messageLength = 24 + payloadLen;
if (dataBuffer.length < messageLength) return;
var command = dataBuffer.slice(4, 16).toString('ascii').replace(/\0+$/, '');
var payload = dataBuffer.slice(24, messageLength);
var checksum = dataBuffer.slice(20, 24);
var checksumConfirm = Hash.sha256sha256(payload).slice(0, 4);
if (buffertools.compare(checksumConfirm, checksum) !== 0) {
dataBuffer.skip(messageLength);
return;
}
dataBuffer.skip(messageLength);
return Message.buildMessage(command, payload);
};
module.exports.parseMessage = parseMessage;
/**
* Internal function that discards data until founds the next message.
*/
function discardUntilNextMessage(network, dataBuffer) {
var magicNumber = network.networkMagic;
var i = 0;
for (;;) {
// check if it's the beginning of a new message
var packageNumber = dataBuffer.slice(0, 4);
if (buffertools.compare(packageNumber, magicNumber) == 0) {
dataBuffer.skip(i);
return true;
}
// did we reach the end of the buffer?
if (i > (dataBuffer.length - 4)) {
dataBuffer.skip(i);
return false;
}
i++; // continue scanning
}
}
2014-12-09 13:02:27 -08:00
/**
* Abstract Message that knows how to parse and serialize itself.
* Concret subclases should implement {fromBuffer} and {getPayload} methods.
*/
function Message() {};
Message.COMMANDS = {};
Message.buildMessage = function(command, payload) {
try {
2014-12-09 13:02:27 -08:00
var CommandClass = Message.COMMANDS[command];
return new CommandClass().fromBuffer(payload);
} catch (err) {
2014-12-09 13:02:27 -08:00
console.log('Error while parsing message', err);
throw err;
}
2014-12-09 13:02:27 -08:00
};
/**
* Parse instance state from buffer.
*
* @param{Buffer} payload - the buffer to read from
* @returns{Message} The same message instance
*/
Message.prototype.fromBuffer = function(payload) {
return this;
};
/**
* Serialize the payload into a buffer.
*
* @returns{Buffer} the serialized payload
*/
Message.prototype.getPayload = function() {
return BufferUtil.EMPTY_BUFFER;
};
/**
* Serialize the message into a buffer.
*
* @returns{Buffer} the serialized message
*/
Message.prototype.serialize = function(network) {
var magic = network.networkMagic;
var commandBuf = new Buffer(this.command, 'ascii');
if (commandBuf.length > 12) throw 'Command name too long';
var payload = this.getPayload();
var checksum = Hash.sha256sha256(payload).slice(0, 4);
// -- HEADER --
var message = new Put();
message.put(magic);
message.put(commandBuf);
message.pad(12 - commandBuf.length); // zero-padded
message.word32le(payload.length);
message.put(checksum);
// -- BODY --
message.put(payload);
return message.buffer();
}
2014-12-09 13:02:27 -08:00
/**
* Version Message
*
* @param{string} subversion - version of the client
* @param{Buffer} nonce - a random 8 bytes buffer
*/
function Version(subversion, nonce) {
this.command = 'version';
2014-12-09 06:21:11 -08:00
this.version = PROTOCOL_VERSION;
this.subversion = subversion || '/BitcoinX:0.1/';
this.nonce = nonce || CONNECTION_NONCE;
2014-12-09 06:21:11 -08:00
};
2014-12-09 13:02:27 -08:00
util.inherits(Version, Message);
Version.prototype.fromBuffer = function(payload) {
var parser = new BufferReader(payload);
2014-12-09 13:02:27 -08:00
this.version = parser.readUInt32LE();
this.services = parser.readUInt64LEBN();
this.timestamp = parser.readUInt64LEBN();
this.addr_me = parser.read(26);
this.addr_you = parser.read(26);
this.nonce = parser.read(8);
this.subversion = parser.readVarintBuf();
this.start_height = parser.readUInt32LE();
return this;
2014-12-09 06:21:11 -08:00
};
2014-12-09 13:02:27 -08:00
Version.prototype.getPayload = function() {
var put = new Put();
2014-12-09 06:21:11 -08:00
put.word32le(this.version); // version
put.word64le(1); // services
put.word64le(Math.round(new Date().getTime() / 1000)); // timestamp
put.pad(26); // addr_me
put.pad(26); // addr_you
put.put(this.nonce);
put.varint(this.subversion.length);
put.put(new Buffer(this.subversion, 'ascii'));
put.word32le(0);
return put.buffer();
2014-12-09 06:21:11 -08:00
};
2014-12-09 13:02:27 -08:00
module.exports.Version = Message.COMMANDS['version'] = Version;
2014-12-09 13:02:27 -08:00
/**
* Inv Message
*
* @param{Array} inventory - reported elements
*/
function Inventory(inventory) {
this.command = 'inv';
this.inventory = inventory || [];
}
2014-12-09 13:02:27 -08:00
util.inherits(Inventory, Message);
Inventory.prototype.fromBuffer = function(payload) {
var parser = new BufferReader(payload);
var count = parser.readVarintNum();
for (var i = 0; i < count; i++) {
2014-12-09 13:02:27 -08:00
this.inventory.push({
type: parser.readUInt32LE(),
hash: parser.read(32)
});
}
2014-12-09 13:02:27 -08:00
return this;
2014-12-09 06:21:11 -08:00
};
2014-12-09 13:02:27 -08:00
Inventory.prototype.getPayload = function() {
var put = new Put();
put.varint(this.inventory.length);
this.inventory.forEach(function(value) {
value instanceof Block ? put.word32le(2) : put.word32le(1);
put.put(value.getHash());
});
return put.buffer();
2014-12-09 06:21:11 -08:00
};
2014-12-09 13:02:27 -08:00
module.exports.Inventory = Message.COMMANDS['inv'] = Inventory;
2014-12-09 13:02:27 -08:00
/**
* Getdata Message
*
* @param{Array} inventory - requested elements
*/
2014-12-09 06:21:11 -08:00
function GetData(inventory) {
this.command = 'getdata';
this.inventory = inventory || [];
}
util.inherits(GetData, Inventory);
module.exports.GetData = GetData;
2014-12-09 13:02:27 -08:00
/**
* Ping Message
*
* @param{Buffer} nonce - a random 8 bytes buffer
*/
function Ping(nonce) {
this.command = 'ping';
this.nonce = nonce || CONNECTION_NONCE;
}
2014-12-09 13:02:27 -08:00
util.inherits(Ping, Message);
Ping.prototype.fromBuffer = function(payload) {
2014-12-09 13:02:27 -08:00
this.nonce = new BufferReader(payload).read(8);
return this;
2014-12-09 06:21:11 -08:00
};
2014-12-09 13:02:27 -08:00
Ping.prototype.getPayload = function() {
return this.nonce;
2014-12-09 06:21:11 -08:00
};
2014-12-09 13:02:27 -08:00
module.exports.Ping = Message.COMMANDS['ping'] = Ping;
2014-12-09 13:02:27 -08:00
/**
* Pong Message
*
* @param{Buffer} nonce - a random 8 bytes buffer
*/
function Pong(nonce) {
this.command = 'pong';
this.nonce = nonce || CONNECTION_NONCE;
}
util.inherits(Pong, Ping);
module.exports.Pong = Pong;
2014-12-09 13:02:27 -08:00
/**
* Addr Message
*
* @param{Array} addresses - array of know addresses
*/
function Addresses(addresses) {
2014-12-09 06:21:11 -08:00
this.command = 'addr';
2014-12-09 13:02:27 -08:00
this.addresses = addresses || [];
2014-12-09 06:21:11 -08:00
}
2014-12-09 13:02:27 -08:00
util.inherits(Addresses, Message);
2014-12-09 06:21:11 -08:00
2014-12-09 13:02:27 -08:00
Addresses.prototype.fromBuffer = function(payload) {
2014-12-09 06:21:11 -08:00
var parser = new BufferReader(payload);
var addrCount = Math.min(parser.readVarintNum(), 1000);
2014-12-09 13:02:27 -08:00
this.addresses = [];
2014-12-09 06:21:11 -08:00
for (var i = 0; i < addrCount; i++) {
// TODO: Time actually depends on the version of the other peer (>=31402)
2014-12-09 13:02:27 -08:00
this.addresses.push({
2014-12-09 06:21:11 -08:00
time: parser.readUInt32LE(),
services: parser.readUInt64LEBN(),
ip: parser.read(16),
port: parser.readUInt16BE()
});
}
2014-12-09 13:02:27 -08:00
return this;
2014-12-09 06:21:11 -08:00
};
2014-12-09 13:02:27 -08:00
Addresses.prototype.getPayload = function() {
2014-12-09 06:21:11 -08:00
return BufferUtil.EMPTY_BUFFER; // TODO
};
2014-12-09 13:02:27 -08:00
module.exports.Addresses = Message.COMMANDS['addr'] = Addresses;
2014-12-09 13:02:27 -08:00
/**
* GetAddr Message
*
*/
2014-12-09 06:21:11 -08:00
function GetAddresses() {
this.command = 'getaddr';
}
2014-12-09 13:02:27 -08:00
util.inherits(GetAddresses, Message);
module.exports.GetAddresses = Message.COMMANDS['getaddr'] = GetAddresses;
2014-12-09 13:02:27 -08:00
/**
* Verack Message
*
*/
2014-12-09 06:21:11 -08:00
function VerAck() {
this.command = 'verack';
}
2014-12-09 13:02:27 -08:00
util.inherits(VerAck, Message);
module.exports.VerAck = Message.COMMANDS['verack'] = VerAck;
2014-12-09 06:21:11 -08:00
2014-12-09 13:02:27 -08:00
/**
* Reject Message
*
*/
2014-12-09 06:21:11 -08:00
function Reject() {
this.command = 'reject';
}
2014-12-09 13:02:27 -08:00
util.inherits(Reject, Message);
2014-12-09 06:21:11 -08:00
2014-12-09 13:02:27 -08:00
// TODO: Parse REJECT message
2014-12-09 06:21:11 -08:00
2014-12-09 13:02:27 -08:00
module.exports.Reject = Message.COMMANDS['reject'] = Reject;
2014-12-09 13:02:27 -08:00
/**
* Alert Message
*
*/
function Alert() {
this.command = 'alert';
}
2014-12-09 13:02:27 -08:00
util.inherits(Alert, Message);
Alert.prototype.fromBuffer = function() {
var parser = new BufferReader(payload);
2014-12-09 13:02:27 -08:00
this.payload = parser.readVarintBuf(); // TODO: Use current format
this.signature = parser.readVarintBuf();
return this;
2014-12-09 06:21:11 -08:00
};
2014-12-09 13:02:27 -08:00
Alert.prototype.getPayload = function() {
2014-12-09 06:21:11 -08:00
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
};
2014-12-09 13:02:27 -08:00
module.exports.Alert = Message.COMMANDS['alert'] = Alert;
2014-12-09 13:02:27 -08:00
/**
* Headers Message
*
* @param{Array} blockheaders - array of block headers
*/
2014-12-09 06:21:11 -08:00
function Headers(blockheaders) {
this.command = 'headers';
this.headers = blockheaders || [];
}
2014-12-09 13:02:27 -08:00
util.inherits(Headers, Message);
Headers.prototype.fromBuffer = function() {
2014-12-09 06:21:11 -08:00
var parser = new BufferReader(payload);
var count = parser.readVarintNum();
2014-12-09 13:02:27 -08:00
this.headers = [];
2014-12-09 06:21:11 -08:00
for (i = 0; i < count; i++) {
var header = Block().fromBufferReader(parser);
2014-12-09 13:02:27 -08:00
this.headers.push(header);
}
2014-12-09 06:21:11 -08:00
2014-12-09 13:02:27 -08:00
return this;
2014-12-09 06:21:11 -08:00
};
2014-12-09 13:02:27 -08:00
Headers.prototype.getPayload = function() {
2014-12-09 06:21:11 -08:00
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
};
2014-12-09 13:02:27 -08:00
module.exports.Headers = Message.COMMANDS['headers'] = Headers;
2014-12-09 13:02:27 -08:00
/**
* Block Message
*
* @param{Block} block
*/
2014-12-09 06:21:11 -08:00
function Block(block) {
this.command = 'block';
this.block = block;
}
2014-12-09 13:02:27 -08:00
util.inherits(Block, Message);
2014-12-09 06:21:11 -08:00
Block.prototype.fromBuffer = function() {
2014-12-09 06:21:11 -08:00
var parser = new BufferReader(payload);
2014-12-09 13:02:27 -08:00
this.block = Block().fromBufferReader(parser);
return this;
2014-12-09 06:21:11 -08:00
};
2014-12-09 13:02:27 -08:00
Block.prototype.getPayload = function() {
2014-12-09 06:21:11 -08:00
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
};
2014-12-09 13:02:27 -08:00
module.exports.Block = Message.COMMANDS['block'] = Block;
2014-12-09 13:02:27 -08:00
/**
* Tx Message
*
* @param{Transaction} transaction
*/
2014-12-09 06:21:11 -08:00
function Transaction(transaction) {
this.command = 'tx';
this.transaction = transaction;
}
2014-12-09 13:02:27 -08:00
util.inherits(Transaction, Message);
2014-12-09 06:21:11 -08:00
Transaction.prototype.fromBuffer = function() {
2014-12-09 06:21:11 -08:00
var parser = new BufferReader(payload);
2014-12-09 13:02:27 -08:00
this.transaction = Transaction().fromBufferReader(parser);
return this;
};
2014-12-09 13:02:27 -08:00
Transaction.prototype.getPayload = function() {
2014-12-09 06:21:11 -08:00
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
};
2014-12-09 13:02:27 -08:00
module.exports.Transaction = Message.COMMANDS['tx'] = Transaction;
2014-12-09 13:02:27 -08:00
/**
* Getblocks Message
*
* @param{Array} starts - array of buffers with the starting block hashes
* @param{Buffer} [stop] - hash of the last block
*/
2014-12-09 06:21:11 -08:00
function GetBlocks(starts, stop) {
this.command = 'getblocks';
this.version = PROTOCOL_VERSION;
this.starts = starts || [];
this.stop = stop || BufferUtil.NULL_HASH;
}
2014-12-09 13:02:27 -08:00
util.inherits(GetBlocks, Message);
2014-12-09 06:21:11 -08:00
GetBlocks.prototype.fromBuffer = function() {
var parser = new BufferReader(payload);
2014-12-09 13:02:27 -08:00
this.version = parser.readUInt32LE();
2014-12-09 06:21:11 -08:00
var startCount = Math.min(parser.readVarintNum(), 500);
2014-12-09 13:02:27 -08:00
this.starts = [];
2014-12-09 06:21:11 -08:00
for (var i = 0; i < startCount; i++) {
2014-12-09 13:02:27 -08:00
this.starts.push(parser.read(32));
2014-12-09 06:21:11 -08:00
}
2014-12-09 13:02:27 -08:00
this.stop = parser.read(32);
return this;
2014-12-09 06:21:11 -08:00
};
2014-12-09 13:02:27 -08:00
GetBlocks.prototype.getPayload = function() {
2014-12-09 06:21:11 -08:00
var put = new Put();
put.word32le(this.version);
put.varint(this.starts.length);
for (var i = 0; i < starts.length; i++) {
if (this.starts[i].length != 32) {
throw new Error('Invalid hash length');
}
put.put(this.starts[i]);
}
2014-12-09 06:21:11 -08:00
if (this.stop.length != 32) {
throw new Error('Invalid hash length');
}
2014-12-09 06:21:11 -08:00
put.put(this.stop);
return put.buffer();
};
2014-12-09 13:02:27 -08:00
module.exports.GetBlocks = Message.COMMANDS['getblocks'] = GetBlocks;
2014-12-09 13:02:27 -08:00
/**
* Getheaders Message
*
* @param{Array} starts - array of buffers with the starting block hashes
* @param{Buffer} [stop] - hash of the last block
*/
2014-12-09 06:21:11 -08:00
function GetHeaders(starts, stop) {
this.command = 'getheaders';
this.version = PROTOCOL_VERSION;
this.starts = starts || [];
this.stop = stop || BufferUtil.NULL_HASH;
}
util.inherits(GetHeaders, GetBlocks);
2014-12-09 13:02:27 -08:00
module.exports.GetHeaders = Message.COMMANDS['getheaders'] = GetHeaders;
// TODO: Remove this PATCH (yemel)
Buffers.prototype.skip = function (i) {
if (i == 0) return;
if (i == this.length) {
this.buffers = [];
this.length = 0;
return;
}
var pos = this.pos(i);
this.buffers = this.buffers.slice(pos.buf);
this.buffers[0] = new Buffer(this.buffers[0].slice(pos.offset));
this.length -= i;
};