bitmessage-js/lib/messages.js

391 lines
11 KiB
JavaScript
Raw Normal View History

2015-01-02 22:45:56 +01:00
/**
* Working with messages.
* @see {@link https://bitmessage.org/wiki/Protocol_specification#Message_types}
2015-01-16 01:08:56 +01:00
* @see {@link https://bitmessage.org/wiki/Protocol_specification_v3#Message_types}
* @see {@link https://bitmessage.org/Bitmessage%20Technical%20Paper.pdf}
* @module bitmessage/messages
2015-01-02 22:45:56 +01:00
*/
2015-01-18 12:37:09 +01:00
// TODO(Kagami): Document object-like params.
2015-01-15 18:13:02 +01:00
"use strict";
var objectAssign = Object.assign || require("object-assign");
var assert = require("./_util").assert;
2015-01-15 18:13:02 +01:00
var structs = require("./structs");
var UserAgent = require("./user-agent");
var util = require("./_util");
var message = structs.message;
var ServicesBitfield = structs.ServicesBitfield;
2015-01-15 18:13:02 +01:00
2015-01-27 14:35:49 +01:00
/**
2015-01-28 14:31:18 +01:00
* Try to get command of the given encoded message.
2015-01-27 14:35:49 +01:00
* Note that this function doesn't do any validation because it is
* already provided by `message.decode` routine. Normally you call this
* for each incoming message and then call decode function of the
* appropriate message handler.
* @param {Buffer} buf - Buffer that starts with encoded message
* @return {?string} Message's command if any
*/
exports.getCommand = function(buf) {
if (buf.length < 16) {
return;
}
var command = buf.slice(4, 16);
var firstNonNull = 0;
for (var i = 11; i >=0; i--) {
if (command[i] !== 0) {
firstNonNull = i + 1;
break;
}
}
return command.slice(0, firstNonNull).toString("ascii");
};
2015-01-15 18:13:02 +01:00
/**
2015-01-15 22:00:27 +01:00
* `version` message.
2015-01-15 18:13:02 +01:00
* @see {@link https://bitmessage.org/wiki/Protocol_specification#version}
* @namespace
* @static
2015-01-15 18:13:02 +01:00
*/
var version = exports.version = {
2015-01-15 18:13:02 +01:00
/** Random nonce used to detect connections to self. */
NONCE: new Buffer("20bde0a3355dad78", "hex"),
/**
* Decode `version` message.
* NOTE: `nonce` is copied.
* @param {Buffer} buf - Message
* @return {Object} Decoded `version` structure.
*/
decode: function(buf) {
var decoded = message.decode(buf);
assert(decoded.command === "version", "Bad command");
return version.decodePayload(decoded.payload);
},
2015-01-15 18:13:02 +01:00
/**
2015-01-16 19:36:57 +01:00
* Decode `version` message payload.
2015-01-15 18:13:02 +01:00
* NOTE: `nonce` is copied.
2015-01-16 19:36:57 +01:00
* @param {Buffer} buf - Message payload
2015-01-15 19:11:33 +01:00
* @return {Object} Decoded `version` structure.
2015-01-15 18:13:02 +01:00
*/
decodePayload: function(buf) {
2015-01-15 18:13:02 +01:00
// 4 + 8 + 8 + 26 + 26 + 8 + (1+) + (1+)
2015-01-15 23:43:15 +01:00
assert(buf.length >= 82, "Buffer is too small");
var protoVersion = buf.readUInt32BE(0, true);
2015-01-19 00:05:53 +01:00
var services = ServicesBitfield(buf.slice(4, 12), {copy: true});
2015-01-15 23:43:15 +01:00
var time = util.readTime64BE(buf, 12);
2015-01-15 18:13:02 +01:00
var short = {short: true};
2015-01-15 23:43:15 +01:00
var addrRecv = structs.net_addr.decode(buf.slice(20, 46), short);
var addrFrom = structs.net_addr.decode(buf.slice(46, 72), short);
2015-01-15 18:13:02 +01:00
var nonce = new Buffer(8);
2015-01-15 23:43:15 +01:00
buf.copy(nonce, 0, 72, 80);
var decodedUa = UserAgent.decode(buf.slice(80));
2015-01-15 18:13:02 +01:00
var decodedStreamNumbers = structs.var_int_list.decode(decodedUa.rest);
return {
version: protoVersion,
services: services,
time: time,
remoteHost: addrRecv.host,
remotePort: addrRecv.port,
port: addrFrom.port,
nonce: nonce,
2015-01-24 13:13:21 +01:00
userAgent: decodedUa.str,
2015-01-15 18:13:02 +01:00
streamNumbers: decodedStreamNumbers.list,
// NOTE(Kagami): Real data length. It may be some gap between end
// of stream numbers list and end of payload:
// [payload..............[stream numbers]xxxx]
// We are currently ignoring that.
length: 80 + decodedUa.length + decodedStreamNumbers.length,
};
},
/**
* Encode `version` message.
* @param {Object} opts - Version options
* @return {Buffer} Encoded message.
*/
encode: function(opts) {
var payload = version.encodePayload(opts);
return message.encode("version", payload);
},
2015-01-15 18:13:02 +01:00
/**
2015-01-16 19:36:57 +01:00
* Encode `version` message payload.
2015-01-15 18:13:02 +01:00
* @param {Object} opts - Version options
2015-01-16 19:36:57 +01:00
* @return {Buffer} Encoded payload.
2015-01-15 18:13:02 +01:00
*/
encodePayload: function(opts) {
2015-01-15 18:13:02 +01:00
// Deal with default options.
2015-01-19 00:05:53 +01:00
var services = opts.services ||
ServicesBitfield().set(ServicesBitfield.NODE_NETWORK);
2015-01-15 18:13:02 +01:00
var time = opts.time || new Date();
var nonce = opts.nonce || version.NONCE;
2015-01-16 00:14:30 +01:00
assert(nonce.length === 8, "Bad nonce");
2015-01-24 13:13:21 +01:00
var userAgent = opts.userAgent || UserAgent.SELF;
2015-01-15 18:13:02 +01:00
var streamNumbers = opts.streamNumbers || [1];
// Start encoding.
var protoVersion = new Buffer(4);
protoVersion.writeUInt32BE(util.PROTOCOL_VERSION, 0);
2015-01-15 18:13:02 +01:00
var addrRecv = structs.net_addr.encode({
services: services,
host: opts.remoteHost,
port: opts.remotePort,
short: true,
});
var addrFrom = structs.net_addr.encode({
services: services,
host: "127.0.0.1",
port: opts.port,
short: true,
});
return Buffer.concat([
protoVersion,
2015-01-19 00:05:53 +01:00
services.buffer,
2015-01-15 18:13:02 +01:00
util.writeTime64BE(null, time),
addrRecv,
addrFrom,
nonce,
2015-01-24 13:13:21 +01:00
UserAgent.encode(userAgent),
2015-01-15 18:13:02 +01:00
structs.var_int_list.encode(streamNumbers),
]);
},
};
2015-01-15 19:11:33 +01:00
/**
2015-01-15 22:00:27 +01:00
* `addr` message. Provide information on known nodes of the network.
2015-01-15 19:11:33 +01:00
* @see {@link https://bitmessage.org/wiki/Protocol_specification#addr}
* @namespace
* @static
2015-01-15 19:11:33 +01:00
*/
var addr = exports.addr = {
/**
* Decode `addr` message.
* @param {Buffer} buf - Message
* @return {Object} Decoded `addr` structure.
*/
decode: function(buf) {
var decoded = message.decode(buf);
assert(decoded.command === "addr", "Bad command");
return addr.decodePayload(decoded.payload);
},
2015-01-15 19:11:33 +01:00
/**
2015-01-16 19:36:57 +01:00
* Decode `addr` message payload.
* @param {Buffer} buf - Message payload
2015-01-15 19:11:33 +01:00
* @return {Object} Decoded `addr` structure.
*/
decodePayload: function(buf) {
2015-01-15 19:11:33 +01:00
var decoded = structs.var_int.decode(buf);
var listLength = decoded.value;
2015-01-15 21:19:52 +01:00
assert(listLength <= 1000, "Too many address entires");
2015-01-15 19:11:33 +01:00
var length = decoded.length + listLength * 38;
assert(buf.length >= length, "Buffer is too small");
var rest = decoded.rest;
var addrs = new Array(listLength);
for (var i = 0; i < listLength; i++) {
addrs[i] = structs.net_addr.decode(rest.slice(i*38, (i+1)*38));
}
return {
addrs: addrs,
// Real data length.
length: length,
};
},
/**
* Encode `addr` message.
* @param {Object[]} addrs - Network addresses
* @return {Buffer} Encoded message.
*/
encode: function(addrs) {
var payload = addr.encodePayload(addrs);
return message.encode("addr", payload);
},
2015-01-15 19:11:33 +01:00
/**
2015-01-16 19:36:57 +01:00
* Encode `addr` message payload.
2015-01-15 19:11:33 +01:00
* @param {Object[]} addrs - Network addresses
2015-01-16 19:36:57 +01:00
* @return {Buffer} Encoded payload.
2015-01-15 19:11:33 +01:00
*/
encodePayload: function(addrs) {
2015-01-15 21:19:52 +01:00
assert(addrs.length <= 1000, "Too many address entires");
2015-01-15 19:11:33 +01:00
var addrsBuf = Buffer.concat(addrs.map(structs.net_addr.encode));
return Buffer.concat([structs.var_int.encode(addrs.length), addrsBuf]);
},
};
2015-01-15 22:00:27 +01:00
/**
* `inv` message. Allows a node to advertise its knowledge of one or
* more objects.
* @see {@link https://bitmessage.org/wiki/Protocol_specification#inv}
* @namespace
2015-01-15 22:07:49 +01:00
* @static
2015-01-15 22:00:27 +01:00
*/
2015-01-15 22:07:49 +01:00
var inv = exports.inv = {
/**
* Decode `inv` message.
* @param {Buffer} buf - Message
* @return {Object} Decoded `inv` structure.
*/
decode: function(buf) {
var decoded = message.decode(buf);
assert(decoded.command === "inv", "Bad command");
return inv.decodePayload(decoded.payload);
},
2015-01-15 22:00:27 +01:00
/**
2015-01-16 19:36:57 +01:00
* Decode `inv` message payload.
* @param {Buffer} buf - Message payload
2015-01-15 22:00:27 +01:00
* @return {Object} Decoded `inv` structure.
*/
decodePayload: function(buf) {
2015-01-15 22:00:27 +01:00
var decoded = structs.var_int.decode(buf);
var listLength = decoded.value;
assert(listLength <= 50000, "Too many inventory entires");
var length = decoded.length + listLength * 32;
assert(buf.length >= length, "Buffer is too small");
var rest = decoded.rest;
var inventory = new Array(listLength);
for (var i = 0; i < listLength; i++) {
inventory[i] = rest.slice(i*32, (i+1)*32);
}
return {
inventory: inventory,
// Real data length.
length: length,
};
},
/**
* Encode `inv` message.
* @param {Buffer[]} inventory - Inventory vector list (encoded)
* @return {Buffer} Encoded message.
*/
encode: function(inventory) {
var payload = inv.encodePayload(inventory);
return message.encode("inv", payload);
},
2015-01-15 22:00:27 +01:00
/**
2015-01-16 19:36:57 +01:00
* Encode `inv` message payload.
2015-01-16 01:08:56 +01:00
* @param {Buffer[]} inventory - Inventory vector list (encoded)
2015-01-16 19:36:57 +01:00
* @return {Buffer} Encoded payload.
2015-01-15 22:00:27 +01:00
*/
encodePayload: function(inventory) {
2015-01-15 22:00:27 +01:00
assert(inventory.length <= 50000, "Too many inventory entires");
var invBuf = Buffer.concat(inventory);
return Buffer.concat([structs.var_int.encode(inventory.length), invBuf]);
},
};
2015-01-15 22:07:49 +01:00
/**
* `getdata` message. `getdata` is used in response to an `inv` message
* to retrieve the content of a specific object after filtering known
* elements.
* @see {@link https://bitmessage.org/wiki/Protocol_specification#getdata}
* @namespace
*/
exports.getdata = objectAssign({}, inv, {
decode: function(buf) {
var decoded = message.decode(buf);
assert(decoded.command === "getdata", "Bad command");
return inv.decodePayload(decoded.payload);
},
encode: function(inventory) {
var payload = inv.encodePayload(inventory);
return message.encode("getdata", payload);
},
});
2015-01-16 00:14:30 +01:00
2015-01-16 01:08:56 +01:00
/**
* `error` message.
* @see {@link https://bitmessage.org/wiki/Protocol_specification_v3#error}
* @namespace
* @static
2015-01-16 01:08:56 +01:00
*/
var error = exports.error = {
/**
* Just a warning.
*/
WARNING: 0,
/**
* It's an error, something was going wrong (e.g. an object got lost).
*/
ERROR: 1,
/**
* It's a fatal error. The node will drop the line for that error and
* maybe ban you for some time.
*/
FATAL: 2,
/**
* Decode `error` message.
* @param {Buffer} buf - Message
* @return {Object} Decoded `error` structure.
*/
decode: function(buf) {
var decoded = message.decode(buf);
assert(decoded.command === "error", "Bad command");
return error.decodePayload(decoded.payload);
},
2015-01-16 01:08:56 +01:00
/**
2015-01-16 19:36:57 +01:00
* Decode `error` message payload.
* @param {Buffer} buf - Message payload
2015-01-16 01:08:56 +01:00
* @return {Object} Decoded `error` structure.
*/
decodePayload: function(buf) {
2015-01-16 01:08:56 +01:00
assert(buf.length >= 4, "Buffer is too small");
var decodedFatal = structs.var_int.decode(buf);
var decodedBanTime = structs.var_int.decode(decodedFatal.rest);
var decodedVector = structs.var_str.decode(decodedBanTime.rest);
var decodedErrorText = structs.var_str.decode(decodedVector.rest);
var length = (
decodedFatal.length +
decodedBanTime.length +
decodedVector.length +
decodedErrorText.length
);
return {
fatal: decodedFatal.value,
banTime: decodedBanTime.value,
vector: decodedVector.str,
errorText: decodedErrorText.str,
// Real data length.
length: length,
};
},
/**
* Encode `error` message.
* @param {Object} opts - Error options
* @return {Buffer} Encoded message.
*/
encode: function(opts) {
var payload = error.encodePayload(opts);
return message.encode("error", payload);
},
2015-01-16 01:08:56 +01:00
/**
2015-01-16 19:36:57 +01:00
* Encode `error` message payload.
2015-01-16 01:08:56 +01:00
* @param {Object} opts - Error options
2015-01-16 19:36:57 +01:00
* @return {Buffer} Encoded payload.
2015-01-16 01:08:56 +01:00
*/
encodePayload: function(opts) {
2015-01-16 01:08:56 +01:00
var fatal = opts.fatal || error.WARNING;
var banTime = opts.banTime || 0;
var vector = opts.vector || "";
var errorText = opts.errorText || "";
return Buffer.concat([
structs.var_int.encode(fatal),
structs.var_int.encode(banTime),
structs.var_str.encode(vector),
structs.var_str.encode(errorText),
]);
},
};