|
|
@ -1,4 +1,5 @@ |
|
|
|
'use strict'; |
|
|
|
/* jshint curly: false */ |
|
|
|
|
|
|
|
var Buffers = require('buffers'); |
|
|
|
var Put = require('bufferput'); |
|
|
@ -85,7 +86,7 @@ function discardUntilNextMessage(network, dataBuffer) { |
|
|
|
* Abstract Message that knows how to parse and serialize itself. |
|
|
|
* Concret subclases should implement {fromBuffer} and {getPayload} methods. |
|
|
|
*/ |
|
|
|
function Message() {}; |
|
|
|
function Message() {} |
|
|
|
|
|
|
|
Message.COMMANDS = {}; |
|
|
|
|
|
|
@ -95,7 +96,6 @@ Message.buildMessage = function(command, payload) { |
|
|
|
return new CommandClass().fromBuffer(payload); |
|
|
|
} catch (err) { |
|
|
|
console.log('Error while parsing message', err); |
|
|
|
throw err; |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
@ -106,6 +106,7 @@ Message.buildMessage = function(command, payload) { |
|
|
|
* @returns{Message} The same message instance |
|
|
|
*/ |
|
|
|
Message.prototype.fromBuffer = function(payload) { |
|
|
|
/* jshint unused: false */ |
|
|
|
return this; |
|
|
|
}; |
|
|
|
|
|
|
@ -188,7 +189,7 @@ Version.prototype.getPayload = function() { |
|
|
|
return put.buffer(); |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports.Version = Message.COMMANDS['version'] = Version; |
|
|
|
module.exports.Version = Message.COMMANDS.version = Version; |
|
|
|
|
|
|
|
/** |
|
|
|
* Inv Message |
|
|
@ -226,7 +227,7 @@ Inventory.prototype.getPayload = function() { |
|
|
|
return put.buffer(); |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports.Inventory = Message.COMMANDS['inv'] = Inventory; |
|
|
|
module.exports.Inventory = Message.COMMANDS.inv = Inventory; |
|
|
|
|
|
|
|
/** |
|
|
|
* Getdata Message |
|
|
@ -261,7 +262,7 @@ Ping.prototype.getPayload = function() { |
|
|
|
return this.nonce; |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports.Ping = Message.COMMANDS['ping'] = Ping; |
|
|
|
module.exports.Ping = Message.COMMANDS.ping = Ping; |
|
|
|
|
|
|
|
/** |
|
|
|
* Pong Message |
|
|
@ -274,7 +275,7 @@ function Pong(nonce) { |
|
|
|
} |
|
|
|
|
|
|
|
util.inherits(Pong, Ping); |
|
|
|
module.exports.Pong = Message.COMMANDS['pong'] = Pong; |
|
|
|
module.exports.Pong = Message.COMMANDS.pong = Pong; |
|
|
|
|
|
|
|
/** |
|
|
|
* Addr Message |
|
|
@ -319,7 +320,7 @@ Addresses.prototype.getPayload = function() { |
|
|
|
return put.buffer(); |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports.Addresses = Message.COMMANDS['addr'] = Addresses; |
|
|
|
module.exports.Addresses = Message.COMMANDS.addr = Addresses; |
|
|
|
|
|
|
|
/** |
|
|
|
* GetAddr Message |
|
|
@ -330,7 +331,7 @@ function GetAddresses() { |
|
|
|
} |
|
|
|
|
|
|
|
util.inherits(GetAddresses, Message); |
|
|
|
module.exports.GetAddresses = Message.COMMANDS['getaddr'] = GetAddresses; |
|
|
|
module.exports.GetAddresses = Message.COMMANDS.getaddr = GetAddresses; |
|
|
|
|
|
|
|
/** |
|
|
|
* Verack Message |
|
|
@ -341,7 +342,7 @@ function VerAck() { |
|
|
|
} |
|
|
|
|
|
|
|
util.inherits(VerAck, Message); |
|
|
|
module.exports.VerAck = Message.COMMANDS['verack'] = VerAck; |
|
|
|
module.exports.VerAck = Message.COMMANDS.verack = VerAck; |
|
|
|
|
|
|
|
/** |
|
|
|
* Reject Message |
|
|
@ -354,7 +355,7 @@ util.inherits(Reject, Message); |
|
|
|
|
|
|
|
// TODO: Parse REJECT message
|
|
|
|
|
|
|
|
module.exports.Reject = Message.COMMANDS['reject'] = Reject; |
|
|
|
module.exports.Reject = Message.COMMANDS.reject = Reject; |
|
|
|
|
|
|
|
/** |
|
|
|
* Alert Message |
|
|
@ -385,7 +386,7 @@ Alert.prototype.getPayload = function() { |
|
|
|
return put.buffer(); |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports.Alert = Message.COMMANDS['alert'] = Alert; |
|
|
|
module.exports.Alert = Message.COMMANDS.alert = Alert; |
|
|
|
|
|
|
|
/** |
|
|
|
* Headers Message |
|
|
@ -423,7 +424,7 @@ Headers.prototype.getPayload = function() { |
|
|
|
return put.buffer(); |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports.Headers = Message.COMMANDS['headers'] = Headers; |
|
|
|
module.exports.Headers = Message.COMMANDS.headers = Headers; |
|
|
|
|
|
|
|
/** |
|
|
|
* Block Message |
|
|
@ -445,7 +446,7 @@ Block.prototype.getPayload = function() { |
|
|
|
return this.block.toBuffer(); |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports.Block = Message.COMMANDS['block'] = Block; |
|
|
|
module.exports.Block = Message.COMMANDS.block = Block; |
|
|
|
|
|
|
|
/** |
|
|
|
* Tx Message |
|
|
@ -467,7 +468,7 @@ Transaction.prototype.getPayload = function() { |
|
|
|
return this.transaction.toBuffer(); |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports.Transaction = Message.COMMANDS['tx'] = Transaction; |
|
|
|
module.exports.Transaction = Message.COMMANDS.tx = Transaction; |
|
|
|
|
|
|
|
/** |
|
|
|
* Getblocks Message |
|
|
@ -503,13 +504,13 @@ GetBlocks.prototype.getPayload = function() { |
|
|
|
put.varint(this.starts.length); |
|
|
|
|
|
|
|
for (var i = 0; i < this.starts.length; i++) { |
|
|
|
if (this.starts[i].length != 32) { |
|
|
|
if (this.starts[i].length !== 32) { |
|
|
|
throw new Error('Invalid hash length'); |
|
|
|
} |
|
|
|
put.put(this.starts[i]); |
|
|
|
} |
|
|
|
|
|
|
|
if (this.stop.length != 32) { |
|
|
|
if (this.stop.length !== 32) { |
|
|
|
throw new Error('Invalid hash length'); |
|
|
|
} |
|
|
|
put.put(this.stop); |
|
|
@ -517,7 +518,7 @@ GetBlocks.prototype.getPayload = function() { |
|
|
|
return put.buffer(); |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports.GetBlocks = Message.COMMANDS['getblocks'] = GetBlocks; |
|
|
|
module.exports.GetBlocks = Message.COMMANDS.getblocks = GetBlocks; |
|
|
|
|
|
|
|
/** |
|
|
|
* Getheaders Message |
|
|
@ -533,14 +534,14 @@ function GetHeaders(starts, stop) { |
|
|
|
} |
|
|
|
|
|
|
|
util.inherits(GetHeaders, GetBlocks); |
|
|
|
module.exports.GetHeaders = Message.COMMANDS['getheaders'] = GetHeaders; |
|
|
|
module.exports.GetHeaders = Message.COMMANDS.getheaders = GetHeaders; |
|
|
|
|
|
|
|
|
|
|
|
// TODO: Remove this PATCH (yemel)
|
|
|
|
Buffers.prototype.skip = function (i) { |
|
|
|
if (i == 0) return; |
|
|
|
if (i === 0) return; |
|
|
|
|
|
|
|
if (i == this.length) { |
|
|
|
if (i === this.length) { |
|
|
|
this.buffers = []; |
|
|
|
this.length = 0; |
|
|
|
return; |
|
|
|