Add inheritance to messages and expose them as a module
This commit is contained in:
parent
68f50b2f61
commit
5dc124a438
|
@ -25,7 +25,7 @@ module.exports.buildMessage = function(command, payload) {
|
||||||
var Message = MESSAGES[command];
|
var Message = MESSAGES[command];
|
||||||
try {
|
try {
|
||||||
console.log('Message Class', Message);
|
console.log('Message Class', Message);
|
||||||
return Message.fromBuffer(payload);
|
return new Message().fromBuffer(payload);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log('Error while parrsing message', command);
|
console.log('Error while parrsing message', command);
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
@ -40,7 +40,7 @@ function Version(subversion, nonce) {
|
||||||
this.nonce = nonce || CONNECTION_NONCE;
|
this.nonce = nonce || CONNECTION_NONCE;
|
||||||
};
|
};
|
||||||
|
|
||||||
Version.fromBuffer = function(payload) {
|
Version.prototype.fromBuffer = function(payload) {
|
||||||
var message = new Version();
|
var message = new Version();
|
||||||
|
|
||||||
var parser = new BufferReader(payload);
|
var parser = new BufferReader(payload);
|
||||||
|
@ -73,13 +73,13 @@ Version.prototype.serialize = function() {
|
||||||
|
|
||||||
module.exports.Version = Version;
|
module.exports.Version = Version;
|
||||||
|
|
||||||
// ====== INV/GETDATA MESSAGE ======
|
// ====== INV MESSAGE ======
|
||||||
function Inventory(inventory) {
|
function Inventory(inventory) {
|
||||||
this.command = 'inv';
|
this.command = 'inv';
|
||||||
this.inventory = inventory || [];
|
this.inventory = inventory || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
Inventory.fromBuffer = function(payload) {
|
Inventory.prototype.fromBuffer = function(payload) {
|
||||||
var message = new Inventory();
|
var message = new Inventory();
|
||||||
|
|
||||||
var parser = new BufferReader(payload);
|
var parser = new BufferReader(payload);
|
||||||
|
@ -106,21 +106,24 @@ Inventory.prototype.serialize = function() {
|
||||||
return put.buffer();
|
return put.buffer();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.Inventory = Inventory;
|
||||||
|
|
||||||
|
// ====== GETDATA MESSAGE ======
|
||||||
function GetData(inventory) {
|
function GetData(inventory) {
|
||||||
this.command = 'getdata';
|
this.command = 'getdata';
|
||||||
this.inventory = inventory || [];
|
this.inventory = inventory || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
GetData.fromBuffer = Inventory.fromBuffer;
|
util.inherits(GetData, Inventory);
|
||||||
GetData.prototype.serialize = Inventory.prototype.serialize;
|
module.exports.GetData = GetData;
|
||||||
|
|
||||||
// ====== PING/PONG MESSAGE ======
|
// ====== PING MESSAGE ======
|
||||||
function Ping(nonce) {
|
function Ping(nonce) {
|
||||||
this.command = 'ping';
|
this.command = 'ping';
|
||||||
this.nonce = nonce || CONNECTION_NONCE;
|
this.nonce = nonce || CONNECTION_NONCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ping.fromBuffer = function(payload) {
|
Ping.prototype.fromBuffer = function(payload) {
|
||||||
var nonce = new BufferReader(payload).read(8);
|
var nonce = new BufferReader(payload).read(8);
|
||||||
return new Ping(nonce);
|
return new Ping(nonce);
|
||||||
};
|
};
|
||||||
|
@ -129,14 +132,16 @@ Ping.prototype.serialize = function() {
|
||||||
return this.nonce;
|
return this.nonce;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.Ping = Ping;
|
||||||
|
|
||||||
|
// ====== PONG MESSAGE ======
|
||||||
function Pong(nonce) {
|
function Pong(nonce) {
|
||||||
this.command = 'pong';
|
this.command = 'pong';
|
||||||
this.nonce = nonce || CONNECTION_NONCE;
|
this.nonce = nonce || CONNECTION_NONCE;
|
||||||
}
|
}
|
||||||
|
|
||||||
Pong.fromBuffer = Ping.fromBuffer;
|
util.inherits(Pong, Ping);
|
||||||
Pong.prototype.serialize = Ping.prototype.serialize;
|
module.exports.Pong = Pong;
|
||||||
|
|
||||||
|
|
||||||
// ====== ADDR MESSAGE ======
|
// ====== ADDR MESSAGE ======
|
||||||
function Addresses(nonce) {
|
function Addresses(nonce) {
|
||||||
|
@ -144,7 +149,7 @@ function Addresses(nonce) {
|
||||||
this.addresses = [];
|
this.addresses = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
Address.fromBuffer = function(payload) {
|
Address.prototype.fromBuffer = function(payload) {
|
||||||
var message = new Address();
|
var message = new Address();
|
||||||
|
|
||||||
var parser = new BufferReader(payload);
|
var parser = new BufferReader(payload);
|
||||||
|
@ -168,12 +173,14 @@ Address.prototype.serialize = function() {
|
||||||
return BufferUtil.EMPTY_BUFFER; // TODO
|
return BufferUtil.EMPTY_BUFFER; // TODO
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.Address = Address;
|
||||||
|
|
||||||
// ====== GETADDR MESSAGE ======
|
// ====== GETADDR MESSAGE ======
|
||||||
function GetAddresses() {
|
function GetAddresses() {
|
||||||
this.command = 'getaddr';
|
this.command = 'getaddr';
|
||||||
}
|
}
|
||||||
|
|
||||||
GetAddresses.fromBuffer = function() {
|
GetAddresses.prototype.fromBuffer = function() {
|
||||||
return new GetAddresses();
|
return new GetAddresses();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -181,13 +188,14 @@ GetAddresses.prototype.serialize = function() {
|
||||||
return BufferUtil.EMPTY_BUFFER;
|
return BufferUtil.EMPTY_BUFFER;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.GetAddresses = GetAddresses;
|
||||||
|
|
||||||
// ====== VERACK MESSAGE ======
|
// ====== VERACK MESSAGE ======
|
||||||
function VerAck() {
|
function VerAck() {
|
||||||
this.command = 'verack';
|
this.command = 'verack';
|
||||||
}
|
}
|
||||||
|
|
||||||
VerAck.fromBuffer = function() {
|
VerAck.prototype.fromBuffer = function() {
|
||||||
return new VerAck();
|
return new VerAck();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -195,13 +203,15 @@ VerAck.prototype.serialize = function() {
|
||||||
return BufferUtil.EMPTY_BUFFER;
|
return BufferUtil.EMPTY_BUFFER;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.VerAck = VerAck;
|
||||||
|
|
||||||
// ====== REJECT MESSAGE ======
|
// ====== REJECT MESSAGE ======
|
||||||
// TODO: Parse REJECT message
|
// TODO: Parse REJECT message
|
||||||
function Reject() {
|
function Reject() {
|
||||||
this.command = 'reject';
|
this.command = 'reject';
|
||||||
}
|
}
|
||||||
|
|
||||||
Reject.fromBuffer = function() {
|
Reject.prototype.fromBuffer = function() {
|
||||||
return new Reject();
|
return new Reject();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -209,12 +219,14 @@ Reject.prototype.serialize = function() {
|
||||||
return BufferUtil.EMPTY_BUFFER;
|
return BufferUtil.EMPTY_BUFFER;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.Reject = Reject;
|
||||||
|
|
||||||
// ====== ALERT MESSAGE ======
|
// ====== ALERT MESSAGE ======
|
||||||
function Alert(payload) {
|
function Alert(payload) {
|
||||||
this.command = 'reject';
|
this.command = 'reject';
|
||||||
}
|
}
|
||||||
|
|
||||||
Alert.fromBuffer = function() {
|
Alert.prototype.fromBuffer = function() {
|
||||||
var message = new Alert();
|
var message = new Alert();
|
||||||
|
|
||||||
var parser = new BufferReader(payload);
|
var parser = new BufferReader(payload);
|
||||||
|
@ -227,13 +239,15 @@ Alert.prototype.serialize = function() {
|
||||||
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
|
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.Alert = Alert;
|
||||||
|
|
||||||
// ====== HEADERS MESSAGE ======
|
// ====== HEADERS MESSAGE ======
|
||||||
function Headers(blockheaders) {
|
function Headers(blockheaders) {
|
||||||
this.command = 'headers';
|
this.command = 'headers';
|
||||||
this.headers = blockheaders || [];
|
this.headers = blockheaders || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
Headers.fromBuffer = function() {
|
Headers.prototype.fromBuffer = function() {
|
||||||
var message = new Headers();
|
var message = new Headers();
|
||||||
|
|
||||||
var parser = new BufferReader(payload);
|
var parser = new BufferReader(payload);
|
||||||
|
@ -252,13 +266,15 @@ Headers.prototype.serialize = function() {
|
||||||
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
|
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.Headers = Headers;
|
||||||
|
|
||||||
// ====== BLOCK MESSAGE ======
|
// ====== BLOCK MESSAGE ======
|
||||||
function Block(block) {
|
function Block(block) {
|
||||||
this.command = 'block';
|
this.command = 'block';
|
||||||
this.block = block;
|
this.block = block;
|
||||||
}
|
}
|
||||||
|
|
||||||
Block.fromBuffer = function() {
|
Block.prototype.fromBuffer = function() {
|
||||||
var parser = new BufferReader(payload);
|
var parser = new BufferReader(payload);
|
||||||
var block = Block().fromBufferReader(parser);
|
var block = Block().fromBufferReader(parser);
|
||||||
return new Block(block);
|
return new Block(block);
|
||||||
|
@ -268,13 +284,15 @@ Block.prototype.serialize = function() {
|
||||||
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
|
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.Block = Block;
|
||||||
|
|
||||||
// ====== TX MESSAGE ======
|
// ====== TX MESSAGE ======
|
||||||
function Transaction(transaction) {
|
function Transaction(transaction) {
|
||||||
this.command = 'tx';
|
this.command = 'tx';
|
||||||
this.transaction = transaction;
|
this.transaction = transaction;
|
||||||
}
|
}
|
||||||
|
|
||||||
Transaction.fromBuffer = function() {
|
Transaction.prototype.fromBuffer = function() {
|
||||||
var parser = new BufferReader(payload);
|
var parser = new BufferReader(payload);
|
||||||
var transaction = Transaction().fromBufferReader(parser);
|
var transaction = Transaction().fromBufferReader(parser);
|
||||||
return new Transaction(transaction);
|
return new Transaction(transaction);
|
||||||
|
@ -284,7 +302,9 @@ Transaction.prototype.serialize = function() {
|
||||||
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
|
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
|
||||||
};
|
};
|
||||||
|
|
||||||
// ====== GETBLOCKS/GETHEADERS MESSAGE ======
|
module.exports.Transaction = Transaction;
|
||||||
|
|
||||||
|
// ====== GETBLOCKS MESSAGE ======
|
||||||
function GetBlocks(starts, stop) {
|
function GetBlocks(starts, stop) {
|
||||||
this.command = 'getblocks';
|
this.command = 'getblocks';
|
||||||
this.version = PROTOCOL_VERSION;
|
this.version = PROTOCOL_VERSION;
|
||||||
|
@ -292,7 +312,7 @@ function GetBlocks(starts, stop) {
|
||||||
this.stop = stop || BufferUtil.NULL_HASH;
|
this.stop = stop || BufferUtil.NULL_HASH;
|
||||||
}
|
}
|
||||||
|
|
||||||
GetBlocks.fromBuffer = function() {
|
GetBlocks.prototype.fromBuffer = function() {
|
||||||
var message = new GetBlocks();
|
var message = new GetBlocks();
|
||||||
|
|
||||||
var parser = new BufferReader(payload);
|
var parser = new BufferReader(payload);
|
||||||
|
@ -326,6 +346,9 @@ GetBlocks.prototype.serialize = function() {
|
||||||
return put.buffer();
|
return put.buffer();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.GetBlocks = GetBlocks;
|
||||||
|
|
||||||
|
// ====== GETHEADERS MESSAGE ======
|
||||||
function GetHeaders(starts, stop) {
|
function GetHeaders(starts, stop) {
|
||||||
this.command = 'getheaders';
|
this.command = 'getheaders';
|
||||||
this.version = PROTOCOL_VERSION;
|
this.version = PROTOCOL_VERSION;
|
||||||
|
@ -333,7 +356,5 @@ function GetHeaders(starts, stop) {
|
||||||
this.stop = stop || BufferUtil.NULL_HASH;
|
this.stop = stop || BufferUtil.NULL_HASH;
|
||||||
}
|
}
|
||||||
|
|
||||||
GetHeaders.fromBuffer = GetBlocks.fromBuffer;
|
util.inherits(GetHeaders, GetBlocks);
|
||||||
GetHeaders.prototype.serialize = GetBlocks.prototype.serialize;
|
module.exports.GetHeaders = GetHeaders;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
var util = require('util');
|
||||||
var Net = require('net');
|
var Net = require('net');
|
||||||
var Put = require('bufferput');
|
var Put = require('bufferput');
|
||||||
var Buffers = require('buffers');
|
var Buffers = require('buffers');
|
||||||
|
|
Loading…
Reference in New Issue