2015-01-03 17:04:14 +01:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="utf-8">
|
|
|
|
<title>JSDoc: Source: messages.js</title>
|
|
|
|
|
|
|
|
<script src="scripts/prettify/prettify.js"> </script>
|
|
|
|
<script src="scripts/prettify/lang-css.js"> </script>
|
|
|
|
<!--[if lt IE 9]>
|
|
|
|
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
|
|
|
|
<![endif]-->
|
|
|
|
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
|
|
|
|
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<div id="main">
|
|
|
|
|
|
|
|
<h1 class="page-title">Source: messages.js</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<section>
|
|
|
|
<article>
|
|
|
|
<pre class="prettyprint source linenums"><code>/**
|
|
|
|
* Working with messages.
|
|
|
|
* @see {@link https://bitmessage.org/wiki/Protocol_specification#Message_types}
|
2015-01-31 12:54:23 +01:00
|
|
|
* @see {@link https://bitmessage.org/wiki/Protocol_specification_v3#Message_types}
|
|
|
|
* @see {@link https://bitmessage.org/Bitmessage%20Technical%20Paper.pdf}
|
2015-01-03 17:04:14 +01:00
|
|
|
* @module bitmessage/messages
|
2015-02-12 11:44:15 +01:00
|
|
|
* @example
|
|
|
|
* var structs = require("bitmessage").structs;
|
|
|
|
* var messages = require("bitmessage").messages;
|
|
|
|
*
|
|
|
|
* // Simple encoding and decoding:
|
|
|
|
* var vermsg = messages.version.encode({
|
|
|
|
* nonce: Buffer(8), // Hack detection connection to self
|
|
|
|
* remoteHost: "1.1.1.1",
|
|
|
|
* remotePort: 8444,
|
|
|
|
* });
|
|
|
|
* console.log(messages.version.decode(vermsg).remoteHost); // 1.1.1.1
|
|
|
|
*
|
|
|
|
* // Low-level encoding and decoding:
|
|
|
|
* var addrPayload = messages.addr.encodePayload([
|
|
|
|
* {host: "2.2.2.2", port: 28444},
|
|
|
|
* ]);
|
|
|
|
* var addrmsg = structs.message.encode("addr", addrPayload);
|
|
|
|
* var decoded = structs.message.decode(addrmsg);
|
|
|
|
* console.log(decoded.command); // addr
|
|
|
|
* var payload = decoded.payload;
|
|
|
|
* var decodedPayload = messages.addr.decodePayload(payload);
|
|
|
|
* console.log(decodedPayload.addrs[0].host); // 2.2.2.2
|
|
|
|
*
|
|
|
|
* // Encode with empty payload:
|
|
|
|
* var verackmsg = structs.message.encode("verack");
|
|
|
|
* console.log(structs.message.decode(verackmsg).command); // verack
|
2015-01-03 17:04:14 +01:00
|
|
|
*/
|
2015-01-31 12:54:23 +01:00
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var objectAssign = Object.assign || require("object-assign");
|
2015-02-12 11:44:15 +01:00
|
|
|
var bufferEqual = require("buffer-equal");
|
2015-01-31 12:54:23 +01:00
|
|
|
var assert = require("./_util").assert;
|
|
|
|
var structs = require("./structs");
|
2015-02-12 11:44:15 +01:00
|
|
|
var bmcrypto = require("./crypto");
|
2015-01-31 12:54:23 +01:00
|
|
|
var UserAgent = require("./user-agent");
|
|
|
|
var util = require("./_util");
|
|
|
|
|
|
|
|
var message = structs.message;
|
|
|
|
var ServicesBitfield = structs.ServicesBitfield;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to get command of the given encoded message.
|
|
|
|
* Note that this function doesn't do any validation because it is
|
|
|
|
* already provided by
|
|
|
|
* [message.decode]{@link module:bitmessage/structs.message.decode}
|
2015-02-12 11:44:15 +01:00
|
|
|
* routine.
|
2015-01-31 12:54:23 +01:00
|
|
|
* @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-02-12 11:44:15 +01:00
|
|
|
// Random nonce used to detect connections to self.
|
|
|
|
var randomNonce = bmcrypto.randomBytes(8);
|
|
|
|
|
2015-01-31 12:54:23 +01:00
|
|
|
/**
|
|
|
|
* `version` message.
|
|
|
|
* @see {@link https://bitmessage.org/wiki/Protocol_specification#version}
|
|
|
|
* @namespace
|
|
|
|
* @static
|
|
|
|
*/
|
2015-02-12 11:44:15 +01:00
|
|
|
// TODO(Kagami): User agent and stream numbers size limits per
|
|
|
|
// <https://github.com/Bitmessage/PyBitmessage/issues/767>.
|
2015-01-31 12:54:23 +01:00
|
|
|
var version = exports.version = {
|
|
|
|
/**
|
2015-02-12 11:44:15 +01:00
|
|
|
* @typedef {Object} DecodeResult
|
2015-02-12 11:51:08 +01:00
|
|
|
* @property {number} version - Identifies protocol version being used
|
|
|
|
* by the node. Should equal 3. Nodes should disconnect if the remote
|
|
|
|
* node's version is lower but continue with the connection if it is
|
|
|
|
* higher.
|
2015-02-12 11:44:15 +01:00
|
|
|
* @property {Object} services -
|
|
|
|
* [Service]{@link module:bitmessage/structs.ServicesBitfield}
|
|
|
|
* features to be enabled for this connection
|
|
|
|
* @property {Date} time - Node time
|
|
|
|
* @property {string} remoteHost - IPv4/IPv6 address of the node
|
|
|
|
* receiving this message
|
|
|
|
* @property {number} remotePort - Port of the node receiving this
|
|
|
|
* message
|
|
|
|
* @property {number} port - Incoming port of the node sending this
|
|
|
|
* message
|
|
|
|
* @property {Buffer} nonce - Random nonce used to detect connection
|
|
|
|
* to self
|
|
|
|
* @property {(Array|string|Buffer)} userAgent -
|
|
|
|
* [User agent]{@link module:bitmessage/user-agent} of the node
|
|
|
|
* @property {number[]} streamNumbers - Streams accepted by the node
|
|
|
|
* @property {number} length - Real data length
|
|
|
|
* @memberof module:bitmessage/messages.version
|
2015-01-31 12:54:23 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2015-02-12 11:44:15 +01:00
|
|
|
* Decode `version` message.
|
2015-01-31 12:54:23 +01:00
|
|
|
* NOTE: `nonce` is copied.
|
|
|
|
* @param {Buffer} buf - Message
|
2015-02-12 11:44:15 +01:00
|
|
|
* @return {DecodeResult}
|
|
|
|
* [Decoded `version` structure.]{@link module:bitmessage/messages.version.DecodeResult}
|
2015-01-31 12:54:23 +01:00
|
|
|
*/
|
|
|
|
decode: function(buf) {
|
|
|
|
var decoded = message.decode(buf);
|
|
|
|
assert(decoded.command === "version", "Bad command");
|
|
|
|
return version.decodePayload(decoded.payload);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode `version` message payload.
|
2015-02-12 11:44:15 +01:00
|
|
|
* The same as [decode]{@link module:bitmessage/messages.version.decode}.
|
2015-01-31 12:54:23 +01:00
|
|
|
*/
|
|
|
|
decodePayload: function(buf) {
|
|
|
|
// 4 + 8 + 8 + 26 + 26 + 8 + (1+) + (1+)
|
|
|
|
assert(buf.length >= 82, "Buffer is too small");
|
|
|
|
var protoVersion = buf.readUInt32BE(0, true);
|
|
|
|
var services = ServicesBitfield(buf.slice(4, 12), {copy: true});
|
|
|
|
var time = util.readTime64BE(buf, 12);
|
|
|
|
var short = {short: true};
|
|
|
|
var addrRecv = structs.net_addr.decode(buf.slice(20, 46), short);
|
|
|
|
var addrFrom = structs.net_addr.decode(buf.slice(46, 72), short);
|
|
|
|
var nonce = new Buffer(8);
|
|
|
|
buf.copy(nonce, 0, 72, 80);
|
2015-02-12 11:44:15 +01:00
|
|
|
assert(!bufferEqual(nonce, randomNonce), "Connection to self");
|
2015-01-31 12:54:23 +01:00
|
|
|
var decodedUa = UserAgent.decode(buf.slice(80));
|
|
|
|
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,
|
|
|
|
userAgent: decodedUa.str,
|
|
|
|
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
|
2015-02-12 11:44:15 +01:00
|
|
|
* @param {Object=} opts.services -
|
|
|
|
* [Service]{@link module:bitmessage/structs.ServicesBitfield}
|
|
|
|
* features to be enabled for this connection (`NODE_NETWORK` by
|
|
|
|
* default)
|
|
|
|
* @param {Date=} opts.time - Node time (current time by default)
|
|
|
|
* @param {string} opts.remoteHost - IPv4/IPv6 address of the node
|
|
|
|
* receiving this message
|
|
|
|
* @param {number} opts.remotePort - Port of the node receiving this
|
|
|
|
* message
|
|
|
|
* @param {number=} opts.port - Incoming port of the node (8444 by
|
|
|
|
* default)
|
|
|
|
* @param {Buffer=} opts.nonce - Random nonce used to detect connection
|
|
|
|
* to self (unique per node.js process by default)
|
|
|
|
* @param {(Array|string|Buffer)=} opts.userAgent -
|
|
|
|
* [User agent]{@link module:bitmessage/user-agent} of the node
|
|
|
|
* (bitmessage's by default)
|
|
|
|
* @param {Array<number>=} opts.streamNumbers - Streams accepted by the
|
|
|
|
* node (1 by default)
|
2015-01-31 12:54:23 +01:00
|
|
|
* @return {Buffer} Encoded message.
|
|
|
|
*/
|
|
|
|
encode: function(opts) {
|
|
|
|
var payload = version.encodePayload(opts);
|
|
|
|
return message.encode("version", payload);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode `version` message payload.
|
2015-02-12 11:44:15 +01:00
|
|
|
* The same as [encode]{@link module:bitmessage/messages.version.encode}.
|
2015-01-31 12:54:23 +01:00
|
|
|
*/
|
|
|
|
encodePayload: function(opts) {
|
|
|
|
// Deal with default options.
|
|
|
|
var services = opts.services ||
|
|
|
|
ServicesBitfield().set(ServicesBitfield.NODE_NETWORK);
|
|
|
|
var time = opts.time || new Date();
|
2015-02-12 11:44:15 +01:00
|
|
|
var nonce = opts.nonce || randomNonce;
|
2015-01-31 12:54:23 +01:00
|
|
|
assert(nonce.length === 8, "Bad nonce");
|
2015-02-12 11:44:15 +01:00
|
|
|
var port = opts.port || 8444;
|
2015-01-31 12:54:23 +01:00
|
|
|
var userAgent = opts.userAgent || UserAgent.SELF;
|
|
|
|
var streamNumbers = opts.streamNumbers || [1];
|
|
|
|
// Start encoding.
|
|
|
|
var protoVersion = new Buffer(4);
|
|
|
|
protoVersion.writeUInt32BE(util.PROTOCOL_VERSION, 0);
|
|
|
|
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",
|
2015-02-12 11:44:15 +01:00
|
|
|
port: port,
|
2015-01-31 12:54:23 +01:00
|
|
|
short: true,
|
|
|
|
});
|
|
|
|
return Buffer.concat([
|
|
|
|
protoVersion,
|
|
|
|
services.buffer,
|
|
|
|
util.writeTime64BE(null, time),
|
|
|
|
addrRecv,
|
|
|
|
addrFrom,
|
|
|
|
nonce,
|
|
|
|
UserAgent.encode(userAgent),
|
|
|
|
structs.var_int_list.encode(streamNumbers),
|
|
|
|
]);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `addr` message. Provide information on known nodes of the network.
|
|
|
|
* @see {@link https://bitmessage.org/wiki/Protocol_specification#addr}
|
|
|
|
* @namespace
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
var addr = exports.addr = {
|
2015-02-12 11:44:15 +01:00
|
|
|
/**
|
|
|
|
* @typedef {Object} DecodeResult
|
|
|
|
* @property {Object[]} addrs - List of
|
|
|
|
* [decoded `net_addr` structures]{@link module:bitmessage/structs.net_addr.DecodeResult}
|
|
|
|
* @property {number} length - Real data length
|
|
|
|
* @memberof module:bitmessage/messages.addr
|
|
|
|
*/
|
|
|
|
|
2015-01-31 12:54:23 +01:00
|
|
|
/**
|
|
|
|
* Decode `addr` message.
|
|
|
|
* @param {Buffer} buf - Message
|
2015-02-12 11:44:15 +01:00
|
|
|
* @return {DecodeResult}
|
|
|
|
* [Decoded `addr` structure.]{@link module:bitmessage/messages.addr.DecodeResult}
|
2015-01-31 12:54:23 +01:00
|
|
|
*/
|
|
|
|
decode: function(buf) {
|
|
|
|
var decoded = message.decode(buf);
|
|
|
|
assert(decoded.command === "addr", "Bad command");
|
|
|
|
return addr.decodePayload(decoded.payload);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode `addr` message payload.
|
2015-02-12 11:44:15 +01:00
|
|
|
* The same as [decode]{@link module:bitmessage/messages.addr.decode}.
|
2015-01-31 12:54:23 +01:00
|
|
|
*/
|
|
|
|
decodePayload: function(buf) {
|
|
|
|
var decoded = structs.var_int.decode(buf);
|
|
|
|
var listLength = decoded.value;
|
|
|
|
assert(listLength <= 1000, "Too many address entires");
|
|
|
|
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.
|
2015-02-12 11:44:15 +01:00
|
|
|
* @param {Object[]} addrs - List of
|
|
|
|
* [net_addr encode options]{@link module:bitmessage/structs.net_addr.encode}
|
2015-01-31 12:54:23 +01:00
|
|
|
* @return {Buffer} Encoded message.
|
|
|
|
*/
|
|
|
|
encode: function(addrs) {
|
|
|
|
var payload = addr.encodePayload(addrs);
|
|
|
|
return message.encode("addr", payload);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode `addr` message payload.
|
2015-02-12 11:44:15 +01:00
|
|
|
* The same as [encode]{@link module:bitmessage/messages.addr.encode}.
|
2015-01-31 12:54:23 +01:00
|
|
|
*/
|
|
|
|
encodePayload: function(addrs) {
|
|
|
|
assert(addrs.length <= 1000, "Too many address entires");
|
2015-02-12 11:44:15 +01:00
|
|
|
var addrBufs = addrs.map(structs.net_addr.encode);
|
|
|
|
var bufs = [structs.var_int.encode(addrs.length)].concat(addrBufs);
|
|
|
|
return Buffer.concat(bufs);
|
2015-01-31 12:54:23 +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
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
var inv = exports.inv = {
|
2015-02-12 11:44:15 +01:00
|
|
|
/**
|
|
|
|
* @typedef {Object} DecodeResult
|
|
|
|
* @property {Buffer[]} inventory - List of
|
|
|
|
* [inventory vectors]{@link module:bitmessage/structs.inv_vect}
|
|
|
|
* @property {number} length - Real data length
|
|
|
|
* @memberof module:bitmessage/messages.inv
|
|
|
|
*/
|
|
|
|
|
2015-01-31 12:54:23 +01:00
|
|
|
/**
|
|
|
|
* Decode `inv` message.
|
|
|
|
* @param {Buffer} buf - Message
|
2015-02-12 11:44:15 +01:00
|
|
|
* @return {DecodeResult}
|
|
|
|
* [Decoded `inv` structure.]{@link module:bitmessage/messages.inv.DecodeResult}
|
2015-01-31 12:54:23 +01:00
|
|
|
*/
|
|
|
|
decode: function(buf) {
|
|
|
|
var decoded = message.decode(buf);
|
|
|
|
assert(decoded.command === "inv", "Bad command");
|
|
|
|
return inv.decodePayload(decoded.payload);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode `inv` message payload.
|
2015-02-12 11:44:15 +01:00
|
|
|
* The same as [decode]{@link module:bitmessage/messages.inv.decode}.
|
2015-01-31 12:54:23 +01:00
|
|
|
*/
|
|
|
|
decodePayload: function(buf) {
|
|
|
|
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.
|
2015-02-12 11:44:15 +01:00
|
|
|
* @param {Buffer[]} inventory -
|
|
|
|
* [Inventory vector]{@link module:bitmessage/structs.inv_vect} list
|
2015-01-31 12:54:23 +01:00
|
|
|
* @return {Buffer} Encoded message.
|
|
|
|
*/
|
|
|
|
encode: function(inventory) {
|
|
|
|
var payload = inv.encodePayload(inventory);
|
|
|
|
return message.encode("inv", payload);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode `inv` message payload.
|
2015-02-12 11:44:15 +01:00
|
|
|
* The same as [encode]{@link module:bitmessage/messages.inv.encode}.
|
2015-01-31 12:54:23 +01:00
|
|
|
*/
|
|
|
|
encodePayload: function(inventory) {
|
|
|
|
assert(inventory.length <= 50000, "Too many inventory entires");
|
2015-02-12 11:44:15 +01:00
|
|
|
// TODO(Kagami): Validate vectors length.
|
|
|
|
var bufs = [structs.var_int.encode(inventory.length)].concat(inventory);
|
|
|
|
return Buffer.concat(bufs);
|
2015-01-31 12:54:23 +01:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `getdata` message. `getdata` is used in response to an
|
|
|
|
* [inv]{@link module:bitmessage/messages.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, {
|
2015-02-12 11:44:15 +01:00
|
|
|
/**
|
|
|
|
* @typedef {Object} DecodeResult
|
|
|
|
* @property {Buffer[]} inventory - List of
|
|
|
|
* [inventory vectors]{@link module:bitmessage/structs.inv_vect}
|
|
|
|
* @property {number} length - Real data length
|
|
|
|
* @memberof module:bitmessage/messages.getdata
|
|
|
|
*/
|
|
|
|
|
2015-01-31 12:54:23 +01:00
|
|
|
/**
|
|
|
|
* Decode `getdata` message.
|
|
|
|
* @param {Buffer} buf - Message
|
2015-02-12 11:44:15 +01:00
|
|
|
* @return {DecodeResult}
|
|
|
|
* [Decoded `getdata` structure.]{@link module:bitmessage/messages.getdata.DecodeResult}
|
2015-01-31 12:54:23 +01:00
|
|
|
* @memberof module:bitmessage/messages.getdata
|
|
|
|
*/
|
|
|
|
decode: function(buf) {
|
|
|
|
var decoded = message.decode(buf);
|
|
|
|
assert(decoded.command === "getdata", "Bad command");
|
|
|
|
return inv.decodePayload(decoded.payload);
|
|
|
|
},
|
2015-02-12 11:44:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode `getdata` message payload.
|
|
|
|
* The same as [decode]{@link module:bitmessage/messages.getdata.decode}.
|
|
|
|
* @function decodePayload
|
|
|
|
* @memberof module:bitmessage/messages.getdata
|
|
|
|
*/
|
|
|
|
|
2015-01-31 12:54:23 +01:00
|
|
|
/**
|
|
|
|
* Encode `getdata` message.
|
2015-02-12 11:44:15 +01:00
|
|
|
* @param {Buffer[]} inventory -
|
|
|
|
* [Inventory vector]{@link module:bitmessage/structs.inv_vect} list
|
2015-01-31 12:54:23 +01:00
|
|
|
* @return {Buffer} Encoded message.
|
|
|
|
* @memberof module:bitmessage/messages.getdata
|
|
|
|
*/
|
|
|
|
encode: function(inventory) {
|
|
|
|
var payload = inv.encodePayload(inventory);
|
|
|
|
return message.encode("getdata", payload);
|
|
|
|
},
|
2015-02-12 11:44:15 +01:00
|
|
|
|
2015-01-31 12:54:23 +01:00
|
|
|
/**
|
|
|
|
* Encode `getdata` message payload.
|
2015-02-12 11:44:15 +01:00
|
|
|
* The same as [encode]{@link module:bitmessage/messages.getdata.encode}.
|
2015-01-31 12:54:23 +01:00
|
|
|
* @function encodePayload
|
|
|
|
* @memberof module:bitmessage/messages.getdata
|
|
|
|
*/
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* `error` message.
|
|
|
|
* @see {@link https://bitmessage.org/wiki/Protocol_specification_v3#error}
|
|
|
|
* @namespace
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
var error = exports.error = {
|
|
|
|
/**
|
|
|
|
* Just a warning.
|
|
|
|
* @constant {number}
|
|
|
|
*/
|
|
|
|
WARNING: 0,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* It's an error, something was going wrong (e.g. an object got lost).
|
|
|
|
* @constant {number}
|
|
|
|
*/
|
|
|
|
ERROR: 1,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* It's a fatal error. The node will drop the line for that error and
|
|
|
|
* maybe ban you for some time.
|
|
|
|
* @constant {number}
|
|
|
|
*/
|
|
|
|
FATAL: 2,
|
|
|
|
|
2015-02-12 11:44:15 +01:00
|
|
|
/**
|
|
|
|
* @typedef {Object} DecodeResult
|
|
|
|
* @property {number} fatal - Type of the error
|
|
|
|
* @property {number} banTime - The other node informs that it will
|
|
|
|
* not accept further connections for this number of seconds
|
|
|
|
* @property {?Buffer} vector -
|
|
|
|
* [Inventory vector]{@link module:bitmessage/structs.inv_vect}
|
|
|
|
* related to the error
|
|
|
|
* @property {string} errorText - A human-readable error description
|
|
|
|
* @property {number} length - Real data length
|
|
|
|
* @memberof module:bitmessage/messages.error
|
|
|
|
*/
|
|
|
|
|
2015-01-31 12:54:23 +01:00
|
|
|
/**
|
|
|
|
* Decode `error` message.
|
|
|
|
* @param {Buffer} buf - Message
|
2015-02-12 11:44:15 +01:00
|
|
|
* @return {DecodeResult}
|
|
|
|
* [Decoded `error` structure.]{@link module:bitmessage/messages.error.DecodeResult}
|
2015-01-31 12:54:23 +01:00
|
|
|
*/
|
|
|
|
decode: function(buf) {
|
|
|
|
var decoded = message.decode(buf);
|
|
|
|
assert(decoded.command === "error", "Bad command");
|
|
|
|
return error.decodePayload(decoded.payload);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decode `error` message payload.
|
2015-02-12 11:44:15 +01:00
|
|
|
* The same as [decode]{@link module:bitmessage/messages.error.decode}.
|
2015-01-31 12:54:23 +01:00
|
|
|
*/
|
|
|
|
decodePayload: function(buf) {
|
|
|
|
assert(buf.length >= 4, "Buffer is too small");
|
|
|
|
var decodedFatal = structs.var_int.decode(buf);
|
|
|
|
var decodedBanTime = structs.var_int.decode(decodedFatal.rest);
|
2015-02-12 11:44:15 +01:00
|
|
|
|
|
|
|
var decodedVectorLength = structs.var_int.decode(decodedBanTime.rest);
|
|
|
|
// NOTE(Kagami): Inventory vector should be only 32-byte in size but
|
|
|
|
// currently we don't ensure it.
|
|
|
|
var vectorLength = decodedVectorLength.value;
|
|
|
|
var rest = decodedVectorLength.rest;
|
|
|
|
assert(rest.length >= vectorLength, "Buffer is too small");
|
|
|
|
var vector = null;
|
|
|
|
if (vectorLength) {
|
|
|
|
vector = new Buffer(vectorLength);
|
|
|
|
rest.copy(vector);
|
|
|
|
rest = rest.slice(vectorLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
var decodedErrorText = structs.var_str.decode(rest);
|
2015-01-31 12:54:23 +01:00
|
|
|
var length = (
|
|
|
|
decodedFatal.length +
|
|
|
|
decodedBanTime.length +
|
2015-02-12 11:44:15 +01:00
|
|
|
decodedVectorLength.length + vectorLength +
|
2015-01-31 12:54:23 +01:00
|
|
|
decodedErrorText.length
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
fatal: decodedFatal.value,
|
|
|
|
banTime: decodedBanTime.value,
|
2015-02-12 11:44:15 +01:00
|
|
|
vector: vector,
|
2015-01-31 12:54:23 +01:00
|
|
|
errorText: decodedErrorText.str,
|
|
|
|
// Real data length.
|
|
|
|
length: length,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode `error` message.
|
|
|
|
* @param {Object} opts - Error options
|
2015-02-12 11:44:15 +01:00
|
|
|
* @param {number=} opts.fatal - Type of the error
|
|
|
|
* ([warning]{@link module:bitmessage/messages.error.WARNING} by
|
|
|
|
* default)
|
|
|
|
* @param {number=} opts.banTime - Inform the other node, that you
|
|
|
|
* will not accept further connections for this number of seconds (0
|
|
|
|
* by default)
|
|
|
|
* @param {Buffer=} opts.vector - A 32-byte
|
|
|
|
* [inventory vector]{@link module:bitmessage/structs.inv_vect}
|
|
|
|
* related to the error (empty by default)
|
|
|
|
* @param {string} opts.errorText - A human-readable error description
|
2015-01-31 12:54:23 +01:00
|
|
|
* @return {Buffer} Encoded message.
|
|
|
|
*/
|
|
|
|
encode: function(opts) {
|
|
|
|
var payload = error.encodePayload(opts);
|
|
|
|
return message.encode("error", payload);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encode `error` message payload.
|
2015-02-12 11:44:15 +01:00
|
|
|
* The same as [encode]{@link module:bitmessage/messages.error.encode}.
|
2015-01-31 12:54:23 +01:00
|
|
|
*/
|
|
|
|
encodePayload: function(opts) {
|
|
|
|
var fatal = opts.fatal || error.WARNING;
|
|
|
|
var banTime = opts.banTime || 0;
|
2015-02-12 11:44:15 +01:00
|
|
|
// TODO(Kagami): Validate vector length.
|
|
|
|
var vector = opts.vector || new Buffer(0);
|
2015-01-31 12:54:23 +01:00
|
|
|
return Buffer.concat([
|
|
|
|
structs.var_int.encode(fatal),
|
|
|
|
structs.var_int.encode(banTime),
|
2015-02-12 11:44:15 +01:00
|
|
|
structs.var_int.encode(vector.length),
|
|
|
|
vector,
|
|
|
|
structs.var_str.encode(opts.errorText),
|
2015-01-31 12:54:23 +01:00
|
|
|
]);
|
|
|
|
},
|
|
|
|
};
|
2015-01-03 17:04:14 +01:00
|
|
|
</code></pre>
|
|
|
|
</article>
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<nav>
|
2015-02-14 12:00:58 +01:00
|
|
|
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-bitmessage.html">bitmessage</a></li><li><a href="module-bitmessage_address.html">bitmessage/address</a></li><li><a href="module-bitmessage_crypto.html">bitmessage/crypto</a></li><li><a href="module-bitmessage_messages.html">bitmessage/messages</a></li><li><a href="module-bitmessage_net_base.html">bitmessage/net/base</a></li><li><a href="module-bitmessage_net_tcp.html">bitmessage/net/tcp</a></li><li><a href="module-bitmessage_net_ws.html">bitmessage/net/ws</a></li><li><a href="module-bitmessage_objects.html">bitmessage/objects</a></li><li><a href="module-bitmessage_pow.html">bitmessage/pow</a></li><li><a href="module-bitmessage_structs.html">bitmessage/structs</a></li><li><a href="module-bitmessage_user-agent.html">bitmessage/user-agent</a></li><li><a href="module-bitmessage_wif.html">bitmessage/wif</a></li></ul><h3>Classes</h3><ul><li><a href="module-bitmessage_address.Address.html">Address</a></li><li><a href="module-bitmessage_net_base.BaseTransport.html">BaseTransport</a></li><li><a href="module-bitmessage_net_tcp.TcpTransport.html">TcpTransport</a></li><li><a href="module-bitmessage_net_ws.WsTransport.html">WsTransport</a></li><li><a href="module-bitmessage_structs.PubkeyBitfield.html">PubkeyBitfield</a></li><li><a href="module-bitmessage_structs.ServicesBitfield.html">ServicesBitfield</a></li></ul><h3>Namespaces</h3><ul><li><a href="module-bitmessage_messages.addr.html">addr</a></li><li><a href="module-bitmessage_messages.error.html">error</a></li><li><a href="module-bitmessage_messages.getdata.html">getdata</a></li><li><a href="module-bitmessage_messages.inv.html">inv</a></li><li><a href="module-bitmessage_messages.version.html">version</a></li><li><a href="module-bitmessage_objects.broadcast.html">broadcast</a></li><li><a href="module-bitmessage_objects.getpubkey.html">getpubkey</a></li><li><a href="module-bitmessage_objects.msg.html">msg</a></li><li><a href="module-bitmessage_objects.pubkey.html">pubkey</a></li><li><a href="module-bitmessage_structs.encrypted.html">encrypted</a></li><li><a href="module-bitmessage_structs.inv_vect.html">inv_vect</a></li><li><a href="module-bitmessage_structs.message.html">message</a></li><li><a href="module-bitmessage_structs.net_addr.html">net_addr</a></li><li><a href="module-bitmessage_structs.object.html">object</a></li><li><a href="module-bitmessage_structs.var_int.html">var_int</a></li><li><a href="module-bitmessage_structs.var_int_list.html">var_int_list</a></li><li><a href="module-bitmessage_structs.var_str.html">var_str</a></li></ul>
|
2015-01-03 17:04:14 +01:00
|
|
|
</nav>
|
|
|
|
|
|
|
|
<br class="clear">
|
|
|
|
|
|
|
|
<footer>
|
2015-02-14 12:06:56 +01:00
|
|
|
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0-dev</a> on Sat Feb 14 2015 14:05:38 GMT+0300 (MSK)
|
2015-01-03 17:04:14 +01:00
|
|
|
</footer>
|
|
|
|
|
|
|
|
<script> prettyPrint(); </script>
|
|
|
|
<script src="scripts/linenumber.js"> </script>
|
|
|
|
</body>
|
|
|
|
</html>
|